# HG changeset patch # User casties # Date 1348603161 -7200 # Node ID 0731c4549065405f19da77da5e1bd1b5462f18dd # Parent 05b631a084d0d2e2b80429020841953373541b3a UI for editing groups and persons works now. (still no authorisation!) diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/Person.java --- a/src/main/java/de/mpiwg/itgroup/annotations/Person.java Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/Person.java Tue Sep 25 21:59:21 2012 +0200 @@ -3,6 +3,8 @@ */ package de.mpiwg.itgroup.annotations; +import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; + /** * @author casties * @@ -54,4 +56,17 @@ } return null; } + + /** + * Sets the name from the id using getFullNameFromLdap of the Application. + * + * @param application + * @return + */ + public String updateName(BaseRestlet application) { + if (id != null) { + name = application.getFullNameFromLdap(id); + } + return name; + } } diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java --- a/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java Tue Sep 25 21:59:21 2012 +0200 @@ -4,6 +4,7 @@ package de.mpiwg.itgroup.annotations.neo4j; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.HashSet; import java.util.List; @@ -27,6 +28,8 @@ import de.mpiwg.itgroup.annotations.Tag; /** + * Neo4J based Annotation store. + * * @author casties * */ @@ -40,6 +43,9 @@ ANNOTATION, PERSON, TARGET, GROUP, TAG } + // types of nodes that should not be automatically deleted. + public Set permanentNodeTypes = new HashSet(Arrays.asList("PERSON", "GROUP", "TAG")); + protected List> nodeIndexes; public static enum RelationTypes implements RelationshipType { @@ -100,8 +106,7 @@ } /** - * Returns List of Groups. - * Key has to be indexed. + * Returns List of Groups. Key has to be indexed. * * @param key * @param query @@ -121,11 +126,9 @@ } return groups; } - - + /** - * Returns List of Tags. - * Key has to be indexed. + * Returns List of Tags. Key has to be indexed. * * @param key * @param query @@ -146,8 +149,7 @@ return tags; } - - /** + /** * Returns List of Groups the person is member of. * * @param person @@ -212,7 +214,7 @@ } return members; } - + /** * Add Person newMember to Group group. * @@ -229,7 +231,7 @@ } return addedMember; } - + /** * Delete Person oldMember from Group group. * @@ -238,17 +240,24 @@ */ public void deleteGroupMember(Group group, Person member) { Node gn = getActorNode(group); + Node pn = getActorNode(member); Iterable rels = gn.getRelationships(RelationTypes.MEMBER_OF); for (Relationship rel : rels) { Node mn = rel.getStartNode(); - if (mn.equals(member)) { - rel.delete(); + if (mn.equals(pn)) { + Transaction tx = graphDb.beginTx(); + try { + rel.delete(); + tx.success(); + } finally { + tx.finish(); + } // there should be only one break; } - } + } } - + /** * Returns the stored Actor matching the given one. * @@ -256,23 +265,79 @@ * @return */ public Actor getActor(Actor actor) { - Node actorNode = getActorNode(actor); - Actor storedActor = createActorFromNode(actorNode); - return storedActor; + Node actorNode = getActorNode(actor); + Actor storedActor = createActorFromNode(actorNode); + return storedActor; } - + /** - * Stores an Actor (Person or Group). Creates a new actor Node or returns an existing one. + * Stores an Actor (Person or Group). Creates a new actor Node or update an + * existing one. * * @param actor * @return */ public Actor storeActor(Actor actor) { - Node actorNode = getOrCreateActorNode(actor); - Actor storedActor = createActorFromNode(actorNode); - return storedActor; + Node actorNode = getOrCreateActorNode(actor); + Transaction tx = graphDb.beginTx(); + try { + // id + String id = actor.getId(); + if (id != null) { + actorNode.setProperty("id", id); + } + // name + String name = actor.getName(); + if (name != null) { + actorNode.setProperty("name", name); + } + // uri + String uri = actor.getUri(); + if (uri != null) { + actorNode.setProperty("uri", uri); + } + tx.success(); + } finally { + tx.finish(); + } + Actor storedActor = createActorFromNode(actorNode); + return storedActor; } - + + /** + * Deletes the given Actor. + * + * @param actor + */ + public void deleteActor(Actor actor) { + String uri = actor.getUriString(); + Index idx; + if (actor.isGroup()) { + idx = getNodeIndex(NodeTypes.GROUP); + } else { + idx = getNodeIndex(NodeTypes.PERSON); + } + Node actorNode = idx.get("uri", uri).getSingle(); + if (actorNode != null) { + // delete relations + Transaction tx = graphDb.beginTx(); + try { + for (Relationship rel : actorNode.getRelationships()) { + rel.delete(); + } + if (!actorNode.hasRelationship()) { + // this shouldn't happen + deleteNode(actorNode); + } else { + logger.error("deleteActor: unable to delete: Node still has relations."); + } + tx.success(); + } finally { + tx.finish(); + } + } + } + /** * Returns the Annotation with the given id. * @@ -375,6 +440,7 @@ * @return */ protected Actor createActorFromNode(Node actorNode) { + if (actorNode == null) return null; String id = (String) actorNode.getProperty("id", null); String uri = (String) actorNode.getProperty("uri", null); String name = (String) actorNode.getProperty("name", null); @@ -386,16 +452,16 @@ } return null; } - + public Tag createTagFromNode(Node tagNode) { - String name = (String) tagNode.getProperty("name", null); - String uri = (String) tagNode.getProperty("uri", null); - String id = (String) tagNode.getProperty("id", null); - - return new Tag(id, uri, name); - - } + if (tagNode == null) return null; + String name = (String) tagNode.getProperty("name", null); + String uri = (String) tagNode.getProperty("uri", null); + String id = (String) tagNode.getProperty("id", null); + return new Tag(id, uri, name); + + } /** * Store a new annotation in the store or update an existing one. Returns @@ -511,7 +577,7 @@ // still tags to add for (String tag : newTags) { // create new tag - Node tagNode = getOrCreateTagNode(new Tag(null,null,tag)); + Node tagNode = getOrCreateTagNode(new Tag(null, null, tag)); getOrCreateRelation(annotNode, RelationTypes.HAS_TAG, tagNode); } } @@ -533,7 +599,7 @@ * * @param id */ - public void deleteById(String id) { + public void deleteAnnotationById(String id) { Node annotNode = getNodeIndex(NodeTypes.ANNOTATION).get("id", id).getSingle(); if (annotNode != null) { // delete related objects @@ -541,10 +607,10 @@ try { for (Relationship rel : annotNode.getRelationships()) { // delete relation and the related node if it has no other - // relations + // relations and is not permanent Node other = rel.getOtherNode(annotNode); rel.delete(); - if (!other.hasRelationship()) { + if (!(other.hasRelationship() || permanentNodeTypes.contains(other.getProperty("TYPE", null)))) { deleteNode(other); } } @@ -569,7 +635,7 @@ * @param offset * @return */ - public List searchByUriUser(String targetUri, String userUri, String limit, String offset) { + public List searchAnnotationByUriUser(String targetUri, String userUri, String limit, String offset) { List annotations = new ArrayList(); if (targetUri != null) { // there should be only one @@ -692,7 +758,7 @@ Node person = persons.getSingle(); return person; } - + protected Node getOrCreateActorNode(Actor actor) { // Person/Group is identified by URI or id String uri = actor.getUriString(); @@ -745,11 +811,11 @@ tag.setProperty("TYPE", NodeTypes.TAG.name()); tag.setProperty("name", tagname); idx.add(tag, "name", tagname); - + tag.setProperty("id", inTag.getId()); tag.setProperty("uri", inTag.getUri()); idx.add(tag, "uri", inTag.getUri()); - + tx.success(); } finally { tx.finish(); @@ -853,20 +919,19 @@ } - public List getAnnotationsByTag(String tagUri) { - - ArrayList ret = new ArrayList(); - Node tag = getTagNodeByUri(tagUri); - - - Iterable rels = tag.getRelationships(Direction.INCOMING,RelationTypes.HAS_TAG); - - for (Relationship rel:rels){ - Node node = rel.getStartNode(); - ret.add(createAnnotationFromNode(node)); - - } - return ret; - } + public List getAnnotationsByTag(String tagUri) { + + ArrayList ret = new ArrayList(); + Node tag = getTagNodeByUri(tagUri); + + Iterable rels = tag.getRelationships(Direction.INCOMING, RelationTypes.HAS_TAG); + + for (Relationship rel : rels) { + Node node = rel.getStartNode(); + ret.add(createAnnotationFromNode(node)); + + } + return ret; + } } diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java Tue Sep 25 21:59:21 2012 +0200 @@ -221,7 +221,7 @@ } // delete annotation - store.deleteById(id); + store.deleteAnnotationById(id); setStatus(Status.SUCCESS_NO_CONTENT); return null; } diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java Tue Sep 25 21:59:21 2012 +0200 @@ -56,7 +56,7 @@ // do search logger.debug(String.format("searching for uri=%s user=%s", uri, user)); AnnotationStore store = getAnnotationStore(); - List annots = store.searchByUriUser(uri, user, limit, offset); + List annots = store.searchAnnotationByUriUser(uri, user, limit, offset); for (Annotation annot : annots) { // check permission if (!annot.isActionAllowed("read", authUser, store)) continue; diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java Tue Sep 25 21:59:21 2012 +0200 @@ -16,7 +16,7 @@ */ public class AnnotationsUiRestlet extends BaseRestlet { - public final String version = "AnnotationManagerN4J/AnnotationStore 0.1"; + public final String version = "AnnotationManagerN4J/AnnotationsUI 0.2"; public static Logger logger = Logger.getLogger(AnnotationsUiRestlet.class); @@ -41,6 +41,10 @@ router.attach("/groups/{id}", GroupResource.class); router.attach("/groups/{id}/", GroupResource.class); router.attach("/groups/{id}/members", GroupMembersResource.class); + router.attach("/persons", PersonsResource.class); + router.attach("/persons/", PersonsResource.class); + router.attach("/persons/{id}", PersonResource.class); + router.attach("/persons/{id}/", PersonResource.class); router.attach("/", InfoResource.class); // authenticator.setNext(router); diff -r 05b631a084d0 -r 0731c4549065 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 Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java Tue Sep 25 21:59:21 2012 +0200 @@ -4,12 +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.Delete; import org.restlet.resource.Get; +import org.restlet.resource.Put; import org.restlet.resource.ResourceException; import org.restlet.resource.ServerResource; @@ -28,11 +31,11 @@ public static Logger logger = Logger.getLogger(GroupResource.class); protected AnnotationStore store; - + protected String requestId; - + protected Group group; - + @Override protected void doInit() throws ResourceException { super.doInit(); @@ -57,26 +60,105 @@ */ @Get("html") public Representation doGetHTML(Representation entity) { - if (requestId == null || requestId.isEmpty()) { + if (group == null) { + // invalid id + setStatus(Status.CLIENT_ERROR_NOT_FOUND); + return null; + } + String result = null; + // get form parameter + Form f = this.getQuery(); + String form = f.getFirstValue("form"); + if (form != null && form.equals("edit")) { + // output edit form + result = "\n"; + result += String.format("

