Mercurial > hg > AnnotationManagerN4J
diff src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java @ 32:0731c4549065
UI for editing groups and persons works now. (still no authorisation!)
author | casties |
---|---|
date | Tue, 25 Sep 2012 21:59:21 +0200 |
parents | |
children | 707902d468f6 |
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/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 = "<html><body>\n"; + result += "<h1>New person</h1>\n"; + result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference()); + result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart()); + result += "<table>"; + result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n"; + result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n"; + result += "</table>\n"; + result += "<p><input type=\"submit\"/></p>\n"; + result += "</form>\n</body>\n</html>"; + } else { + // list all groups + result = "<html><body>\n<h1>Persons</h1>\n<table>"; + result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; + List<Actor> persons = store.getActors("uri", "*", NodeTypes.PERSON); + for (Actor person : persons) { + Reference url = this.getReference().clone(); + url.addSegment(person.getId()); + result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url, + person.getIdString(), person.getName(), person.getUri()); + } + result += "</table>\n"; + result += "<p><a href=\"?form=new_person\">Add new person</a></p>\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("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; + } + +}