Mercurial > hg > AnnotationManagerN4J
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 |
rev | line source |
---|---|
18 | 1 /** |
2 * | |
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 | 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 | |
18 | 28 import java.util.List; |
29 | |
30 import org.apache.log4j.Logger; | |
31 import org.restlet.data.Form; | |
32 import org.restlet.data.MediaType; | |
22 | 33 import org.restlet.data.Reference; |
34 import org.restlet.data.Status; | |
18 | 35 import org.restlet.representation.Representation; |
36 import org.restlet.representation.StringRepresentation; | |
37 import org.restlet.resource.Get; | |
22 | 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 | 40 import org.restlet.resource.ServerResource; |
41 | |
22 | 42 import de.mpiwg.itgroup.annotations.Actor; |
18 | 43 import de.mpiwg.itgroup.annotations.Group; |
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 | 46 |
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 | 50 * @author casties |
22 | 51 * |
18 | 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 | 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 | 56 |
57 private AnnotationStore store; | |
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 | 68 /** |
69 * GET with HTML content type. Lists all groups. | |
70 * | |
71 * @param entity | |
72 * @return | |
73 */ | |
18 | 74 @Get("html") |
22 | 75 public Representation doGetHTML(Representation entity) { |
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 | 107 } |
22 | 108 return new StringRepresentation(result, MediaType.TEXT_HTML); |
109 } | |
110 | |
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 | 113 * |
114 * @return | |
115 */ | |
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 | 119 // TODO: do authentication |
120 Form form = new Form(entity); | |
121 String id = form.getFirstValue("id"); | |
122 String name = form.getFirstValue("name"); | |
123 if (id == null || id.isEmpty()) { | |
124 // invalid id | |
125 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
126 return null; | |
127 } | |
128 String gid = makeGroupId(id); | |
129 Group newGroup = new Group(gid, null, name); | |
130 Actor storedGroup = store.storeActor(newGroup); | |
131 gid = storedGroup.getId(); | |
132 // return 303: see other | |
133 setStatus(Status.REDIRECTION_SEE_OTHER); | |
134 // go GET URL for this group | |
135 Reference groupUrl = this.getReference(); | |
136 groupUrl.addSegment(gid); | |
137 this.getResponse().setLocationRef(groupUrl); | |
138 return null; | |
139 } | |
140 | |
141 /** | |
142 * Returns a group id based on the given id. | |
143 * | |
144 * @param id | |
145 * @return | |
146 */ | |
147 protected String makeGroupId(String id) { | |
148 // TODO: should we use different ids? | |
149 id = id.replaceAll("\\W", "_"); | |
150 id = id.toLowerCase(); | |
151 return id; | |
18 | 152 } |
153 | |
154 protected AnnotationStore getAnnotationStore() { | |
155 if (store == null) { | |
19 | 156 store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
18 | 157 } |
158 return store; | |
159 } | |
160 } |