source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotationStoreGroups.java @ 22:b1fb0d117877

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

adding and listing groups via html works now.
no editing of group membership yet.
no authentication yet.

File size: 4.6 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet;
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;
23
24/**
25 * @author casties
26 *
27 */
28public class AnnotationStoreGroups extends ServerResource {
29
30    public static Logger logger = Logger.getLogger(AnnotationStoreGroups.class);
31
32    private AnnotationStore store;
33
34    /**
35     * GET with HTML content type. Lists all groups.
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        String result = null;
46        store = getAnnotationStore();
47        if (id == null) {
48            // list all groups
49            result = "<html><body>\n<h1>Groups</h1>\n<table>";
50            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
51            List<Group> groups = store.getGroups("uri", "*");
52            for (Group group : groups) {
53                Reference groupUrl = this.getReference();
54                groupUrl.addSegment(group.getId());
55                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl,
56                        group.getId(), group.getName(), group.getUri());
57            }
58            result += "</table>\n</body>\n</html>";
59        } else {
60            // just one group
61            Reference groupsUrl = this.getReference().getParentRef();
62            result = "<html><body>\n<h1>Group</h1>\n";
63            result += String.format("<p><a href=\"%s\">All groups</a></p>", groupsUrl);
64            Group group = new Group(id);
65            group = (Group) store.getActor(group);
66            result += "<table>";
67            result += String.format("<tr><td><b>id</b></td><td>%s</td></tr>\n", group.getId());
68            result += String.format("<tr><td><b>name</b></td><td>%s</td></tr>\n", group.getName());
69            result += String.format("<tr><td><b>uri</b></td><td>%s</td></tr>\n", group.getUri());
70            result += "<tr><td><b>members</b></td><td>";
71            List<Person> members = store.getMembersOfGroup(group);
72            for (Person p : members) {
73                result += String.format("%s (%s)\n", p.getName(), p.getIdString());               
74            }
75            result += "</td></tr>\n";
76            result += "</table>\n</body>\n</html>";
77        }
78
79        logger.debug("sending:");
80        logger.debug(result);
81        return new StringRepresentation(result, MediaType.TEXT_HTML);
82    }
83
84    /**
85     * POST with HTML content-type. Creates a new Group.
86     *
87     * @return
88     */
89    @Post
90    public Representation doPostHTML(Representation entity) {
91        logger.debug("AnnotationStoreGroups doPostHTML!");
92        // TODO: do authentication
93        Form form = new Form(entity);
94        String id = form.getFirstValue("id");
95        String name = form.getFirstValue("name");
96        if (id == null || id.isEmpty()) {
97            // invalid id
98            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
99            return null;
100        }
101        String gid = makeGroupId(id);
102        Group newGroup = new Group(gid, null, name);
103        store = getAnnotationStore();
104        Actor storedGroup = store.storeActor(newGroup);
105        gid = storedGroup.getId();
106        // return 303: see other
107        setStatus(Status.REDIRECTION_SEE_OTHER);
108        // go GET URL for this group
109        Reference groupUrl = this.getReference();
110        groupUrl.addSegment(gid);
111        this.getResponse().setLocationRef(groupUrl);
112        return null;
113    }
114
115    /**
116     * Returns a group id based on the given id.
117     *
118     * @param id
119     * @return
120     */
121    protected String makeGroupId(String id) {
122        // TODO: should we use different ids?
123        id = id.replaceAll("\\W", "_");
124        id = id.toLowerCase();
125        return id;
126    }
127
128    protected AnnotationStore getAnnotationStore() {
129        if (store == null) {
130            store = ((BaseRestlet) getApplication()).getAnnotationStore();
131        }
132        return store;
133    }
134}
Note: See TracBrowser for help on using the repository browser.