Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java @ 23:d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
author | casties |
---|---|
date | Sat, 22 Sep 2012 20:12:18 +0200 |
parents | src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreGroups.java@b1fb0d117877 |
children | 0731c4549065 |
comparison
equal
deleted
inserted
replaced
22:b1fb0d117877 | 23:d22d01ba953a |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui; | |
5 | |
6 import java.util.List; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 import org.restlet.data.Form; | |
10 import org.restlet.data.MediaType; | |
11 import org.restlet.data.Reference; | |
12 import org.restlet.data.Status; | |
13 import org.restlet.representation.Representation; | |
14 import org.restlet.representation.StringRepresentation; | |
15 import org.restlet.resource.Get; | |
16 import org.restlet.resource.Post; | |
17 import org.restlet.resource.ServerResource; | |
18 | |
19 import de.mpiwg.itgroup.annotations.Actor; | |
20 import de.mpiwg.itgroup.annotations.Group; | |
21 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
22 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; | |
23 | |
24 /** | |
25 * Resource class for the list of annotation groups. | |
26 * | |
27 * @author casties | |
28 * | |
29 */ | |
30 public class GroupsResource extends ServerResource { | |
31 | |
32 public static Logger logger = Logger.getLogger(GroupsResource.class); | |
33 | |
34 private AnnotationStore store; | |
35 | |
36 /** | |
37 * GET with HTML content type. Lists all groups. | |
38 * | |
39 * @param entity | |
40 * @return | |
41 */ | |
42 @Get("html") | |
43 public Representation doGetHTML(Representation entity) { | |
44 String result = null; | |
45 store = getAnnotationStore(); | |
46 // list all groups | |
47 result = "<html><body>\n<h1>Groups</h1>\n<table>"; | |
48 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; | |
49 List<Group> groups = store.getGroups("uri", "*"); | |
50 for (Group group : groups) { | |
51 Reference groupUrl = this.getReference(); | |
52 groupUrl.addSegment(group.getId()); | |
53 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(), | |
54 group.getName(), group.getUri()); | |
55 } | |
56 result += "</table>\n</body>\n</html>"; | |
57 logger.debug("sending:"); | |
58 logger.debug(result); | |
59 return new StringRepresentation(result, MediaType.TEXT_HTML); | |
60 } | |
61 | |
62 /** | |
63 * POST creates a new Group. | |
64 * | |
65 * @return | |
66 */ | |
67 @Post | |
68 public Representation doPost(Representation entity) { | |
69 logger.debug("AnnotationsUiGroups doPost!"); | |
70 // TODO: do authentication | |
71 Form form = new Form(entity); | |
72 String id = form.getFirstValue("id"); | |
73 String name = form.getFirstValue("name"); | |
74 if (id == null || id.isEmpty()) { | |
75 // invalid id | |
76 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); | |
77 return null; | |
78 } | |
79 String gid = makeGroupId(id); | |
80 Group newGroup = new Group(gid, null, name); | |
81 store = getAnnotationStore(); | |
82 Actor storedGroup = store.storeActor(newGroup); | |
83 gid = storedGroup.getId(); | |
84 // return 303: see other | |
85 setStatus(Status.REDIRECTION_SEE_OTHER); | |
86 // go GET URL for this group | |
87 Reference groupUrl = this.getReference(); | |
88 groupUrl.addSegment(gid); | |
89 this.getResponse().setLocationRef(groupUrl); | |
90 return null; | |
91 } | |
92 | |
93 /** | |
94 * Returns a group id based on the given id. | |
95 * | |
96 * @param id | |
97 * @return | |
98 */ | |
99 protected String makeGroupId(String id) { | |
100 // TODO: should we use different ids? | |
101 id = id.replaceAll("\\W", "_"); | |
102 id = id.toLowerCase(); | |
103 return id; | |
104 } | |
105 | |
106 protected AnnotationStore getAnnotationStore() { | |
107 if (store == null) { | |
108 store = ((BaseRestlet) getApplication()).getAnnotationStore(); | |
109 } | |
110 return store; | |
111 } | |
112 } |