view src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java @ 70:2b1e6df5e21a

added lgpl_v3 license information.
author casties
date Thu, 06 Mar 2014 15:09:04 +0100
parents 0731c4549065
children 25eb2e1df106
line wrap: on
line source

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

/*
 * #%L
 * AnnotationManager
 * %%
 * Copyright (C) 2012 - 2014 MPIWG Berlin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

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.ResourceException;
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;

    @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.
     * 
     * @param entity
     * @return
     */
    @Get("html")
    public Representation doGetHTML(Representation entity) {
        String result = null;
        // 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>";
        }
        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);
        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;
    }
}