Ignore:
Timestamp:
Nov 23, 2012, 4:55:04 PM (11 years ago)
Author:
casties
Branch:
default
Message:

fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r61 r63  
    1313import org.json.JSONObject;
    1414import org.restlet.data.Form;
    15 import org.restlet.data.Parameter;
    1615import org.restlet.data.Status;
    1716import org.restlet.ext.json.JsonRepresentation;
     
    6059
    6160        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
    6570        AnnotationStore store = getAnnotationStore();
    6671        Annotation annot = store.getAnnotationById(id);
     
    7176            }
    7277            JSONObject result = createAnnotatorJson(annot, (authUser == null));
    73             logger.debug("sending:");
    74             logger.debug(result);
    7578            return new JsonRepresentation(result);
    7679        } else {
     
    8184    }
    8285
    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) {
    9387        AnnotationStore store = getAnnotationStore();
    9488        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
    95 
     89       
     90        // read all annotations
    9691        List<Annotation> annotations = store.getAnnotations(null, null);
    9792        for (Annotation annotation : annotations) {
    9893            // check permission
    9994            if (!annotation.isActionAllowed("read", authUser, store)) continue;
    100 
     95            // add annotation to list
    10196            JSONObject jo = createAnnotatorJson(annotation, false);
    10297            results.add(jo);
    10398        }
    10499
     100        // sort if necessary
    105101        if (sortBy != null) {
    106102            JSONObjectComparator.sortAnnotations(results, sortBy);
    107103        }
    108 
    109         JSONArray resultsJa = new JSONArray();
     104       
     105        // put in JSON list
     106        JSONArray rows = new JSONArray();
     107        int cnt = 0;
    110108        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;
    112113        }
    113114
     
    115116        JSONObject result = new JSONObject();
    116117        try {
    117             result.put("rows", resultsJa);
    118             result.put("total", resultsJa.length());
     118            result.put("rows", rows);
     119            result.put("total", rows.length());
    119120        } catch (JSONException e) {
    120121            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
    121122            return null;
    122123        }
    123         logger.debug("sending:");
    124         logger.debug(result);
    125124        return new JsonRepresentation(result);
    126125    }
     
    234233            return retRep;
    235234        } catch (JSONException e) {
    236             e.printStackTrace();
     235            logger.error("Error in doPutJSON", e);
    237236            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
    238237        } catch (IOException e) {
    239             e.printStackTrace();
     238            logger.error("Error in doPutJSON", e);
    240239            setStatus(Status.SERVER_ERROR_INTERNAL, "Other Error");
    241240        }
     
    269268            }
    270269        }
    271 
    272270        // delete annotation
    273271        store.deleteAnnotationById(id);
Note: See TracChangeset for help on using the changeset viewer.