comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java @ 88:b406507a953d

upped version to 0.5. can use display name and groups from auth token.
author casties
date Tue, 03 Feb 2015 19:01:27 +0100
parents 25eb2e1df106
children cf44d9e1a4a7
comparison
equal deleted inserted replaced
87:2beafb8e19e4 88:b406507a953d
1 /**
2 * ReST API for accessing groups in the Annotation store.
3 */
4 package de.mpiwg.itgroup.annotations.restlet; 1 package de.mpiwg.itgroup.annotations.restlet;
5 2
6 /* 3 /*
7 * #%L 4 * #%L
8 * AnnotationManager 5 * AnnotationManager
37 import org.restlet.representation.Representation; 34 import org.restlet.representation.Representation;
38 import org.restlet.resource.Get; 35 import org.restlet.resource.Get;
39 36
40 import de.mpiwg.itgroup.annotations.Actor; 37 import de.mpiwg.itgroup.annotations.Actor;
41 import de.mpiwg.itgroup.annotations.Group; 38 import de.mpiwg.itgroup.annotations.Group;
39 import de.mpiwg.itgroup.annotations.Person;
42 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 40 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
43 41
44 42
45 /** 43 /**
46 * API for accessing groups in the Annotation store. 44 * API for accessing groups in the Annotation store.
64 */ 62 */
65 @Get("json") 63 @Get("json")
66 public Representation doGetJSON(Representation entity) { 64 public Representation doGetJSON(Representation entity) {
67 logger.fine("AnnotatorGroups doGetJSON!"); 65 logger.fine("AnnotatorGroups doGetJSON!");
68 setCorsHeaders(); 66 setCorsHeaders();
69 Form form = getRequest().getResourceRef().getQueryAsForm(); 67 // get user from auth token (preferred)
70 String user = form.getFirstValue("user"); 68 Person authUser = getUserFromAuthToken(entity);
71 String uri = form.getFirstValue("uri"); 69 JSONArray results = null;
70 if (authUser != null && authUser.groups != null) {
71 results = getGroupsFromPerson(authUser);
72 } else {
73 // get user or uri from request
74 Form form = getRequest().getResourceRef().getQueryAsForm();
75 String user = form.getFirstValue("user");
76 String uri = form.getFirstValue("uri");
77 results = getGroupsFromStore(uri, user);
78 }
79 // assemble result object
80 JSONObject result = new JSONObject();
81 try {
82 result.put("rows", results);
83 result.put("total", results.length());
84 } catch (JSONException e) {
85 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
86 return null;
87 }
88 logger.fine("sending:");
89 logger.fine(result.toString());
90 return new JsonRepresentation(result);
91 }
92
93 public JSONArray getGroupsFromPerson(Person person) {
94 JSONArray results = new JSONArray();
95 for (String group : person.groups) {
96 JSONObject jo = new JSONObject();
97 try {
98 jo.put("id", group);
99 jo.put("name", group);
100 } catch (JSONException e) {
101 }
102 results.put(jo);
103 }
104 return results;
105 }
106
107 public JSONArray getGroupsFromStore(String uri, String user) {
108 JSONArray results = new JSONArray();
72 if (uri == null || uri.isEmpty()) { 109 if (uri == null || uri.isEmpty()) {
73 // get uri from user-id 110 // get uri from user-id
74 uri = Actor.getUriFromId(user, false); 111 uri = Actor.getUriFromId(user, false);
75 } 112 }
76 JSONArray results = new JSONArray();
77 AnnotationStore store = getAnnotationStore(); 113 AnnotationStore store = getAnnotationStore();
78 Node person = store.getPersonNodeByUri(uri); 114 Node person = store.getPersonNodeByUri(uri);
79 if (person != null) { 115 if (person != null) {
80 List<Group> groups = store.getGroupsForPersonNode(person); 116 List<Group> groups = store.getGroupsForPersonNode(person);
81 for (Group group : groups) { 117 for (Group group : groups) {
87 } catch (JSONException e) { 123 } catch (JSONException e) {
88 } 124 }
89 results.put(jo); 125 results.put(jo);
90 } 126 }
91 } 127 }
92 // assemble result object 128 return results;
93 JSONObject result = new JSONObject();
94 try {
95 result.put("rows", results);
96 result.put("total", results.length());
97 } catch (JSONException e) {
98 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
99 return null;
100 }
101 logger.fine("sending:");
102 logger.fine(result.toString());
103 return new JsonRepresentation(result);
104
105 } 129 }
106 } 130 }