/** * */ 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 * . * #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("\n

Annotations

\n"); result.append("

"); if (offset > 0) { result.append(String.format(" prev ", 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(" next", baseUrl, Math.min(max, numannots), limit)); } result.append("

\n"); result.append(""); result.append(""); List annotations = store.getAnnotations("id", "*", limit, offset); for (Annotation annotation : annotations) { Reference url = this.getReference().clone(); url.addSegment(annotation.getUrlId()); result.append(String.format("\n", url, annotation.getUri(), annotation.getBodyText(), annotation.getTargetBaseUri(), annotation.getTargetBaseUri(), annotation.getTargetFragment(), annotation.getCreatorName())); } result.append("
uritexttargetfragmentcreator
%s%s%s%s%s
\n"); result.append("\n"); return new StringRepresentation(result, MediaType.TEXT_HTML); } }