source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupResource.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: 2.7 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5
6import org.apache.log4j.Logger;
7import org.restlet.data.MediaType;
8import org.restlet.data.Reference;
9import org.restlet.data.Status;
10import org.restlet.representation.Representation;
11import org.restlet.representation.StringRepresentation;
12import org.restlet.resource.Get;
13import org.restlet.resource.ResourceException;
14import org.restlet.resource.ServerResource;
15
16import de.mpiwg.itgroup.annotations.Group;
17import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
18import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
19
20/**
21 * Resource class for a single group.
22 *
23 * @author casties
24 *
25 */
26public class GroupResource extends ServerResource {
27
28    public static Logger logger = Logger.getLogger(GroupResource.class);
29
30    protected AnnotationStore store;
31   
32    protected String requestId;
33   
34    protected Group group;
35   
36    @Override
37    protected void doInit() throws ResourceException {
38        super.doInit();
39        // id from URI /annotations/groups/{id}
40        requestId = (String) getRequest().getAttributes().get("id");
41        logger.debug("group-id=" + requestId);
42        // get store instance
43        if (store == null) {
44            store = ((BaseRestlet) getApplication()).getAnnotationStore();
45        }
46        // get group from store
47        if (requestId != null) {
48            group = (Group) store.getActor(new Group(requestId));
49        }
50    }
51
52    /**
53     * GET with HTML content type. Shows the group.
54     *
55     * @param entity
56     * @return
57     */
58    @Get("html")
59    public Representation doGetHTML(Representation entity) {
60        if (requestId == null || requestId.isEmpty()) {
61            // invalid id
62            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
63            return null;
64        }
65        String result = null;
66        Reference groupsUrl = this.getReference().getParentRef();
67        result = "<html><body>\n<h1>Group</h1>\n";
68        result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
69        result += "<table>";
70        result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
71        result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
72        result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
73        result += String.format("<tr><td><b>members</b></td><td><a href=\"%s\">view members</a></td></tr>\n", this.getReference()
74                .addSegment("members"));
75        result += "</table>\n</body>\n</html>";
76
77        logger.debug("sending:");
78        logger.debug(result);
79        return new StringRepresentation(result, MediaType.TEXT_HTML);
80    }
81
82}
Note: See TracBrowser for help on using the repository browser.