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
|
|
10 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
5
|
11 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
12 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
|
0
|
13
|
|
14 public class RDFSearcher {
|
|
15
|
17
|
16 private String context;
|
0
|
17
|
17
|
18 private Logger logger = Logger.getRootLogger();
|
0
|
19
|
17
|
20 public RDFSearcher(String context) {
|
|
21 this.context = context;
|
|
22 }
|
16
|
23
|
17
|
24 /**
|
30
|
25 * Retrieves Annotations by ID.
|
17
|
26 *
|
|
27 * @param id
|
30
|
28 * id of the Annotation
|
17
|
29 * @return
|
|
30 * @throws TripleStoreHandlerException
|
|
31 * @throws TripleStoreSearchError
|
|
32 */
|
|
33 public List<Annotation> searchById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
34 if (id == null) {
|
|
35 return null;
|
|
36 }
|
|
37 List<Annotation> retAnnots = new ArrayList<Annotation>();
|
|
38 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
39 String queryString = "";
|
|
40 // 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 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id));
|
|
43 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.", id));
|
|
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.w3.org/2000/10/annotation-ns#author> ?author.", id));
|
|
46 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id));
|
|
47 whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
|
16
|
48
|
30
|
49 /* StringBuilder whereString = new StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id));
|
|
50 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id));
|
|
51 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.", id));
|
|
52 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id));
|
|
53 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id));
|
|
54 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id));
|
|
55 whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}"); */
|
|
56
|
17
|
57 queryString = String.format("select distinct * where {%s}", whereString);
|
16
|
58
|
17
|
59 logger.debug("RDFSearcher:" + queryString);
|
16
|
60
|
17
|
61 try {
|
|
62 TupleQueryResult results = th.querySPARQL(queryString);
|
|
63 while (results.hasNext()) {
|
|
64 BindingSet result = results.next();
|
|
65 String annotUri = result.getBinding("uri").getValue().stringValue();
|
|
66 String annotUser = result.getBinding("author").getValue().stringValue();
|
|
67 String textString = "";
|
|
68 if (result.getBinding("text") != null) {
|
|
69 textString = result.getBinding("text").getValue().stringValue();
|
|
70 }
|
|
71
|
|
72 String xpointer = result.getBinding("xpointer").getValue().stringValue();
|
|
73 String created = result
|
|
74 .getBinding("created").getValue().stringValue();
|
|
75 Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id);
|
|
76 retAnnots.add(annot);
|
|
77 }
|
|
78 } catch (Exception e) {
|
|
79 e.printStackTrace();
|
|
80 throw new TripleStoreSearchError();
|
|
81 }
|
|
82 // TODO Auto-generated method stub
|
|
83 return retAnnots;
|
|
84 }
|
16
|
85
|
17
|
86 /**
|
|
87 * Sucht im Triplestore nach Annotationen
|
|
88 *
|
|
89 * @param uri
|
|
90 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource
|
|
91 * oder NULL oder leer
|
|
92 * @param user
|
|
93 * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt
|
|
94 * wurden.
|
|
95 * @param limit
|
|
96 * @param offset
|
|
97 * @return
|
|
98 * @throws TripleStoreHandlerException
|
|
99 * @throws TripleStoreSearchError
|
|
100 */
|
|
101 public List<Annotation> searchByUriUser(String uri, String user, String limit, String offset)
|
|
102 throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
103
|
|
104 List<Annotation> retAnnots = new ArrayList<Annotation>();
|
|
105 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
|
|
106 String queryString = "";
|
|
107
|
|
108 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
|
|
109 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
|
|
110
|
|
111 if (uri != null && !uri.equals("")) {
|
|
112 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.",
|
|
113 uri);
|
|
114 } else {
|
|
115 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.");
|
|
116 }
|
|
117 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.");
|
|
118 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.");
|
|
119
|
|
120 if (user != null && !user.equals("")) {
|
|
121 if (user.startsWith("http")) {
|
|
122 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> <%s>.", user);
|
|
123 } else {
|
|
124 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".", user);
|
|
125 }
|
|
126 } else {
|
|
127 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author.");
|
16
|
128 }
|
|
129
|
17
|
130 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#created> ?created.");
|
|
131
|
|
132 whereString += String
|
|
133 .format(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
|
|
134
|
|
135 queryString = String.format("select distinct * where {%s}", whereString);
|
|
136
|
|
137 logger.debug("RDFSearcher:" + queryString);
|
0
|
138
|
17
|
139 try {
|
|
140 TupleQueryResult results = th.querySPARQL(queryString);
|
|
141
|
|
142 while (results.hasNext()) {
|
|
143 BindingSet result = results.next();
|
|
144 String annotUri;
|
|
145 if (uri != null && !uri.equals("")) {
|
|
146 annotUri = uri;
|
|
147 } else {
|
|
148 annotUri = result.getBinding("uri").getValue().stringValue();
|
16
|
149 }
|
0
|
150
|
17
|
151 String annotUser;
|
|
152 if (user != null && !user.equals("")) {
|
|
153 annotUser = user;
|
|
154 } else {
|
|
155 annotUser = result.getBinding("author").getValue().stringValue();
|
|
156 }
|
0
|
157
|
17
|
158 String textString = "";
|
|
159 if (result.getBinding("text") != null) {
|
|
160 textString = result.getBinding("text").getValue().stringValue();
|
|
161 }
|
|
162 Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), annotUser, result
|
|
163 .getBinding("created").getValue().stringValue(), textString, null, annotUri, result.getBinding("s")
|
|
164 .getValue().stringValue());
|
|
165 retAnnots.add(annot);
|
|
166 }
|
|
167 } catch (Exception e) {
|
|
168 e.printStackTrace();
|
|
169 throw new TripleStoreSearchError();
|
|
170 }
|
|
171 // TODO Auto-generated method stub
|
|
172 return retAnnots;
|
|
173 }
|
0
|
174
|
17
|
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);
|
0
|
185
|
17
|
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 }
|
0
|
195 }
|