changeset 24:e208a7b1a37a

more work on groups ui.
author casties
date Sun, 23 Sep 2012 16:28:05 +0200
parents d22d01ba953a
children 2140ef107551 f4ed2ed33e5b
files src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java
diffstat 3 files changed, 62 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Sat Sep 22 20:12:18 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java	Sun Sep 23 16:28:05 2012 +0200
@@ -73,6 +73,21 @@
         return person;
     }
 
+    public List<Actor> getActors(String key, String query, NodeTypes type) {
+        ArrayList<Actor> actors = new ArrayList<Actor>();
+        Index<Node> idx = getNodeIndex(type);
+        if (key == null) {
+            key = "uri";
+            query = "*";
+        }
+        IndexHits<Node> actorNodes = idx.query(key, query);
+        for (Node actorNode : actorNodes) {
+            Actor actor = createActorFromNode(actorNode);
+            actors.add(actor);
+        }
+        return actors;
+    }
+
     /**
      * Returns List of Groups.
      * Key has to be indexed.
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java	Sat Sep 22 20:12:18 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java	Sun Sep 23 16:28:05 2012 +0200
@@ -5,7 +5,6 @@
 
 import java.util.List;
 
-import org.apache.log4j.Logger;
 import org.restlet.data.Form;
 import org.restlet.data.MediaType;
 import org.restlet.data.Reference;
@@ -14,13 +13,10 @@
 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;
+import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore.NodeTypes;
 
 /**
  * Resource class for the members of an annotation group.
@@ -28,11 +24,7 @@
  * @author casties
  * 
  */
-public class GroupMembersResource extends ServerResource {
-
-    public static Logger logger = Logger.getLogger(GroupMembersResource.class);
-
-    private AnnotationStore store;
+public class GroupMembersResource extends GroupResource {
 
     /**
      * GET with HTML content type. Shows the members of the group.
@@ -43,30 +35,29 @@
     @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()) {
+        if (requestId == null || requestId.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);
+        Reference thisUrl = this.getReference();
+        Reference groupsUrl = thisUrl.getParentRef();
+        result = "<html><body>\n<h1>Group members</h1>\n";
         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 += String.format("<tr><td>%s</td><td>(%s)</td>", p.getName(), p.getIdString());
+            //result += String.format("<td></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 += String.format("<form method=\"post\" action=\"%s\">\n", thisUrl);
+        result += "<p>Add new member: <select name=\"add_member\">\n";
+        for (Actor p : store.getActors("uri", "*", NodeTypes.PERSON)) {
+            result += String.format("<option value=\"%s\">%s</option>\n", p.getIdString(), p.getName());
+        }
         result += "</select>\n";
         result += "<input type=\"submit\"/>\n";
         result += "</form>\n";
@@ -76,40 +67,30 @@
     }
 
     /**
-     * POST creates a new Group.
+     * POST adds or deletes members of the Group.
      * 
      * @return
      */
     @Post
     public Representation doPost(Representation entity) {
-        logger.debug("AnnotationsUiGroup doPost!");
+        logger.debug("GroupMembersResource 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
+        String addMemberId = form.getFirstValue("add_member");
+        String delMemberId = form.getFirstValue("del_member");
+        if (group == null || ((addMemberId == null || addMemberId.isEmpty()) 
+                && (delMemberId == null || delMemberId.isEmpty()))) {
+            // no 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);
+        // go get same URL
+        Reference thisUrl = this.getReference();
+        this.getResponse().setLocationRef(thisUrl);
         return null;
     }
 
-    protected AnnotationStore getAnnotationStore() {
-        if (store == null) {
-            store = ((BaseRestlet) getApplication()).getAnnotationStore();
-        }
-        return store;
-    }
 }
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java	Sat Sep 22 20:12:18 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java	Sun Sep 23 16:28:05 2012 +0200
@@ -4,17 +4,15 @@
 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.ResourceException;
 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;
@@ -29,7 +27,27 @@
 
     public static Logger logger = Logger.getLogger(GroupResource.class);
 
-    private AnnotationStore store;
+    protected AnnotationStore store;
+    
+    protected String requestId;
+    
+    protected Group group;
+    
+    @Override
+    protected void doInit() throws ResourceException {
+        super.doInit();
+        // id from URI /annotations/groups/{id}
+        requestId = (String) getRequest().getAttributes().get("id");
+        logger.debug("group-id=" + requestId);
+        // get store instance
+        if (store == null) {
+            store = ((BaseRestlet) getApplication()).getAnnotationStore();
+        }
+        // get group from store
+        if (requestId != null) {
+            group = (Group) store.getActor(new Group(requestId));
+        }
+    }
 
     /**
      * GET with HTML content type. Shows the group.
@@ -39,21 +57,15 @@
      */
     @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()) {
+        if (requestId == null || requestId.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());
@@ -67,41 +79,4 @@
         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;
-    }
 }