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