Edit group %s

\n", group.getId()); + result += String.format("

All groups

", this.getReference().getParentRef()); + // tunnel PUT method through POST + result += String.format("
\n", this.getReference().getHierarchicalPart()); + result += ""; + result += String.format("\n", + group.getName()); + result += String.format("\n", + group.getUriString()); + result += "
name
uri
\n"; + result += "

"; + result += "\n
\n\n"; + } else { + // output group content + result = "\n

Group

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

All groups

", this.getReference().getParentRef()); + result += ""; + result += String.format("\n", group.getId()); + result += String.format("\n", group.getName()); + result += String.format("\n", group.getUri()); + result += String.format("\n", this + .getReference().addSegment("members")); + result += "
id%s
name%s
uri%s
membersview members
\n"; + result += "

Edit group

\n"; + // tunnel POST as DELETE + result += String.format( + "
\n", + this.getReference().getHierarchicalPart()); + result += "\n"; + } + return new StringRepresentation(result, MediaType.TEXT_HTML); + } + + /** + * PUT updates the group. + * + * @param entity + * @return + */ + @Put + public Representation doPut(Representation entity) { + logger.debug("GroupResource.doPut!"); + if (group == null) { // invalid id setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return null; } - String result = null; - Reference groupsUrl = this.getReference().getParentRef(); - result = "\n

