diff src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.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 d22d01ba953a
children 2b1e6df5e21a
line wrap: on
line diff
--- 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 = "<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());
+        // get form parameter
+        Form f = this.getQuery();
+        String form = f.getFirstValue("form");
+        if (form != null && form.equals("new_group")) {
+            // output new group form
+            result = "<html><body>\n";
+            result += "<h1>New group</h1>\n";
+            result += String.format("<p><a href=\"%s\">All groups</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>Groups</h1>\n";
+            result += "<table>\n";
+            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().clone();
+                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";
+            result += "<p><a href=\"?form=new_group\">Add new group</a></p>\n";
+            result += "</body>\n</html>";
         }
-        result += "</table>\n</body>\n</html>";
-        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