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