annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
1 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
2 * Implements the "search" uri of the Annotator API.
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
3 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
4 package de.mpiwg.itgroup.annotations.restlet;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
5
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
6 import java.util.ArrayList;
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
7 import java.util.List;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
8
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
9 import org.json.JSONArray;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
10 import org.json.JSONException;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
11 import org.json.JSONObject;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
12 import org.restlet.data.Form;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
13 import org.restlet.data.Status;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
14 import org.restlet.ext.json.JsonRepresentation;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
15 import org.restlet.representation.Representation;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
16 import org.restlet.resource.Get;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
17
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
18 import de.mpiwg.itgroup.annotations.Annotation;
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
19 import de.mpiwg.itgroup.annotations.Person;
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
21 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
22
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
23 /**
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
24 * Implements the "search" uri of the Annotator API. see
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
25 * <https://github.com/okfn/annotator/wiki/Storage>
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
26 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
27 * @author casties
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
28 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
29 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
30 public class AnnotatorSearch extends AnnotatorResourceImpl {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
31
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
32 protected String getAllowedMethodsForHeader() {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
33 return "OPTIONS,GET";
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
34 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
35
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
36 /**
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
37 * result for JSON content-type. optional search parameters: uri, user, limit,
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
38 * offset, sortBy.
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
39 *
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
40 * @param entity
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
41 * @return
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
42 */
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
43 @Get("json")
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
44 public Representation doGetJSON(Representation entity) {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
45 logger.debug("AnnotatorSearch doGetJSON!");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
46 setCorsHeaders();
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
47 // do authentication
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
48 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
49 logger.debug("request authenticated=" + authUser);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
50
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
51 Form form = getRequest().getResourceRef().getQueryAsForm();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
52 String uri = form.getFirstValue("uri");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
53 String user = form.getFirstValue("user");
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
54 int limit = getInt(form.getFirstValue("limit"));
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
55 int offset = getInt(form.getFirstValue("offset"));
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
56 String sortBy = form.getFirstValue("sortBy");
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
57
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
58 // do search
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
59 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
60 logger.debug(String.format("searching for uri=%s user=%s", uri, user));
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
61 AnnotationStore store = getAnnotationStore();
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
62 List<Annotation> annots = store.searchAnnotationByUriUser(uri, user);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
63 for (Annotation annot : annots) {
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
64 // check permission
15
58357a4b86de ASSIGNED - # 249: Annotations shared in groups
casties
parents: 14
diff changeset
65 if (!annot.isActionAllowed("read", authUser, store)) continue;
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
66 JSONObject jo = createAnnotatorJson(annot, (authUser == null));
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
67 if (jo != null) {
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
68 results.add(jo);
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
69 } else {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
70 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
71 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
72 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
73 }
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
74
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
75 // sort if necessary
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
76 if (sortBy != null) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
77 JSONObjectComparator.sortAnnotations(results, sortBy);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
78 }
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
79
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
80 // put in JSON list
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
81 JSONArray rows = new JSONArray();
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
82 int cnt = 0;
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
83 for (JSONObject result : results) {
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
84 cnt += 1;
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
85 if (offset > 0 && cnt < offset) continue;
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
86 rows.put(result);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
87 if (limit > 0 && cnt >= limit) break;
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
88 }
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
89
14
629e15b345aa permissions mostly work. need more server-side checking.
casties
parents: 4
diff changeset
90 // assemble result object
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
91 JSONObject result = new JSONObject();
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
92 try {
63
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
93 result.put("rows", rows);
9f8c9611848a fixed bug with new rectangle shapes. added limit, offset and sortBy parameters to annotator/ and annotator/search.
casties
parents: 32
diff changeset
94 result.put("total", rows.length());
4
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
95 } catch (JSONException e) {
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
96 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
97 return null;
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
98 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
99
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
100 logger.debug("sending:");
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
101 logger.debug(result);
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
102 return new JsonRepresentation(result);
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
103 }
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
104
3599b29c393f store seems to work now :-)
casties
parents:
diff changeset
105 }