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