Group

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

All groups

", groupsUrl); - result += ""; - result += String.format("\n", group.getId()); - result += String.format("\n", group.getName()); - result += String.format("\n", group.getUri()); - result += String.format("\n", this.getReference() - .addSegment("members")); - result += "
id%s
name%s
uri%s
membersview members
\n\n"; - - logger.debug("sending:"); - logger.debug(result); - return new StringRepresentation(result, MediaType.TEXT_HTML); + // TODO: do authentication + Form form = new Form(entity); + String name = form.getFirstValue("name"); + String uri = form.getFirstValue("uri"); + if (name != null && !name.isEmpty()) { + group.setName(name); + } + if (uri != null && !uri.isEmpty()) { + group.setUri(uri); + } + store.storeActor(group); + // return 303: see other + setStatus(Status.REDIRECTION_SEE_OTHER); + // go GET same URL + Reference url = this.getReference(); + this.getResponse().setLocationRef(url); + return null; } + /** + * DELETE deletes the group. + * + * @param entity + * @return + */ + @Delete + public Representation doDelete(Representation entity) { + logger.debug("GroupResource.doDelete!"); + if (group == null) { + // invalid id + setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + return null; + } + // TODO: do authentication + store.deleteActor(group); + // return 303: see other + setStatus(Status.REDIRECTION_SEE_OTHER); + // go GET parent URL + Reference url = this.getReference().getParentRef(); + this.getResponse().setLocationRef(url); + return null; + } } diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java --- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java Tue Sep 25 21:59:21 2012 +0200 @@ -14,6 +14,7 @@ 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; @@ -33,6 +34,15 @@ private AnnotationStore store; + @Override + protected void doInit() throws ResourceException { + super.doInit(); + // get store instance + if (store == null) { + store = ((BaseRestlet) getApplication()).getAnnotationStore(); + } + } + /** * GET with HTML content type. Lists all groups. * @@ -42,20 +52,37 @@ @Get("html") public Representation doGetHTML(Representation entity) { String result = null; - store = getAnnotationStore(); - // list all groups - result = "\n

