comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 63:9f8c9611848a

fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
author casties
date Fri, 23 Nov 2012 17:55:04 +0100
parents b8ef15c8c4a5
children c0dd5314bada
comparison
equal deleted inserted replaced
62:15569aa35d62 63:9f8c9611848a
10 10
11 import org.json.JSONArray; 11 import org.json.JSONArray;
12 import org.json.JSONException; 12 import org.json.JSONException;
13 import org.json.JSONObject; 13 import org.json.JSONObject;
14 import org.restlet.data.Form; 14 import org.restlet.data.Form;
15 import org.restlet.data.Parameter;
16 import org.restlet.data.Status; 15 import org.restlet.data.Status;
17 import org.restlet.ext.json.JsonRepresentation; 16 import org.restlet.ext.json.JsonRepresentation;
18 import org.restlet.representation.Representation; 17 import org.restlet.representation.Representation;
19 import org.restlet.resource.Delete; 18 import org.restlet.resource.Delete;
20 import org.restlet.resource.Get; 19 import org.restlet.resource.Get;
57 // do authentication 56 // do authentication
58 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 57 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
59 logger.debug("request authenticated=" + authUser); 58 logger.debug("request authenticated=" + authUser);
60 59
61 if (id == null) { 60 if (id == null) {
62 return getAllAnnotations(authUser); 61 // no id -- send all annotations
63 } 62 Form form = getRequest().getResourceRef().getQueryAsForm();
64 63 int limit = getInt(form.getFirstValue("limit"));
64 int offset = getInt(form.getFirstValue("offset"));
65 String sortBy = form.getFirstValue("sortBy");
66 return getAllAnnotations(authUser, limit, offset, sortBy);
67 }
68
69 // send annotation with id
65 AnnotationStore store = getAnnotationStore(); 70 AnnotationStore store = getAnnotationStore();
66 Annotation annot = store.getAnnotationById(id); 71 Annotation annot = store.getAnnotationById(id);
67 if (annot != null) { 72 if (annot != null) {
68 if (!annot.isActionAllowed("read", authUser, store)) { 73 if (!annot.isActionAllowed("read", authUser, store)) {
69 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 74 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
70 return null; 75 return null;
71 } 76 }
72 JSONObject result = createAnnotatorJson(annot, (authUser == null)); 77 JSONObject result = createAnnotatorJson(annot, (authUser == null));
73 logger.debug("sending:");
74 logger.debug(result);
75 return new JsonRepresentation(result); 78 return new JsonRepresentation(result);
76 } else { 79 } else {
77 // not found 80 // not found
78 setStatus(Status.CLIENT_ERROR_NOT_FOUND); 81 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
79 return null; 82 return null;
80 } 83 }
81 } 84 }
82 85
83 private Representation getAllAnnotations(Person authUser) { 86 private Representation getAllAnnotations(Person authUser, int limit, int offset, String sortBy) {
84
85 Form form = getRequest().getResourceRef().getQueryAsForm();
86 String sortBy = null;
87 for (Parameter parameter : form) {
88 if (parameter.getName().equals("sortBy")) {
89 sortBy = parameter.getValue();
90 }
91 }
92
93 AnnotationStore store = getAnnotationStore(); 87 AnnotationStore store = getAnnotationStore();
94 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); 88 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
95 89
90 // read all annotations
96 List<Annotation> annotations = store.getAnnotations(null, null); 91 List<Annotation> annotations = store.getAnnotations(null, null);
97 for (Annotation annotation : annotations) { 92 for (Annotation annotation : annotations) {
98 // check permission 93 // check permission
99 if (!annotation.isActionAllowed("read", authUser, store)) continue; 94 if (!annotation.isActionAllowed("read", authUser, store)) continue;
100 95 // add annotation to list
101 JSONObject jo = createAnnotatorJson(annotation, false); 96 JSONObject jo = createAnnotatorJson(annotation, false);
102 results.add(jo); 97 results.add(jo);
103 } 98 }
104 99
100 // sort if necessary
105 if (sortBy != null) { 101 if (sortBy != null) {
106 JSONObjectComparator.sortAnnotations(results, sortBy); 102 JSONObjectComparator.sortAnnotations(results, sortBy);
107 } 103 }
108 104
109 JSONArray resultsJa = new JSONArray(); 105 // put in JSON list
106 JSONArray rows = new JSONArray();
107 int cnt = 0;
110 for (JSONObject result : results) { 108 for (JSONObject result : results) {
111 resultsJa.put(result); 109 cnt += 1;
110 if (offset > 0 && cnt < offset) continue;
111 rows.put(result);
112 if (limit > 0 && cnt >= limit) break;
112 } 113 }
113 114
114 // assemble result object 115 // assemble result object
115 JSONObject result = new JSONObject(); 116 JSONObject result = new JSONObject();
116 try { 117 try {
117 result.put("rows", resultsJa); 118 result.put("rows", rows);
118 result.put("total", resultsJa.length()); 119 result.put("total", rows.length());
119 } catch (JSONException e) { 120 } catch (JSONException e) {
120 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); 121 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
121 return null; 122 return null;
122 } 123 }
123 logger.debug("sending:");
124 logger.debug(result);
125 return new JsonRepresentation(result); 124 return new JsonRepresentation(result);
126 } 125 }
127 126
128 /** 127 /**
129 * POST with JSON content-type. Creates a new Annotation. 128 * POST with JSON content-type. Creates a new Annotation.
231 // return new annotation 230 // return new annotation
232 jo = createAnnotatorJson(storedAnnot, (authUser == null)); 231 jo = createAnnotatorJson(storedAnnot, (authUser == null));
233 JsonRepresentation retRep = new JsonRepresentation(jo); 232 JsonRepresentation retRep = new JsonRepresentation(jo);
234 return retRep; 233 return retRep;
235 } catch (JSONException e) { 234 } catch (JSONException e) {
236 e.printStackTrace(); 235 logger.error("Error in doPutJSON", e);
237 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 236 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
238 } catch (IOException e) { 237 } catch (IOException e) {
239 e.printStackTrace(); 238 logger.error("Error in doPutJSON", e);
240 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error"); 239 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
241 } 240 }
242 return null; 241 return null;
243 } 242 }
244 243
266 if (!annot.isActionAllowed("delete", authUser, store)) { 265 if (!annot.isActionAllowed("delete", authUser, store)) {
267 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 266 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
268 return null; 267 return null;
269 } 268 }
270 } 269 }
271
272 // delete annotation 270 // delete annotation
273 store.deleteAnnotationById(id); 271 store.deleteAnnotationById(id);
274 setStatus(Status.SUCCESS_NO_CONTENT); 272 setStatus(Status.SUCCESS_NO_CONTENT);
275 return null; 273 return null;
276 } 274 }