Changeset 63:9f8c9611848a in AnnotationManagerN4J for src
- Timestamp:
- Nov 23, 2012, 4:55:04 PM (12 years ago)
- Branch:
- default
- Location:
- src/main/java/de/mpiwg/itgroup/annotations
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r59 r63 790 790 * @param uri 791 791 * @param userUri 792 * @param limit 793 * @param offset 794 * @return 795 */ 796 public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri, String limit, String offset) { 792 * @return 793 */ 794 public List<Annotation> searchAnnotationByUriUser(String targetUri, String userUri) { 797 795 List<Annotation> annotations = new ArrayList<Annotation>(); 798 796 if (targetUri != null) { 799 797 // there should be only one 800 Node target = getNode Index(NodeTypes.TARGET).get("uri", targetUri).getSingle();798 Node target = getNodeFromIndex("uri", targetUri, NodeTypes.TARGET); 801 799 if (target != null) { 802 800 Iterable<Relationship> relations = target.getRelationships(RelationTypes.ANNOTATES); … … 829 827 } 830 828 // TODO: if both uri and user are given we should intersect 829 831 830 return annotations; 832 831 } -
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); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r61 r63 387 387 String y = geom.getString("y"); 388 388 fragment = String.format("xywh=fraction:%s,%s,0,0", x, y); 389 } else if (type.equalsIgnoreCase(" point")) {389 } else if (type.equalsIgnoreCase("rectangle")) { 390 390 String x = geom.getString("x"); 391 391 String y = geom.getString("y"); 392 String width = shape.getString("width");393 String height = shape.getString("height");392 String width = geom.getString("width"); 393 String height = geom.getString("height"); 394 394 fragment = String.format("xywh=fraction:%s,%s,%s,%s", x, y, width, height); 395 395 } else { … … 417 417 String startOffset = range.getString("startOffset"); 418 418 String endOffset = range.getString("endOffset"); 419 420 419 String fragment = String.format( 421 420 "xpointer(start-point(string-range(\"%s\",%s,1))/range-to(end-point(string-range(\"%s\",%s,1))))", start, … … 560 559 try { 561 560 if (jo.has("ranges")) { 562 JSONObject ranges = jo.getJSONArray("ranges").getJSONObject(0); 563 annot.setFragmentType(FragmentTypes.XPOINTER); 564 String fragment = parseRange(ranges); 565 annot.setTargetFragment(fragment); 561 JSONArray ranges = jo.getJSONArray("ranges"); 562 if (ranges.length() > 0) { 563 JSONObject range = ranges.getJSONObject(0); 564 annot.setFragmentType(FragmentTypes.XPOINTER); 565 String fragment = parseRange(range); 566 annot.setTargetFragment(fragment); 567 } 566 568 } 567 569 } catch (JSONException e) { … … 570 572 try { 571 573 if (jo.has("shapes")) { 572 JSONObject shapes = jo.getJSONArray("shapes").getJSONObject(0); 573 annot.setFragmentType(FragmentTypes.AREA); 574 String fragment = parseShape(shapes); 575 annot.setTargetFragment(fragment); 574 JSONArray shapes = jo.getJSONArray("shapes"); 575 if (shapes.length() > 0) { 576 JSONObject shape = shapes.getJSONObject(0); 577 annot.setFragmentType(FragmentTypes.AREA); 578 String fragment = parseShape(shape); 579 annot.setTargetFragment(fragment); 580 } 576 581 } 577 582 } catch (JSONException e) { … … 581 586 try { 582 587 if (jo.has("areas")) { 583 JSONObject area = jo.getJSONArray("areas").getJSONObject(0); 584 annot.setFragmentType(FragmentTypes.AREA); 585 String fragment = parseArea(area); 586 annot.setTargetFragment(fragment); 588 JSONArray areas = jo.getJSONArray("areas"); 589 if (areas.length() > 0) { 590 JSONObject area = areas.getJSONObject(0); 591 annot.setFragmentType(FragmentTypes.AREA); 592 String fragment = parseArea(area); 593 annot.setTargetFragment(fragment); 594 } 587 595 } 588 596 } catch (JSONException e) { 589 597 // nothing to do 590 598 } 591 599 // no fragment is an error 600 if (annot.getFragmentType() == null || annot.getTargetFragment() == null) { 601 throw new JSONException("Annotation has no valid target fragment!"); 602 } 603 592 604 /* 593 605 * permissions … … 650 662 } 651 663 652 public float getFloat(String s) {664 public static float getFloat(String s) { 653 665 try { 654 666 return Float.parseFloat(s); … … 657 669 return 0f; 658 670 } 671 672 public static int getInt(String s) { 673 try { 674 return Integer.parseInt(s); 675 } catch (NumberFormatException e) { 676 } 677 return 0; 678 } 659 679 } -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorRestlet.java
r62 r63 15 15 public class AnnotatorRestlet extends BaseRestlet { 16 16 17 public final String version = "AnnotationManagerN4J/Annotator 0.3. 0";17 public final String version = "AnnotationManagerN4J/Annotator 0.3.1"; 18 18 19 19 public static Logger logger = Logger.getLogger(AnnotatorRestlet.class); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java
r32 r63 4 4 package de.mpiwg.itgroup.annotations.restlet; 5 5 6 import java.util.ArrayList; 6 7 import java.util.List; 7 8 … … 18 19 import de.mpiwg.itgroup.annotations.Person; 19 20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 21 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; 20 22 21 23 /** … … 34 36 /** 35 37 * result for JSON content-type. optional search parameters: uri, user, limit, 36 * offset .38 * offset, sortBy. 37 39 * 38 40 * @param entity … … 50 52 String uri = form.getFirstValue("uri"); 51 53 String user = form.getFirstValue("user"); 52 String limit = form.getFirstValue("limit"); 53 String offset = form.getFirstValue("offset"); 54 int limit = getInt(form.getFirstValue("limit")); 55 int offset = getInt(form.getFirstValue("offset")); 56 String sortBy = form.getFirstValue("sortBy"); 54 57 55 JSONArray results = new JSONArray();56 58 // do search 59 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); 57 60 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); 58 61 AnnotationStore store = getAnnotationStore(); 59 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user , limit, offset);62 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user); 60 63 for (Annotation annot : annots) { 61 64 // check permission … … 63 66 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); 64 67 if (jo != null) { 65 results. put(jo);68 results.add(jo); 66 69 } else { 67 70 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); … … 69 72 } 70 73 } 74 75 // sort if necessary 76 if (sortBy != null) { 77 JSONObjectComparator.sortAnnotations(results, sortBy); 78 } 79 80 // put in JSON list 81 JSONArray rows = new JSONArray(); 82 int cnt = 0; 83 for (JSONObject result : results) { 84 cnt += 1; 85 if (offset > 0 && cnt < offset) continue; 86 rows.put(result); 87 if (limit > 0 && cnt >= limit) break; 88 } 89 71 90 // assemble result object 72 91 JSONObject result = new JSONObject(); 73 92 try { 74 result.put("rows", r esults);75 result.put("total", r esults.length());93 result.put("rows", rows); 94 result.put("total", rows.length()); 76 95 } catch (JSONException e) { 77 96 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); -
src/main/java/de/mpiwg/itgroup/annotations/restlet/utils/JSONObjectComparator.java
r31 r63 3 3 import java.util.Comparator; 4 4 import java.util.List; 5 6 import de.mpiwg.itgroup.annotations.Annotation;7 5 8 6 import org.json.JSONException;
Note: See TracChangeset
for help on using the changeset viewer.