diff 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
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java	Thu Nov 22 17:45:23 2012 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java	Fri Nov 23 17:55:04 2012 +0100
@@ -12,7 +12,6 @@
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.restlet.data.Form;
-import org.restlet.data.Parameter;
 import org.restlet.data.Status;
 import org.restlet.ext.json.JsonRepresentation;
 import org.restlet.representation.Representation;
@@ -59,9 +58,15 @@
         logger.debug("request authenticated=" + authUser);
 
         if (id == null) {
-            return getAllAnnotations(authUser);
+            // no id -- send all annotations
+            Form form = getRequest().getResourceRef().getQueryAsForm();
+            int limit = getInt(form.getFirstValue("limit"));
+            int offset = getInt(form.getFirstValue("offset"));
+            String sortBy = form.getFirstValue("sortBy");
+            return getAllAnnotations(authUser, limit, offset, sortBy);
         }
 
+        // send annotation with id
         AnnotationStore store = getAnnotationStore();
         Annotation annot = store.getAnnotationById(id);
         if (annot != null) {
@@ -70,8 +75,6 @@
                 return null;
             }
             JSONObject result = createAnnotatorJson(annot, (authUser == null));
-            logger.debug("sending:");
-            logger.debug(result);
             return new JsonRepresentation(result);
         } else {
             // not found
@@ -80,48 +83,44 @@
         }
     }
 
-    private Representation getAllAnnotations(Person authUser) {
-
-        Form form = getRequest().getResourceRef().getQueryAsForm();
-        String sortBy = null;
-        for (Parameter parameter : form) {
-            if (parameter.getName().equals("sortBy")) {
-                sortBy = parameter.getValue();
-            }
-        }
-
+    private Representation getAllAnnotations(Person authUser, int limit, int offset, String sortBy) {
         AnnotationStore store = getAnnotationStore();
         ArrayList<JSONObject> results = new ArrayList<JSONObject>();
-
+        
+        // read all annotations
         List<Annotation> annotations = store.getAnnotations(null, null);
         for (Annotation annotation : annotations) {
             // check permission
             if (!annotation.isActionAllowed("read", authUser, store)) continue;
-
+            // add annotation to list
             JSONObject jo = createAnnotatorJson(annotation, false);
             results.add(jo);
         }
 
+        // sort if necessary
         if (sortBy != null) {
             JSONObjectComparator.sortAnnotations(results, sortBy);
         }
-
-        JSONArray resultsJa = new JSONArray();
+        
+        // put in JSON list
+        JSONArray rows = new JSONArray();
+        int cnt = 0;
         for (JSONObject result : results) {
-            resultsJa.put(result);
+            cnt += 1;
+            if (offset > 0 && cnt < offset) continue;
+            rows.put(result);
+            if (limit > 0 && cnt >= limit) break;
         }
 
         // assemble result object
         JSONObject result = new JSONObject();
         try {
-            result.put("rows", resultsJa);
-            result.put("total", resultsJa.length());
+            result.put("rows", rows);
+            result.put("total", rows.length());
         } catch (JSONException e) {
             setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
             return null;
         }
-        logger.debug("sending:");
-        logger.debug(result);
         return new JsonRepresentation(result);
     }
 
@@ -233,10 +232,10 @@
             JsonRepresentation retRep = new JsonRepresentation(jo);
             return retRep;
         } catch (JSONException e) {
-            e.printStackTrace();
+            logger.error("Error in doPutJSON", e);
             setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         } catch (IOException e) {
-            e.printStackTrace();
+            logger.error("Error in doPutJSON", e);
             setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
         }
         return null;
@@ -268,7 +267,6 @@
                 return null;
             }
         }
-
         // delete annotation
         store.deleteAnnotationById(id);
         setStatus(Status.SUCCESS_NO_CONTENT);