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