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