annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java @ 59:e2f86ef9b871

make annotation uri in store configurable. fix npe with no tags.
author casties
date Tue, 20 Nov 2012 19:16:43 +0100
parents 0731c4549065
children c0dd5314bada
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
6 import org.apache.log4j.Logger;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
7 import org.restlet.data.Form;
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
8 import org.restlet.data.MediaType;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
9 import org.restlet.data.Reference;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
10 import org.restlet.data.Status;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
11 import org.restlet.representation.Representation;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
12 import org.restlet.representation.StringRepresentation;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
13 import org.restlet.resource.Delete;
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
14 import org.restlet.resource.Get;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
15 import org.restlet.resource.Put;
24
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
16 import org.restlet.resource.ResourceException;
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
17 import org.restlet.resource.ServerResource;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
18
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
19 import de.mpiwg.itgroup.annotations.Group;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
21 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
22
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
23 /**
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
24 * Resource class for a single group.
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
25 *
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
26 * @author casties
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
27 *
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
28 */
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
29 public class GroupResource extends ServerResource {
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
30
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
31 public static Logger logger = Logger.getLogger(GroupResource.class);
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
32
24
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
33 protected AnnotationStore store;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
34
24
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
35 protected String requestId;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
36
24
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
37 protected Group group;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
38
24
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
39 @Override
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
40 protected void doInit() throws ResourceException {
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
41 super.doInit();
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
42 // id from URI /annotations/groups/{id}
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
43 requestId = (String) getRequest().getAttributes().get("id");
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
44 logger.debug("group-id=" + requestId);
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
45 // get store instance
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
46 if (store == null) {
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
47 store = ((BaseRestlet) getApplication()).getAnnotationStore();
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
48 }
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
49 // get group from store
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
50 if (requestId != null) {
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
51 group = (Group) store.getActor(new Group(requestId));
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
52 }
e208a7b1a37a more work on groups ui.
casties
parents: 23
diff changeset
53 }
23
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 /**
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
56 * GET with HTML content type. Shows the group.
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
57 *
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
58 * @param entity
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
59 * @return
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
60 */
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
61 @Get("html")
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
62 public Representation doGetHTML(Representation entity) {
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
63 if (group == null) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
64 // invalid id
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
65 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
66 return null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
67 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
68 String result = null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
69 // get form parameter
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
70 Form f = this.getQuery();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
71 String form = f.getFirstValue("form");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
72 if (form != null && form.equals("edit")) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
73 // output edit form
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
74 result = "<html><body>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
75 result += String.format("<h1>Edit group %s</h1>\n", group.getId());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
76 result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
77 // tunnel PUT method through POST
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
78 result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
79 result += "<table>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
80 result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n",
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
81 group.getName());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
82 result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n",
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
83 group.getUriString());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
84 result += "</table>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
85 result += "<p><input type=\"submit\"/></p>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
86 result += "</table>\n</form>\n</body>\n</html>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
87 } else {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
88 // output group content
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
89 result = "<html><body>\n<h1>Group</h1>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
90 result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
91 result += "<table>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
92 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
93 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
94 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
95 result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
96 .getReference().addSegment("members"));
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
97 result += "</table>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
98 result += "<p><a href=\"?form=edit\">Edit group</a></p>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
99 // tunnel POST as DELETE
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
100 result += String.format(
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
101 "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete group\"/></form>\n",
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
102 this.getReference().getHierarchicalPart());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
103 result += "</body>\n</html>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
104 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
105 return new StringRepresentation(result, MediaType.TEXT_HTML);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
106 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
107
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
108 /**
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
109 * PUT updates the group.
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
110 *
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
111 * @param entity
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
112 * @return
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
113 */
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
114 @Put
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
115 public Representation doPut(Representation entity) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
116 logger.debug("GroupResource.doPut!");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
117 if (group == null) {
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
118 // invalid id
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
119 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
120 return null;
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
121 }
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
122 // TODO: do authentication
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
123 Form form = new Form(entity);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
124 String name = form.getFirstValue("name");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
125 String uri = form.getFirstValue("uri");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
126 if (name != null && !name.isEmpty()) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
127 group.setName(name);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
128 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
129 if (uri != null && !uri.isEmpty()) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
130 group.setUri(uri);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
131 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
132 store.storeActor(group);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
133 // return 303: see other
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
134 setStatus(Status.REDIRECTION_SEE_OTHER);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
135 // go GET same URL
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
136 Reference url = this.getReference();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
137 this.getResponse().setLocationRef(url);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
138 return null;
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
139 }
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
140
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
141 /**
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
142 * DELETE deletes the group.
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
143 *
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
144 * @param entity
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
145 * @return
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
146 */
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
147 @Delete
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
148 public Representation doDelete(Representation entity) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
149 logger.debug("GroupResource.doDelete!");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
150 if (group == null) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
151 // invalid id
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
152 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
153 return null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
154 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
155 // TODO: do authentication
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
156 store.deleteActor(group);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
157 // return 303: see other
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
158 setStatus(Status.REDIRECTION_SEE_OTHER);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
159 // go GET parent URL
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
160 Reference url = this.getReference().getParentRef();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
161 this.getResponse().setLocationRef(url);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
162 return null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents: 24
diff changeset
163 }
23
d22d01ba953a reorganised code for annotations and groups ui. work in progress.
casties
parents:
diff changeset
164 }