15
|
1 package de.mpiwg.itgroup.annotations.restlet;
|
|
2
|
70
|
3 /*
|
|
4 * #%L
|
|
5 * AnnotationManager
|
|
6 * %%
|
|
7 * Copyright (C) 2012 - 2014 MPIWG Berlin
|
|
8 * %%
|
|
9 * This program is free software: you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU Lesser General Public License as
|
|
11 * published by the Free Software Foundation, either version 3 of the
|
|
12 * License, or (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Lesser Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Lesser Public
|
|
20 * License along with this program. If not, see
|
|
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
22 * #L%
|
|
23 */
|
|
24
|
15
|
25 import java.util.List;
|
|
26
|
|
27 import org.json.JSONArray;
|
|
28 import org.json.JSONException;
|
|
29 import org.json.JSONObject;
|
|
30 import org.neo4j.graphdb.Node;
|
|
31 import org.restlet.data.Form;
|
|
32 import org.restlet.data.Status;
|
|
33 import org.restlet.ext.json.JsonRepresentation;
|
|
34 import org.restlet.representation.Representation;
|
|
35 import org.restlet.resource.Get;
|
|
36
|
|
37 import de.mpiwg.itgroup.annotations.Actor;
|
|
38 import de.mpiwg.itgroup.annotations.Group;
|
88
|
39 import de.mpiwg.itgroup.annotations.Person;
|
15
|
40 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
41
|
|
42
|
|
43 /**
|
|
44 * API for accessing groups in the Annotation store.
|
|
45 *
|
|
46 * @author casties
|
|
47 *
|
|
48 */
|
|
49 public class AnnotatorGroups extends AnnotatorResourceImpl {
|
|
50 protected String getAllowedMethodsForHeader() {
|
|
51 return "OPTIONS,GET";
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * GET with JSON content-type.
|
|
56 * Parameters:
|
|
57 * user: short user name
|
|
58 * uri: user uri
|
|
59 *
|
|
60 * @param entity
|
|
61 * @return
|
|
62 */
|
|
63 @Get("json")
|
|
64 public Representation doGetJSON(Representation entity) {
|
75
|
65 logger.fine("AnnotatorGroups doGetJSON!");
|
15
|
66 setCorsHeaders();
|
88
|
67 // get user from auth token (preferred)
|
|
68 Person authUser = getUserFromAuthToken(entity);
|
|
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();
|
15
|
109 if (uri == null || uri.isEmpty()) {
|
|
110 // get uri from user-id
|
|
111 uri = Actor.getUriFromId(user, false);
|
|
112 }
|
|
113 AnnotationStore store = getAnnotationStore();
|
|
114 Node person = store.getPersonNodeByUri(uri);
|
|
115 if (person != null) {
|
|
116 List<Group> groups = store.getGroupsForPersonNode(person);
|
|
117 for (Group group : groups) {
|
|
118 JSONObject jo = new JSONObject();
|
|
119 try {
|
|
120 jo.put("id", group.getId());
|
|
121 jo.put("name", group.getName());
|
|
122 jo.put("uri", group.getUriString());
|
|
123 } catch (JSONException e) {
|
|
124 }
|
|
125 results.put(jo);
|
|
126 }
|
|
127 }
|
88
|
128 return results;
|
15
|
129 }
|
|
130 }
|