source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/annotations_ui/GroupsResource.java @ 70:2b1e6df5e21a

Last change on this file since 70:2b1e6df5e21a was 70:2b1e6df5e21a, checked in by casties, 10 years ago

added lgpl_v3 license information.

File size: 5.4 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations.restlet.annotations_ui;
5
6/*
7 * #%L
8 * AnnotationManager
9 * %%
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
11 * %%
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Lesser Public License for more details.
21 *
22 * You should have received a copy of the GNU General Lesser Public
23 * License along with this program.  If not, see
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
25 * #L%
26 */
27
28import java.util.List;
29
30import org.apache.log4j.Logger;
31import org.restlet.data.Form;
32import org.restlet.data.MediaType;
33import org.restlet.data.Reference;
34import org.restlet.data.Status;
35import org.restlet.representation.Representation;
36import org.restlet.representation.StringRepresentation;
37import org.restlet.resource.Get;
38import org.restlet.resource.Post;
39import org.restlet.resource.ResourceException;
40import org.restlet.resource.ServerResource;
41
42import de.mpiwg.itgroup.annotations.Actor;
43import de.mpiwg.itgroup.annotations.Group;
44import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
45import de.mpiwg.itgroup.annotations.restlet.BaseRestlet;
46
47/**
48 * Resource class for the list of annotation groups.
49 *
50 * @author casties
51 *
52 */
53public class GroupsResource extends ServerResource {
54
55    public static Logger logger = Logger.getLogger(GroupsResource.class);
56
57    private AnnotationStore store;
58
59    @Override
60    protected void doInit() throws ResourceException {
61        super.doInit();
62        // get store instance
63        if (store == null) {
64            store = ((BaseRestlet) getApplication()).getAnnotationStore();
65        }
66    }
67
68    /**
69     * GET with HTML content type. Lists all groups.
70     *
71     * @param entity
72     * @return
73     */
74    @Get("html")
75    public Representation doGetHTML(Representation entity) {
76        String result = null;
77        // get form parameter
78        Form f = this.getQuery();
79        String form = f.getFirstValue("form");
80        if (form != null && form.equals("new_group")) {
81            // output new group form
82            result = "<html><body>\n";
83            result += "<h1>New group</h1>\n";
84            result += String.format("<p><a href=\"%s\">All groups</a></p>", this.getReference());
85            result += String.format("<form method=\"post\" action=\"%s\">\n", this.getReference().getHierarchicalPart());
86            result += "<table>";
87            result += "<tr><td><b>id</b></td><td><input type=\"text\" name=\"id\"/></td></tr>\n";
88            result += "<tr><td><b>name</b></td><td><input type=\"text\" name=\"name\"/></td></tr>\n";
89            result += "</table>\n";
90            result += "<p><input type=\"submit\"/></p>\n";
91            result += "</form>\n</body>\n</html>";
92        } else {
93            // list all groups
94            result = "<html><body>\n<h1>Groups</h1>\n";
95            result += "<table>\n";
96            result += "<tr><th>id</th><th>name</th><th>uri</th></tr>";
97            List<Group> groups = store.getGroups("uri", "*");
98            for (Group group : groups) {
99                Reference groupUrl = this.getReference().clone();
100                groupUrl.addSegment(group.getId());
101                result += String.format("<tr><td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td></tr>\n", groupUrl, group.getId(),
102                        group.getName(), group.getUri());
103            }
104            result += "</table>\n";
105            result += "<p><a href=\"?form=new_group\">Add new group</a></p>\n";
106            result += "</body>\n</html>";
107        }
108        return new StringRepresentation(result, MediaType.TEXT_HTML);
109    }
110
111    /**
112     * POST creates a new Group.
113     *
114     * @return
115     */
116    @Post
117    public Representation doPost(Representation entity) {
118        logger.debug("AnnotationsUiGroups doPost!");
119        // TODO: do authentication
120        Form form = new Form(entity);
121        String id = form.getFirstValue("id");
122        String name = form.getFirstValue("name");
123        if (id == null || id.isEmpty()) {
124            // invalid id
125            setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
126            return null;
127        }
128        String gid = makeGroupId(id);
129        Group newGroup = new Group(gid, null, name);
130        Actor storedGroup = store.storeActor(newGroup);
131        gid = storedGroup.getId();
132        // return 303: see other
133        setStatus(Status.REDIRECTION_SEE_OTHER);
134        // go GET URL for this group
135        Reference groupUrl = this.getReference();
136        groupUrl.addSegment(gid);
137        this.getResponse().setLocationRef(groupUrl);
138        return null;
139    }
140
141    /**
142     * Returns a group id based on the given id.
143     *
144     * @param id
145     * @return
146     */
147    protected String makeGroupId(String id) {
148        // TODO: should we use different ids?
149        id = id.replaceAll("\\W", "_");
150        id = id.toLowerCase();
151        return id;
152    }
153
154    protected AnnotationStore getAnnotationStore() {
155        if (store == null) {
156            store = ((BaseRestlet) getApplication()).getAnnotationStore();
157        }
158        return store;
159    }
160}
Note: See TracBrowser for help on using the repository browser.