1 | /** |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet; |
---|
5 | |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import javax.servlet.ServletContext; |
---|
9 | |
---|
10 | import org.apache.log4j.Logger; |
---|
11 | import org.restlet.data.Form; |
---|
12 | import org.restlet.data.MediaType; |
---|
13 | import org.restlet.representation.Representation; |
---|
14 | import org.restlet.representation.StringRepresentation; |
---|
15 | import org.restlet.resource.Get; |
---|
16 | import org.restlet.resource.ServerResource; |
---|
17 | |
---|
18 | import de.mpiwg.itgroup.annotations.Group; |
---|
19 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
20 | |
---|
21 | /** |
---|
22 | * @author casties |
---|
23 | * |
---|
24 | */ |
---|
25 | public class AnnotationStoreGroups extends ServerResource { |
---|
26 | |
---|
27 | public static Logger logger = Logger.getLogger(AnnotationStoreGroups.class); |
---|
28 | |
---|
29 | private AnnotationStore store; |
---|
30 | |
---|
31 | @Get("html") |
---|
32 | public Representation doGetHTML(Representation entity){ |
---|
33 | Form form = getRequest().getResourceRef().getQueryAsForm(); |
---|
34 | // id from URI /annotations/groups/{id} |
---|
35 | String id = (String) getRequest().getAttributes().get("id"); |
---|
36 | logger.debug("group-id=" + id); |
---|
37 | String result="<html><body>\n<h1>Groups</h1>\n<table>"; |
---|
38 | result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; |
---|
39 | store = getAnnotationStore(); |
---|
40 | if (id == null) { |
---|
41 | // list all groups |
---|
42 | List<Group> groups = store.getGroups("uri", "*"); |
---|
43 | for (Group group : groups) { |
---|
44 | String groupLink = group.getId(); |
---|
45 | result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, group.getId(), group.getName(), group.getUri()); |
---|
46 | } |
---|
47 | } else { |
---|
48 | // just one group |
---|
49 | List<Group> groups = store.getGroups("uri", "*"); |
---|
50 | for (Group group : groups) { |
---|
51 | String groupLink = group.getId(); |
---|
52 | result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, group.getId(), group.getName(), group.getUri()); |
---|
53 | } |
---|
54 | } |
---|
55 | result += "</table>\n</body>\n</html>"; |
---|
56 | |
---|
57 | logger.debug("sending:"); |
---|
58 | logger.debug(result); |
---|
59 | return new StringRepresentation(result,MediaType.TEXT_HTML); |
---|
60 | } |
---|
61 | |
---|
62 | protected AnnotationStore getAnnotationStore() { |
---|
63 | if (store == null) { |
---|
64 | store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
---|
65 | } |
---|
66 | return store; |
---|
67 | } |
---|
68 | } |
---|