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