source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java @ 86:e3f0613b2f2d

Last change on this file since 86:e3f0613b2f2d was 86:e3f0613b2f2d, checked in by casties, 9 years ago

renamed getFullname to make it configurable.
fixed Restlet at 2.2.3.

File size: 5.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.data.Status;
35import org.restlet.representation.Representation;
36import org.restlet.representation.StringRepresentation;
37import org.restlet.resource.Get;
38import org.restlet.resource.Post;
39import org.restlet.resource.ResourceException;
40import org.restlet.resource.ServerResource;
41
42import de.mpiwg.itgroup.annotations.Actor;
43import de.mpiwg.itgroup.annotations.Person;
44import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
45import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
46
47/**
48 * Resource class for the list of annotation users (Person class).
49 *
50 * @author casties
51 *
52 */
53public class PersonsResource extends ServerResource {
54
55    public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.annotations_ui.PersonsResource");
56
57    private AnnotationStore store;
58
59    @Override
60    protected void doInit() throws ResourceException {
61        super.doInit();
62        // get store instance
63        if (store == null) {
64            store = ((BaseRestlet) getApplication()).getAnnotationStore();
65        }
66    }
67
68    /**
69     * GET with HTML content type. Lists all persons.
70     *
71     * @param entity
72     * @return
73     */
74    @Get("html")
75    public Representation doGetHTML(Representation entity) {
76        String result = null;
77        // get form parameter
78        Form f = this.getQuery();
79        String form = f.getFirstValue("form");
80        if (form != null && form.equals("new_person")) {
81            // output new person form
82            result = "<html><body>\n";
83            result += "<h1>New person</h1>\n";
84            result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference());
85            result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
86            result += "<table>";
87            result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
88            result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
89            result += "</table>\n";
90            result += "<p><input type=\"submit\"/></p>\n";
91            result += "</form>\n</body>\n</html>";
92        } else {
93            // list all persons
94            result = "<html><body>\n<h1>Persons</h1>\n<table>";
95            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
96            List<Person> persons = store.getPersons("uri", "*");
97            for (Person person : persons) {
98                Reference url = this.getReference().clone();
99                url.addSegment(person.getIdString());
100                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
101                        person.getIdString(), person.getName(), person.getUri());
102            }
103            result += "</table>\n";
104            result += "<p><a href=\"?form=new_person\">Add new person</a></p>\n";
105            result += "</body>\n</html>";
106        }
107        return new StringRepresentation(result, MediaType.TEXT_HTML);
108    }
109
110    /**
111     * POST creates a new person.
112     *
113     * @return
114     */
115    @Post
116    public Representation doPost(Representation entity) {
117        logger.fine("PersonsResource doPost!");
118        // TODO: do authentication
119        Form form = new Form(entity);
120        String id = form.getFirstValue("id");
121        if (id == null || id.isEmpty() || id.matches("\\W")) {
122            // invalid id
123            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
124            return null;
125        }
126        String name = form.getFirstValue("name");
127        if (name == null || name.isEmpty()) {
128            name = ((BaseRestlet) getApplication()).getFullNameForId(id);
129        }
130        String uri = form.getFirstValue("uri");
131        if (uri != null && uri.isEmpty()) uri = null;
132        Person newPerson = new Person(id, uri, name);
133        Actor storedPerson = store.storeActor(newPerson);
134        id = storedPerson.getId();
135        // return 303: see other
136        setStatus(Status.REDIRECTION_SEE_OTHER);
137        // go GET URL for this person
138        Reference url = this.getReference().clone();
139        url.addSegment(id);
140        this.getResponse().setLocationRef(url);
141        return null;
142    }
143
144}
Note: See TracBrowser for help on using the repository browser.