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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
1 /**
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
2 *
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
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
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
5
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
6 import java.util.List;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
7
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
8 import org.apache.log4j.Logger;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
9 import org.restlet.data.Form;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
10 import org.restlet.data.MediaType;
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
11 import org.restlet.data.Reference;
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
12 import org.restlet.data.Status;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
13 import org.restlet.representation.Representation;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
14 import org.restlet.representation.StringRepresentation;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
15 import org.restlet.resource.Get;
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
16 import org.restlet.resource.Post;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
17 import org.restlet.resource.ServerResource;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
18
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
19 import de.mpiwg.itgroup.annotations.Actor;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
20 import de.mpiwg.itgroup.annotations.Group;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
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
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
23
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
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
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
27 * @author casties
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
28 *
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
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
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
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
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
33
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
34 private AnnotationStore store;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
35
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
36 /**
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
37 * GET with HTML content type. Lists all groups.
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
38 *
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
39 * @param entity
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
40 * @return
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
41 */
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
42 @Get("html")
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
43 public Representation doGetHTML(Representation entity) {
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
44 String result = null;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
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
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
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
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
57 logger.debug("sending:");
19
f0f55ab768c9 more work on HTML UI.
casties
parents: 18
diff changeset
58 logger.debug(result);
22
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
59 return new StringRepresentation(result, MediaType.TEXT_HTML);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
60 }
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
61
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
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
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
64 *
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
65 * @return
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
66 */
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
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
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
70 // TODO: do authentication
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
71 Form form = new Form(entity);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
72 String id = form.getFirstValue("id");
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
73 String name = form.getFirstValue("name");
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
74 if (id == null || id.isEmpty()) {
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
75 // invalid id
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
76 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
77 return null;
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
78 }
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
79 String gid = makeGroupId(id);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
80 Group newGroup = new Group(gid, null, name);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
81 store = getAnnotationStore();
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
82 Actor storedGroup = store.storeActor(newGroup);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
83 gid = storedGroup.getId();
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
84 // return 303: see other
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
85 setStatus(Status.REDIRECTION_SEE_OTHER);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
86 // go GET URL for this group
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
87 Reference groupUrl = this.getReference();
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
88 groupUrl.addSegment(gid);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
89 this.getResponse().setLocationRef(groupUrl);
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
90 return null;
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
91 }
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
92
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
93 /**
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
94 * Returns a group id based on the given id.
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
95 *
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
96 * @param id
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
97 * @return
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
98 */
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
99 protected String makeGroupId(String id) {
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
100 // TODO: should we use different ids?
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
101 id = id.replaceAll("\\W", "_");
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
102 id = id.toLowerCase();
b1fb0d117877 adding and listing groups via html works now.
casties
parents: 21
diff changeset
103 return id;
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
104 }
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
105
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
106 protected AnnotationStore getAnnotationStore() {
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
107 if (store == null) {
19
f0f55ab768c9 more work on HTML UI.
casties
parents: 18
diff changeset
108 store = ((BaseRestlet) getApplication()).getAnnotationStore();
18
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
109 }
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
110 return store;
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
111 }
aafa3884b2c4 new AnnotationStore restlet for HTML-UI.
casties
parents:
diff changeset
112 }