diff src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.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 0731c4549065
children 2b1e6df5e21a
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java	Thu Nov 22 17:45:23 2012 +0100
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java	Fri Nov 23 17:55:04 2012 +0100
@@ -3,6 +3,7 @@
  */
 package de.mpiwg.itgroup.annotations.restlet;
 
+import java.util.ArrayList;
 import java.util.List;
 
 import org.json.JSONArray;
@@ -17,6 +18,7 @@
 import de.mpiwg.itgroup.annotations.Annotation;
 import de.mpiwg.itgroup.annotations.Person;
 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
+import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
 
 /**
  * Implements the "search" uri of the Annotator API. see
@@ -33,7 +35,7 @@
 
     /**
      * result for JSON content-type. optional search parameters: uri, user, limit,
-     * offset.
+     * offset, sortBy.
      * 
      * @param entity
      * @return
@@ -49,30 +51,47 @@
         Form form = getRequest().getResourceRef().getQueryAsForm();
         String uri = form.getFirstValue("uri");
         String user = form.getFirstValue("user");
-        String limit = form.getFirstValue("limit");
-        String offset = form.getFirstValue("offset");
+        int limit = getInt(form.getFirstValue("limit"));
+        int offset = getInt(form.getFirstValue("offset"));
+        String sortBy = form.getFirstValue("sortBy");
 
-        JSONArray results = new JSONArray();
         // do search
+        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
         logger.debug(String.format("searching for uri=%s user=%s", uri, user));
         AnnotationStore store = getAnnotationStore();
-        List<Annotation> annots = store.searchAnnotationByUriUser(uri, user, limit, offset);
+        List<Annotation> annots = store.searchAnnotationByUriUser(uri, user);
         for (Annotation annot : annots) {
             // check permission
             if (!annot.isActionAllowed("read", authUser, store)) continue;
             JSONObject jo = createAnnotatorJson(annot, (authUser == null));
             if (jo != null) {
-                results.put(jo);
+                results.add(jo);
             } else {
                 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
                 return null;
             }
         }
+
+        // sort if necessary
+        if (sortBy != null) {
+            JSONObjectComparator.sortAnnotations(results, sortBy);
+        }
+        
+        // put in JSON list
+        JSONArray rows = new JSONArray();
+        int cnt = 0;
+        for (JSONObject result : results) {
+            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", results);
-            result.put("total", results.length());
+            result.put("rows", rows);
+            result.put("total", rows.length());
         } catch (JSONException e) {
             setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
             return null;