1 | /** |
---|
2 | * |
---|
3 | */ |
---|
4 | package de.mpiwg.itgroup.annotations.restlet.annotations_ui; |
---|
5 | |
---|
6 | import java.util.List; |
---|
7 | |
---|
8 | import org.apache.log4j.Logger; |
---|
9 | import org.restlet.data.Form; |
---|
10 | import org.restlet.data.MediaType; |
---|
11 | import org.restlet.data.Reference; |
---|
12 | import org.restlet.data.Status; |
---|
13 | import org.restlet.representation.Representation; |
---|
14 | import org.restlet.representation.StringRepresentation; |
---|
15 | import org.restlet.resource.Get; |
---|
16 | import org.restlet.resource.Post; |
---|
17 | import org.restlet.resource.ServerResource; |
---|
18 | |
---|
19 | import de.mpiwg.itgroup.annotations.Actor; |
---|
20 | import de.mpiwg.itgroup.annotations.Group; |
---|
21 | import de.mpiwg.itgroup.annotations.Person; |
---|
22 | import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
---|
23 | import de.mpiwg.itgroup.annotations.restlet.BaseRestlet; |
---|
24 | |
---|
25 | /** |
---|
26 | * Resource class for the members of an annotation group. |
---|
27 | * |
---|
28 | * @author casties |
---|
29 | * |
---|
30 | */ |
---|
31 | public class GroupMembersResource extends ServerResource { |
---|
32 | |
---|
33 | public static Logger logger = Logger.getLogger(GroupMembersResource.class); |
---|
34 | |
---|
35 | private AnnotationStore store; |
---|
36 | |
---|
37 | /** |
---|
38 | * GET with HTML content type. Shows the members of the group. |
---|
39 | * |
---|
40 | * @param entity |
---|
41 | * @return |
---|
42 | */ |
---|
43 | @Get("html") |
---|
44 | public Representation doGetHTML(Representation entity) { |
---|
45 | // id from URI /annotations/groups/{id}/members |
---|
46 | String id = (String) getRequest().getAttributes().get("id"); |
---|
47 | logger.debug("group-id=" + id); |
---|
48 | if (id == null || id.isEmpty()) { |
---|
49 | // invalid id |
---|
50 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
51 | return null; |
---|
52 | } |
---|
53 | String result = null; |
---|
54 | store = getAnnotationStore(); |
---|
55 | Reference groupsUrl = this.getReference().getParentRef(); |
---|
56 | result = "<html><body>\n<h1>Members of group</h1>\n"; |
---|
57 | Group group = new Group(id); |
---|
58 | group = (Group) store.getActor(group); |
---|
59 | result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId()); |
---|
60 | result += "<p>Members:</p>\n"; |
---|
61 | result += "<table>"; |
---|
62 | List<Person> members = store.getMembersOfGroup(group); |
---|
63 | for (Person p : members) { |
---|
64 | result += String.format("<tr><td>%s</td><td>(%s)</td></tr>\n", p.getName(), p.getIdString()); |
---|
65 | } |
---|
66 | result += "</table>\n"; |
---|
67 | result += "<form method=\"post\" action=\"\">\n"; |
---|
68 | result += "<p>Add new member: <select name=\"new_member\">\n"; |
---|
69 | result += String.format("<option value=\"%s\">%s</option>\n", "casties", "casties"); |
---|
70 | result += "</select>\n"; |
---|
71 | result += "<input type=\"submit\"/>\n"; |
---|
72 | result += "</form>\n"; |
---|
73 | result += "</body>\n</html>"; |
---|
74 | |
---|
75 | return new StringRepresentation(result, MediaType.TEXT_HTML); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * POST creates a new Group. |
---|
80 | * |
---|
81 | * @return |
---|
82 | */ |
---|
83 | @Post |
---|
84 | public Representation doPost(Representation entity) { |
---|
85 | logger.debug("AnnotationsUiGroup doPost!"); |
---|
86 | // TODO: do authentication |
---|
87 | Form form = new Form(entity); |
---|
88 | String id = form.getFirstValue("id"); |
---|
89 | String name = form.getFirstValue("name"); |
---|
90 | if (id == null || id.isEmpty()) { |
---|
91 | // invalid id |
---|
92 | setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
---|
93 | return null; |
---|
94 | } |
---|
95 | String gid = null; |
---|
96 | Group newGroup = new Group(gid, null, name); |
---|
97 | store = getAnnotationStore(); |
---|
98 | Actor storedGroup = store.storeActor(newGroup); |
---|
99 | gid = storedGroup.getId(); |
---|
100 | // return 303: see other |
---|
101 | setStatus(Status.REDIRECTION_SEE_OTHER); |
---|
102 | // go GET URL for this group |
---|
103 | Reference groupUrl = this.getReference(); |
---|
104 | groupUrl.addSegment(gid); |
---|
105 | this.getResponse().setLocationRef(groupUrl); |
---|
106 | return null; |
---|
107 | } |
---|
108 | |
---|
109 | protected AnnotationStore getAnnotationStore() { |
---|
110 | if (store == null) { |
---|
111 | store = ((BaseRestlet) getApplication()).getAnnotationStore(); |
---|
112 | } |
---|
113 | return store; |
---|
114 | } |
---|
115 | } |
---|