Mercurial > hg > AnnotationManagerN4J
annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java @ 32:0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
author | casties |
---|---|
date | Tue, 25 Sep 2012 21:59:21 +0200 |
parents | d22d01ba953a |
children | 2b1e6df5e21a |
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 |
6 import java.util.List; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 import org.restlet.data.Form; | |
10 import org.restlet.data.MediaType; | |
22 | 11 import org.restlet.data.Reference; |
12 import org.restlet.data.Status; | |
18 | 13 import org.restlet.representation.Representation; |
14 import org.restlet.representation.StringRepresentation; | |
15 import org.restlet.resource.Get; | |
22 | 16 import org.restlet.resource.Post; |
32
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
17 import org.restlet.resource.ResourceException; |
18 | 18 import org.restlet.resource.ServerResource; |
19 | |
22 | 20 import de.mpiwg.itgroup.annotations.Actor; |
18 | 21 import de.mpiwg.itgroup.annotations.Group; |
22 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
23 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; |
18 | 24 |
25 /** | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
26 * Resource class for the list of annotation groups. |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
27 * |
18 | 28 * @author casties |
22 | 29 * |
18 | 30 */ |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
31 public class GroupsResource extends ServerResource { |
18 | 32 |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
33 public static Logger logger = Logger.getLogger(GroupsResource.class); |
18 | 34 |
35 private AnnotationStore store; | |
36 | |
32
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
37 @Override |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
38 protected void doInit() throws ResourceException { |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
39 super.doInit(); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
40 // get store instance |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
41 if (store == null) { |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
42 store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
43 } |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
44 } |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
45 |
22 | 46 /** |
47 * GET with HTML content type. Lists all groups. | |
48 * | |
49 * @param entity | |
50 * @return | |
51 */ | |
18 | 52 @Get("html") |
22 | 53 public Representation doGetHTML(Representation entity) { |
54 String result = null; | |
32
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
55 // get form parameter |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
56 Form f = this.getQuery(); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
57 String form = f.getFirstValue("form"); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
58 if (form != null && form.equals("new_group")) { |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
59 // output new group form |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
60 result = "<html><body>\n"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
61 result += "<h1>New group</h1>\n"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
62 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
|
63 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
|
64 result += "<table>"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
65 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
|
66 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
|
67 result += "</table>\n"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
68 result += "<p><input type=\"submit\"/></p>\n"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
69 result += "</form>\n</body>\n</html>"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
70 } else { |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
71 // list all groups |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
72 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
|
73 result += "<table>\n"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
74 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
|
75 List<Group> groups = store.getGroups("uri", "*"); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
76 for (Group group : groups) { |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
77 Reference groupUrl = this.getReference().clone(); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
78 groupUrl.addSegment(group.getId()); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
79 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
|
80 group.getName(), group.getUri()); |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
81 } |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
82 result += "</table>\n"; |
0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
23
diff
changeset
|
83 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
|
84 result += "</body>\n</html>"; |
18 | 85 } |
22 | 86 return new StringRepresentation(result, MediaType.TEXT_HTML); |
87 } | |
88 | |
89 /** | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
90 * POST creates a new Group. |
22 | 91 * |
92 * @return | |
93 */ | |
94 @Post | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
95 public Representation doPost(Representation entity) { |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
96 logger.debug("AnnotationsUiGroups doPost!"); |
22 | 97 // TODO: do authentication |
98 Form form = new Form(entity); | |
99 String id = form.getFirstValue("id"); | |
100 String name = form.getFirstValue("name"); | |
101 if (id == null || id.isEmpty()) { | |
102 // invalid id | |
103 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
104 return null; | |
105 } | |
106 String gid = makeGroupId(id); | |
107 Group newGroup = new Group(gid, null, name); | |
108 Actor storedGroup = store.storeActor(newGroup); | |
109 gid = storedGroup.getId(); | |
110 // return 303: see other | |
111 setStatus(Status.REDIRECTION_SEE_OTHER); | |
112 // go GET URL for this group | |
113 Reference groupUrl = this.getReference(); | |
114 groupUrl.addSegment(gid); | |
115 this.getResponse().setLocationRef(groupUrl); | |
116 return null; | |
117 } | |
118 | |
119 /** | |
120 * Returns a group id based on the given id. | |
121 * | |
122 * @param id | |
123 * @return | |
124 */ | |
125 protected String makeGroupId(String id) { | |
126 // TODO: should we use different ids? | |
127 id = id.replaceAll("\\W", "_"); | |
128 id = id.toLowerCase(); | |
129 return id; | |
18 | 130 } |
131 | |
132 protected AnnotationStore getAnnotationStore() { | |
133 if (store == null) { | |
19 | 134 store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
18 | 135 } |
136 return store; | |
137 } | |
138 } |