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