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

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

reorganised code for annotations and groups ui. work in progress.

File size: 3.8 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5
6import java.util.List;
7
8import org.apache.log4j.Logger;
9import org.restlet.data.Form;
10import org.restlet.data.MediaType;
11import org.restlet.data.Reference;
12import org.restlet.data.Status;
13import org.restlet.representation.Representation;
14import org.restlet.representation.StringRepresentation;
15import org.restlet.resource.Get;
16import org.restlet.resource.Post;
17import org.restlet.resource.ServerResource;
18
19import de.mpiwg.itgroup.annotations.Actor;
20import de.mpiwg.itgroup.annotations.Group;
21import de.mpiwg.itgroup.annotations.Person;
22import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
23import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
24
25/**
26 * Resource class for the members of an annotation group.
27 *
28 * @author casties
29 *
30 */
31public 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}
Note: See TracBrowser for help on using the repository browser.