diff src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.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
children e208a7b1a37a
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java	Sat Sep 22 20:12:18 2012 +0200
@@ -0,0 +1,107 @@
+/**
+ * 
+ */
+package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
+
+import org.apache.log4j.Logger;
+import org.restlet.data.Form;
+import org.restlet.data.MediaType;
+import org.restlet.data.Reference;
+import org.restlet.data.Status;
+import org.restlet.representation.Representation;
+import org.restlet.representation.StringRepresentation;
+import org.restlet.resource.Get;
+import org.restlet.resource.Post;
+import org.restlet.resource.ServerResource;
+
+import de.mpiwg.itgroup.annotations.Actor;
+import de.mpiwg.itgroup.annotations.Group;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
+import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
+
+/**
+ * Resource class for a single group.
+ * 
+ * @author casties
+ * 
+ */
+public class GroupResource extends ServerResource {
+
+    public static Logger logger = Logger.getLogger(GroupResource.class);
+
+    private AnnotationStore store;
+
+    /**
+     * GET with HTML content type. Shows the group.
+     * 
+     * @param entity
+     * @return
+     */
+    @Get("html")
+    public Representation doGetHTML(Representation entity) {
+        // id from URI /annotations/groups/{id}
+        String id = (String) getRequest().getAttributes().get("id");
+        logger.debug("group-id=" + id);
+        if (id == null || id.isEmpty()) {
+            // invalid id
+            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
+            return null;
+        }
+        String result = null;
+        store = getAnnotationStore();
+        Reference groupsUrl = this.getReference().getParentRef();
+        result = "<html><body>\n<h1>Group</h1>\n";
+        result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
+        Group group = new Group(id);
+        group = (Group) store.getActor(group);
+        result += "<table>";
+        result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
+        result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
+        result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
+        result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference()
+                .addSegment("members"));
+        result += "</table>\n</body>\n</html>";
+
+        logger.debug("sending:");
+        logger.debug(result);
+        return new StringRepresentation(result, MediaType.TEXT_HTML);
+    }
+
+    /**
+     * POST creates a new Group.
+     * 
+     * @return
+     */
+    @Post
+    public Representation doPost(Representation entity) {
+        logger.debug("AnnotationsUiGroup doPost!");
+        // TODO: do authentication
+        Form form = new Form(entity);
+        String id = form.getFirstValue("id");
+        String name = form.getFirstValue("name");
+        if (id == null || id.isEmpty()) {
+            // invalid id
+            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
+            return null;
+        }
+        String gid = null;
+        Group newGroup = new Group(gid, null, name);
+        store = getAnnotationStore();
+        Actor storedGroup = store.storeActor(newGroup);
+        gid = storedGroup.getId();
+        // return 303: see other
+        setStatus(Status.REDIRECTION_SEE_OTHER);
+        // go GET URL for this group
+        Reference groupUrl = this.getReference();
+        groupUrl.addSegment(gid);
+        this.getResponse().setLocationRef(groupUrl);
+        return null;
+    }
+
+    protected AnnotationStore getAnnotationStore() {
+        if (store == null) {
+            store = ((BaseRestlet) getApplication()).getAnnotationStore();
+        }
+        return store;
+    }
+}