view src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.java @ 24:e208a7b1a37a

more work on groups ui.
author casties
date Sun, 23 Sep 2012 16:28:05 +0200
parents d22d01ba953a
children 0731c4549065
line wrap: on
line source

/**
 * 
 */
package de.mpiwg.itgroup.annotations.restlet.annotations_ui;

import org.apache.log4j.Logger;
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.ResourceException;
import org.restlet.resource.ServerResource;

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);

    protected AnnotationStore store;
    
    protected String requestId;
    
    protected Group group;
    
    @Override
    protected void doInit() throws ResourceException {
        super.doInit();
        // id from URI /annotations/groups/{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) {
            group = (Group) store.getActor(new Group(requestId));
        }
    }

    /**
     * GET with HTML content type. Shows the group.
     * 
     * @param entity
     * @return
     */
    @Get("html")
    public Representation doGetHTML(Representation entity) {
        if (requestId == null || requestId.isEmpty()) {
            // invalid id
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return null;
        }
        String result = null;
        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);
        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);
    }

}