source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.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: 3.3 KB
Line 
1/**
2 * ReST API for accessing groups in the Annotation store.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
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.json.JSONArray;
31import org.json.JSONException;
32import org.json.JSONObject;
33import org.neo4j.graphdb.Node;
34import org.restlet.data.Form;
35import org.restlet.data.Status;
36import org.restlet.ext.json.JsonRepresentation;
37import org.restlet.representation.Representation;
38import org.restlet.resource.Get;
39
40import de.mpiwg.itgroup.annotations.Actor;
41import de.mpiwg.itgroup.annotations.Group;
42import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
43
44
45/**
46 * API for accessing groups in the Annotation store.
47 *
48 * @author casties
49 *
50 */
51public class AnnotatorGroups extends AnnotatorResourceImpl {
52    protected String getAllowedMethodsForHeader() {
53        return "OPTIONS,GET";
54    }
55
56    /**
57     * GET with JSON content-type.
58     * Parameters:
59     *   user: short user name
60     *   uri: user uri
61     *   
62     * @param entity
63     * @return
64     */
65    @Get("json")
66    public Representation doGetJSON(Representation entity) {
67        logger.debug("AnnotatorGroups doGetJSON!");
68        setCorsHeaders();
69        Form form = getRequest().getResourceRef().getQueryAsForm();
70        String user = form.getFirstValue("user");
71        String uri = form.getFirstValue("uri");
72        if (uri == null || uri.isEmpty()) {
73            // get uri from user-id
74            uri = Actor.getUriFromId(user, false);
75        }
76        JSONArray results = new JSONArray();
77        AnnotationStore store = getAnnotationStore();
78        Node person = store.getPersonNodeByUri(uri);
79        if (person != null) {
80            List<Group> groups = store.getGroupsForPersonNode(person);
81            for (Group group : groups) {
82                JSONObject jo = new JSONObject();
83                try {
84                    jo.put("id", group.getId());
85                    jo.put("name", group.getName());
86                    jo.put("uri", group.getUriString());
87                } catch (JSONException e) {
88                }
89                results.put(jo);
90            }
91        }
92        // assemble result object
93        JSONObject result = new JSONObject();
94        try {
95            result.put("rows", results);
96            result.put("total", results.length());
97        } catch (JSONException e) {
98            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
99            return null;
100        }
101        logger.debug("sending:");
102        logger.debug(result);
103        return new JsonRepresentation(result);
104
105    }
106}
Note: See TracBrowser for help on using the repository browser.