Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java @ 70:2b1e6df5e21a
added lgpl_v3 license information.
author | casties |
---|---|
date | Thu, 06 Mar 2014 15:09:04 +0100 |
parents | 64aa756c60cc |
children | 25eb2e1df106 |
rev | line source |
---|---|
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
1 /** |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
2 * |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
3 */ |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
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 | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
28 import java.util.List; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
29 |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
30 import org.restlet.data.Form; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
31 import org.restlet.data.MediaType; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
32 import org.restlet.data.Reference; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
33 import org.restlet.data.Status; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
34 import org.restlet.representation.Representation; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
35 import org.restlet.representation.StringRepresentation; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
36 import org.restlet.resource.Get; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
37 import org.restlet.resource.Post; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
38 |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
39 import de.mpiwg.itgroup.annotations.Person; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
40 |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
41 /** |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
42 * Resource class for the members of an annotation group. |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
43 * |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
44 * @author casties |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
45 * |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
46 */ |
24 | 47 public class GroupMembersResource extends GroupResource { |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
48 |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
49 /** |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
50 * GET with HTML content type. Shows the members of the group. |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
51 * |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
52 * @param entity |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
53 * @return |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
54 */ |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
55 @Get("html") |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
56 public Representation doGetHTML(Representation entity) { |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
57 // id from URI /annotations/groups/{id}/members |
24 | 58 if (requestId == null || requestId.isEmpty()) { |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
59 // invalid id |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
60 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
61 return null; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
62 } |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
63 String result = null; |
27
6bc918105c9a
back to this.getReference, problem was with mod_proxy (ProxyPreserveHost).
casties
parents:
26
diff
changeset
|
64 Reference thisUrl = this.getReference(); |
24 | 65 Reference groupsUrl = thisUrl.getParentRef(); |
66 result = "<html><body>\n<h1>Group members</h1>\n"; | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
67 result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId()); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
68 result += "<p>Members:</p>\n"; |
27
6bc918105c9a
back to this.getReference, problem was with mod_proxy (ProxyPreserveHost).
casties
parents:
26
diff
changeset
|
69 result += String.format("<form method=\"post\" action=\"%s\">", thisUrl); |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
70 result += "<table>"; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
71 List<Person> members = store.getMembersOfGroup(group); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
72 for (Person p : members) { |
24 | 73 result += String.format("<tr><td>%s</td><td>(%s)</td>", p.getName(), p.getIdString()); |
25 | 74 result += String.format("<td><input type=\"submit\" name=\"del_member\" value=\"delete:%s\"></td></tr>\n", p.getIdString()); |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
75 } |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
76 result += "</table>\n"; |
25 | 77 result += "</form>\n"; |
24 | 78 result += String.format("<form method=\"post\" action=\"%s\">\n", thisUrl); |
79 result += "<p>Add new member: <select name=\"add_member\">\n"; | |
45 | 80 for (Person p : store.getPersons("uri", "*")) { |
24 | 81 result += String.format("<option value=\"%s\">%s</option>\n", p.getIdString(), p.getName()); |
82 } | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
83 result += "</select>\n"; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
84 result += "<input type=\"submit\"/>\n"; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
85 result += "</form>\n"; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
86 result += "</body>\n</html>"; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
87 |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
88 return new StringRepresentation(result, MediaType.TEXT_HTML); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
89 } |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
90 |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
91 /** |
24 | 92 * POST adds or deletes members of the Group. |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
93 * |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
94 * @return |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
95 */ |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
96 @Post |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
97 public Representation doPost(Representation entity) { |
24 | 98 logger.debug("GroupMembersResource doPost!"); |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
99 // TODO: do authentication |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
100 Form form = new Form(entity); |
24 | 101 String addMemberId = form.getFirstValue("add_member"); |
102 String delMemberId = form.getFirstValue("del_member"); | |
103 if (group == null || ((addMemberId == null || addMemberId.isEmpty()) | |
104 && (delMemberId == null || delMemberId.isEmpty()))) { | |
105 // no id | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
106 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
107 return null; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
108 } |
25 | 109 if (addMemberId != null) { |
110 logger.debug("adding member: "+addMemberId); | |
111 Person member = new Person(addMemberId); | |
112 store.addGroupMember(group, member); | |
113 } else if (delMemberId != null) { | |
114 if (delMemberId.startsWith("delete:")) { | |
115 delMemberId = delMemberId.substring(7); | |
116 } | |
117 logger.debug("deleting member: "+delMemberId); | |
118 Person member = new Person(delMemberId); | |
119 store.deleteGroupMember(group, member); | |
120 } | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
121 // return 303: see other |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
122 setStatus(Status.REDIRECTION_SEE_OTHER); |
24 | 123 // go get same URL |
124 Reference thisUrl = this.getReference(); | |
125 this.getResponse().setLocationRef(thisUrl); | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
126 return null; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
127 } |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
128 |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff
changeset
|
129 } |