view src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java @ 88:b406507a953d

upped version to 0.5. can use display name and groups from auth token.
author casties
date Tue, 03 Feb 2015 19:01:27 +0100
parents 25eb2e1df106
children cf44d9e1a4a7
line wrap: on
line source

package de.mpiwg.itgroup.annotations.restlet;

/*
 * #%L
 * AnnotationManager
 * %%
 * Copyright (C) 2012 - 2014 MPIWG Berlin
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.neo4j.graphdb.Node;
import org.restlet.data.Form;
import org.restlet.data.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;

import de.mpiwg.itgroup.annotations.Actor;
import de.mpiwg.itgroup.annotations.Group;
import de.mpiwg.itgroup.annotations.Person;
import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;


/**
 * API for accessing groups in the Annotation store.
 * 
 * @author casties
 *
 */
public class AnnotatorGroups extends AnnotatorResourceImpl {
    protected String getAllowedMethodsForHeader() {
        return "OPTIONS,GET";
    }

    /**
     * GET with JSON content-type.
     * Parameters: 
     *   user: short user name
     *   uri: user uri
     *   
     * @param entity
     * @return
     */
    @Get("json")
    public Representation doGetJSON(Representation entity) {
        logger.fine("AnnotatorGroups doGetJSON!");
        setCorsHeaders();
        // get user from auth token (preferred)
        Person authUser = getUserFromAuthToken(entity);
        JSONArray results = null;
        if (authUser != null && authUser.groups != null) {
            results  = getGroupsFromPerson(authUser);
        } else {
            // get user or uri from request
            Form form = getRequest().getResourceRef().getQueryAsForm();
            String user = form.getFirstValue("user");
            String uri = form.getFirstValue("uri");
            results = getGroupsFromStore(uri, user);
        }
        // assemble result object
        JSONObject result = new JSONObject();
        try {
            result.put("rows", results);
            result.put("total", results.length());
        } catch (JSONException e) {
            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
            return null;
        }
        logger.fine("sending:");
        logger.fine(result.toString());
        return new JsonRepresentation(result);
    }

    public JSONArray getGroupsFromPerson(Person person) {
        JSONArray results = new JSONArray();
        for (String group : person.groups) {
            JSONObject jo = new JSONObject();
            try {
                jo.put("id", group);
                jo.put("name", group);
            } catch (JSONException e) {
            }
            results.put(jo);
        }
        return results;
    }

    public JSONArray getGroupsFromStore(String uri, String user) {
        JSONArray results = new JSONArray();
        if (uri == null || uri.isEmpty()) {
            // get uri from user-id
            uri = Actor.getUriFromId(user, false);
        }
        AnnotationStore store = getAnnotationStore();
        Node person = store.getPersonNodeByUri(uri);
        if (person != null) {
            List<Group> groups = store.getGroupsForPersonNode(person);
            for (Group group : groups) {
                JSONObject jo = new JSONObject();
                try {
                    jo.put("id", group.getId());
                    jo.put("name", group.getName());
                    jo.put("uri", group.getUriString());
                } catch (JSONException e) {
                }
                results.put(jo);
            }
        }
        return results;
    }
}