Ignore:
Timestamp:
Sep 25, 2012, 7:59:21 PM (12 years ago)
Author:
casties
Branch:
default
Message:

UI for editing groups and persons works now. (still no authorisation!)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java

    r24 r32  
    55
    66import org.apache.log4j.Logger;
     7import org.restlet.data.Form;
    78import org.restlet.data.MediaType;
    89import org.restlet.data.Reference;
     
    1011import org.restlet.representation.Representation;
    1112import org.restlet.representation.StringRepresentation;
     13import org.restlet.resource.Delete;
    1214import org.restlet.resource.Get;
     15import org.restlet.resource.Put;
    1316import org.restlet.resource.ResourceException;
    1417import org.restlet.resource.ServerResource;
     
    2932
    3033    protected AnnotationStore store;
    31    
     34
    3235    protected String requestId;
    33    
     36
    3437    protected Group group;
    35    
     38
    3639    @Override
    3740    protected void doInit() throws ResourceException {
     
    5861    @Get("html")
    5962    public Representation doGetHTML(Representation entity) {
    60         if (requestId == null || requestId.isEmpty()) {
     63        if (group == null) {
     64            // invalid id
     65            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
     66            return null;
     67        }
     68        String result = null;
     69        // get form parameter
     70        Form f = this.getQuery();
     71        String form = f.getFirstValue("form");
     72        if (form != null && form.equals("edit")) {
     73            // output edit form
     74            result = "<html><body>\n";
     75            result += String.format("<h1>Edit group %s</h1>\n", group.getId());
     76            result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef());
     77            // tunnel PUT method through POST
     78            result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart());
     79            result += "<table>";
     80            result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n",
     81                    group.getName());
     82            result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n",
     83                    group.getUriString());
     84            result += "</table>\n";
     85            result += "<p><input type=\"submit\"/></p>";
     86            result += "</table>\n</form>\n</body>\n</html>";
     87        } else {
     88            // output group content
     89            result = "<html><body>\n<h1>Group</h1>\n";
     90            result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference().getParentRef());
     91            result += "<table>";
     92            result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
     93            result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
     94            result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
     95            result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this
     96                    .getReference().addSegment("members"));
     97            result += "</table>\n";
     98            result += "<p><a href=\"?form=edit\">Edit group</a></p>\n";
     99            // tunnel POST as DELETE
     100            result += String.format(
     101                    "<form method=\"post\" action=\"%s?method=DELETE\"><input type=\"submit\" value=\"Delete group\"/></form>\n",
     102                    this.getReference().getHierarchicalPart());
     103            result += "</body>\n</html>";
     104        }
     105        return new StringRepresentation(result, MediaType.TEXT_HTML);
     106    }
     107
     108    /**
     109     * PUT updates the group.
     110     *
     111     * @param entity
     112     * @return
     113     */
     114    @Put
     115    public Representation doPut(Representation entity) {
     116        logger.debug("GroupResource.doPut!");
     117        if (group == null) {
    61118            // invalid id
    62119            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
    63120            return null;
    64121        }
    65         String result = null;
    66         Reference groupsUrl = this.getReference().getParentRef();
    67         result = "<html><body>\n<h1>Group</h1>\n";
    68         result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
    69         result += "<table>";
    70         result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
    71         result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
    72         result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
    73         result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference()
    74                 .addSegment("members"));
    75         result += "</table>\n</body>\n</html>";
    76 
    77         logger.debug("sending:");
    78         logger.debug(result);
    79         return new StringRepresentation(result, MediaType.TEXT_HTML);
     122        // TODO: do authentication
     123        Form form = new Form(entity);
     124        String name = form.getFirstValue("name");
     125        String uri = form.getFirstValue("uri");
     126        if (name != null && !name.isEmpty()) {
     127            group.setName(name);
     128        }
     129        if (uri != null && !uri.isEmpty()) {
     130            group.setUri(uri);
     131        }
     132        store.storeActor(group);
     133        // return 303: see other
     134        setStatus(Status.REDIRECTION_SEE_OTHER);
     135        // go GET same URL
     136        Reference url = this.getReference();
     137        this.getResponse().setLocationRef(url);
     138        return null;
    80139    }
    81140
     141    /**
     142     * DELETE deletes the group.
     143     *
     144     * @param entity
     145     * @return
     146     */
     147    @Delete
     148    public Representation doDelete(Representation entity) {
     149        logger.debug("GroupResource.doDelete!");
     150        if (group == null) {
     151            // invalid id
     152            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
     153            return null;
     154        }
     155        // TODO: do authentication
     156        store.deleteActor(group);
     157        // return 303: see other
     158        setStatus(Status.REDIRECTION_SEE_OTHER);
     159        // go GET parent URL
     160        Reference url = this.getReference().getParentRef();
     161        this.getResponse().setLocationRef(url);
     162        return null;
     163    }
    82164}
Note: See TracChangeset for help on using the changeset viewer.