Mercurial > hg > AnnotationManager
comparison src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java @ 33:e5f5848892a2
new annotation model basically works.
author | casties |
---|---|
date | Thu, 31 May 2012 19:08:48 +0200 |
parents | b37487b756ac |
children | bd414fe235b5 |
comparison
equal
deleted
inserted
replaced
32:40846c0b344d | 33:e5f5848892a2 |
---|---|
5 | 5 |
6 import org.apache.log4j.Logger; | 6 import org.apache.log4j.Logger; |
7 import org.openrdf.query.BindingSet; | 7 import org.openrdf.query.BindingSet; |
8 import org.openrdf.query.TupleQueryResult; | 8 import org.openrdf.query.TupleQueryResult; |
9 | 9 |
10 import de.mpiwg.itgroup.annotationManager.Constants.NS; | |
10 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError; | 11 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError; |
11 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException; | 12 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException; |
12 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler; | 13 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler; |
13 | 14 |
14 public class RDFSearcher { | 15 public class RDFSearcher { |
36 } | 37 } |
37 List<Annotation> retAnnots = new ArrayList<Annotation>(); | 38 List<Annotation> retAnnots = new ArrayList<Annotation>(); |
38 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); | 39 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); |
39 String queryString = ""; | 40 String queryString = ""; |
40 // query for tuples with id as subject | 41 // query for tuples with id as subject |
41 StringBuilder whereString = new StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id)); | 42 StringBuilder whereString = new StringBuilder(); |
42 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id)); | 43 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasTarget> ?target.", id)); |
43 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.", id)); | 44 whereString.append("?target <http://www.purl.org/dc/terms/isPartOf> ?uri."); |
44 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id)); | 45 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasBody> ?body.", id)); |
45 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id)); | 46 whereString.append("?body <http://www.w3.org/2011/content#chars> ?bodyText."); |
46 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id)); | 47 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/creator> ?userUrl.", id)); |
47 whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}"); | 48 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/created> ?creationDate", id)); |
48 | 49 |
49 /* StringBuilder whereString = new StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id)); | 50 /* |
50 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id)); | 51 * StringBuilder whereString = new |
51 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.", id)); | 52 * StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id)); |
52 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id)); | 53 * whereString.append(String |
53 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id)); | 54 * .format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id)); |
54 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id)); | 55 * whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.", |
55 whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}"); */ | 56 * id)); whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id)); |
56 | 57 * whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id)); |
57 queryString = String.format("select distinct * where {%s}", whereString); | 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 */ | |
61 | |
62 queryString = String.format("select * from <%s> where {%s}", context, whereString); | |
58 | 63 |
59 logger.debug("RDFSearcher:" + queryString); | 64 logger.debug("RDFSearcher:" + queryString); |
60 | 65 |
61 try { | 66 try { |
62 TupleQueryResult results = th.querySPARQL(queryString); | 67 TupleQueryResult results = th.querySPARQL(queryString); |
63 while (results.hasNext()) { | 68 while (results.hasNext()) { |
64 BindingSet result = results.next(); | 69 BindingSet result = results.next(); |
65 String annotUri = result.getBinding("uri").getValue().stringValue(); | 70 String annotUri = result.getBinding("uri").getValue().stringValue(); |
66 String annotUser = result.getBinding("author").getValue().stringValue(); | 71 String annotUser = result.getBinding("userUrl").getValue().stringValue(); |
67 String textString = ""; | 72 String textString = ""; |
68 if (result.getBinding("text") != null) { | 73 if (result.getBinding("bodyText") != null) { |
69 textString = result.getBinding("text").getValue().stringValue(); | 74 textString = result.getBinding("bodyText").getValue().stringValue(); |
70 } | 75 } |
71 | 76 |
72 String xpointer = result.getBinding("xpointer").getValue().stringValue(); | 77 String xpointer = result.getBinding("target").getValue().stringValue(); |
73 String created = result | 78 String created = result.getBinding("creationDate").getValue().stringValue(); |
74 .getBinding("created").getValue().stringValue(); | |
75 Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id); | 79 Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id); |
76 retAnnots.add(annot); | 80 retAnnots.add(annot); |
77 } | 81 } |
78 } catch (Exception e) { | 82 } catch (Exception e) { |
79 e.printStackTrace(); | 83 e.printStackTrace(); |
80 throw new TripleStoreSearchError(); | 84 throw new TripleStoreSearchError(); |
81 } | 85 } |
82 // TODO Auto-generated method stub | |
83 return retAnnots; | 86 return retAnnots; |
84 } | 87 } |
85 | 88 |
86 /** | 89 /** |
87 * Sucht im Triplestore nach Annotationen | 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. } | |
88 * | 96 * |
89 * @param uri | 97 * @param uri |
90 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource | 98 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource |
91 * oder NULL oder leer | 99 * oder NULL oder leer |
92 * @param user | 100 * @param user |
107 | 115 |
108 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>."; | 116 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>."; |
109 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link."; | 117 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link."; |
110 | 118 |
111 if (uri != null && !uri.equals("")) { | 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 } | |
177 // TODO Auto-generated method stub | |
178 return retAnnots; | |
179 } | |
180 | |
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 } | |
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 */ | |
217 public List<Annotation> searchByUriUserAlt(String uri, String user, String limit, String offset) | |
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("")) { | |
112 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.", | 228 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.", |
113 uri); | 229 uri); |
114 } else { | 230 } else { |
115 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri."); | 231 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri."); |
116 } | 232 } |
170 } | 286 } |
171 // TODO Auto-generated method stub | 287 // TODO Auto-generated method stub |
172 return retAnnots; | 288 return retAnnots; |
173 } | 289 } |
174 | 290 |
175 public void deleteById(String id) throws TripleStoreHandlerException, TripleStoreSearchError { | |
176 if (id == null) { | |
177 return; | |
178 } | |
179 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler(); | |
180 // delete triples with id as subject | |
181 /* wish Virtuoso would speak SparQL1.1... | |
182 String queryString = String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); | |
183 */ | |
184 String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id); | |
185 | |
186 logger.debug("RDFSearcher:" + queryString); | |
187 | |
188 try { | |
189 th.querySPARQL(queryString); | |
190 } catch (Exception e) { | |
191 e.printStackTrace(); | |
192 throw new TripleStoreSearchError(); | |
193 } | |
194 } | |
195 } | 291 } |