Mercurial > hg > AnnotationManagerN4J
comparison 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 |
comparison
equal
deleted
inserted
replaced
30:05b631a084d0 | 32:0731c4549065 |
---|---|
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.Person; | |
20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
21 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; | |
22 | |
23 /** | |
24 * Resource class for a single person. | |
25 * | |
26 * @author casties | |
27 * | |
28 */ | |
29 public class PersonResource extends ServerResource { | |
30 | |
31 public static Logger logger = Logger.getLogger(PersonResource.class); | |
32 | |
33 protected AnnotationStore store; | |
34 | |
35 protected String requestId; | |
36 | |
37 protected Person person; | |
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("group-id=" + requestId); | |
45 // get store instance | |
46 if (store == null) { | |
47 store = ((BaseRestlet) getApplication()).getAnnotationStore(); | |
48 } | |
49 // get group from store | |
50 if (requestId != null) { | |
51 person = (Person) store.getActor(new Person(requestId)); | |
52 } | |
53 } | |
54 | |
55 /** | |
56 * GET with HTML content type. Shows the person. | |
57 * | |
58 * @param entity | |
59 * @return | |
60 */ | |
61 @Get("html") | |
62 public Representation doGetHTML(Representation entity) { | |
63 if (person == null) { | |
64 // invalid id | |
65 setStatus(Status.CLIENT_ERROR_NOT_FOUND); | |
66 return null; | |
67 } | |
68 String result = null; | |
69 // get form parameter | |
70 Form f = this.getQuery(); | |
71 String form = f.getFirstValue("form"); | |
72 if (form != null && form.equals("edit")) { | |
73 // output edit form | |
74 result = "<html><body>\n"; | |
75 result += String.format("<h1>Edit person %s</h1>\n", person.getId()); | |
76 result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference().getParentRef()); | |
77 // tunnel PUT method through POST | |
78 result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart()); | |
79 result += "<table>"; | |
80 result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n", | |
81 person.getName()); | |
82 result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n", | |
83 person.getUriString()); | |
84 result += "</table>\n"; | |
85 result += "<p><input type=\"submit\"/></p>"; | |
86 result += "</table>\n</form>\n</body>\n</html>"; | |
87 } else { | |
88 // output person content | |
89 result = "<html><body>\n<h1>Person</h1>\n"; | |
90 result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference().getParentRef()); | |
91 result += "<table>"; | |
92 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", person.getId()); | |
93 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", person.getName()); | |
94 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", person.getUri()); | |
95 result += "</table>\n"; | |
96 result += "<p><a href=\"?form=edit\">Edit person</a></p>\n"; | |
97 // tunnel POST as DELETE | |
98 result += String.format( | |
99 "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete person\"/></form>\n", | |
100 this.getReference().getHierarchicalPart()); | |
101 result += "</body>\n</html>"; | |
102 } | |
103 return new StringRepresentation(result, MediaType.TEXT_HTML); | |
104 } | |
105 | |
106 /** | |
107 * PUT updates the person. | |
108 * | |
109 * @param entity | |
110 * @return | |
111 */ | |
112 @Put | |
113 public Representation doPut(Representation entity) { | |
114 logger.debug("PersonResource.doPut!"); | |
115 if (person == null) { | |
116 // invalid id | |
117 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
118 return null; | |
119 } | |
120 // TODO: do authentication | |
121 Form form = new Form(entity); | |
122 String name = form.getFirstValue("name"); | |
123 String uri = form.getFirstValue("uri"); | |
124 if (name != null && !name.isEmpty()) { | |
125 person.setName(name); | |
126 } | |
127 if (uri != null && !uri.isEmpty()) { | |
128 person.setUri(uri); | |
129 } | |
130 store.storeActor(person); | |
131 // return 303: see other | |
132 setStatus(Status.REDIRECTION_SEE_OTHER); | |
133 // go GET same URL | |
134 Reference url = this.getReference(); | |
135 this.getResponse().setLocationRef(url); | |
136 return null; | |
137 } | |
138 | |
139 /** | |
140 * DELETE deletes the person. | |
141 * | |
142 * @param entity | |
143 * @return | |
144 */ | |
145 @Delete | |
146 public Representation doDelete(Representation entity) { | |
147 logger.debug("PersonResource.doDelete!"); | |
148 if (person == null) { | |
149 // invalid id | |
150 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
151 return null; | |
152 } | |
153 // TODO: do authentication | |
154 store.deleteActor(person); | |
155 // return 303: see other | |
156 setStatus(Status.REDIRECTION_SEE_OTHER); | |
157 // go GET parent URL | |
158 Reference url = this.getReference().getParentRef(); | |
159 this.getResponse().setLocationRef(url); | |
160 return null; | |
161 } | |
162 } |