source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java @ 58:f5c0e6df7e88

Last change on this file since 58:f5c0e6df7e88 was 58:f5c0e6df7e88, checked in by casties, 11 years ago

made uri prefixes in store configurable.

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