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

annotations ui can show and delete annotations now.
author casties
date Thu, 27 Sep 2012 17:12:08 +0200
parents f4ed2ed33e5b
children f5c0e6df7e88
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.Status;
import org.restlet.ext.json.JsonRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Get;

import de.mpiwg.itgroup.annotations.NS;
import de.mpiwg.itgroup.annotations.Tag;
import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;


/**
 * API for accessing tags in the Annotation store.
 * 
 * @author dwinter
 *
 */
public class AnnotatorTags 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();
       
        String jsonId = (String) getRequest().getAttributes().get("id");
        //String id = decodeJsonId(jsonId);
        String id = jsonId;
        logger.debug("annotation-id=" + id);

        if (id==null){
        return getAllTags();
        } else {

        	return getTag(id);
        }
    }
    
    protected Representation getTag(String id){
    	  AnnotationStore store = getAnnotationStore();
    	  String tagUri=NS.MPIWG_TAGS_URL+id;
          Node tagNode = store.getTagNodeByUri(tagUri);
          Tag tag = store.createTagFromNode(tagNode);
          JSONObject jo = new JSONObject();
          try {
              jo.put("id", tag.getId());
              jo.put("name", tag.getName());
              jo.put("uri", tag.getUri());
          } catch (JSONException e) {
          }
          
          return new JsonRepresentation(jo);
    }
	protected Representation getAllTags() {
		JSONArray results = new JSONArray();
        AnnotationStore store = getAnnotationStore();
        
       
            List<Tag> tags = store.getTags(null, null);
            for (Tag tag : tags) {
                JSONObject jo = new JSONObject();
                try {
                    jo.put("id", tag.getId());
                    jo.put("name", tag.getName());
                    jo.put("uri", tag.getUri());
                } 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);
	}        	
        

    
}