annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java @ 70:2b1e6df5e21a

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