aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbyss777 <abyss@fox5.ru>2017-05-23 14:37:33 +0500
committerAbyss777 <abyss@fox5.ru>2017-05-23 14:37:33 +0500
commit51e77091952a1279bfbfa1fdec194f04308136ac (patch)
treedc89856785c4b728f691e75b640322b4aaa4cf47
parent864bc98ab078d5bc13249553c9f7865195083354 (diff)
downloadtrackermap-server-51e77091952a1279bfbfa1fdec194f04308136ac.tar.gz
trackermap-server-51e77091952a1279bfbfa1fdec194f04308136ac.tar.bz2
trackermap-server-51e77091952a1279bfbfa1fdec194f04308136ac.zip
Split POST on two functions
-rw-r--r--src/org/traccar/api/resource/AttributeResource.java54
1 files changed, 31 insertions, 23 deletions
diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java
index a1840cf51..4d326779b 100644
--- a/src/org/traccar/api/resource/AttributeResource.java
+++ b/src/org/traccar/api/resource/AttributeResource.java
@@ -84,34 +84,42 @@ public class AttributeResource extends BaseResource {
}
+ private Response add(Attribute entity) throws SQLException {
+ Context.getAttributesManager().addAttribute(entity);
+ Context.getDataManager().linkAttribute(getUserId(), entity.getId());
+ Context.getAttributesManager().refreshUserAttributes();
+ return Response.ok(entity).build();
+ }
+
+ private Response test(long deviceId, Attribute entity) {
+ Position last = Context.getIdentityManager().getLastPosition(deviceId);
+ if (last != null) {
+ Object result = new ComputedAttributesHandler().computeAttribute(entity, last);
+ if (result != null) {
+ switch (entity.getType()) {
+ case "number":
+ return Response.ok((Number) result).build();
+ case "boolean":
+ return Response.ok((Boolean) result).build();
+ default:
+ return Response.ok(result.toString()).build();
+ }
+ } else {
+ return Response.noContent().build();
+ }
+ } else {
+ throw new IllegalArgumentException("Device has no last position");
+ }
+ }
+
@POST
- public Response add(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException {
+ public Response post(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException {
Context.getPermissionsManager().checkReadonly(getUserId());
if (deviceId != 0) {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
- Position last = Context.getIdentityManager().getLastPosition(deviceId);
- if (last != null) {
- Object result = new ComputedAttributesHandler().computeAttribute(entity, last);
- if (result != null) {
- switch (entity.getType()) {
- case "number":
- return Response.ok((Number) result).build();
- case "boolean":
- return Response.ok((Boolean) result).build();
- default:
- return Response.ok(result.toString()).build();
- }
- } else {
- return Response.noContent().build();
- }
- } else {
- throw new IllegalArgumentException("Device has no last position");
- }
+ return test(deviceId, entity);
} else {
- Context.getAttributesManager().addAttribute(entity);
- Context.getDataManager().linkAttribute(getUserId(), entity.getId());
- Context.getAttributesManager().refreshUserAttributes();
- return Response.ok(entity).build();
+ return add(entity);
}
}