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

Last change on this file since 101:7268c3ca025b was 101:7268c3ca025b, checked in by casties, 9 years ago

make admin ui view of all annotations scale better.

File size: 4.1 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5
6/*
7 * #%L
8 * AnnotationManager
9 * %%
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
11 * %%
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Lesser Public License for more details.
21 *
22 * You should have received a copy of the GNU General Lesser Public
23 * License along with this program.  If not, see
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
25 * #L%
26 */
27
28import java.util.List;
29import java.util.logging.Logger;
30
31import org.restlet.data.Form;
32import org.restlet.data.MediaType;
33import org.restlet.data.Reference;
34import org.restlet.representation.Representation;
35import org.restlet.representation.StringRepresentation;
36import org.restlet.resource.Get;
37import org.restlet.resource.ResourceException;
38import org.restlet.resource.ServerResource;
39
40import de.mpiwg.itgroup.annotations.Annotation;
41import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
42import de.mpiwg.itgroup.annotations.restlet.AnnotatorResourceImpl;
43import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
44
45/**
46 * Resource class for the list of annotations.
47 *
48 * @author casties
49 *
50 */
51public class AnnotationsResource extends ServerResource {
52
53    public static Logger logger = Logger.getLogger(AnnotationsResource.class.getCanonicalName());
54
55    private AnnotationStore store;
56
57    @Override
58    protected void doInit() throws ResourceException {
59        super.doInit();
60        // get store instance
61        if (store == null) {
62            store = ((BaseRestlet) getApplication()).getAnnotationStore();
63        }
64    }
65
66    /**
67     * GET with HTML content type. Lists all annotations.
68     *
69     * @param entity
70     * @return
71     */
72    @Get("html")
73    public Representation doGetHTML(Representation entity) {
74        Form form = getRequest().getResourceRef().getQueryAsForm();
75        int limit = AnnotatorResourceImpl.getInt(form.getFirstValue("limit", "100"));
76        int offset = AnnotatorResourceImpl.getInt(form.getFirstValue("offset", "0"));
77        int max = offset + limit;
78        int numannots = store.getAnnotationCount();
79        String baseUrl = this.getReference().getHierarchicalPart();
80        StringBuilder result = null;
81        // list all annotations
82        result = new StringBuilder("<html><body>\n<h1>Annotations</h1>\n");
83        result.append("<p>");
84        if (offset > 0) {
85            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">prev</a> ", baseUrl, Math.max(offset - limit, 0), limit));
86        }
87        result.append(String.format("| %s - %s of %s annotations |", offset, (offset + limit), numannots));
88        if (max < numannots) {
89            result.append(String.format(" <a href=\"%s?offset=%s&limit=%s\">next</a>", baseUrl, Math.min(max, numannots), limit));
90        }
91        result.append("</p>\n");
92        result.append("<table>");
93        result.append("<tr><th>uri</th><th>text</th><th>target</th><th>fragment</th><th>creator</th></tr>");
94        List<Annotation> annotations = store.getAnnotations("id", "*", limit, offset);
95        for (Annotation annotation : annotations) {
96            Reference url = this.getReference().clone();
97            url.addSegment(annotation.getUrlId());
98            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,
99                    annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetBaseUri(), 
100                    annotation.getTargetFragment(), annotation.getCreatorName()));
101        }
102        result.append("</table>\n");
103        result.append("</body>\n</html>");
104        return new StringRepresentation(result, MediaType.TEXT_HTML);
105    }
106
107}
Note: See TracBrowser for help on using the repository browser.