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