1 | /** |
---|
2 | * |
---|
3 | */ |
---|
4 | package 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 | |
---|
28 | import java.util.List; |
---|
29 | |
---|
30 | import org.apache.log4j.Logger; |
---|
31 | import org.restlet.data.Form; |
---|
32 | import org.restlet.data.MediaType; |
---|
33 | import org.restlet.data.Reference; |
---|
34 | import org.restlet.data.Status; |
---|
35 | import org.restlet.representation.Representation; |
---|
36 | import org.restlet.representation.StringRepresentation; |
---|
37 | import org.restlet.resource.Get; |
---|
38 | import org.restlet.resource.Post; |
---|
39 | import org.restlet.resource.ResourceException; |
---|
40 | import org.restlet.resource.ServerResource; |
---|
41 | |
---|
42 | import de.mpiwg.itgroup.annotations.Actor; |
---|
43 | import de.mpiwg.itgroup.annotations.Person; |
---|
44 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
45 | import 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 | */ |
---|
53 | public class PersonsResource extends ServerResource { |
---|
54 | |
---|
55 | public static Logger logger = Logger.getLogger(PersonsResource.class); |
---|
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.debug("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()).getFullNameFromLdap(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 | } |
---|