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