44
|
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;
|
44
|
27 import java.util.ArrayList;
|
|
28 import java.util.List;
|
|
29
|
|
30 import org.json.JSONArray;
|
|
31 import org.json.JSONException;
|
|
32 import org.json.JSONObject;
|
|
33 import org.restlet.data.Form;
|
|
34 import org.restlet.data.Parameter;
|
|
35 import org.restlet.data.Status;
|
|
36 import org.restlet.ext.json.JsonRepresentation;
|
|
37 import org.restlet.representation.Representation;
|
|
38 import org.restlet.resource.Get;
|
|
39
|
|
40 import de.mpiwg.itgroup.annotations.Annotation;
|
105
|
41 import de.mpiwg.itgroup.annotations.Annotation.Action;
|
44
|
42 import de.mpiwg.itgroup.annotations.Person;
|
|
43 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
|
|
44 import de.mpiwg.itgroup.annotations.restlet.utils.JSONObjectComparator;
|
|
45
|
|
46 /**
|
|
47 * API for accessing tags in the Annotation store.
|
|
48 *
|
|
49 * @author dwinter
|
65
|
50 *
|
44
|
51 */
|
|
52 public class AnnotatorAnnotationsByResources extends AnnotatorResourceImpl {
|
65
|
53
|
44
|
54 @Get("json")
|
|
55 public Representation doGetJSON(Representation entity) {
|
75
|
56 logger.fine("AnnotatorAnnotatonsByResource doGetJSON!");
|
65
|
57
|
44
|
58 // do authentication
|
88
|
59 Person authUser = getUserFromAuthToken(entity);
|
75
|
60 logger.fine("request authenticated=" + authUser);
|
44
|
61
|
65
|
62 String id = null;
|
44
|
63 String jsonId = (String) getRequest().getAttributes().get("id");
|
65
|
64 if (jsonId != null) {
|
|
65 // URL decode
|
|
66 try {
|
|
67 jsonId = URLDecoder.decode(jsonId, "UTF-8");
|
|
68 } catch (UnsupportedEncodingException e) {
|
|
69 // this shouldn't happen
|
|
70 }
|
|
71 id = decodeJsonId(jsonId);
|
|
72 // String id = jsonId;
|
75
|
73 logger.fine("ressource-id=" + id);
|
44
|
74 }
|
65
|
75
|
|
76 Form form = getRequest().getResourceRef().getQueryAsForm();
|
|
77 String sortBy = null;
|
|
78 for (Parameter parameter : form) {
|
|
79 if (parameter.getName().equals("sortBy")) {
|
|
80 sortBy = parameter.getValue();
|
|
81 }
|
|
82 }
|
|
83
|
44
|
84 AnnotationStore store = getAnnotationStore();
|
65
|
85 // String tagUri=NS.MPIWG_TAGS_URL+id;
|
44
|
86 List<Annotation> annotations = store.getAnnotationsByResource(id);
|
65
|
87
|
|
88 // JSONArray results = new JSONArray();
|
44
|
89 ArrayList<JSONObject> results = new ArrayList<JSONObject>();
|
65
|
90
|
|
91 for (Annotation annot : annotations) {
|
|
92 // check permission
|
105
|
93 if (!annot.isActionAllowed(Action.read, authUser, store))
|
65
|
94 continue;
|
|
95
|
|
96 JSONObject jo = createAnnotatorJson(annot, false);
|
44
|
97 results.add(jo);
|
|
98 }
|
65
|
99
|
|
100 if (sortBy != null) {
|
|
101 JSONObjectComparator.sortAnnotations(results, sortBy);
|
|
102 }
|
|
103
|
|
104 JSONArray resultsJa = new JSONArray();
|
|
105 for (JSONObject result : results) {
|
|
106 resultsJa.put(result);
|
|
107 }
|
|
108
|
|
109 // assemble result object
|
|
110 JSONObject result = new JSONObject();
|
|
111 try {
|
|
112 result.put("rows", resultsJa);
|
|
113 result.put("total", resultsJa.length());
|
|
114 } catch (JSONException e) {
|
|
115 setStatus(Status.SERVER_ERROR_INTERNAL, "JSON Error");
|
|
116 return null;
|
|
117 }
|
|
118 return new JsonRepresentation(result);
|
44
|
119 }
|
|
120
|
|
121 } |