Groups

\n"; - result += ""; - List groups = store.getGroups("uri", "*"); - for (Group group : groups) { - Reference groupUrl = this.getReference(); - groupUrl.addSegment(group.getId()); - result += String.format("\n", groupUrl, group.getId(), - group.getName(), group.getUri()); + // get form parameter + Form f = this.getQuery(); + String form = f.getFirstValue("form"); + if (form != null && form.equals("new_group")) { + // output new group form + result = "\n"; + result += "

New group

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

All groups

", this.getReference()); + result += String.format("\n", this.getReference().getHierarchicalPart()); + result += "
idnameuri
%s%s%s
"; + result += "\n"; + result += "\n"; + result += "
id
name
\n"; + result += "

\n"; + result += "\n\n"; + } else { + // list all groups + result = "\n

Groups

\n"; + result += "\n"; + result += ""; + List groups = store.getGroups("uri", "*"); + for (Group group : groups) { + Reference groupUrl = this.getReference().clone(); + groupUrl.addSegment(group.getId()); + result += String.format("\n", groupUrl, group.getId(), + group.getName(), group.getUri()); + } + result += "
idnameuri
%s%s%s
\n"; + result += "

Add new group

\n"; + result += "\n"; } - result += "\n\n"; - logger.debug("sending:"); - logger.debug(result); return new StringRepresentation(result, MediaType.TEXT_HTML); } @@ -78,7 +105,6 @@ } String gid = makeGroupId(id); Group newGroup = new Group(gid, null, name); - store = getAnnotationStore(); Actor storedGroup = store.storeActor(newGroup); gid = storedGroup.getId(); // return 303: see other diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonResource.java Tue Sep 25 21:59:21 2012 +0200 @@ -0,0 +1,162 @@ +/** + * + */ +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.Delete; +import org.restlet.resource.Get; +import org.restlet.resource.Put; +import org.restlet.resource.ResourceException; +import org.restlet.resource.ServerResource; + +import de.mpiwg.itgroup.annotations.Person; +import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; +import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; + +/** + * Resource class for a single person. + * + * @author casties + * + */ +public class PersonResource extends ServerResource { + + public static Logger logger = Logger.getLogger(PersonResource.class); + + protected AnnotationStore store; + + protected String requestId; + + protected Person person; + + @Override + protected void doInit() throws ResourceException { + super.doInit(); + // id from URI /annotations/persons/{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) { + person = (Person) store.getActor(new Person(requestId)); + } + } + + /** + * GET with HTML content type. Shows the person. + * + * @param entity + * @return + */ + @Get("html") + public Representation doGetHTML(Representation entity) { + if (person == null) { + // invalid id + setStatus(Status.CLIENT_ERROR_NOT_FOUND); + return null; + } + String result = null; + // get form parameter + Form f = this.getQuery(); + String form = f.getFirstValue("form"); + if (form != null && form.equals("edit")) { + // output edit form + result = "\n"; + result += String.format("

