# HG changeset patch # User casties # Date 1348410485 -7200 # Node ID e208a7b1a37ad58c188464cb3b38d74f1b090f61 # Parent d22d01ba953a5222743743877be232b5149bbf93 more work on groups ui. diff -r d22d01ba953a -r e208a7b1a37a src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java --- 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 getActors(String key, String query, NodeTypes type) { + ArrayList actors = new ArrayList(); + Index idx = getNodeIndex(type); + if (key == null) { + key = "uri"; + query = "*"; + } + IndexHits 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. diff -r d22d01ba953a -r e208a7b1a37a src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java --- 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 = "\n

Members of group

\n"; - Group group = new Group(id); - group = (Group) store.getActor(group); + Reference thisUrl = this.getReference(); + Reference groupsUrl = thisUrl.getParentRef(); + result = "\n

Group members

\n"; result += String.format("

Group: %s (%s)

\n", group.getName(), groupsUrl, group.getId()); result += "

Members:

\n"; result += ""; List members = store.getMembersOfGroup(group); for (Person p : members) { - result += String.format("\n", p.getName(), p.getIdString()); + result += String.format("", p.getName(), p.getIdString()); + //result += String.format("\n", p.getName(), p.getIdString()); } result += "
%s(%s)
%s(%s)
\n"; - result += "
\n"; - result += "

Add new member: \n"; + for (Actor p : store.getActors("uri", "*", NodeTypes.PERSON)) { + result += String.format("\n", p.getIdString(), p.getName()); + } result += "\n"; result += "\n"; result += "

\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; - } } diff -r d22d01ba953a -r e208a7b1a37a src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java --- 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 = "\n

Group

\n"; result += String.format("

All groups

", groupsUrl); - Group group = new Group(id); - group = (Group) store.getActor(group); result += ""; result += String.format("\n", group.getId()); result += String.format("\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; - } }
id%s
name%s