1 | /** |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet.annotations_ui; |
---|
5 | |
---|
6 | import org.apache.log4j.Logger; |
---|
7 | import org.restlet.data.MediaType; |
---|
8 | import org.restlet.data.Reference; |
---|
9 | import org.restlet.data.Status; |
---|
10 | import org.restlet.representation.Representation; |
---|
11 | import org.restlet.representation.StringRepresentation; |
---|
12 | import org.restlet.resource.Get; |
---|
13 | import org.restlet.resource.ResourceException; |
---|
14 | import org.restlet.resource.ServerResource; |
---|
15 | |
---|
16 | import de.mpiwg.itgroup.annotations.Group; |
---|
17 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
18 | import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; |
---|
19 | |
---|
20 | /** |
---|
21 | * Resource class for a single group. |
---|
22 | * |
---|
23 | * @author casties |
---|
24 | * |
---|
25 | */ |
---|
26 | public class GroupResource extends ServerResource { |
---|
27 | |
---|
28 | public static Logger logger = Logger.getLogger(GroupResource.class); |
---|
29 | |
---|
30 | protected AnnotationStore store; |
---|
31 | |
---|
32 | protected String requestId; |
---|
33 | |
---|
34 | protected Group group; |
---|
35 | |
---|
36 | @Override |
---|
37 | protected void doInit() throws ResourceException { |
---|
38 | super.doInit(); |
---|
39 | // id from URI /annotations/groups/{id} |
---|
40 | requestId = (String) getRequest().getAttributes().get("id"); |
---|
41 | logger.debug("group-id=" + requestId); |
---|
42 | // get store instance |
---|
43 | if (store == null) { |
---|
44 | store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
---|
45 | } |
---|
46 | // get group from store |
---|
47 | if (requestId != null) { |
---|
48 | group = (Group) store.getActor(new Group(requestId)); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * GET with HTML content type. Shows the group. |
---|
54 | * |
---|
55 | * @param entity |
---|
56 | * @return |
---|
57 | */ |
---|
58 | @Get("html") |
---|
59 | public Representation doGetHTML(Representation entity) { |
---|
60 | if (requestId == null || requestId.isEmpty()) { |
---|
61 | // invalid id |
---|
62 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
63 | return null; |
---|
64 | } |
---|
65 | String result = null; |
---|
66 | Reference groupsUrl = this.getReference().getParentRef(); |
---|
67 | result = "<html><body>\n<h1>Group</h1>\n"; |
---|
68 | result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl); |
---|
69 | result += "<table>"; |
---|
70 | result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId()); |
---|
71 | result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName()); |
---|
72 | result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); |
---|
73 | result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference() |
---|
74 | .addSegment("members")); |
---|
75 | result += "</table>\n</body>\n</html>"; |
---|
76 | |
---|
77 | logger.debug("sending:"); |
---|
78 | logger.debug(result); |
---|
79 | return new StringRepresentation(result, MediaType.TEXT_HTML); |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|