view src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationResource.java @ 93:3d1a00418b42 admin_ui_rel_links

starting web admin ui with relative links. doesn't fully work yet.
author casties
date Mon, 09 Feb 2015 12:55:51 +0100
parents e973cbf5d58f
children
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.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.logging.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.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

import de.mpiwg.itgroup.annotations.Annotation;
import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;

/**
 * Resource class for a single annotation.
 * 
 * @author casties
 * 
 */
public class AnnotationResource extends ServerResource {

    public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.annotations_ui.AnnotationResource");

    protected AnnotationStore store;

    protected String requestId;

    protected Annotation annotation;

    @Override
    protected void doInit() throws ResourceException {
        super.doInit();
        // id from URI /annotations/persons/{id}
        requestId = (String) getRequest().getAttributes().get("id");
        logger.fine("annoation-id=" + requestId);
        // get store instance
        if (store == null) {
            store = ((BaseRestlet) getApplication()).getAnnotationStore();
        }
        // get annotation from store
        if (requestId != null) {
            // URL decode
            try {
                requestId = URLDecoder.decode(requestId, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // this shouldn't happen
            }
            // the ID in the path is encoded
            String id = Annotation.decodeId(requestId);
            annotation = store.getAnnotationById(id);
        }
    }

    /**
     * GET with HTML content type. Shows the person.
     * 
     * @param entity
     * @return
     */
    @Get("html")
    public Representation doGetHTML(Representation entity) {
        if (annotation == null) {
            // invalid id
            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return null;
        }
        String result = null;
        // get form parameter
        Form f = this.getQuery();
        String form = f.getFirstValue("form");
        if (form != null && form.equals("edit")) {
            /*
            // output edit form
            result = "<html><body>\n";
            result += String.format("<h1>Edit person %s</h1>\n", annotation.getId());
            result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference().getParentRef());
            // tunnel PUT method through POST
            result += String.format("<form method=\"post\" action=\"%s?method=PUT\">\n", this.getReference().getHierarchicalPart());
            result += "<table>";
            result += String.format("<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\" value=\"%s\"/></td></tr>\n",
                    annotation.getName());
            result += String.format("<tr><td><b>uri</b></td><td><input type=\"text\" name=\"uri\" value=\"%s\"/></td></tr>\n",
                    annotation.getUriString());
            result += "</table>\n";
            result += "<p><input type=\"submit\"/></p>";
            result += "</table>\n</form>\n</body>\n</html>";
            */
        } else {
            // output person content
            result = "<html><body>\n<h1>Annotation</h1>\n";
            result += "<p><a href=\"./\">All annotations</a></p>";
            result += "<table>";
            result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", annotation.getUri());
            result += String.format("<tr><td><b>text</b></td><td>%s</td></tr>\n", annotation.getBodyText());
            result += String.format("<tr><td><b>target</b></td><td>%s</td></tr>\n", annotation.getTargetBaseUri());
            result += String.format("<tr><td><b>fragment</b></td><td>%s</td></tr>\n", annotation.getTargetFragment());
            result += String.format("<tr><td><b>quote</b></td><td>%s</td></tr>\n", annotation.getQuote());
            result += String.format("<tr><td><b>resource</b></td><td>%s</td></tr>\n", annotation.getResourceUri());
            result += String.format("<tr><td><b>creator</b></td><td>%s</td></tr>\n", annotation.getCreatorName());
            result += "</table>\n";
            result += "<br/>\n";
            result += "<table>";
            result += String.format("<tr><td><b>admin permission</b></td><td>%s</td></tr>\n", annotation.getAdminPermission() != null ? annotation.getAdminPermission().getIdString() : "null");
            result += String.format("<tr><td><b>delete permission</b></td><td>%s</td></tr>\n", annotation.getDeletePermission() != null ? annotation.getDeletePermission().getIdString() : "null");
            result += String.format("<tr><td><b>update permission</b></td><td>%s</td></tr>\n", annotation.getUpdatePermission() != null ? annotation.getUpdatePermission().getIdString() : "null");
            result += String.format("<tr><td><b>read permission</b></td><td>%s</td></tr>\n", annotation.getReadPermission() != null ? annotation.getReadPermission().getIdString() : "null");
            result += "</table>\n";
            //result += "<p><a href=\"?form=edit\">Edit annotation</a></p>\n";
            // tunnel POST as DELETE
            result += "<form method=\"post\" action=\"?method=DELETE\"><input type=\"submit\" value=\"Delete annotation\"/></form>\n";
            result += "</body>\n</html>";
        }
        return new StringRepresentation(result, MediaType.TEXT_HTML);
    }

    /**
     * PUT updates the annotation.
     * 
     * @param entity
     * @return
     */
    @Put
    public Representation doPut(Representation entity) {
        logger.fine("AnnotationResource.doPut!");
        if (annotation == null) {
            // invalid id
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return null;
        }
        // NOT YET
        return null;
        /*
        // TODO: do authentication
        Form form = new Form(entity);
        String name = form.getFirstValue("name");
        String uri = form.getFirstValue("uri");
        if (name != null && !name.isEmpty()) {
            annotation.setName(name);
        }
        if (uri != null && !uri.isEmpty()) {
            annotation.setUri(uri);
        }
        store.storeActor(annotation);
        // return 303: see other
        setStatus(Status.REDIRECTION_SEE_OTHER);
        // go GET same URL
        Reference url = this.getReference();
        this.getResponse().setLocationRef(url);
        return null;
        */
    }

    /**
     * DELETE deletes the annotation.
     * 
     * @param entity
     * @return
     */
    @Delete
    public Representation doDelete(Representation entity) {
        logger.fine("AnnotationResource.doDelete!");
        if (annotation == null) {
            // invalid id
            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return null;
        }
        // TODO: do authentication
        store.deleteAnnotationById(annotation.getUri());
        // return 303: see other
        setStatus(Status.REDIRECTION_SEE_OTHER);
        // go GET parent URL
        Reference url = this.getReference().getParentRef();
        this.getResponse().setLocationRef(url);
        return null;
    }
}