source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.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.9 KB
Line 
1/**
2 * ReST API for accessing groups in the Annotation store.
3 */
4package de.mpiwg.itgroup.annotations.restlet;
5
6import java.util.List;
7
8import org.json.JSONArray;
9import org.json.JSONException;
10import org.json.JSONObject;
11import org.neo4j.graphdb.Node;
12import org.restlet.data.Status;
13import org.restlet.ext.json.JsonRepresentation;
14import org.restlet.representation.Representation;
15import org.restlet.resource.Get;
16
17import de.mpiwg.itgroup.annotations.Tag;
18import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
19
20
21/**
22 * API for accessing tags in the Annotation store.
23 *
24 * @author dwinter
25 *
26 */
27public class AnnotatorTags extends AnnotatorResourceImpl {
28    protected String getAllowedMethodsForHeader() {
29        return "OPTIONS,GET";
30    }
31
32    /**
33     * GET with JSON content-type.
34     * Parameters:
35     *   user: short user name
36     *   uri: user uri
37     *   
38     * @param entity
39     * @return
40     */
41    @Get("json")
42    public Representation doGetJSON(Representation entity) {
43        logger.debug("AnnotatorGroups doGetJSON!");
44        setCorsHeaders();
45       
46        String jsonId = (String) getRequest().getAttributes().get("id");
47        //String id = decodeJsonId(jsonId);
48        String id = jsonId;
49        logger.debug("annotation-id=" + id);
50
51        if (id==null){
52        return getAllTags();
53        } else {
54
55                return getTag(id);
56        }
57    }
58   
59    protected Representation getTag(String id){
60          AnnotationStore store = getAnnotationStore();
61          String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
62          Node tagNode = store.getTagNodeByUri(tagUri);
63          Tag tag = store.createTagFromNode(tagNode);
64          JSONObject jo = new JSONObject();
65          try {
66              jo.put("id", tag.getId());
67              jo.put("name", tag.getName());
68              jo.put("uri", tag.getUri());
69          } catch (JSONException e) {
70          }
71         
72          return new JsonRepresentation(jo);
73    }
74        protected Representation getAllTags() {
75                JSONArray results = new JSONArray();
76        AnnotationStore store = getAnnotationStore();
77       
78       
79            List<Tag> tags = store.getTags(null, null);
80            for (Tag tag : tags) {
81                JSONObject jo = new JSONObject();
82                try {
83                    jo.put("id", tag.getId());
84                    jo.put("name", tag.getName());
85                    jo.put("uri", tag.getUri());
86                } catch (JSONException e) {
87                }
88                results.put(jo);
89            }
90       
91        // assemble result object
92        JSONObject result = new JSONObject();
93        try {
94            result.put("rows", results);
95            result.put("total", results.length());
96        } catch (JSONException e) {
97            setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
98            return null;
99        }
100        logger.debug("sending:");
101        logger.debug(result);
102        return new JsonRepresentation(result);
103        }               
104       
105
106   
107}
Note: See TracBrowser for help on using the repository browser.