view src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java @ 94:fcb6fe10e08c

added config option for webapp URL prefix.
author casties
date Tue, 10 Feb 2015 17:45:56 +0100
parents 25eb2e1df106
children 199143c669e4
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 java.util.logging.Logger;

import org.restlet.data.MediaType;
import org.restlet.data.Reference;
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.Annotation;
import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;

/**
 * Resource class for the list of annotations.
 * 
 * @author casties
 * 
 */
public class AnnotationsResource extends ServerResource {

    public static Logger logger = Logger.getLogger(AnnotationsResource.class.getCanonicalName());

    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 annotations.
     * 
     * @param entity
     * @return
     */
    @Get("html")
    public Representation doGetHTML(Representation entity) {
        String result = null;
        // list all annotations
        result = "<html><body>\n<h1>Annotations</h1>\n<table>";
        result += "<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>";
        List<Annotation> annotations = store.getAnnotations("id", "*");
        for (Annotation annotation : annotations) {
            Reference url = this.getReference().clone();
            url.addSegment(annotation.getUrlId());
            result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>\n", url,
                    annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetFragment(),
                    annotation.getCreatorName());
        }
        result += "</table>\n";
        result += "</body>\n</html>";
        return new StringRepresentation(result, MediaType.TEXT_HTML);
    }

}