28
|
1 package de.mpiwg.itgroup.annotations.restlet;
|
|
2
|
70
|
3 /*
|
|
4 * #%L
|
|
5 * AnnotationManager
|
|
6 * %%
|
|
7 * Copyright (C) 2012 - 2014 MPIWG Berlin
|
|
8 * %%
|
|
9 * This program is free software: you can redistribute it and/or modify
|
|
10 * it under the terms of the GNU Lesser General Public License as
|
|
11 * published by the Free Software Foundation, either version 3 of the
|
|
12 * License, or (at your option) any later version.
|
|
13 *
|
|
14 * This program is distributed in the hope that it will be useful,
|
|
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 * GNU General Lesser Public License for more details.
|
|
18 *
|
|
19 * You should have received a copy of the GNU General Lesser Public
|
|
20 * License along with this program. If not, see
|
|
21 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
22 * #L%
|
|
23 */
|
|
24
|
65
|
25 import java.io.UnsupportedEncodingException;
|
|
26 import java.net.URLDecoder;
|
28
|
27 import java.util.List;
|
|
28
|
|
29 import org.json.JSONArray;
|
|
30 import org.json.JSONException;
|
|
31 import org.json.JSONObject;
|
|
32 import org.neo4j.graphdb.Node;
|
|
33 import org.restlet.data.Status;
|
|
34 import org.restlet.ext.json.JsonRepresentation;
|
|
35 import org.restlet.representation.Representation;
|
|
36 import org.restlet.resource.Get;
|
|
37
|
|
38 import de.mpiwg.itgroup.annotations.Tag;
|
|
39 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
40
|
|
41 /**
|
|
42 * API for accessing tags in the Annotation store.
|
|
43 *
|
|
44 * @author dwinter
|
65
|
45 *
|
28
|
46 */
|
|
47 public class AnnotatorTags extends AnnotatorResourceImpl {
|
|
48
|
|
49 /**
|
65
|
50 * GET with JSON content-type.
|
28
|
51 * Parameters:
|
65
|
52 * user: short user name
|
28
|
53 * uri: user uri
|
65
|
54 *
|
28
|
55 * @param entity
|
|
56 * @return
|
|
57 */
|
|
58 @Get("json")
|
|
59 public Representation doGetJSON(Representation entity) {
|
75
|
60 logger.fine("AnnotatorGroups doGetJSON!");
|
65
|
61
|
28
|
62 String jsonId = (String) getRequest().getAttributes().get("id");
|
65
|
63 // String id = decodeJsonId(jsonId);
|
28
|
64 String id = jsonId;
|
75
|
65 logger.fine("annotation-id=" + id);
|
28
|
66
|
65
|
67 if (id == null) {
|
|
68 return getAllTags();
|
28
|
69 } else {
|
65
|
70 // URL decode
|
|
71 try {
|
|
72 id = URLDecoder.decode(id, "UTF-8");
|
|
73 } catch (UnsupportedEncodingException e) {
|
|
74 // this shouldn't happen
|
|
75 }
|
|
76 return getTag(id);
|
28
|
77 }
|
|
78 }
|
65
|
79
|
|
80 protected Representation getTag(String id) {
|
|
81 AnnotationStore store = getAnnotationStore();
|
|
82 String tagUri = BaseRestlet.TAGS_URI_PREFIX + id;
|
|
83 Node tagNode = store.getTagNodeByUri(tagUri);
|
|
84 Tag tag = store.createTagFromNode(tagNode);
|
|
85 JSONObject jo = new JSONObject();
|
|
86 try {
|
|
87 jo.put("id", tag.getId());
|
|
88 jo.put("name", tag.getName());
|
|
89 jo.put("uri", tag.getUri());
|
|
90 } catch (JSONException e) {
|
|
91 }
|
|
92
|
|
93 return new JsonRepresentation(jo);
|
28
|
94 }
|
65
|
95
|
|
96 protected Representation getAllTags() {
|
|
97 JSONArray results = new JSONArray();
|
28
|
98 AnnotationStore store = getAnnotationStore();
|
65
|
99
|
|
100 List<Tag> tags = store.getTags(null, null);
|
|
101 for (Tag tag : tags) {
|
|
102 JSONObject jo = new JSONObject();
|
|
103 try {
|
|
104 jo.put("id", tag.getId());
|
|
105 jo.put("name", tag.getName());
|
|
106 jo.put("uri", tag.getUri());
|
|
107 } catch (JSONException e) {
|
28
|
108 }
|
65
|
109 results.put(jo);
|
|
110 }
|
|
111
|
28
|
112 // assemble result object
|
|
113 JSONObject result = new JSONObject();
|
|
114 try {
|
|
115 result.put("rows", results);
|
|
116 result.put("total", results.length());
|
|
117 } catch (JSONException e) {
|
|
118 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
|
|
119 return null;
|
|
120 }
|
75
|
121 logger.fine("sending:");
|
|
122 logger.fine(result.toString());
|
28
|
123 return new JsonRepresentation(result);
|
65
|
124 }
|
28
|
125
|
|
126 }
|