source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotationsByTags.java @ 50:64aa756c60cc

Last change on this file since 50:64aa756c60cc was 50:64aa756c60cc, checked in by casties, 12 years ago

annotations ui can show and delete annotations now.

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