Mercurial > hg > AnnotationManagerN4J
annotate 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 |
rev | line source |
---|---|
18 | 1 /** |
2 * | |
3 */ | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui; |
18 | 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; | |
22 | 11 import org.restlet.data.Reference; |
12 import org.restlet.data.Status; | |
18 | 13 import org.restlet.representation.Representation; |
14 import org.restlet.representation.StringRepresentation; | |
15 import org.restlet.resource.Get; | |
22 | 16 import org.restlet.resource.Post; |
18 | 17 import org.restlet.resource.ServerResource; |
18 | |
22 | 19 import de.mpiwg.itgroup.annotations.Actor; |
18 | 20 import de.mpiwg.itgroup.annotations.Group; |
21 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
22 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; |
18 | 23 |
24 /** | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
25 * Resource class for the list of annotation groups. |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
26 * |
18 | 27 * @author casties |
22 | 28 * |
18 | 29 */ |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
30 public class GroupsResource extends ServerResource { |
18 | 31 |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
32 public static Logger logger = Logger.getLogger(GroupsResource.class); |
18 | 33 |
34 private AnnotationStore store; | |
35 | |
22 | 36 /** |
37 * GET with HTML content type. Lists all groups. | |
38 * | |
39 * @param entity | |
40 * @return | |
41 */ | |
18 | 42 @Get("html") |
22 | 43 public Representation doGetHTML(Representation entity) { |
44 String result = null; | |
18 | 45 store = getAnnotationStore(); |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
46 // list all groups |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
47 result = "<html><body>\n<h1>Groups</h1>\n<table>"; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
48 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
49 List<Group> groups = store.getGroups("uri", "*"); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
50 for (Group group : groups) { |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
51 Reference groupUrl = this.getReference(); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
52 groupUrl.addSegment(group.getId()); |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
53 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(), |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
54 group.getName(), group.getUri()); |
18 | 55 } |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
56 result += "</table>\n</body>\n</html>"; |
18 | 57 logger.debug("sending:"); |
19 | 58 logger.debug(result); |
22 | 59 return new StringRepresentation(result, MediaType.TEXT_HTML); |
60 } | |
61 | |
62 /** | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
63 * POST creates a new Group. |
22 | 64 * |
65 * @return | |
66 */ | |
67 @Post | |
23
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
68 public Representation doPost(Representation entity) { |
d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
casties
parents:
22
diff
changeset
|
69 logger.debug("AnnotationsUiGroups doPost!"); |
22 | 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; | |
18 | 104 } |
105 | |
106 protected AnnotationStore getAnnotationStore() { | |
107 if (store == null) { | |
19 | 108 store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
18 | 109 } |
110 return store; | |
111 } | |
112 } |