view src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/AnnotationsResource.java @ 101:7268c3ca025b

make admin ui view of all annotations scale better.
author casties
date Fri, 13 Feb 2015 18:10:11 +0100
parents 199143c669e4
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.util.List;
import java.util.logging.Logger;

import org.restlet.data.Form;
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.AnnotatorResourceImpl;
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) {
        Form form = getRequest().getResourceRef().getQueryAsForm();
        int limit = AnnotatorResourceImpl.getInt(form.getFirstValue("limit", "100"));
        int offset = AnnotatorResourceImpl.getInt(form.getFirstValue("offset", "0"));
        int max = offset + limit;
        int numannots = store.getAnnotationCount();
        String baseUrl = this.getReference().getHierarchicalPart();
        StringBuilder result = null;
        // list all annotations
        result = new StringBuilder("<html><body>\n<h1>Annotations</h1>\n");
        result.append("<p>");
        if (offset > 0) {
            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">prev</a> ", baseUrl, Math.max(offset - limit, 0), limit));
        }
        result.append(String.format("| %s - %s of %s annotations |", offset, (offset + limit), numannots));
        if (max < numannots) {
            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">next</a>", baseUrl, Math.min(max, numannots), limit));
        }
        result.append("</p>\n");
        result.append("<table>");
        result.append("<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>");
        List<Annotation> annotations = store.getAnnotations("id", "*", limit, offset);
        for (Annotation annotation : annotations) {
            Reference url = this.getReference().clone();
            url.addSegment(annotation.getUrlId());
            result.append(String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
                    annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetBaseUri(), 
                    annotation.getTargetFragment(), annotation.getCreatorName()));
        }
        result.append("</table>\n");
        result.append("</body>\n</html>");
        return new StringRepresentation(result, MediaType.TEXT_HTML);
    }

}