source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupMembersResource.java @ 24:e208a7b1a37a

Last change on this file since 24:e208a7b1a37a was 24:e208a7b1a37a, checked in by casties, 12 years ago

more work on groups ui.

File size: 3.3 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5
6import java.util.List;
7
8import org.restlet.data.Form;
9import org.restlet.data.MediaType;
10import org.restlet.data.Reference;
11import org.restlet.data.Status;
12import org.restlet.representation.Representation;
13import org.restlet.representation.StringRepresentation;
14import org.restlet.resource.Get;
15import org.restlet.resource.Post;
16
17import de.mpiwg.itgroup.annotations.Actor;
18import de.mpiwg.itgroup.annotations.Person;
19import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore.NodeTypes;
20
21/**
22 * Resource class for the members of an annotation group.
23 *
24 * @author casties
25 *
26 */
27public class GroupMembersResource extends GroupResource {
28
29    /**
30     * GET with HTML content type. Shows the members of the group.
31     *
32     * @param entity
33     * @return
34     */
35    @Get("html")
36    public Representation doGetHTML(Representation entity) {
37        // id from URI /annotations/groups/{id}/members
38        if (requestId == null || requestId.isEmpty()) {
39            // invalid id
40            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
41            return null;
42        }
43        String result = null;
44        Reference thisUrl = this.getReference();
45        Reference groupsUrl = thisUrl.getParentRef();
46        result = "<html><body>\n<h1>Group members</h1>\n";
47        result += String.format("<p>Group: %s <a href=\"%s\">(%s)</a></p>\n", group.getName(), groupsUrl, group.getId());
48        result += "<p>Members:</p>\n";
49        result += "<table>";
50        List<Person> members = store.getMembersOfGroup(group);
51        for (Person p : members) {
52            result += String.format("<tr><td>%s</td><td>(%s)</td>", p.getName(), p.getIdString());
53            //result += String.format("<td></td></tr>\n", p.getName(), p.getIdString());
54        }
55        result += "</table>\n";
56        result += String.format("<form method=\"post\" action=\"%s\">\n", thisUrl);
57        result += "<p>Add new member: <select name=\"add_member\">\n";
58        for (Actor p : store.getActors("uri", "*", NodeTypes.PERSON)) {
59            result += String.format("<option value=\"%s\">%s</option>\n", p.getIdString(), p.getName());
60        }
61        result += "</select>\n";
62        result += "<input type=\"submit\"/>\n";
63        result += "</form>\n";
64        result += "</body>\n</html>";
65
66        return new StringRepresentation(result, MediaType.TEXT_HTML);
67    }
68
69    /**
70     * POST adds or deletes members of the Group.
71     *
72     * @return
73     */
74    @Post
75    public Representation doPost(Representation entity) {
76        logger.debug("GroupMembersResource doPost!");
77        // TODO: do authentication
78        Form form = new Form(entity);
79        String addMemberId = form.getFirstValue("add_member");
80        String delMemberId = form.getFirstValue("del_member");
81        if (group == null || ((addMemberId == null || addMemberId.isEmpty()) 
82                && (delMemberId == null || delMemberId.isEmpty()))) {
83            // no id
84            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
85            return null;
86        }
87       
88        // return 303: see other
89        setStatus(Status.REDIRECTION_SEE_OTHER);
90        // go get same URL
91        Reference thisUrl = this.getReference();
92        this.getResponse().setLocationRef(thisUrl);
93        return null;
94    }
95
96}
Note: See TracBrowser for help on using the repository browser.