Mercurial > hg > AnnotationManagerN4J
changeset 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 | 15569aa35d62 |
children | c48435e7f312 |
files | src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/JSONObjectComparator.java |
diffstat | 6 files changed, 93 insertions(+), 59 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Thu Nov 22 17:45:23 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Fri Nov 23 17:55:04 2012 +0100 @@ -789,15 +789,13 @@ * * @param uri * @param userUri - * @param limit - * @param offset * @return */ - public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri, String limit, String offset) { + public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri) { List<Annotation> annotations = new ArrayList<Annotation>(); if (targetUri != null) { // there should be only one - Node target = getNodeIndex(NodeTypes.TARGET).get("uri", targetUri).getSingle(); + Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET); if (target != null) { Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES); for (Relationship relation : relations) { @@ -828,6 +826,7 @@ } } // TODO: if both uri and user are given we should intersect + return annotations; }
--- 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);
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java Thu Nov 22 17:45:23 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java Fri Nov 23 17:55:04 2012 +0100 @@ -386,11 +386,11 @@ String x = geom.getString("x"); String y = geom.getString("y"); fragment = String.format("xywh=fraction:%s,%s,0,0", x, y); - } else if (type.equalsIgnoreCase("point")) { + } else if (type.equalsIgnoreCase("rectangle")) { String x = geom.getString("x"); String y = geom.getString("y"); - String width = shape.getString("width"); - String height = shape.getString("height"); + String width = geom.getString("width"); + String height = geom.getString("height"); fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); } else { logger.error("Unable to parse this shape: " + shape); @@ -416,7 +416,6 @@ String end = range.getString("end"); String startOffset = range.getString("startOffset"); String endOffset = range.getString("endOffset"); - String fragment = String.format( "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start, startOffset, end, endOffset); @@ -559,20 +558,26 @@ */ try { if (jo.has("ranges")) { - JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0); - annot.setFragmentType(FragmentTypes.XPOINTER); - String fragment = parseRange(ranges); - annot.setTargetFragment(fragment); + JSONArray ranges = jo.getJSONArray("ranges"); + if (ranges.length() > 0) { + JSONObject range = ranges.getJSONObject(0); + annot.setFragmentType(FragmentTypes.XPOINTER); + String fragment = parseRange(range); + annot.setTargetFragment(fragment); + } } } catch (JSONException e) { // nothing to do } try { if (jo.has("shapes")) { - JSONObject shapes = jo.getJSONArray("shapes").getJSONObject(0); - annot.setFragmentType(FragmentTypes.AREA); - String fragment = parseShape(shapes); - annot.setTargetFragment(fragment); + JSONArray shapes = jo.getJSONArray("shapes"); + if (shapes.length() > 0) { + JSONObject shape = shapes.getJSONObject(0); + annot.setFragmentType(FragmentTypes.AREA); + String fragment = parseShape(shape); + annot.setTargetFragment(fragment); + } } } catch (JSONException e) { // nothing to do @@ -580,15 +585,22 @@ // deprecated areas type try { if (jo.has("areas")) { - JSONObject area = jo.getJSONArray("areas").getJSONObject(0); - annot.setFragmentType(FragmentTypes.AREA); - String fragment = parseArea(area); - annot.setTargetFragment(fragment); + JSONArray areas = jo.getJSONArray("areas"); + if (areas.length() > 0) { + JSONObject area = areas.getJSONObject(0); + annot.setFragmentType(FragmentTypes.AREA); + String fragment = parseArea(area); + annot.setTargetFragment(fragment); + } } } catch (JSONException e) { // nothing to do } - + // no fragment is an error + if (annot.getFragmentType() == null || annot.getTargetFragment() == null) { + throw new JSONException("Annotation has no valid target fragment!"); + } + /* * permissions */ @@ -649,11 +661,19 @@ return actor; } - public float getFloat(String s) { + public static float getFloat(String s) { try { return Float.parseFloat(s); } catch (NumberFormatException e) { } return 0f; } + + public static int getInt(String s) { + try { + return Integer.parseInt(s); + } catch (NumberFormatException e) { + } + return 0; + } }
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java Thu Nov 22 17:45:23 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java Fri Nov 23 17:55:04 2012 +0100 @@ -14,7 +14,7 @@ */ public class AnnotatorRestlet extends BaseRestlet { - public final String version = "AnnotationManagerN4J/Annotator 0.3.0"; + public final String version = "AnnotationManagerN4J/Annotator 0.3.1"; public static Logger logger = Logger.getLogger(AnnotatorRestlet.class);
--- 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;
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/JSONObjectComparator.java Thu Nov 22 17:45:23 2012 +0100 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/JSONObjectComparator.java Fri Nov 23 17:55:04 2012 +0100 @@ -3,8 +3,6 @@ import java.util.Comparator; import java.util.List; -import de.mpiwg.itgroup.annotations.Annotation; - import org.json.JSONException; import org.json.JSONObject;