annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java @ 50:64aa756c60cc

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