comparison src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java @ 31:9f653697437e

annotationbrowser
author dwinter
date Tue, 25 Sep 2012 21:47:58 +0200
parents b1fb0d117877
children 8427930c5f88
comparison
equal deleted inserted replaced
30:05b631a084d0 31:9f653697437e
3 * <https://github.com/okfn/annotator/wiki/Storage> 3 * <https://github.com/okfn/annotator/wiki/Storage>
4 */ 4 */
5 package de.mpiwg.itgroup.annotations.restlet; 5 package de.mpiwg.itgroup.annotations.restlet;
6 6
7 import java.io.IOException; 7 import java.io.IOException;
8 8 import java.util.ArrayList;
9 import java.util.List;
10
11 import org.json.JSONArray;
9 import org.json.JSONException; 12 import org.json.JSONException;
10 import org.json.JSONObject; 13 import org.json.JSONObject;
14 import org.restlet.data.Form;
15 import org.restlet.data.Parameter;
11 import org.restlet.data.Status; 16 import org.restlet.data.Status;
12 import org.restlet.ext.json.JsonRepresentation; 17 import org.restlet.ext.json.JsonRepresentation;
13 import org.restlet.representation.Representation; 18 import org.restlet.representation.Representation;
14 import org.restlet.resource.Delete; 19 import org.restlet.resource.Delete;
15 import org.restlet.resource.Get; 20 import org.restlet.resource.Get;
16 import org.restlet.resource.Post; 21 import org.restlet.resource.Post;
17 import org.restlet.resource.Put; 22 import org.restlet.resource.Put;
18 23
19 import de.mpiwg.itgroup.annotations.Annotation; 24 import de.mpiwg.itgroup.annotations.Annotation;
20 import de.mpiwg.itgroup.annotations.Person; 25 import de.mpiwg.itgroup.annotations.Person;
26 import de.mpiwg.itgroup.annotations.Tag;
21 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 27 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
28 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
22 29
23 /** 30 /**
24 * Implements the "annotations" uri of the Annotator API. see 31 * Implements the "annotations" uri of the Annotator API. see
25 * <https://github.com/okfn/annotator/wiki/Storage> 32 * <https://github.com/okfn/annotator/wiki/Storage>
26 * 33 *
47 String jsonId = (String) getRequest().getAttributes().get("id"); 54 String jsonId = (String) getRequest().getAttributes().get("id");
48 String id = decodeJsonId(jsonId); 55 String id = decodeJsonId(jsonId);
49 logger.debug("annotation-id=" + id); 56 logger.debug("annotation-id=" + id);
50 57
51 if (id == null) { 58 if (id == null) {
52 // TODO: what to return without id - list all annotations? 59
53 setStatus(Status.CLIENT_ERROR_BAD_REQUEST); 60 return getAllAnnotations();
54 return null;
55 } 61 }
56 62
57 // do authentication 63 // do authentication
58 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 64 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
59 logger.debug("request authenticated=" + authUser); 65 logger.debug("request authenticated=" + authUser);
74 setStatus(Status.CLIENT_ERROR_NOT_FOUND); 80 setStatus(Status.CLIENT_ERROR_NOT_FOUND);
75 return null; 81 return null;
76 } 82 }
77 } 83 }
78 84
79 /** 85 private Representation getAllAnnotations() {
86
87 Form form = getRequest().getResourceRef().getQueryAsForm();
88 String sortBy=null;
89 for (Parameter parameter : form) {
90 if (parameter.getName().equals("sortBy")){
91 sortBy = parameter.getValue();
92 }
93 }
94
95 AnnotationStore store = getAnnotationStore();
96 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
97
98 List<Annotation> annotations = store.getAnnotations(null, null);
99 for (Annotation annotation : annotations) {
100
101 JSONObject jo = createAnnotatorJson(annotation,false);
102 results.add(jo);
103
104 }
105
106 if (sortBy!=null){
107 JSONObjectComparator.sortAnnotations(results,sortBy);
108 }
109
110 JSONArray resultsJa = new JSONArray();
111 for (JSONObject result:results){
112 resultsJa.put(result);
113 }
114
115 // assemble result object
116 JSONObject result = new JSONObject();
117 try {
118 result.put("rows", resultsJa);
119 result.put("total", resultsJa.length());
120 } catch (JSONException e) {
121 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
122 return null;
123 }
124 logger.debug("sending:");
125 logger.debug(result);
126 return new JsonRepresentation(result);
127 }
128
129
130
131
132
133 /**
80 * POST with JSON content-type. Creates a new Annotation. 134 * POST with JSON content-type. Creates a new Annotation.
81 * 135 *
82 * @return 136 * @return
83 */ 137 */
84 @Post("json") 138 @Post("json")