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

added lgpl_v3 license information.
author casties
date Thu, 06 Mar 2014 15:09:04 +0100
parents 0731c4549065
children 25eb2e1df106
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
1 /**
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
2 *
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
3 */
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
5
70
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
6 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
7 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
8 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
9 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
11 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
12 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
13 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
14 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
15 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
16 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
17 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
20 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
21 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
22 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
23 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
25 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
26 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 32
diff changeset
27
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
28 import java.util.List;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
29
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
30 import org.apache.log4j.Logger;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
31 import org.restlet.data.Form;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
32 import org.restlet.data.MediaType;
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
33 import org.restlet.data.Reference;
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
34 import org.restlet.data.Status;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
35 import org.restlet.representation.Representation;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
36 import org.restlet.representation.StringRepresentation;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
37 import org.restlet.resource.Get;
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
38 import org.restlet.resource.Post;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
39 import org.restlet.resource.ResourceException;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
40 import org.restlet.resource.ServerResource;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
41
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
42 import de.mpiwg.itgroup.annotations.Actor;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
43 import de.mpiwg.itgroup.annotations.Group;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
44 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
45 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
46
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
47 /**
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
48 * Resource class for the list of annotation groups.
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
49 *
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
50 * @author casties
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
51 *
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
52 */
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
53 public class GroupsResource extends ServerResource {
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
54
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
55 public static Logger logger = Logger.getLogger(GroupsResource.class);
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
56
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
57 private AnnotationStore store;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
58
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
59 @Override
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
60 protected void doInit() throws ResourceException {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
61 super.doInit();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
62 // get store instance
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
63 if (store == null) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
64 store = ((BaseRestlet) getApplication()).getAnnotationStore();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
65 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
66 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
67
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
68 /**
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
69 * GET with HTML content type. Lists all groups.
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
70 *
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
71 * @param entity
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
72 * @return
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
73 */
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
74 @Get("html")
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
75 public Representation doGetHTML(Representation entity) {
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
76 String result = null;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
77 // get form parameter
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
78 Form f = this.getQuery();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
79 String form = f.getFirstValue("form");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
80 if (form != null && form.equals("new_group")) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
81 // output new group form
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
82 result = "<html><body>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
83 result += "<h1>New group</h1>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
84 result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
85 result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
86 result += "<table>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
87 result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
88 result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
89 result += "</table>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
90 result += "<p><input type=\"submit\"/></p>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
91 result += "</form>\n</body>\n</html>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
92 } else {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
93 // list all groups
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
94 result = "<html><body>\n<h1>Groups</h1>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
95 result += "<table>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
96 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
97 List<Group> groups = store.getGroups("uri", "*");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
98 for (Group group : groups) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
99 Reference groupUrl = this.getReference().clone();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
100 groupUrl.addSegment(group.getId());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
101 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(),
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
102 group.getName(), group.getUri());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
103 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
104 result += "</table>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
105 result += "<p><a href=\"?form=new_group\">Add new group</a></p>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 23
diff changeset
106 result += "</body>\n</html>";
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
107 }
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
108 return new StringRepresentation(result, MediaType.TEXT_HTML);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
109 }
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
110
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
111 /**
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
112 * POST creates a new Group.
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
113 *
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
114 * @return
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
115 */
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
116 @Post
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
117 public Representation doPost(Representation entity) {
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents: 22
diff changeset
118 logger.debug("AnnotationsUiGroups doPost!");
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
119 // TODO: do authentication
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
120 Form form = new Form(entity);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
121 String id = form.getFirstValue("id");
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
122 String name = form.getFirstValue("name");
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
123 if (id == null || id.isEmpty()) {
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
124 // invalid id
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
125 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
126 return null;
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
127 }
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
128 String gid = makeGroupId(id);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
129 Group newGroup = new Group(gid, null, name);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
130 Actor storedGroup = store.storeActor(newGroup);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
131 gid = storedGroup.getId();
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
132 // return 303: see other
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
133 setStatus(Status.REDIRECTION_SEE_OTHER);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
134 // go GET URL for this group
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
135 Reference groupUrl = this.getReference();
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
136 groupUrl.addSegment(gid);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
137 this.getResponse().setLocationRef(groupUrl);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
138 return null;
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
139 }
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
140
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
141 /**
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
142 * Returns a group id based on the given id.
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
143 *
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
144 * @param id
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
145 * @return
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
146 */
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
147 protected String makeGroupId(String id) {
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
148 // TODO: should we use different ids?
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
149 id = id.replaceAll("\\W", "_");
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
150 id = id.toLowerCase();
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
151 return id;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
152 }
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
153
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
154 protected AnnotationStore getAnnotationStore() {
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
155 if (store == null) {
19
f0f55ab768c9 more work on HTML UI.
casties
parents: 18
diff changeset
156 store = ((BaseRestlet) getApplication()).getAnnotationStore();
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
157 }
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
158 return store;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
159 }
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
160 }