source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java @ 70:2b1e6df5e21a

Last change on this file since 70:2b1e6df5e21a was 70:2b1e6df5e21a, checked in by casties, 10 years ago

added lgpl_v3 license information.

File size: 4.6 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;
29
30import org.restlet.data.Form;
31import org.restlet.data.MediaType;
32import org.restlet.data.Reference;
33import org.restlet.data.Status;
34import org.restlet.representation.Representation;
35import org.restlet.representation.StringRepresentation;
36import org.restlet.resource.Get;
37import org.restlet.resource.Post;
38
39import de.mpiwg.itgroup.annotations.Person;
40
41/**
42 * Resource class for the members of an annotation group.
43 *
44 * @author casties
45 *
46 */
47public class GroupMembersResource extends GroupResource {
48
49    /**
50     * GET with HTML content type. Shows the members of the group.
51     *
52     * @param entity
53     * @return
54     */
55    @Get("html")
56    public Representation doGetHTML(Representation entity) {
57        // id from URI /annotations/groups/{id}/members
58        if (requestId == null || requestId.isEmpty()) {
59            // invalid id
60            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
61            return null;
62        }
63        String result = null;
64        Reference thisUrl = this.getReference();
65        Reference groupsUrl = thisUrl.getParentRef();
66        result = "<html><body>\n<h1>Group members</h1>\n";
67        result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId());
68        result += "<p>Members:</p>\n";
69        result += String.format("<form method=\"post\" action=\"%s\">", thisUrl);
70        result += "<table>";
71        List<Person> members = store.getMembersOfGroup(group);
72        for (Person p : members) {
73            result += String.format("<tr><td>%s</td><td>(%s)</td>", p.getName(), p.getIdString());
74            result += String.format("<td><input type=\"submit\" name=\"del_member\" value=\"delete:%s\"></td></tr>\n", p.getIdString());
75        }
76        result += "</table>\n";
77        result += "</form>\n";
78        result += String.format("<form method=\"post\" action=\"%s\">\n", thisUrl);
79        result += "<p>Add new member: <select name=\"add_member\">\n";
80        for (Person p : store.getPersons("uri", "*")) {
81            result += String.format("<option value=\"%s\">%s</option>\n", p.getIdString(), p.getName());
82        }
83        result += "</select>\n";
84        result += "<input type=\"submit\"/>\n";
85        result += "</form>\n";
86        result += "</body>\n</html>";
87
88        return new StringRepresentation(result, MediaType.TEXT_HTML);
89    }
90
91    /**
92     * POST adds or deletes members of the Group.
93     *
94     * @return
95     */
96    @Post
97    public Representation doPost(Representation entity) {
98        logger.debug("GroupMembersResource doPost!");
99        // TODO: do authentication
100        Form form = new Form(entity);
101        String addMemberId = form.getFirstValue("add_member");
102        String delMemberId = form.getFirstValue("del_member");
103        if (group == null || ((addMemberId == null || addMemberId.isEmpty()) 
104                && (delMemberId == null || delMemberId.isEmpty()))) {
105            // no id
106            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
107            return null;
108        }
109        if (addMemberId != null) {
110            logger.debug("adding member: "+addMemberId);
111            Person member = new Person(addMemberId);
112            store.addGroupMember(group, member);
113        } else if (delMemberId != null) {
114            if (delMemberId.startsWith("delete:")) {
115                delMemberId = delMemberId.substring(7);
116            }
117            logger.debug("deleting member: "+delMemberId);
118            Person member = new Person(delMemberId);
119            store.deleteGroupMember(group, member);
120        }
121        // return 303: see other
122        setStatus(Status.REDIRECTION_SEE_OTHER);
123        // go get same URL
124        Reference thisUrl = this.getReference();
125        this.getResponse().setLocationRef(thisUrl);
126        return null;
127    }
128
129}
Note: See TracBrowser for help on using the repository browser.