source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorTags.java @ 28:f4ed2ed33e5b

Last change on this file since 28:f4ed2ed33e5b was 28:f4ed2ed33e5b, checked in by dwinter, 12 years ago

Restinterface zur Anzeige von Tags hinzugefuegt-

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