annotate src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/PersonsResource.java @ 105:7417f5915181 default tip

check admin permission before changing permissions. Enum for typesafe actions.
author casties
date Fri, 10 Feb 2017 15:45:35 +0100
parents e3f0613b2f2d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
1 /**
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
2 *
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
3 */
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
4 package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
5
70
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
6 /*
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
7 * #%L
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
8 * AnnotationManager
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
9 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
11 * %%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
12 * This program is free software: you can redistribute it and/or modify
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
13 * it under the terms of the GNU Lesser General Public License as
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
14 * published by the Free Software Foundation, either version 3 of the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
15 * License, or (at your option) any later version.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
16 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
17 * This program is distributed in the hope that it will be useful,
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
20 * GNU General Lesser Public License for more details.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
21 *
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
22 * You should have received a copy of the GNU General Lesser Public
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
23 * License along with this program. If not, see
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
25 * #L%
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
26 */
2b1e6df5e21a added lgpl_v3 license information.
casties
parents: 65
diff changeset
27
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
28 import java.util.List;
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
29 import java.util.logging.Logger;
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
30
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
31 import org.restlet.data.Form;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
32 import org.restlet.data.MediaType;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
33 import org.restlet.data.Reference;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
34 import org.restlet.data.Status;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
35 import org.restlet.representation.Representation;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
36 import org.restlet.representation.StringRepresentation;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
37 import org.restlet.resource.Get;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
38 import org.restlet.resource.Post;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
39 import org.restlet.resource.ResourceException;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
40 import org.restlet.resource.ServerResource;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
41
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
42 import de.mpiwg.itgroup.annotations.Actor;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
43 import de.mpiwg.itgroup.annotations.Person;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
44 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
45 import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
46
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
47 /**
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
48 * Resource class for the list of annotation users (Person class).
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
49 *
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
50 * @author casties
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
51 *
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
52 */
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
53 public class PersonsResource extends ServerResource {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
54
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
55 public static Logger logger = Logger.getLogger("de.mpiwg.itgroup.annotations.restlet.annotations_ui.PersonsResource");
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
56
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
57 private AnnotationStore store;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
58
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
59 @Override
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
60 protected void doInit() throws ResourceException {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
61 super.doInit();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
62 // get store instance
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
63 if (store == null) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
64 store = ((BaseRestlet) getApplication()).getAnnotationStore();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
65 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
66 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
67
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
68 /**
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
69 * GET with HTML content type. Lists all persons.
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
70 *
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
71 * @param entity
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
72 * @return
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
73 */
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
74 @Get("html")
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
75 public Representation doGetHTML(Representation entity) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
76 String result = null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
77 // get form parameter
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
78 Form f = this.getQuery();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
79 String form = f.getFirstValue("form");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
80 if (form != null && form.equals("new_person")) {
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
81 // output new person form
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
82 result = "<html><body>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
83 result += "<h1>New person</h1>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
84 result += String.format("<p><a href=\"%s\">All persons</a></p>", this.getReference());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
85 result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
86 result += "<table>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
87 result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
88 result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
89 result += "</table>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
90 result += "<p><input type=\"submit\"/></p>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
91 result += "</form>\n</body>\n</html>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
92 } else {
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
93 // list all persons
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
94 result = "<html><body>\n<h1>Persons</h1>\n<table>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
95 result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
45
707902d468f6 store reads and sends annotations resources now.
casties
parents: 32
diff changeset
96 List<Person> persons = store.getPersons("uri", "*");
707902d468f6 store reads and sends annotations resources now.
casties
parents: 32
diff changeset
97 for (Person person : persons) {
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
98 Reference url = this.getReference().clone();
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
99 url.addSegment(person.getIdString());
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
100 result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", url,
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
101 person.getIdString(), person.getName(), person.getUri());
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
102 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
103 result += "</table>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
104 result += "<p><a href=\"?form=new_person\">Add new person</a></p>\n";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
105 result += "</body>\n</html>";
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
106 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
107 return new StringRepresentation(result, MediaType.TEXT_HTML);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
108 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
109
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
110 /**
65
c0dd5314bada deal with special characters in urls.
casties
parents: 50
diff changeset
111 * POST creates a new person.
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
112 *
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
113 * @return
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
114 */
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
115 @Post
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
116 public Representation doPost(Representation entity) {
75
25eb2e1df106 change logging to java.util.logging.
casties
parents: 70
diff changeset
117 logger.fine("PersonsResource doPost!");
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
118 // TODO: do authentication
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
119 Form form = new Form(entity);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
120 String id = form.getFirstValue("id");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
121 if (id == null || id.isEmpty() || id.matches("\\W")) {
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
122 // invalid id
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
123 setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
124 return null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
125 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
126 String name = form.getFirstValue("name");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
127 if (name == null || name.isEmpty()) {
86
e3f0613b2f2d renamed getFullname to make it configurable.
casties
parents: 75
diff changeset
128 name = ((BaseRestlet) getApplication()).getFullNameForId(id);
32
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
129 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
130 String uri = form.getFirstValue("uri");
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
131 if (uri != null && uri.isEmpty()) uri = null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
132 Person newPerson = new Person(id, uri, name);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
133 Actor storedPerson = store.storeActor(newPerson);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
134 id = storedPerson.getId();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
135 // return 303: see other
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
136 setStatus(Status.REDIRECTION_SEE_OTHER);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
137 // go GET URL for this person
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
138 Reference url = this.getReference().clone();
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
139 url.addSegment(id);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
140 this.getResponse().setLocationRef(url);
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
141 return null;
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
142 }
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
143
0731c4549065 UI for editing groups and persons works now. (still no authorisation!)
casties
parents:
diff changeset
144 }