source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java @ 32:0731c4549065

Last change on this file since 32:0731c4549065 was 32:0731c4549065, checked in by casties, 12 years ago

UI for editing groups and persons works now. (still no authorisation!)

File size: 4.6 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.ResourceException;
18import org.restlet.resource.ServerResource;
19
20import de.mpiwg.itgroup.annotations.Actor;
21import de.mpiwg.itgroup.annotations.Group;
22import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
23import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
24
25/**
26 * Resource class for the list of annotation groups.
27 *
28 * @author casties
29 *
30 */
31public class GroupsResource extends ServerResource {
32
33    public static Logger logger = Logger.getLogger(GroupsResource.class);
34
35    private AnnotationStore store;
36
37    @Override
38    protected void doInit() throws ResourceException {
39        super.doInit();
40        // get store instance
41        if (store == null) {
42            store = ((BaseRestlet) getApplication()).getAnnotationStore();
43        }
44    }
45
46    /**
47     * GET with HTML content type. Lists all groups.
48     *
49     * @param entity
50     * @return
51     */
52    @Get("html")
53    public Representation doGetHTML(Representation entity) {
54        String result = null;
55        // get form parameter
56        Form f = this.getQuery();
57        String form = f.getFirstValue("form");
58        if (form != null && form.equals("new_group")) {
59            // output new group form
60            result = "<html><body>\n";
61            result += "<h1>New group</h1>\n";
62            result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference());
63            result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
64            result += "<table>";
65            result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
66            result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
67            result += "</table>\n";
68            result += "<p><input type=\"submit\"/></p>\n";
69            result += "</form>\n</body>\n</html>";
70        } else {
71            // list all groups
72            result = "<html><body>\n<h1>Groups</h1>\n";
73            result += "<table>\n";
74            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
75            List<Group> groups = store.getGroups("uri", "*");
76            for (Group group : groups) {
77                Reference groupUrl = this.getReference().clone();
78                groupUrl.addSegment(group.getId());
79                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(),
80                        group.getName(), group.getUri());
81            }
82            result += "</table>\n";
83            result += "<p><a href=\"?form=new_group\">Add new group</a></p>\n";
84            result += "</body>\n</html>";
85        }
86        return new StringRepresentation(result, MediaType.TEXT_HTML);
87    }
88
89    /**
90     * POST creates a new Group.
91     *
92     * @return
93     */
94    @Post
95    public Representation doPost(Representation entity) {
96        logger.debug("AnnotationsUiGroups doPost!");
97        // TODO: do authentication
98        Form form = new Form(entity);
99        String id = form.getFirstValue("id");
100        String name = form.getFirstValue("name");
101        if (id == null || id.isEmpty()) {
102            // invalid id
103            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
104            return null;
105        }
106        String gid = makeGroupId(id);
107        Group newGroup = new Group(gid, null, name);
108        Actor storedGroup = store.storeActor(newGroup);
109        gid = storedGroup.getId();
110        // return 303: see other
111        setStatus(Status.REDIRECTION_SEE_OTHER);
112        // go GET URL for this group
113        Reference groupUrl = this.getReference();
114        groupUrl.addSegment(gid);
115        this.getResponse().setLocationRef(groupUrl);
116        return null;
117    }
118
119    /**
120     * Returns a group id based on the given id.
121     *
122     * @param id
123     * @return
124     */
125    protected String makeGroupId(String id) {
126        // TODO: should we use different ids?
127        id = id.replaceAll("\\W", "_");
128        id = id.toLowerCase();
129        return id;
130    }
131
132    protected AnnotationStore getAnnotationStore() {
133        if (store == null) {
134            store = ((BaseRestlet) getApplication()).getAnnotationStore();
135        }
136        return store;
137    }
138}
Note: See TracBrowser for help on using the repository browser.