/** * */ 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.data.Status; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.Get; import org.restlet.resource.Post; import org.restlet.resource.ResourceException; import org.restlet.resource.ServerResource; import de.mpiwg.itgroup.annotations.Actor; import de.mpiwg.itgroup.annotations.Person; import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; /** * Resource class for the list of annotation users (Person class). * * @author casties * */ public class PersonsResource extends ServerResource { public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.annotations_ui.PersonsResource"); 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 persons. * * @param entity * @return */ @Get("html") public Representation doGetHTML(Representation entity) { String result = null; // get form parameter Form f = this.getQuery(); String form = f.getFirstValue("form"); if (form != null && form.equals("new_person")) { // output new person form result = "\n"; result += "

New person

\n"; result += String.format("

All persons

", this.getReference()); result += String.format("
\n", this.getReference().getHierarchicalPart()); result += ""; result += "\n"; result += "\n"; result += "
id
name
\n"; result += "

\n"; result += "
\n\n"; } else { // list all persons result = "\n

Persons

\n"; result += ""; List persons = store.getPersons("uri", "*"); for (Person person : persons) { Reference url = this.getReference().clone(); url.addSegment(person.getIdString()); result += String.format("\n", url, person.getIdString(), person.getName(), person.getUri()); } result += "
idnameuri
%s%s%s
\n"; result += "

Add new person

\n"; result += "\n"; } return new StringRepresentation(result, MediaType.TEXT_HTML); } /** * POST creates a new person. * * @return */ @Post public Representation doPost(Representation entity) { logger.fine("PersonsResource doPost!"); // TODO: do authentication Form form = new Form(entity); String id = form.getFirstValue("id"); if (id == null || id.isEmpty() || id.matches("\\W")) { // invalid id setStatus(Status.CLIENT_ERROR_BAD_REQUEST); return null; } String name = form.getFirstValue("name"); if (name == null || name.isEmpty()) { name = ((BaseRestlet) getApplication()).getFullNameForId(id); } String uri = form.getFirstValue("uri"); if (uri != null && uri.isEmpty()) uri = null; Person newPerson = new Person(id, uri, name); Actor storedPerson = store.storeActor(newPerson); id = storedPerson.getId(); // return 303: see other setStatus(Status.REDIRECTION_SEE_OTHER); // go GET URL for this person Reference url = this.getReference().clone(); url.addSegment(id); this.getResponse().setLocationRef(url); return null; } }