view src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorGroups.java @ 50:64aa756c60cc

annotations ui can show and delete annotations now.
author casties
date Thu, 27 Sep 2012 17:12:08 +0200
parents 58357a4b86de
children 2b1e6df5e21a
line wrap: on
line source

/**
 * ReST API for accessing groups in the Annotation store.
 */
package de.mpiwg.itgroup.annotations.restlet;

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.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.debug("AnnotatorGroups doGetJSON!");
        setCorsHeaders();
        Form form = getRequest().getResourceRef().getQueryAsForm();
        String user = form.getFirstValue("user");
        String uri = form.getFirstValue("uri");
        if (uri == null || uri.isEmpty()) {
            // get uri from user-id
            uri = Actor.getUriFromId(user, false);
        }
        JSONArray results = new JSONArray();
        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);
            }
        }
        // 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.debug("sending:");
        logger.debug(result);
        return new JsonRepresentation(result);

    }
}