Changeset 63:9f8c9611848a in AnnotationManagerN4J for src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r61 r63 13 13 import org.json.JSONObject; 14 14 import org.restlet.data.Form; 15 import org.restlet.data.Parameter;16 15 import org.restlet.data.Status; 17 16 import org.restlet.ext.json.JsonRepresentation; … … 60 59 61 60 if (id == null) { 62 return getAllAnnotations(authUser); 63 } 64 61 // no id -- send all annotations 62 Form form = getRequest().getResourceRef().getQueryAsForm(); 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 70 AnnotationStore store = getAnnotationStore(); 66 71 Annotation annot = store.getAnnotationById(id); … … 71 76 } 72 77 JSONObject result = createAnnotatorJson(annot, (authUser == null)); 73 logger.debug("sending:");74 logger.debug(result);75 78 return new JsonRepresentation(result); 76 79 } else { … … 81 84 } 82 85 83 private Representation getAllAnnotations(Person authUser) { 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 86 private Representation getAllAnnotations(Person authUser, int limit, int offset, String sortBy) { 93 87 AnnotationStore store = getAnnotationStore(); 94 88 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); 95 89 90 // read all annotations 96 91 List<Annotation> annotations = store.getAnnotations(null, null); 97 92 for (Annotation annotation : annotations) { 98 93 // check permission 99 94 if (!annotation.isActionAllowed("read", authUser, store)) continue; 100 95 // add annotation to list 101 96 JSONObject jo = createAnnotatorJson(annotation, false); 102 97 results.add(jo); 103 98 } 104 99 100 // sort if necessary 105 101 if (sortBy != null) { 106 102 JSONObjectComparator.sortAnnotations(results, sortBy); 107 103 } 108 109 JSONArray resultsJa = new JSONArray(); 104 105 // put in JSON list 106 JSONArray rows = new JSONArray(); 107 int cnt = 0; 110 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 … … 115 116 JSONObject result = new JSONObject(); 116 117 try { 117 result.put("rows", r esultsJa);118 result.put("total", r esultsJa.length());118 result.put("rows", rows); 119 result.put("total", rows.length()); 119 120 } catch (JSONException e) { 120 121 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); 121 122 return null; 122 123 } 123 logger.debug("sending:");124 logger.debug(result);125 124 return new JsonRepresentation(result); 126 125 } … … 234 233 return retRep; 235 234 } catch (JSONException e) { 236 e.printStackTrace();235 logger.error("Error in doPutJSON", e); 237 236 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 238 237 } catch (IOException e) { 239 e.printStackTrace();238 logger.error("Error in doPutJSON", e); 240 239 setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error"); 241 240 } … … 269 268 } 270 269 } 271 272 270 // delete annotation 273 271 store.deleteAnnotationById(id);
Note: See TracChangeset
for help on using the changeset viewer.