28
|
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.NS;
|
37
|
18 import de.mpiwg.itgroup.annotations.Person;
|
28
|
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
|
37
|
38 // do authentication
|
|
39 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
|
|
40 logger.debug("request authenticated=" + authUser);
|
|
41
|
|
42
|
28
|
43 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
44
|
|
45 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
46 String sortBy=null;
|
|
47 for (Parameter parameter : form) {
|
|
48 if (parameter.getName().equals("sortBy")){
|
|
49 sortBy = parameter.getValue();
|
|
50 }
|
|
51 }
|
|
52
|
|
53 //String id = decodeJsonId(jsonId);
|
|
54 String id = jsonId;
|
|
55 logger.debug("annotation-id=" + id);
|
|
56
|
|
57 AnnotationStore store = getAnnotationStore();
|
|
58 String tagUri=NS.MPIWG_TAGS_URL+id;
|
|
59 List<Annotation> annotations = store.getAnnotationsByTag(tagUri);
|
|
60
|
|
61 //JSONArray results = new JSONArray();
|
|
62 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
|
|
63
|
|
64 for (Annotation annot : annotations) {
|
37
|
65 //check permission
|
|
66 if (!annot.isActionAllowed("read", authUser, store)) continue;
|
|
67
|
|
68 JSONObject jo = createAnnotatorJson(annot,false);
|
28
|
69 results.add(jo);
|
|
70 }
|
|
71
|
|
72 if (sortBy!=null){
|
31
|
73 JSONObjectComparator.sortAnnotations(results,sortBy);
|
28
|
74 }
|
|
75
|
|
76 JSONArray resultsJa = new JSONArray();
|
|
77 for (JSONObject result:results){
|
|
78 resultsJa.put(result);
|
|
79 }
|
|
80
|
|
81 // assemble result object
|
|
82 JSONObject result = new JSONObject();
|
|
83 try {
|
|
84 result.put("rows", resultsJa);
|
|
85 result.put("total", resultsJa.length());
|
|
86 } catch (JSONException e) {
|
|
87 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
|
|
88 return null;
|
|
89 }
|
|
90 logger.debug("sending:");
|
|
91 logger.debug(result);
|
|
92 return new JsonRepresentation(result);
|
|
93 }
|
|
94
|
|
95
|
|
96
|
|
97 } |