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.Group; |
---|
20 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
21 | import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; |
---|
22 | |
---|
23 | /** |
---|
24 | * Resource class for a single group. |
---|
25 | * |
---|
26 | * @author casties |
---|
27 | * |
---|
28 | */ |
---|
29 | public class GroupResource extends ServerResource { |
---|
30 | |
---|
31 | public static Logger logger = Logger.getLogger(GroupResource.class); |
---|
32 | |
---|
33 | protected AnnotationStore store; |
---|
34 | |
---|
35 | protected String requestId; |
---|
36 | |
---|
37 | protected Group group; |
---|
38 | |
---|
39 | @Override |
---|
40 | protected void doInit() throws ResourceException { |
---|
41 | super.doInit(); |
---|
42 | // id from URI /annotations/groups/{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 | group = (Group) store.getActor(new Group(requestId)); |
---|
52 | } |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * GET with HTML content type. Shows the group. |
---|
57 | * |
---|
58 | * @param entity |
---|
59 | * @return |
---|
60 | */ |
---|
61 | @Get("html") |
---|
62 | public Representation doGetHTML(Representation entity) { |
---|
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) { |
---|
118 | // invalid id |
---|
119 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
120 | return null; |
---|
121 | } |
---|
122 | // TODO: do authentication |
---|
123 | Form form = new Form(entity); |
---|
124 | String name = form.getFirstValue("name"); |
---|
125 | String uri = form.getFirstValue("uri"); |
---|
126 | if (name != null && !name.isEmpty()) { |
---|
127 | group.setName(name); |
---|
128 | } |
---|
129 | if (uri != null && !uri.isEmpty()) { |
---|
130 | group.setUri(uri); |
---|
131 | } |
---|
132 | store.storeActor(group); |
---|
133 | // return 303: see other |
---|
134 | setStatus(Status.REDIRECTION_SEE_OTHER); |
---|
135 | // go GET same URL |
---|
136 | Reference url = this.getReference(); |
---|
137 | this.getResponse().setLocationRef(url); |
---|
138 | return null; |
---|
139 | } |
---|
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 | } |
---|
164 | } |
---|