Edit person %s

\n", person.getId()); + result += String.format("

All persons

", this.getReference().getParentRef()); + // tunnel PUT method through POST + result += String.format("
\n", this.getReference().getHierarchicalPart()); + result += ""; + result += String.format("\n", + person.getName()); + result += String.format("\n", + person.getUriString()); + result += "
name
uri
\n"; + result += "

"; + result += "\n
\n\n"; + } else { + // output person content + result = "\n

Person

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

All persons

", this.getReference().getParentRef()); + result += ""; + result += String.format("\n", person.getId()); + result += String.format("\n", person.getName()); + result += String.format("\n", person.getUri()); + result += "
id%s
name%s
uri%s
\n"; + result += "

Edit person

\n"; + // tunnel POST as DELETE + result += String.format( + "
\n", + this.getReference().getHierarchicalPart()); + result += "\n"; + } + return new StringRepresentation(result, MediaType.TEXT_HTML); + } + + /** + * PUT updates the person. + * + * @param entity + * @return + */ + @Put + public Representation doPut(Representation entity) { + logger.debug("PersonResource.doPut!"); + if (person == null) { + // invalid id + setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + return null; + } + // TODO: do authentication + Form form = new Form(entity); + String name = form.getFirstValue("name"); + String uri = form.getFirstValue("uri"); + if (name != null && !name.isEmpty()) { + person.setName(name); + } + if (uri != null && !uri.isEmpty()) { + person.setUri(uri); + } + store.storeActor(person); + // return 303: see other + setStatus(Status.REDIRECTION_SEE_OTHER); + // go GET same URL + Reference url = this.getReference(); + this.getResponse().setLocationRef(url); + return null; + } + + /** + * DELETE deletes the person. + * + * @param entity + * @return + */ + @Delete + public Representation doDelete(Representation entity) { + logger.debug("PersonResource.doDelete!"); + if (person == null) { + // invalid id + setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + return null; + } + // TODO: do authentication + store.deleteActor(person); + // return 303: see other + setStatus(Status.REDIRECTION_SEE_OTHER); + // go GET parent URL + Reference url = this.getReference().getParentRef(); + this.getResponse().setLocationRef(url); + return null; + } +} diff -r 05b631a084d0 -r 0731c4549065 src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java Tue Sep 25 21:59:21 2012 +0200 @@ -0,0 +1,123 @@ +/** + * + */ +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.ResourceException; +import org.restlet.resource.ServerResource; + +import de.mpiwg.itgroup.annotations.Actor; +import de.mpiwg.itgroup.annotations.Person; +import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; +import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore.NodeTypes; +import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; + +/** + * Resource class for the list of annotation users (Person class). + * + * @author casties + * + */ +public class PersonsResource extends ServerResource { + + public static Logger logger = Logger.getLogger(PersonsResource.class); + + private AnnotationStore store; + + @Override + protected void doInit() throws ResourceException { + super.doInit(); + // get store instance + if (store == null) { + store = ((BaseRestlet) getApplication()).getAnnotationStore(); + } + } + + /** + * GET with HTML content type. Lists all persons. + * + * @param entity + * @return + */ + @Get("html") + public Representation doGetHTML(Representation entity) { + String result = null; + // get form parameter + Form f = this.getQuery(); + String form = f.getFirstValue("form"); + if (form != null && form.equals("new_person")) { + // output new group form + result = "\n"; + result += "

