view src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreGroups.java @ 21:1ac626309352

more work on HTML UI (doesn't work yet).
author casties
date Fri, 07 Sep 2012 18:10:18 +0200
parents f0f55ab768c9
children b1fb0d117877
line wrap: on
line source

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

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import de.mpiwg.itgroup.annotations.Group;
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("html")
    public Representation doGetHTML(Representation entity){
        Form form = getRequest().getResourceRef().getQueryAsForm();
        // id from URI /annotations/groups/{id}
        String id = (String) getRequest().getAttributes().get("id");
        logger.debug("group-id=" + id);
        String result="<html><body>\n<h1>Groups</h1>\n<table>";
        result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
        store = getAnnotationStore();
        if (id == null) {
            // list all groups
            List<Group> groups = store.getGroups("uri", "*");
            for (Group group : groups) {
                String groupLink = group.getId();
                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, group.getId(), group.getName(), group.getUri());
            }
        } else {
            // just one group
            List<Group> groups = store.getGroups("uri", "*");
            for (Group group : groups) {
                String groupLink = group.getId();
                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupLink, 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);
    }

    protected AnnotationStore getAnnotationStore() {
        if (store == null) {
            store = ((BaseRestlet) getApplication()).getAnnotationStore();
        }
        return store;
    }
}