comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 61:b8ef15c8c4a5

implemented new shape format for image annotations. minor cleanups.
author casties
date Thu, 22 Nov 2012 17:38:53 +0100
parents 5d4260344db5
children 9f8c9611848a
comparison
equal deleted inserted replaced
60:99d9afcfd04d 61:b8ef15c8c4a5
52 // id from URI /annotations/{id} 52 // id from URI /annotations/{id}
53 String jsonId = (String) getRequest().getAttributes().get("id"); 53 String jsonId = (String) getRequest().getAttributes().get("id");
54 String id = decodeJsonId(jsonId); 54 String id = decodeJsonId(jsonId);
55 logger.debug("annotation-id=" + id); 55 logger.debug("annotation-id=" + id);
56 56
57
58 // do authentication 57 // do authentication
59 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 58 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
60 logger.debug("request authenticated=" + authUser); 59 logger.debug("request authenticated=" + authUser);
61 60
62 if (id == null) { 61 if (id == null) {
63
64 return getAllAnnotations(authUser); 62 return getAllAnnotations(authUser);
65 } 63 }
66 64
67
68 AnnotationStore store = getAnnotationStore(); 65 AnnotationStore store = getAnnotationStore();
69 Annotation annot = store.getAnnotationById(id); 66 Annotation annot = store.getAnnotationById(id);
70 if (annot != null) { 67 if (annot != null) {
71 if (! annot.isActionAllowed("read", authUser, store)) { 68 if (!annot.isActionAllowed("read", authUser, store)) {
72 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 69 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
73 return null; 70 return null;
74 } 71 }
75 JSONObject result = createAnnotatorJson(annot, (authUser == null)); 72 JSONObject result = createAnnotatorJson(annot, (authUser == null));
76 logger.debug("sending:"); 73 logger.debug("sending:");
82 return null; 79 return null;
83 } 80 }
84 } 81 }
85 82
86 private Representation getAllAnnotations(Person authUser) { 83 private Representation getAllAnnotations(Person authUser) {
87 84
88 Form form = getRequest().getResourceRef().getQueryAsForm(); 85 Form form = getRequest().getResourceRef().getQueryAsForm();
89 String sortBy=null; 86 String sortBy = null;
90 for (Parameter parameter : form) { 87 for (Parameter parameter : form) {
91 if (parameter.getName().equals("sortBy")){ 88 if (parameter.getName().equals("sortBy")) {
92 sortBy = parameter.getValue(); 89 sortBy = parameter.getValue();
93 } 90 }
94 } 91 }
95 92
96 AnnotationStore store = getAnnotationStore(); 93 AnnotationStore store = getAnnotationStore();
97 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); 94 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
98 95
99 List<Annotation> annotations = store.getAnnotations(null, null); 96 List<Annotation> annotations = store.getAnnotations(null, null);
100 for (Annotation annotation : annotations) { 97 for (Annotation annotation : annotations) {
101 //check permission 98 // check permission
102 if (!annotation.isActionAllowed("read", authUser, store)) continue; 99 if (!annotation.isActionAllowed("read", authUser, store)) continue;
103 100
104 JSONObject jo = createAnnotatorJson(annotation,false); 101 JSONObject jo = createAnnotatorJson(annotation, false);
105 results.add(jo); 102 results.add(jo);
106 103 }
107 } 104
108 105 if (sortBy != null) {
109 if (sortBy!=null){ 106 JSONObjectComparator.sortAnnotations(results, sortBy);
110 JSONObjectComparator.sortAnnotations(results,sortBy); 107 }
111 } 108
112 109 JSONArray resultsJa = new JSONArray();
113 JSONArray resultsJa = new JSONArray(); 110 for (JSONObject result : results) {
114 for (JSONObject result:results){ 111 resultsJa.put(result);
115 resultsJa.put(result); 112 }
116 } 113
117
118 // assemble result object 114 // assemble result object
119 JSONObject result = new JSONObject(); 115 JSONObject result = new JSONObject();
120 try { 116 try {
121 result.put("rows", resultsJa); 117 result.put("rows", resultsJa);
122 result.put("total", resultsJa.length()); 118 result.put("total", resultsJa.length());
127 logger.debug("sending:"); 123 logger.debug("sending:");
128 logger.debug(result); 124 logger.debug(result);
129 return new JsonRepresentation(result); 125 return new JsonRepresentation(result);
130 } 126 }
131 127
132 128 /**
133
134
135
136 /**
137 * POST with JSON content-type. Creates a new Annotation. 129 * POST with JSON content-type. Creates a new Annotation.
138 * 130 *
139 * @return 131 * @return
140 */ 132 */
141 @Post("json") 133 @Post("json")
142 public Representation doPostJson(Representation entity) { 134 public Representation doPostJson(Representation entity) {
143 logger.debug("AnnotatorAnnotations doPostJSON!"); 135 logger.debug("AnnotatorAnnotations doPostJSON!");
144 // set headers 136 // set headers
145 setCorsHeaders(); 137 setCorsHeaders();
146 138
147 // do authentication TODO: who's allowed to create? 139 // do authentication TODO: who's allowed to create?
148 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 140 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
149 logger.debug("request authenticated=" + authUser); 141 logger.debug("request authenticated=" + authUser);
150 if (authUser == null) { 142 if (authUser == null) {
151 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 143 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
152 return null; 144 return null;
219 Annotation storedAnnot = store.getAnnotationById(id); 211 Annotation storedAnnot = store.getAnnotationById(id);
220 if (storedAnnot == null) { 212 if (storedAnnot == null) {
221 setStatus(Status.CLIENT_ERROR_NOT_FOUND); 213 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
222 return null; 214 return null;
223 } 215 }
224 if (! storedAnnot.isActionAllowed("update", authUser, store)) { 216 if (!storedAnnot.isActionAllowed("update", authUser, store)) {
225 setStatus(Status.CLIENT_ERROR_FORBIDDEN); 217 setStatus(Status.CLIENT_ERROR_FORBIDDEN);
226 return null; 218 return null;
227 } 219 }
228 // update from posted JSON 220 // update from posted JSON
229 annot = updateAnnotation(storedAnnot, jo, entity); 221 annot = updateAnnotation(storedAnnot, jo, entity);
269 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 261 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
270 logger.debug("request authenticated=" + authUser); 262 logger.debug("request authenticated=" + authUser);
271 AnnotationStore store = getAnnotationStore(); 263 AnnotationStore store = getAnnotationStore();
272 Annotation annot = store.getAnnotationById(id); 264 Annotation annot = store.getAnnotationById(id);
273 if (annot != null) { 265 if (annot != null) {
274 if (! annot.isActionAllowed("delete", authUser, store)) { 266 if (!annot.isActionAllowed("delete", authUser, store)) {
275 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 267 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
276 return null; 268 return null;
277 } 269 }
278 } 270 }
279 271
280 // delete annotation 272 // delete annotation
281 store.deleteAnnotationById(id); 273 store.deleteAnnotationById(id);
282 setStatus(Status.SUCCESS_NO_CONTENT); 274 setStatus(Status.SUCCESS_NO_CONTENT);
283 return null; 275 return null;
284 } 276 }