New person

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

All persons

", this.getReference()); + result += String.format("
\n", this.getReference().getHierarchicalPart()); + result += ""; + result += "\n"; + result += "\n"; + result += "
id
name
\n"; + result += "

\n"; + result += "
\n\n"; + } else { + // list all groups + result = "\n

Persons

\n"; + result += ""; + List persons = store.getActors("uri", "*", NodeTypes.PERSON); + for (Actor person : persons) { + Reference url = this.getReference().clone(); + url.addSegment(person.getId()); + result += String.format("\n", url, + person.getIdString(), person.getName(), person.getUri()); + } + result += "
idnameuri
%s%s%s
\n"; + result += "

Add new person

\n"; + result += "\n"; + } + return new StringRepresentation(result, MediaType.TEXT_HTML); + } + + /** + * POST creates a new Group. + * + * @return + */ + @Post + public Representation doPost(Representation entity) { + logger.debug("PersonsResource doPost!"); + // TODO: do authentication + Form form = new Form(entity); + String id = form.getFirstValue("id"); + if (id == null || id.isEmpty() || id.matches("\\W")) { + // invalid id + setStatus(Status.CLIENT_ERROR_BAD_REQUEST); + return null; + } + String name = form.getFirstValue("name"); + if (name == null || name.isEmpty()) { + name = ((BaseRestlet) getApplication()).getFullNameFromLdap(id); + } + String uri = form.getFirstValue("uri"); + if (uri != null && uri.isEmpty()) uri = null; + Person newPerson = new Person(id, uri, name); + Actor storedPerson = store.storeActor(newPerson); + id = storedPerson.getId(); + // return 303: see other + setStatus(Status.REDIRECTION_SEE_OTHER); + // go GET URL for this person + Reference url = this.getReference().clone(); + url.addSegment(id); + this.getResponse().setLocationRef(url); + return null; + } + +} diff -r 05b631a084d0 -r 0731c4549065 src/main/webapp/groups/index.html --- a/src/main/webapp/groups/index.html Tue Sep 25 16:08:11 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ - - - - - - -Annotation Server Groups - - - - -

Annotation groups

- - - - diff -r 05b631a084d0 -r 0731c4549065 src/main/webapp/groups/new_group.html --- a/src/main/webapp/groups/new_group.html Tue Sep 25 16:08:11 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ - - - - - - -Annotation Server Groups - - - - -

New group

-
- - - - - - - - - -
ID (short name)
Name
- -
- - diff -r 05b631a084d0 -r 0731c4549065 src/main/webapp/index.html --- a/src/main/webapp/index.html Tue Sep 25 16:08:11 2012 +0200 +++ b/src/main/webapp/index.html Tue Sep 25 21:59:21 2012 +0200 @@ -11,8 +11,14 @@

Annotation Server

+

View

+ +

Admin