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.neo4j.AnnotationStore; |
---|
21 | |
---|
22 | |
---|
23 | /** |
---|
24 | * API for accessing groups in the Annotation store. |
---|
25 | * |
---|
26 | * @author casties |
---|
27 | * |
---|
28 | */ |
---|
29 | public class AnnotatorGroups extends AnnotatorResourceImpl { |
---|
30 | protected String getAllowedMethodsForHeader() { |
---|
31 | return "OPTIONS,GET"; |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * GET with JSON content-type. |
---|
36 | * Parameters: |
---|
37 | * user: short user name |
---|
38 | * uri: user uri |
---|
39 | * |
---|
40 | * @param entity |
---|
41 | * @return |
---|
42 | */ |
---|
43 | @Get("json") |
---|
44 | public Representation doGetJSON(Representation entity) { |
---|
45 | logger.debug("AnnotatorGroups doGetJSON!"); |
---|
46 | setCorsHeaders(); |
---|
47 | Form form = getRequest().getResourceRef().getQueryAsForm(); |
---|
48 | String user = form.getFirstValue("user"); |
---|
49 | String uri = form.getFirstValue("uri"); |
---|
50 | if (uri == null || uri.isEmpty()) { |
---|
51 | // get uri from user-id |
---|
52 | uri = Actor.getUriFromId(user, false); |
---|
53 | } |
---|
54 | JSONArray results = new JSONArray(); |
---|
55 | AnnotationStore store = getAnnotationStore(); |
---|
56 | Node person = store.getPersonNodeByUri(uri); |
---|
57 | if (person != null) { |
---|
58 | List<Group> groups = store.getGroupsForPersonNode(person); |
---|
59 | for (Group group : groups) { |
---|
60 | JSONObject jo = new JSONObject(); |
---|
61 | try { |
---|
62 | jo.put("id", group.getId()); |
---|
63 | jo.put("name", group.getName()); |
---|
64 | jo.put("uri", group.getUriString()); |
---|
65 | } catch (JSONException e) { |
---|
66 | } |
---|
67 | results.put(jo); |
---|
68 | } |
---|
69 | } |
---|
70 | // assemble result object |
---|
71 | JSONObject result = new JSONObject(); |
---|
72 | try { |
---|
73 | result.put("rows", results); |
---|
74 | result.put("total", results.length()); |
---|
75 | } catch (JSONException e) { |
---|
76 | setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error"); |
---|
77 | return null; |
---|
78 | } |
---|
79 | logger.debug("sending:"); |
---|
80 | logger.debug(result); |
---|
81 | return new JsonRepresentation(result); |
---|
82 | |
---|
83 | } |
---|
84 | } |
---|