1 | package de.mpiwg.itgroup.annotations.restlet; |
---|
2 | |
---|
3 | import java.io.UnsupportedEncodingException; |
---|
4 | import java.net.URLDecoder; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import org.json.JSONArray; |
---|
9 | import org.json.JSONException; |
---|
10 | import org.json.JSONObject; |
---|
11 | import org.restlet.data.Form; |
---|
12 | import org.restlet.data.Status; |
---|
13 | import org.restlet.ext.json.JsonRepresentation; |
---|
14 | import org.restlet.representation.Representation; |
---|
15 | import org.restlet.resource.Get; |
---|
16 | |
---|
17 | import de.mpiwg.itgroup.annotations.Annotation; |
---|
18 | import de.mpiwg.itgroup.annotations.Person; |
---|
19 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
20 | import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator; |
---|
21 | |
---|
22 | /** |
---|
23 | * API for accessing tags in the Annotation store. |
---|
24 | * |
---|
25 | * @author dwinter |
---|
26 | * |
---|
27 | */ |
---|
28 | public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl { |
---|
29 | protected String getAllowedMethodsForHeader() { |
---|
30 | return "OPTIONS,GET"; |
---|
31 | } |
---|
32 | |
---|
33 | @Get("json") |
---|
34 | public Representation doGetJSON(Representation entity) { |
---|
35 | logger.debug("AnnotatorAnnotatonsBytag doGetJSON!"); |
---|
36 | setCorsHeaders(); |
---|
37 | |
---|
38 | // do authentication |
---|
39 | Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); |
---|
40 | logger.debug("request authenticated=" + authUser); |
---|
41 | |
---|
42 | String id = (String) getRequest().getAttributes().get("id"); |
---|
43 | // URL decode |
---|
44 | try { |
---|
45 | id = URLDecoder.decode(id, "UTF-8"); |
---|
46 | } catch (UnsupportedEncodingException e) { |
---|
47 | // this shouldn't happen |
---|
48 | } |
---|
49 | logger.debug("annotation-id=" + id); |
---|
50 | |
---|
51 | Form form = getRequest().getResourceRef().getQueryAsForm(); |
---|
52 | String sortBy = form.getFirstValue("sortBy"); |
---|
53 | |
---|
54 | AnnotationStore store = getAnnotationStore(); |
---|
55 | String tagUri = BaseRestlet.TAGS_URI_PREFIX + id; |
---|
56 | List<Annotation> annotations = store.getAnnotationsByTag(tagUri); |
---|
57 | |
---|
58 | // JSONArray results = new JSONArray(); |
---|
59 | ArrayList<JSONObject> results = new ArrayList<JSONObject>(); |
---|
60 | |
---|
61 | for (Annotation annot : annotations) { |
---|
62 | // check permission |
---|
63 | if (!annot.isActionAllowed("read", authUser, store)) |
---|
64 | continue; |
---|
65 | |
---|
66 | JSONObject jo = createAnnotatorJson(annot, false); |
---|
67 | results.add(jo); |
---|
68 | } |
---|
69 | |
---|
70 | if (sortBy != null) { |
---|
71 | JSONObjectComparator.sortAnnotations(results, sortBy); |
---|
72 | } |
---|
73 | |
---|
74 | JSONArray resultsJa = new JSONArray(); |
---|
75 | for (JSONObject result : results) { |
---|
76 | resultsJa.put(result); |
---|
77 | } |
---|
78 | |
---|
79 | // assemble result object |
---|
80 | JSONObject result = new JSONObject(); |
---|
81 | try { |
---|
82 | result.put("rows", resultsJa); |
---|
83 | result.put("total", resultsJa.length()); |
---|
84 | } catch (JSONException e) { |
---|
85 | setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
---|
86 | return null; |
---|
87 | } |
---|
88 | return new JsonRepresentation(result); |
---|
89 | } |
---|
90 | |
---|
91 | } |
---|