source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.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.4 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.neo4j.AnnotationStore;
22import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
23
24/**
25 * Resource class for the list of annotation groups.
26 *
27 * @author casties
28 *
29 */
30public class GroupsResource extends ServerResource {
31
32    public static Logger logger = Logger.getLogger(GroupsResource.class);
33
34    private AnnotationStore store;
35
36    /**
37     * GET with HTML content type. Lists all groups.
38     *
39     * @param entity
40     * @return
41     */
42    @Get("html")
43    public Representation doGetHTML(Representation entity) {
44        String result = null;
45        store = getAnnotationStore();
46        // list all groups
47        result = "<html><body>\n<h1>Groups</h1>\n<table>";
48        result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
49        List<Group> groups = store.getGroups("uri", "*");
50        for (Group group : groups) {
51            Reference groupUrl = this.getReference();
52            groupUrl.addSegment(group.getId());
53            result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(),
54                    group.getName(), group.getUri());
55        }
56        result += "</table>\n</body>\n</html>";
57        logger.debug("sending:");
58        logger.debug(result);
59        return new StringRepresentation(result, MediaType.TEXT_HTML);
60    }
61
62    /**
63     * POST creates a new Group.
64     *
65     * @return
66     */
67    @Post
68    public Representation doPost(Representation entity) {
69        logger.debug("AnnotationsUiGroups doPost!");
70        // TODO: do authentication
71        Form form = new Form(entity);
72        String id = form.getFirstValue("id");
73        String name = form.getFirstValue("name");
74        if (id == null || id.isEmpty()) {
75            // invalid id
76            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
77            return null;
78        }
79        String gid = makeGroupId(id);
80        Group newGroup = new Group(gid, null, name);
81        store = getAnnotationStore();
82        Actor storedGroup = store.storeActor(newGroup);
83        gid = storedGroup.getId();
84        // return 303: see other
85        setStatus(Status.REDIRECTION_SEE_OTHER);
86        // go GET URL for this group
87        Reference groupUrl = this.getReference();
88        groupUrl.addSegment(gid);
89        this.getResponse().setLocationRef(groupUrl);
90        return null;
91    }
92
93    /**
94     * Returns a group id based on the given id.
95     *
96     * @param id
97     * @return
98     */
99    protected String makeGroupId(String id) {
100        // TODO: should we use different ids?
101        id = id.replaceAll("\\W", "_");
102        id = id.toLowerCase();
103        return id;
104    }
105
106    protected AnnotationStore getAnnotationStore() {
107        if (store == null) {
108            store = ((BaseRestlet) getApplication()).getAnnotationStore();
109        }
110        return store;
111    }
112}
Note: See TracBrowser for help on using the repository browser.