Mercurial > hg > AnnotationManagerN4J
comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResources.java @ 61:b8ef15c8c4a5
implemented new shape format for image annotations.
minor cleanups.
author | casties |
---|---|
date | Thu, 22 Nov 2012 17:38:53 +0100 |
parents | 64aa756c60cc |
children | c0dd5314bada |
comparison
equal
deleted
inserted
replaced
60:99d9afcfd04d | 61:b8ef15c8c4a5 |
---|---|
15 import org.restlet.resource.Get; | 15 import org.restlet.resource.Get; |
16 | 16 |
17 import de.mpiwg.itgroup.annotations.Resource; | 17 import de.mpiwg.itgroup.annotations.Resource; |
18 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; | 18 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; |
19 | 19 |
20 | |
21 /** | 20 /** |
22 * API for accessing tags in the Annotation store. | 21 * API for accessing resource objects in the Annotation store. |
23 * | 22 * |
24 * @author dwinter | 23 * @author dwinter |
25 * | 24 * |
26 */ | 25 */ |
27 public class AnnotatorResources extends AnnotatorResourceImpl { | 26 public class AnnotatorResources extends AnnotatorResourceImpl { |
28 protected String getAllowedMethodsForHeader() { | 27 protected String getAllowedMethodsForHeader() { |
29 return "OPTIONS,GET"; | 28 return "OPTIONS,GET"; |
30 } | 29 } |
31 | 30 |
32 /** | 31 /** |
33 * GET with JSON content-type. | 32 * GET with JSON content-type. |
34 * Parameters: | 33 * Parameters: user: short user name uri: user uri |
35 * user: short user name | 34 * |
36 * uri: user uri | |
37 * | |
38 * @param entity | 35 * @param entity |
39 * @return | 36 * @return |
40 */ | 37 */ |
41 @Get("json") | 38 @Get("json") |
42 public Representation doGetJSON(Representation entity) { | 39 public Representation doGetJSON(Representation entity) { |
43 logger.debug("AnnotatorGroups doGetJSON!"); | 40 logger.debug("AnnotatorResources doGetJSON!"); |
44 setCorsHeaders(); | 41 setCorsHeaders(); |
45 | 42 |
46 String jsonId = (String) getRequest().getAttributes().get("id"); | 43 String jsonId = (String) getRequest().getAttributes().get("id"); |
47 String uri = decodeJsonId(jsonId); | 44 String uri = decodeJsonId(jsonId); |
48 | 45 |
49 logger.debug("resources-id=" + uri); | 46 logger.debug("resources-id=" + uri); |
50 | 47 |
51 if (uri==null){ | 48 if (uri == null) { |
52 return getAllResources(); | 49 return getAllResources(); |
53 } else { | 50 } else { |
54 | 51 return getResource(uri); |
55 return getResource(uri); | |
56 } | 52 } |
57 } | 53 } |
58 | 54 |
59 protected Representation getResource(String uri){ | 55 protected Representation getResource(String uri) { |
60 AnnotationStore store = getAnnotationStore(); | 56 AnnotationStore store = getAnnotationStore(); |
61 //String tagUri=NS.MPIWG_TAGS_URL+id; | 57 Node resNode = store.getResourceNodeByUri(uri); |
62 Node tagNode = store.getResourceNodeByUri(uri); | 58 Resource resource = store.createResourceFromNode(resNode); |
63 Resource resource = store.createResourceFromNode(tagNode); | 59 JSONObject jo = new JSONObject(); |
64 JSONObject jo = new JSONObject(); | 60 try { |
65 try { | 61 jo.put("id", encodeJsonId(resource.getUri())); |
66 jo.put("id", encodeJsonId(resource.getUri())); | 62 jo.put("uri", resource.getUri()); |
67 jo.put("uri", resource.getUri()); | 63 } catch (JSONException e) { |
68 } catch (JSONException e) { | 64 } |
69 } | 65 |
70 | 66 return new JsonRepresentation(jo); |
71 return new JsonRepresentation(jo); | |
72 } | 67 } |
73 protected Representation getAllResources() { | 68 |
74 JSONArray results = new JSONArray(); | 69 protected Representation getAllResources() { |
70 JSONArray results = new JSONArray(); | |
75 AnnotationStore store = getAnnotationStore(); | 71 AnnotationStore store = getAnnotationStore(); |
76 | 72 |
77 | 73 List<Resource> resources = store.getResources(null, null); |
78 List<Resource> resources = store.getResources(null, null); | 74 for (Resource resource : resources) { |
79 for (Resource resource : resources) { | 75 JSONObject jo = new JSONObject(); |
80 JSONObject jo = new JSONObject(); | 76 try { |
81 try { | 77 jo.put("id", encodeJsonId(resource.getUri())); |
82 jo.put("id", encodeJsonId(resource.getUri())); | 78 jo.put("uri", resource.getUri()); |
83 jo.put("uri", resource.getUri()); | 79 } catch (JSONException e) { |
84 } catch (JSONException e) { | |
85 } | |
86 results.put(jo); | |
87 } | 80 } |
88 | 81 results.put(jo); |
82 } | |
83 | |
89 // assemble result object | 84 // assemble result object |
90 JSONObject result = new JSONObject(); | 85 JSONObject result = new JSONObject(); |
91 try { | 86 try { |
92 result.put("rows", results); | 87 result.put("rows", results); |
93 result.put("total", results.length()); | 88 result.put("total", results.length()); |
96 return null; | 91 return null; |
97 } | 92 } |
98 logger.debug("sending:"); | 93 logger.debug("sending:"); |
99 logger.debug(result); | 94 logger.debug(result); |
100 return new JsonRepresentation(result); | 95 return new JsonRepresentation(result); |
101 } | 96 } |
102 | |
103 | 97 |
104 | |
105 } | 98 } |