0
|
1 package de.mpiwg.itgroup.annotationManager.RDFHandling;
|
|
2
|
|
3 import java.util.ArrayList;
|
|
4 import java.util.List;
|
|
5
|
|
6 import org.apache.log4j.Logger;
|
|
7 import org.openrdf.query.BindingSet;
|
|
8 import org.openrdf.query.TupleQueryResult;
|
|
9
|
33
|
10 import de.mpiwg.itgroup.annotationManager.Constants.NS;
|
0
|
11 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
5
|
12 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
13 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
|
0
|
14
|
|
15 public class RDFSearcher {
|
|
16
|
17
|
17 private String context;
|
0
|
18
|
17
|
19 private Logger logger = Logger.getRootLogger();
|
0
|
20
|
17
|
21 public RDFSearcher(String context) {
|
|
22 this.context = context;
|
|
23 }
|
16
|
24
|
17
|
25 /**
|
30
|
26 * Retrieves Annotations by ID.
|
17
|
27 *
|
|
28 * @param id
|
30
|
29 * id of the Annotation
|
17
|
30 * @return
|
|
31 * @throws TripleStoreHandlerException
|
|
32 * @throws TripleStoreSearchError
|
|
33 */
|
|
34 public List<Annotation> searchById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
35 if (id == null) {
|
|
36 return null;
|
|
37 }
|
|
38 List<Annotation> retAnnots = new ArrayList<Annotation>();
|
|
39 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
40 String queryString = "";
|
|
41 // query for tuples with id as subject
|
33
|
42 StringBuilder whereString = new StringBuilder();
|
|
43 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasTarget> ?target.", id));
|
|
44 whereString.append("?target <http://www.purl.org/dc/terms/isPartOf> ?uri.");
|
|
45 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasBody> ?body.", id));
|
|
46 whereString.append("?body <http://www.w3.org/2011/content#chars> ?bodyText.");
|
|
47 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/creator> ?userUrl.", id));
|
|
48 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/created> ?creationDate", id));
|
|
49
|
|
50 /*
|
|
51 * StringBuilder whereString = new
|
|
52 * StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id));
|
|
53 * whereString.append(String
|
|
54 * .format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id));
|
|
55 * whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.",
|
|
56 * id)); whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id));
|
|
57 * whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id));
|
|
58 * whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id));
|
|
59 * whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
|
|
60 */
|
16
|
61
|
33
|
62 queryString = String.format("select * from <%s> where {%s}", context, whereString);
|
16
|
63
|
17
|
64 logger.debug("RDFSearcher:" + queryString);
|
16
|
65
|
17
|
66 try {
|
|
67 TupleQueryResult results = th.querySPARQL(queryString);
|
|
68 while (results.hasNext()) {
|
|
69 BindingSet result = results.next();
|
|
70 String annotUri = result.getBinding("uri").getValue().stringValue();
|
33
|
71 String annotUser = result.getBinding("userUrl").getValue().stringValue();
|
17
|
72 String textString = "";
|
33
|
73 if (result.getBinding("bodyText") != null) {
|
|
74 textString = result.getBinding("bodyText").getValue().stringValue();
|
17
|
75 }
|
33
|
76
|
|
77 String xpointer = result.getBinding("target").getValue().stringValue();
|
|
78 String created = result.getBinding("creationDate").getValue().stringValue();
|
17
|
79 Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id);
|
|
80 retAnnots.add(annot);
|
|
81 }
|
|
82 } catch (Exception e) {
|
|
83 e.printStackTrace();
|
|
84 throw new TripleStoreSearchError();
|
|
85 }
|
33
|
86 return retAnnots;
|
|
87 }
|
|
88
|
|
89 /**
|
|
90 * Sucht im Triplestore nach Annotationen
|
|
91 *
|
|
92 * select * from <file:///annotations2> where { ?target <http://www.purl.org/dc/terms/isPartOf>
|
|
93 * <file:///Volumes/Schlachteplatte/Users/casties/Library/Eclipse/annotator/dev.html>. ?ann
|
|
94 * <http://www.openannotation.org/ns/hasTarget> ?target. ?ann <http://www.openannotation.org/ns/hasBody> ?body. ?body
|
|
95 * <http://www.w3.org/2011/content#chars> ?bodyText. }
|
|
96 *
|
|
97 * @param uri
|
|
98 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource
|
|
99 * oder NULL oder leer
|
|
100 * @param user
|
|
101 * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt
|
|
102 * wurden.
|
|
103 * @param limit
|
|
104 * @param offset
|
|
105 * @return
|
|
106 * @throws TripleStoreHandlerException
|
|
107 * @throws TripleStoreSearchError
|
|
108 */
|
|
109 public List<Annotation> searchByUriUser(String uri, String user, String limit, String offset)
|
|
110 throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
111
|
|
112 List<Annotation> retAnnots = new ArrayList<Annotation>();
|
|
113 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
114 String queryString = "";
|
|
115
|
|
116 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
|
|
117 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
|
|
118
|
|
119 if (uri != null && !uri.equals("")) {
|
|
120 whereString = String.format("?target <http://www.purl.org/dc/terms/isPartOf> <%s>.", uri);
|
|
121 } else {
|
|
122 whereString = "?target <http://www.purl.org/dc/terms/isPartOf> ?uri.";
|
|
123 }
|
|
124 whereString += "?annotation <http://www.openannotation.org/ns/hasTarget> ?target.";
|
|
125 whereString += "?annotation <http://www.openannotation.org/ns/hasBody> ?body.";
|
|
126 whereString += "?body <http://www.w3.org/2011/content#chars> ?bodyText.";
|
|
127
|
|
128 if (user != null && !user.equals("")) {
|
|
129 if (user.startsWith("http")) {
|
|
130 whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> <%s>", user);
|
|
131 } else {
|
|
132 whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> \"%s\".", user);
|
|
133 }
|
|
134 } else {
|
|
135 whereString += "?annotation <http://www.purl.org/dc/terms/creator> ?userUrl.";
|
|
136 }
|
|
137
|
|
138 whereString += "?annotation <http://www.purl.org/dc/terms/created> ?creationDate";
|
|
139
|
|
140 queryString = String.format("select * from <%s> where {%s}", context, whereString);
|
|
141
|
|
142 logger.debug("RDFSearcher:" + queryString);
|
|
143
|
|
144 try {
|
|
145 TupleQueryResult results = th.querySPARQL(queryString);
|
|
146
|
|
147 while (results.hasNext()) {
|
|
148 BindingSet result = results.next();
|
|
149 String annotatedUri;
|
|
150 if (uri != null && !uri.equals("")) {
|
|
151 annotatedUri = uri;
|
|
152 } else {
|
|
153 annotatedUri = result.getBinding("uri").getValue().stringValue();
|
|
154 }
|
|
155
|
|
156 String annotUser;
|
|
157 if (user != null && !user.equals("")) {
|
|
158 annotUser = user;
|
|
159 } else {
|
|
160 annotUser = result.getBinding("userUrl").getValue().stringValue();
|
|
161 }
|
|
162
|
|
163 String textString = "";
|
|
164 if (result.getBinding("bodyText") != null) {
|
|
165 textString = result.getBinding("bodyText").getValue().stringValue();
|
|
166 }
|
|
167 String xpointer = result.getBinding("target").getValue().stringValue();
|
|
168 String time = result.getBinding("creationDate").getValue().stringValue();
|
|
169 String annotationUri = result.getBinding("annotation").getValue().stringValue();
|
|
170 Annotation annot = new Annotation(xpointer, annotUser, time, textString, null, annotatedUri, annotationUri);
|
|
171 retAnnots.add(annot);
|
|
172 }
|
|
173 } catch (Exception e) {
|
|
174 e.printStackTrace();
|
|
175 throw new TripleStoreSearchError();
|
|
176 }
|
17
|
177 // TODO Auto-generated method stub
|
|
178 return retAnnots;
|
|
179 }
|
16
|
180
|
33
|
181 public void deleteById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
182 if (id == null) {
|
|
183 return;
|
|
184 }
|
|
185 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
186 // delete triples with id as subject
|
|
187 /*
|
|
188 * wish Virtuoso would speak SparQL1.1... String queryString =
|
|
189 * String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
|
|
190 */
|
|
191 String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
|
|
192
|
|
193 logger.debug("RDFSearcher:" + queryString);
|
|
194
|
|
195 try {
|
|
196 th.querySPARQL(queryString);
|
|
197 } catch (Exception e) {
|
|
198 e.printStackTrace();
|
|
199 throw new TripleStoreSearchError();
|
|
200 }
|
|
201 }
|
17
|
202 /**
|
|
203 * Sucht im Triplestore nach Annotationen
|
|
204 *
|
|
205 * @param uri
|
|
206 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource
|
|
207 * oder NULL oder leer
|
|
208 * @param user
|
|
209 * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt
|
|
210 * wurden.
|
|
211 * @param limit
|
|
212 * @param offset
|
|
213 * @return
|
|
214 * @throws TripleStoreHandlerException
|
|
215 * @throws TripleStoreSearchError
|
|
216 */
|
33
|
217 public List<Annotation> searchByUriUserAlt(String uri, String user, String limit, String offset)
|
17
|
218 throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
219
|
|
220 List<Annotation> retAnnots = new ArrayList<Annotation>();
|
|
221 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
222 String queryString = "";
|
|
223
|
|
224 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
|
|
225 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
|
|
226
|
|
227 if (uri != null && !uri.equals("")) {
|
|
228 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.",
|
|
229 uri);
|
|
230 } else {
|
|
231 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.");
|
|
232 }
|
|
233 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.");
|
|
234 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.");
|
|
235
|
|
236 if (user != null && !user.equals("")) {
|
|
237 if (user.startsWith("http")) {
|
|
238 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> <%s>.", user);
|
|
239 } else {
|
|
240 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".", user);
|
|
241 }
|
|
242 } else {
|
|
243 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author.");
|
16
|
244 }
|
|
245
|
17
|
246 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#created> ?created.");
|
|
247
|
|
248 whereString += String
|
|
249 .format(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
|
|
250
|
|
251 queryString = String.format("select distinct * where {%s}", whereString);
|
|
252
|
|
253 logger.debug("RDFSearcher:" + queryString);
|
0
|
254
|
17
|
255 try {
|
|
256 TupleQueryResult results = th.querySPARQL(queryString);
|
|
257
|
|
258 while (results.hasNext()) {
|
|
259 BindingSet result = results.next();
|
|
260 String annotUri;
|
|
261 if (uri != null && !uri.equals("")) {
|
|
262 annotUri = uri;
|
|
263 } else {
|
|
264 annotUri = result.getBinding("uri").getValue().stringValue();
|
16
|
265 }
|
0
|
266
|
17
|
267 String annotUser;
|
|
268 if (user != null && !user.equals("")) {
|
|
269 annotUser = user;
|
|
270 } else {
|
|
271 annotUser = result.getBinding("author").getValue().stringValue();
|
|
272 }
|
0
|
273
|
17
|
274 String textString = "";
|
|
275 if (result.getBinding("text") != null) {
|
|
276 textString = result.getBinding("text").getValue().stringValue();
|
|
277 }
|
|
278 Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), annotUser, result
|
|
279 .getBinding("created").getValue().stringValue(), textString, null, annotUri, result.getBinding("s")
|
|
280 .getValue().stringValue());
|
|
281 retAnnots.add(annot);
|
|
282 }
|
|
283 } catch (Exception e) {
|
|
284 e.printStackTrace();
|
|
285 throw new TripleStoreSearchError();
|
|
286 }
|
|
287 // TODO Auto-generated method stub
|
|
288 return retAnnots;
|
|
289 }
|
0
|
290
|
|
291 }
|