1 | /** |
---|
2 | * ReST API for accessing groups in the Annotation store. |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet; |
---|
5 | |
---|
6 | import java.io.UnsupportedEncodingException; |
---|
7 | import java.net.URLDecoder; |
---|
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 |
---|
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 | // 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); |
---|
62 | } |
---|
63 | } |
---|
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); |
---|
79 | } |
---|
80 | |
---|
81 | protected Representation getAllTags() { |
---|
82 | JSONArray results = new JSONArray(); |
---|
83 | AnnotationStore store = getAnnotationStore(); |
---|
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) { |
---|
93 | } |
---|
94 | results.put(jo); |
---|
95 | } |
---|
96 | |
---|
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); |
---|
109 | } |
---|
110 | |
---|
111 | } |
---|