28
|
1 /**
|
|
2 * ReST API for accessing groups in the Annotation store.
|
|
3 */
|
|
4 package de.mpiwg.itgroup.annotations.restlet;
|
|
5
|
|
6 import java.util.List;
|
|
7
|
|
8 import org.json.JSONArray;
|
|
9 import org.json.JSONException;
|
|
10 import org.json.JSONObject;
|
|
11 import org.neo4j.graphdb.Node;
|
|
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.NS;
|
|
18 import de.mpiwg.itgroup.annotations.Tag;
|
|
19 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
20
|
|
21
|
|
22 /**
|
|
23 * API for accessing tags in the Annotation store.
|
|
24 *
|
|
25 * @author dwinter
|
|
26 *
|
|
27 */
|
|
28 public class AnnotatorTags extends AnnotatorResourceImpl {
|
|
29 protected String getAllowedMethodsForHeader() {
|
|
30 return "OPTIONS,GET";
|
|
31 }
|
|
32
|
|
33 /**
|
|
34 * GET with JSON content-type.
|
|
35 * Parameters:
|
|
36 * user: short user name
|
|
37 * uri: user uri
|
|
38 *
|
|
39 * @param entity
|
|
40 * @return
|
|
41 */
|
|
42 @Get("json")
|
|
43 public Representation doGetJSON(Representation entity) {
|
|
44 logger.debug("AnnotatorGroups doGetJSON!");
|
|
45 setCorsHeaders();
|
|
46
|
|
47 String jsonId = (String) getRequest().getAttributes().get("id");
|
|
48 //String id = decodeJsonId(jsonId);
|
|
49 String id = jsonId;
|
|
50 logger.debug("annotation-id=" + id);
|
|
51
|
|
52 if (id==null){
|
|
53 return getAllTags();
|
|
54 } else {
|
|
55
|
|
56 return getTag(id);
|
|
57 }
|
|
58 }
|
|
59
|
|
60 protected Representation getTag(String id){
|
|
61 AnnotationStore store = getAnnotationStore();
|
|
62 String tagUri=NS.MPIWG_TAGS_URL+id;
|
|
63 Node tagNode = store.getTagNodeByUri(tagUri);
|
|
64 Tag tag = store.createTagFromNode(tagNode);
|
|
65 JSONObject jo = new JSONObject();
|
|
66 try {
|
|
67 jo.put("id", tag.getId());
|
|
68 jo.put("name", tag.getName());
|
|
69 jo.put("uri", tag.getUri());
|
|
70 } catch (JSONException e) {
|
|
71 }
|
|
72
|
|
73 return new JsonRepresentation(jo);
|
|
74 }
|
|
75 protected Representation getAllTags() {
|
|
76 JSONArray results = new JSONArray();
|
|
77 AnnotationStore store = getAnnotationStore();
|
|
78
|
|
79
|
|
80 List<Tag> tags = store.getTags(null, null);
|
|
81 for (Tag tag : tags) {
|
|
82 JSONObject jo = new JSONObject();
|
|
83 try {
|
|
84 jo.put("id", tag.getId());
|
|
85 jo.put("name", tag.getName());
|
|
86 jo.put("uri", tag.getUri());
|
|
87 } catch (JSONException e) {
|
|
88 }
|
|
89 results.put(jo);
|
|
90 }
|
|
91
|
|
92 // assemble result object
|
|
93 JSONObject result = new JSONObject();
|
|
94 try {
|
|
95 result.put("rows", results);
|
|
96 result.put("total", results.length());
|
|
97 } catch (JSONException e) {
|
|
98 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
|
|
99 return null;
|
|
100 }
|
|
101 logger.debug("sending:");
|
|
102 logger.debug(result);
|
|
103 return new JsonRepresentation(result);
|
|
104 }
|
|
105
|
|
106
|
|
107
|
|
108 }
|