Mercurial > hg > AnnotationManagerN4J
changeset 23:d22d01ba953a
reorganised code for annotations and groups ui. work in progress.
author | casties |
---|---|
date | Sat, 22 Sep 2012 20:12:18 +0200 |
parents | b1fb0d117877 |
children | e208a7b1a37a |
files | src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreGroups.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreInfo.java src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreRestlet.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.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 src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/InfoResource.java src/main/webapp/WEB-INF/web.xml |
diffstat | 9 files changed, 417 insertions(+), 213 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreGroups.java Thu Sep 20 17:42:26 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -/** - * - */ -package de.mpiwg.itgroup.annotations.restlet; - -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; - -/** - * @author casties - * - */ -public class AnnotationStoreGroups extends ServerResource { - - public static Logger logger = Logger.getLogger(AnnotationStoreGroups.class); - - private AnnotationStore store; - - /** - * GET with HTML content type. Lists all groups. - * - * @param entity - * @return - */ - @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); - String result = null; - store = getAnnotationStore(); - if (id == null) { - // list all groups - result = "<html><body>\n<h1>Groups</h1>\n<table>"; - result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; - List<Group> groups = store.getGroups("uri", "*"); - for (Group group : groups) { - Reference groupUrl = this.getReference(); - groupUrl.addSegment(group.getId()); - result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, - group.getId(), group.getName(), group.getUri()); - } - result += "</table>\n</body>\n</html>"; - } else { - // just one group - 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()); - result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); - result += "<tr><td><b>members</b></td><td>"; - List<Person> members = store.getMembersOfGroup(group); - for (Person p : members) { - result += String.format("%s (%s)\n", p.getName(), p.getIdString()); - } - result += "</td></tr>\n"; - result += "</table>\n</body>\n</html>"; - } - - logger.debug("sending:"); - logger.debug(result); - return new StringRepresentation(result, MediaType.TEXT_HTML); - } - - /** - * POST with HTML content-type. Creates a new Group. - * - * @return - */ - @Post - public Representation doPostHTML(Representation entity) { - logger.debug("AnnotationStoreGroups doPostHTML!"); - // 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 = makeGroupId(id); - 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; - } - - /** - * Returns a group id based on the given id. - * - * @param id - * @return - */ - protected String makeGroupId(String id) { - // TODO: should we use different ids? - id = id.replaceAll("\\W", "_"); - id = id.toLowerCase(); - return id; - } - - protected AnnotationStore getAnnotationStore() { - if (store == null) { - store = ((BaseRestlet) getApplication()).getAnnotationStore(); - } - return store; - } -}
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreInfo.java Thu Sep 20 17:42:26 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -package de.mpiwg.itgroup.annotations.restlet; - -import java.io.InputStream; - -import org.restlet.data.MediaType; -import org.restlet.representation.InputRepresentation; -import org.restlet.representation.Representation; -import org.restlet.resource.Get; -import org.restlet.resource.ServerResource; - - -public class AnnotationStoreInfo extends ServerResource { - - @Get("html") - public Representation getHTML(){ - InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/annotations/annotationstore-info.html"); - - Representation rep = new InputRepresentation(is,MediaType.TEXT_HTML); - return rep; - - } - -} -
--- a/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreRestlet.java Thu Sep 20 17:42:26 2012 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/** - * - */ -package de.mpiwg.itgroup.annotations.restlet; - -import org.apache.log4j.Logger; -import org.restlet.Context; -import org.restlet.Restlet; -import org.restlet.routing.Router; - -/** - * @author casties - * - */ -public class AnnotationStoreRestlet extends BaseRestlet { - - public final String version = "AnnotationManagerN4J/AnnotationStore 0.1"; - - public static Logger logger = Logger.getLogger(AnnotationStoreRestlet.class); - - public AnnotationStoreRestlet(Context context) { - super(context); - logger.debug("StoreRestlet!"); - } - - /* - * (non-Javadoc) - * - * @see org.restlet.Application#createInboundRoot() - */ - @Override - public Restlet createInboundRoot() { - // this.authenticator = createAuthenticator(); - - Router router = new Router(getContext()); - - router.attach("/groups", AnnotationStoreGroups.class); - router.attach("/groups/", AnnotationStoreGroups.class); - router.attach("/groups/{id}", AnnotationStoreGroups.class); - - router.attach("/", AnnotationStoreInfo.class); - // authenticator.setNext(router); - // return authenticator; - - return router; - } - - - @Override - public String getVersion() { - return version; - } - -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsUiRestlet.java Sat Sep 22 20:12:18 2012 +0200 @@ -0,0 +1,58 @@ +/** + * + */ +package de.mpiwg.itgroup.annotations.restlet.annotations_ui; + +import org.apache.log4j.Logger; +import org.restlet.Context; +import org.restlet.Restlet; +import org.restlet.routing.Router; + +import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; + +/** + * @author casties + * + */ +public class AnnotationsUiRestlet extends BaseRestlet { + + public final String version = "AnnotationManagerN4J/AnnotationStore 0.1"; + + public static Logger logger = Logger.getLogger(AnnotationsUiRestlet.class); + + public AnnotationsUiRestlet(Context context) { + super(context); + logger.debug("StoreRestlet!"); + } + + /* + * (non-Javadoc) + * + * @see org.restlet.Application#createInboundRoot() + */ + @Override + public Restlet createInboundRoot() { + // this.authenticator = createAuthenticator(); + + Router router = new Router(getContext()); + + router.attach("/groups", GroupsResource.class); + router.attach("/groups/", GroupsResource.class); + router.attach("/groups/{id}", GroupResource.class); + router.attach("/groups/{id}/", GroupResource.class); + router.attach("/groups/{id}/members", GroupMembersResource.class); + + router.attach("/", InfoResource.class); + // authenticator.setNext(router); + // return authenticator; + + return router; + } + + + @Override + public String getVersion() { + return version; + } + +}
--- /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; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java Sat Sep 22 20:12:18 2012 +0200 @@ -0,0 +1,107 @@ +/** + * + */ +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.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; + +/** + * Resource class for a single group. + * + * @author casties + * + */ +public class GroupResource extends ServerResource { + + public static Logger logger = Logger.getLogger(GroupResource.class); + + private AnnotationStore store; + + /** + * GET with HTML content type. Shows the group. + * + * @param entity + * @return + */ + @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()) { + // 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()); + result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri()); + result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference() + .addSegment("members")); + result += "</table>\n</body>\n</html>"; + + logger.debug("sending:"); + logger.debug(result); + 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; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java Sat Sep 22 20:12:18 2012 +0200 @@ -0,0 +1,112 @@ +/** + * + */ +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.neo4j.AnnotationStore; +import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; + +/** + * Resource class for the list of annotation groups. + * + * @author casties + * + */ +public class GroupsResource extends ServerResource { + + public static Logger logger = Logger.getLogger(GroupsResource.class); + + private AnnotationStore store; + + /** + * GET with HTML content type. Lists all groups. + * + * @param entity + * @return + */ + @Get("html") + public Representation doGetHTML(Representation entity) { + String result = null; + store = getAnnotationStore(); + // list all groups + result = "<html><body>\n<h1>Groups</h1>\n<table>"; + result += "<tr><th>id</th><th>name</th><th>uri</th></tr>"; + List<Group> groups = store.getGroups("uri", "*"); + for (Group group : groups) { + Reference groupUrl = this.getReference(); + groupUrl.addSegment(group.getId()); + result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(), + group.getName(), group.getUri()); + } + result += "</table>\n</body>\n</html>"; + logger.debug("sending:"); + logger.debug(result); + return new StringRepresentation(result, MediaType.TEXT_HTML); + } + + /** + * POST creates a new Group. + * + * @return + */ + @Post + public Representation doPost(Representation entity) { + logger.debug("AnnotationsUiGroups 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 = makeGroupId(id); + 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; + } + + /** + * Returns a group id based on the given id. + * + * @param id + * @return + */ + protected String makeGroupId(String id) { + // TODO: should we use different ids? + id = id.replaceAll("\\W", "_"); + id = id.toLowerCase(); + return id; + } + + protected AnnotationStore getAnnotationStore() { + if (store == null) { + store = ((BaseRestlet) getApplication()).getAnnotationStore(); + } + return store; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/InfoResource.java Sat Sep 22 20:12:18 2012 +0200 @@ -0,0 +1,24 @@ +package de.mpiwg.itgroup.annotations.restlet.annotations_ui; + +import java.io.InputStream; + +import org.restlet.data.MediaType; +import org.restlet.representation.InputRepresentation; +import org.restlet.representation.Representation; +import org.restlet.resource.Get; +import org.restlet.resource.ServerResource; + + +public class InfoResource extends ServerResource { + + @Get("html") + public Representation getHTML(){ + InputStream is = getClass().getResourceAsStream("/de/mpiwg/itgroup/annotations/annotationstore-info.html"); + + Representation rep = new InputRepresentation(is,MediaType.TEXT_HTML); + return rep; + + } + +} +
--- a/src/main/webapp/WEB-INF/web.xml Thu Sep 20 17:42:26 2012 +0200 +++ b/src/main/webapp/WEB-INF/web.xml Sat Sep 22 20:12:18 2012 +0200 @@ -24,7 +24,7 @@ <!-- Application classname --> <init-param> <param-name>org.restlet.application</param-name> - <param-value>de.mpiwg.itgroup.annotations.restlet.AnnotationStoreRestlet</param-value> + <param-value>de.mpiwg.itgroup.annotations.restlet.annotations_ui.AnnotationsUiRestlet</param-value> </init-param> <!-- Load this servlet at server startup time --> <load-on-startup>