diff src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.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/GroupMembersResource.java	Sat Sep 22 20:12:18 2012 +0200
@@ -0,0 +1,115 @@
+/**
+ * 
+ */
+package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
+
+import java.util.List;
+
+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.Person;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
+import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
+
+/**
+ * Resource class for the members of an annotation group.
+ * 
+ * @author casties
+ * 
+ */
+public class GroupMembersResource extends ServerResource {
+
+    public static Logger logger = Logger.getLogger(GroupMembersResource.class);
+
+    private AnnotationStore store;
+
+    /**
+     * GET with HTML content type. Shows the members of the group.
+     * 
+     * @param entity
+     * @return
+     */
+    @Get("html")
+    public Representation doGetHTML(Representation entity) {
+        // id from URI /annotations/groups/{id}/members
+        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>Members of group</h1>\n";
+        Group group = new Group(id);
+        group = (Group) store.getActor(group);
+        result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId());
+        result += "<p>Members:</p>\n";
+        result += "<table>";
+        List<Person> members = store.getMembersOfGroup(group);
+        for (Person p : members) {
+            result += String.format("<tr><td>%s</td><td>(%s)</td></tr>\n", p.getName(), p.getIdString());
+        }
+        result += "</table>\n";
+        result += "<form method=\"post\" action=\"\">\n";
+        result += "<p>Add new member: <select name=\"new_member\">\n";
+        result += String.format("<option value=\"%s\">%s</option>\n", "casties", "casties");
+        result += "</select>\n";
+        result += "<input type=\"submit\"/>\n";
+        result += "</form>\n";
+        result += "</body>\n</html>";
+
+        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;
+    }
+}