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