Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.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 | e208a7b1a37a |
children | c0dd5314bada |
comparison
equal
deleted
inserted
replaced
30:05b631a084d0 | 32:0731c4549065 |
---|---|
2 * | 2 * |
3 */ | 3 */ |
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui; | 4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui; |
5 | 5 |
6 import org.apache.log4j.Logger; | 6 import org.apache.log4j.Logger; |
7 import org.restlet.data.Form; | |
7 import org.restlet.data.MediaType; | 8 import org.restlet.data.MediaType; |
8 import org.restlet.data.Reference; | 9 import org.restlet.data.Reference; |
9 import org.restlet.data.Status; | 10 import org.restlet.data.Status; |
10 import org.restlet.representation.Representation; | 11 import org.restlet.representation.Representation; |
11 import org.restlet.representation.StringRepresentation; | 12 import org.restlet.representation.StringRepresentation; |
13 import org.restlet.resource.Delete; | |
12 import org.restlet.resource.Get; | 14 import org.restlet.resource.Get; |
15 import org.restlet.resource.Put; | |
13 import org.restlet.resource.ResourceException; | 16 import org.restlet.resource.ResourceException; |
14 import org.restlet.resource.ServerResource; | 17 import org.restlet.resource.ServerResource; |
15 | 18 |
16 import de.mpiwg.itgroup.annotations.Group; | 19 import de.mpiwg.itgroup.annotations.Group; |
17 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | 20 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
26 public class GroupResource extends ServerResource { | 29 public class GroupResource extends ServerResource { |
27 | 30 |
28 public static Logger logger = Logger.getLogger(GroupResource.class); | 31 public static Logger logger = Logger.getLogger(GroupResource.class); |
29 | 32 |
30 protected AnnotationStore store; | 33 protected AnnotationStore store; |
31 | 34 |
32 protected String requestId; | 35 protected String requestId; |
33 | 36 |
34 protected Group group; | 37 protected Group group; |
35 | 38 |
36 @Override | 39 @Override |
37 protected void doInit() throws ResourceException { | 40 protected void doInit() throws ResourceException { |
38 super.doInit(); | 41 super.doInit(); |
39 // id from URI /annotations/groups/{id} | 42 // id from URI /annotations/groups/{id} |
40 requestId = (String) getRequest().getAttributes().get("id"); | 43 requestId = (String) getRequest().getAttributes().get("id"); |
55 * @param entity | 58 * @param entity |
56 * @return | 59 * @return |
57 */ | 60 */ |
58 @Get("html") | 61 @Get("html") |
59 public Representation doGetHTML(Representation entity) { | 62 public Representation doGetHTML(Representation entity) { |
60 if (requestId == null || requestId.isEmpty()) { | 63 if (group == 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 group %s</h1>\n", group.getId()); | |
76 result += String.format("<p><a href=\"%s\">All groups</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 group.getName()); | |
82 result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n", | |
83 group.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 group content | |
89 result = "<html><body>\n<h1>Group</h1>\n"; | |
90 result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef()); | |
91 result += "<table>"; | |
92 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); | |
93 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); | |
94 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); | |
95 result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this | |
96 .getReference().addSegment("members")); | |
97 result += "</table>\n"; | |
98 result += "<p><a href=\"?form=edit\">Edit group</a></p>\n"; | |
99 // tunnel POST as DELETE | |
100 result += String.format( | |
101 "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete group\"/></form>\n", | |
102 this.getReference().getHierarchicalPart()); | |
103 result += "</body>\n</html>"; | |
104 } | |
105 return new StringRepresentation(result, MediaType.TEXT_HTML); | |
106 } | |
107 | |
108 /** | |
109 * PUT updates the group. | |
110 * | |
111 * @param entity | |
112 * @return | |
113 */ | |
114 @Put | |
115 public Representation doPut(Representation entity) { | |
116 logger.debug("GroupResource.doPut!"); | |
117 if (group == null) { | |
61 // invalid id | 118 // invalid id |
62 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | 119 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
63 return null; | 120 return null; |
64 } | 121 } |
65 String result = null; | 122 // TODO: do authentication |
66 Reference groupsUrl = this.getReference().getParentRef(); | 123 Form form = new Form(entity); |
67 result = "<html><body>\n<h1>Group</h1>\n"; | 124 String name = form.getFirstValue("name"); |
68 result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl); | 125 String uri = form.getFirstValue("uri"); |
69 result += "<table>"; | 126 if (name != null && !name.isEmpty()) { |
70 result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); | 127 group.setName(name); |
71 result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); | 128 } |
72 result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); | 129 if (uri != null && !uri.isEmpty()) { |
73 result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference() | 130 group.setUri(uri); |
74 .addSegment("members")); | 131 } |
75 result += "</table>\n</body>\n</html>"; | 132 store.storeActor(group); |
76 | 133 // return 303: see other |
77 logger.debug("sending:"); | 134 setStatus(Status.REDIRECTION_SEE_OTHER); |
78 logger.debug(result); | 135 // go GET same URL |
79 return new StringRepresentation(result, MediaType.TEXT_HTML); | 136 Reference url = this.getReference(); |
137 this.getResponse().setLocationRef(url); | |
138 return null; | |
80 } | 139 } |
81 | 140 |
141 /** | |
142 * DELETE deletes the group. | |
143 * | |
144 * @param entity | |
145 * @return | |
146 */ | |
147 @Delete | |
148 public Representation doDelete(Representation entity) { | |
149 logger.debug("GroupResource.doDelete!"); | |
150 if (group == null) { | |
151 // invalid id | |
152 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
153 return null; | |
154 } | |
155 // TODO: do authentication | |
156 store.deleteActor(group); | |
157 // return 303: see other | |
158 setStatus(Status.REDIRECTION_SEE_OTHER); | |
159 // go GET parent URL | |
160 Reference url = this.getReference().getParentRef(); | |
161 this.getResponse().setLocationRef(url); | |
162 return null; | |
163 } | |
82 } | 164 } |