Mercurial > hg > AnnotationManagerN4J
comparison 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 |
comparison
equal
deleted
inserted
replaced
62:15569aa35d62 | 63:9f8c9611848a |
---|---|
1 /** | 1 /** |
2 * Implements the "search" uri of the Annotator API. | 2 * Implements the "search" uri of the Annotator API. |
3 */ | 3 */ |
4 package de.mpiwg.itgroup.annotations.restlet; | 4 package de.mpiwg.itgroup.annotations.restlet; |
5 | 5 |
6 import java.util.ArrayList; | |
6 import java.util.List; | 7 import java.util.List; |
7 | 8 |
8 import org.json.JSONArray; | 9 import org.json.JSONArray; |
9 import org.json.JSONException; | 10 import org.json.JSONException; |
10 import org.json.JSONObject; | 11 import org.json.JSONObject; |
15 import org.restlet.resource.Get; | 16 import org.restlet.resource.Get; |
16 | 17 |
17 import de.mpiwg.itgroup.annotations.Annotation; | 18 import de.mpiwg.itgroup.annotations.Annotation; |
18 import de.mpiwg.itgroup.annotations.Person; | 19 import de.mpiwg.itgroup.annotations.Person; |
19 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | 20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
21 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; | |
20 | 22 |
21 /** | 23 /** |
22 * Implements the "search" uri of the Annotator API. see | 24 * Implements the "search" uri of the Annotator API. see |
23 * <https://github.com/okfn/annotator/wiki/Storage> | 25 * <https://github.com/okfn/annotator/wiki/Storage> |
24 * | 26 * |
31 return "OPTIONS,GET"; | 33 return "OPTIONS,GET"; |
32 } | 34 } |
33 | 35 |
34 /** | 36 /** |
35 * result for JSON content-type. optional search parameters: uri, user, limit, | 37 * result for JSON content-type. optional search parameters: uri, user, limit, |
36 * offset. | 38 * offset, sortBy. |
37 * | 39 * |
38 * @param entity | 40 * @param entity |
39 * @return | 41 * @return |
40 */ | 42 */ |
41 @Get("json") | 43 @Get("json") |
47 logger.debug("request authenticated=" + authUser); | 49 logger.debug("request authenticated=" + authUser); |
48 | 50 |
49 Form form = getRequest().getResourceRef().getQueryAsForm(); | 51 Form form = getRequest().getResourceRef().getQueryAsForm(); |
50 String uri = form.getFirstValue("uri"); | 52 String uri = form.getFirstValue("uri"); |
51 String user = form.getFirstValue("user"); | 53 String user = form.getFirstValue("user"); |
52 String limit = form.getFirstValue("limit"); | 54 int limit = getInt(form.getFirstValue("limit")); |
53 String offset = form.getFirstValue("offset"); | 55 int offset = getInt(form.getFirstValue("offset")); |
56 String sortBy = form.getFirstValue("sortBy"); | |
54 | 57 |
55 JSONArray results = new JSONArray(); | |
56 // do search | 58 // do search |
59 ArrayList<JSONObject> results = new ArrayList<JSONObject>(); | |
57 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); | 60 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); |
58 AnnotationStore store = getAnnotationStore(); | 61 AnnotationStore store = getAnnotationStore(); |
59 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user, limit, offset); | 62 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user); |
60 for (Annotation annot : annots) { | 63 for (Annotation annot : annots) { |
61 // check permission | 64 // check permission |
62 if (!annot.isActionAllowed("read", authUser, store)) continue; | 65 if (!annot.isActionAllowed("read", authUser, store)) continue; |
63 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); | 66 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); |
64 if (jo != null) { | 67 if (jo != null) { |
65 results.put(jo); | 68 results.add(jo); |
66 } else { | 69 } else { |
67 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | 70 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
68 return null; | 71 return null; |
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 // assemble result object | 90 // assemble result object |
72 JSONObject result = new JSONObject(); | 91 JSONObject result = new JSONObject(); |
73 try { | 92 try { |
74 result.put("rows", results); | 93 result.put("rows", rows); |
75 result.put("total", results.length()); | 94 result.put("total", rows.length()); |
76 } catch (JSONException e) { | 95 } catch (JSONException e) { |
77 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); | 96 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
78 return null; | 97 return null; |
79 } | 98 } |
80 | 99 |