source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java @ 31:9f653697437e

Last change on this file since 31:9f653697437e was 31:9f653697437e, checked in by dwinter, 12 years ago

annotationbrowser

File size: 2.6 KB
Line 
1package de.mpiwg.itgroup.annotations.restlet;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import org.json.JSONArray;
7import org.json.JSONException;
8import org.json.JSONObject;
9import org.neo4j.graphdb.Node;
10import org.restlet.data.Form;
11import org.restlet.data.Parameter;
12import org.restlet.data.Status;
13import org.restlet.ext.json.JsonRepresentation;
14import org.restlet.representation.Representation;
15import org.restlet.resource.Get;
16import org.restlet.resource.ServerResource;
17
18import de.mpiwg.itgroup.annotations.Annotation;
19import de.mpiwg.itgroup.annotations.NS;
20import de.mpiwg.itgroup.annotations.Tag;
21import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
22import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
23
24/**
25 * API for accessing tags in the Annotation store.
26 *
27 * @author dwinter
28 *
29 */
30public class AnnotatorAnnotationsByTags extends AnnotatorResourceImpl {
31    protected String getAllowedMethodsForHeader() {
32        return "OPTIONS,GET";
33    }
34   
35    @Get("json")
36    public Representation doGetJSON(Representation entity) {
37        logger.debug("AnnotatorAnnotatonsBytag doGetJSON!");
38        setCorsHeaders();
39       
40        String jsonId = (String) getRequest().getAttributes().get("id");
41       
42        Form form = getRequest().getResourceRef().getQueryAsForm();
43        String sortBy=null;
44        for (Parameter parameter : form) {
45          if (parameter.getName().equals("sortBy")){
46          sortBy =  parameter.getValue();
47          }
48        }
49       
50        //String id = decodeJsonId(jsonId);
51        String id = jsonId;
52        logger.debug("annotation-id=" + id);
53       
54        AnnotationStore store = getAnnotationStore();
55                String tagUri=NS.MPIWG_TAGS_URL+id;
56        List<Annotation> annotations = store.getAnnotationsByTag(tagUri);
57       
58                //JSONArray results = new JSONArray();
59        ArrayList<JSONObject> results = new ArrayList<JSONObject>();
60       
61                for (Annotation annot : annotations) {
62            JSONObject jo = createAnnotatorJson(annot,false);
63            results.add(jo);
64        }
65   
66        if (sortBy!=null){
67                JSONObjectComparator.sortAnnotations(results,sortBy);
68        }
69               
70        JSONArray resultsJa = new JSONArray();
71        for (JSONObject result:results){
72                resultsJa.put(result);
73        }
74       
75    // assemble result object
76    JSONObject result = new JSONObject();
77    try {
78        result.put("rows", resultsJa);
79        result.put("total", resultsJa.length());
80    } catch (JSONException e) {
81        setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
82        return null;
83    }
84    logger.debug("sending:");
85    logger.debug(result);
86    return new JsonRepresentation(result);
87}
88
89   
90       
91}
Note: See TracBrowser for help on using the repository browser.