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
|
34
|
21 static final String baseGraph = " ?annotation <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Annotation>."
|
|
22 + " ?annotation <" + NS.OAC_NS + "hasTarget> ?target."
|
|
23 + " ?annotation <" + NS.OAC_NS + "hasBody> ?body."
|
|
24 + " ?annotation <" + NS.DCTERMS_NS + "creator> ?creator."
|
|
25 + " ?annotation <" + NS.DCTERMS_NS + "created> ?created."
|
|
26 + " ?body <" + NS.CNT_NS + "chars> ?text."
|
|
27 + " ?target <" + NS.DCTERMS_NS + "isPartOf> ?uri.";
|
|
28 static final String optGraph = " OPTIONAL { ?body <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Body>}"
|
|
29 + " OPTIONAL { ?body <" + NS.RDF_NS + "type> <" + NS.CNT_NS + "ContentAsText>}"
|
|
30 + " OPTIONAL { ?target <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Target>}";
|
|
31 static final String nonOptGraph = " ?body <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Body>."
|
|
32 + " ?body <" + NS.RDF_NS + "type> <" + NS.CNT_NS + "ContentAsText>."
|
|
33 + " ?target <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Target>.";
|
|
34
|
|
35 static final String annotGraph = baseGraph + optGraph;
|
|
36
|
17
|
37 public RDFSearcher(String context) {
|
|
38 this.context = context;
|
|
39 }
|
16
|
40
|
17
|
41 /**
|
30
|
42 * Retrieves Annotations by ID.
|
17
|
43 *
|
|
44 * @param id
|
30
|
45 * id of the Annotation
|
17
|
46 * @return
|
|
47 * @throws TripleStoreHandlerException
|
|
48 * @throws TripleStoreSearchError
|
|
49 */
|
|
50 public List<Annotation> searchById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
51 if (id == null) {
|
|
52 return null;
|
|
53 }
|
|
54 List<Annotation> retAnnots = new ArrayList<Annotation>();
|
|
55 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
56 String queryString = "";
|
33
|
57 StringBuilder whereString = new StringBuilder();
|
|
58 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasTarget> ?target.", id));
|
|
59 whereString.append("?target <http://www.purl.org/dc/terms/isPartOf> ?uri.");
|
|
60 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasBody> ?body.", id));
|
|
61 whereString.append("?body <http://www.w3.org/2011/content#chars> ?bodyText.");
|
|
62 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/creator> ?userUrl.", id));
|
|
63 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/created> ?creationDate", id));
|
34
|
64
|
33
|
65 /*
|
|
66 * StringBuilder whereString = new
|
|
67 * StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id));
|
|
68 * whereString.append(String
|
|
69 * .format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id));
|
|
70 * whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.",
|
|
71 * id)); whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id));
|
|
72 * whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id));
|
|
73 * whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id));
|
|
74 * whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
|
|
75 */
|
16
|
76
|
33
|
77 queryString = String.format("select * from <%s> where {%s}", context, whereString);
|
16
|
78
|
17
|
79 logger.debug("RDFSearcher:" + queryString);
|
16
|
80
|
17
|
81 try {
|
|
82 TupleQueryResult results = th.querySPARQL(queryString);
|
|
83 while (results.hasNext()) {
|
|
84 BindingSet result = results.next();
|
|
85 String annotUri = result.getBinding("uri").getValue().stringValue();
|
33
|
86 String annotUser = result.getBinding("userUrl").getValue().stringValue();
|
17
|
87 String textString = "";
|
33
|
88 if (result.getBinding("bodyText") != null) {
|
|
89 textString = result.getBinding("bodyText").getValue().stringValue();
|
17
|
90 }
|
33
|
91
|
|
92 String xpointer = result.getBinding("target").getValue().stringValue();
|
|
93 String created = result.getBinding("creationDate").getValue().stringValue();
|
17
|
94 Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id);
|
|
95 retAnnots.add(annot);
|
|
96 }
|
|
97 } catch (Exception e) {
|
|
98 e.printStackTrace();
|
|
99 throw new TripleStoreSearchError();
|
|
100 }
|
33
|
101 return retAnnots;
|
|
102 }
|
|
103
|
|
104 /**
|
|
105 * Sucht im Triplestore nach Annotationen
|
|
106 *
|
|
107 * select * from <file:///annotations2> where { ?target <http://www.purl.org/dc/terms/isPartOf>
|
|
108 * <file:///Volumes/Schlachteplatte/Users/casties/Library/Eclipse/annotator/dev.html>. ?ann
|
|
109 * <http://www.openannotation.org/ns/hasTarget> ?target. ?ann <http://www.openannotation.org/ns/hasBody> ?body. ?body
|
|
110 * <http://www.w3.org/2011/content#chars> ?bodyText. }
|
|
111 *
|
|
112 * @param uri
|
|
113 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource
|
|
114 * oder NULL oder leer
|
|
115 * @param user
|
|
116 * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt
|
|
117 * wurden.
|
|
118 * @param limit
|
|
119 * @param offset
|
|
120 * @return
|
|
121 * @throws TripleStoreHandlerException
|
|
122 * @throws TripleStoreSearchError
|
|
123 */
|
|
124 public List<Annotation> searchByUriUser(String uri, String user, String limit, String offset)
|
|
125 throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
126
|
|
127 List<Annotation> retAnnots = new ArrayList<Annotation>();
|
|
128 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
129 String queryString = "";
|
|
130
|
34
|
131 String whereString = annotGraph;
|
33
|
132
|
|
133 if (uri != null && !uri.equals("")) {
|
34
|
134 whereString += String.format("FILTER (?uri = <%s>)", uri);
|
33
|
135 }
|
|
136
|
|
137 if (user != null && !user.equals("")) {
|
|
138 if (user.startsWith("http")) {
|
34
|
139 whereString += String.format("FILTER (?creator = <%s>)", user);
|
33
|
140 } else {
|
34
|
141 whereString += String.format("FILTER (?creator = \"%s\")", user);
|
33
|
142 }
|
|
143 }
|
|
144
|
34
|
145 queryString = String.format("SELECT * FROM <%s> WHERE {%s}", context, whereString);
|
33
|
146
|
|
147 logger.debug("RDFSearcher:" + queryString);
|
|
148
|
|
149 try {
|
|
150 TupleQueryResult results = th.querySPARQL(queryString);
|
|
151
|
|
152 while (results.hasNext()) {
|
|
153 BindingSet result = results.next();
|
|
154 String annotatedUri;
|
|
155 if (uri != null && !uri.equals("")) {
|
|
156 annotatedUri = uri;
|
|
157 } else {
|
|
158 annotatedUri = result.getBinding("uri").getValue().stringValue();
|
|
159 }
|
|
160 String annotUser;
|
|
161 if (user != null && !user.equals("")) {
|
|
162 annotUser = user;
|
|
163 } else {
|
34
|
164 annotUser = result.getBinding("creator").getValue().stringValue();
|
33
|
165 }
|
|
166 String textString = "";
|
34
|
167 if (result.getBinding("text") != null) {
|
|
168 textString = result.getBinding("text").getValue().stringValue();
|
33
|
169 }
|
|
170 String xpointer = result.getBinding("target").getValue().stringValue();
|
34
|
171 String time = result.getBinding("created").getValue().stringValue();
|
33
|
172 String annotationUri = result.getBinding("annotation").getValue().stringValue();
|
|
173 Annotation annot = new Annotation(xpointer, annotUser, time, textString, null, annotatedUri, annotationUri);
|
|
174 retAnnots.add(annot);
|
|
175 }
|
|
176 } catch (Exception e) {
|
|
177 e.printStackTrace();
|
|
178 throw new TripleStoreSearchError();
|
|
179 }
|
17
|
180 // TODO Auto-generated method stub
|
|
181 return retAnnots;
|
|
182 }
|
16
|
183
|
33
|
184 public void deleteById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
185 if (id == null) {
|
|
186 return;
|
|
187 }
|
|
188 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
189 // delete triples with id as subject
|
|
190 /*
|
|
191 * wish Virtuoso would speak SparQL1.1... String queryString =
|
|
192 * String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
|
|
193 */
|
34
|
194 String whereString = annotGraph + String.format(" FILTER(?annotation = <%s>)", id);
|
|
195 String queryString = String.format("DELETE FROM <%s> {%s} WHERE {%s}", context, baseGraph+nonOptGraph, whereString);
|
33
|
196
|
|
197 logger.debug("RDFSearcher:" + queryString);
|
|
198
|
|
199 try {
|
|
200 th.querySPARQL(queryString);
|
|
201 } catch (Exception e) {
|
|
202 e.printStackTrace();
|
|
203 throw new TripleStoreSearchError();
|
|
204 }
|
|
205 }
|
17
|
206
|
0
|
207
|
|
208 }
|