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