annotate src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java @ 23:a3e324009990

now Mavenified! removed tiny-mce javascript from html frontend. still needs TripleStoreManager project in Eclipse. jsontoken 1.1 has to be built manually.
author casties
date Tue, 03 Apr 2012 13:05:05 +0200
parents src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java@b0ef5c860464
children b37487b756ac
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dwinter
parents:
diff changeset
1 package de.mpiwg.itgroup.annotationManager.RDFHandling;
dwinter
parents:
diff changeset
2
dwinter
parents:
diff changeset
3 import java.util.ArrayList;
dwinter
parents:
diff changeset
4 import java.util.List;
dwinter
parents:
diff changeset
5
dwinter
parents:
diff changeset
6 import org.apache.log4j.Logger;
dwinter
parents:
diff changeset
7 import org.openrdf.query.BindingSet;
dwinter
parents:
diff changeset
8 import org.openrdf.query.TupleQueryResult;
dwinter
parents:
diff changeset
9
dwinter
parents:
diff changeset
10 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
5
0be9d53a6967 editor for annotations
dwinter
parents: 2
diff changeset
11 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
0be9d53a6967 editor for annotations
dwinter
parents: 2
diff changeset
12 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
0
dwinter
parents:
diff changeset
13
dwinter
parents:
diff changeset
14 public class RDFSearcher {
dwinter
parents:
diff changeset
15
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
16 private String urlBase = "http://ontologies.mpiwg-berlin.mpg.de/annotations/"; // TODO should go into config
0
dwinter
parents:
diff changeset
17
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
18 private String context;
0
dwinter
parents:
diff changeset
19
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
20 private Logger logger = Logger.getRootLogger();
0
dwinter
parents:
diff changeset
21
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
22 public RDFSearcher(String context) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
23 this.context = context;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
24 }
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
25
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
26 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
27 * Sucht im Triplestore nach Annotationen.
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
28 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
29 * @param id
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
30 * id der Annotation
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
31 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
32 * @throws TripleStoreHandlerException
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
33 * @throws TripleStoreSearchError
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
34 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
35 public List<Annotation> searchById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
36 if (id == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
37 return null;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
38 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
39 List<Annotation> retAnnots = new ArrayList<Annotation>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
40 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
41 String queryString = "";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
42 // query for tuples with id as subject
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
43 StringBuilder whereString = new StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id));
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
44 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id));
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
45 whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.", id));
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
46 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id));
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
47 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id));
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
48 whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id));
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
49 whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
50
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
51 queryString = String.format("select distinct * where {%s}", whereString);
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
52
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
53 logger.debug("RDFSearcher:" + queryString);
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
54
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
55 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
56 TupleQueryResult results = th.querySPARQL(queryString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
57 while (results.hasNext()) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
58 BindingSet result = results.next();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
59 String annotUri = result.getBinding("uri").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
60 String annotUser = result.getBinding("author").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
61 String textString = "";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
62 if (result.getBinding("text") != null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
63 textString = result.getBinding("text").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
64 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
65
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
66 String xpointer = result.getBinding("xpointer").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
67 String created = result
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
68 .getBinding("created").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
69 Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
70 retAnnots.add(annot);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
71 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
72 } catch (Exception e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
73 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
74 throw new TripleStoreSearchError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
75 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
76 // TODO Auto-generated method stub
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
77 return retAnnots;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
78 }
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
79
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
80 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
81 * Sucht im Triplestore nach Annotationen
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
82 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
83 * @param uri
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
84 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
85 * oder NULL oder leer
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
86 * @param user
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
87 * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
88 * wurden.
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
89 * @param limit
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
90 * @param offset
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
91 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
92 * @throws TripleStoreHandlerException
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
93 * @throws TripleStoreSearchError
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
94 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
95 public List<Annotation> searchByUriUser(String uri, String user, String limit, String offset)
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
96 throws TripleStoreHandlerException, TripleStoreSearchError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
97
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
98 List<Annotation> retAnnots = new ArrayList<Annotation>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
99 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
100 String queryString = "";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
101
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
102 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
103 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
104
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
105 if (uri != null && !uri.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
106 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.",
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
107 uri);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
108 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
109 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
110 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
111 whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
112 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
113
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
114 if (user != null && !user.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
115 if (user.startsWith("http")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
116 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> <%s>.", user);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
117 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
118 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".", user);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
119 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
120 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
121 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author.");
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
122 }
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
123
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
124 whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#created> ?created.");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
125
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
126 whereString += String
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
127 .format(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
128
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
129 queryString = String.format("select distinct * where {%s}", whereString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
130
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
131 logger.debug("RDFSearcher:" + queryString);
0
dwinter
parents:
diff changeset
132
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
133 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
134 TupleQueryResult results = th.querySPARQL(queryString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
135
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
136 while (results.hasNext()) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
137 BindingSet result = results.next();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
138 String annotUri;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
139 if (uri != null && !uri.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
140 annotUri = uri;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
141 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
142 annotUri = result.getBinding("uri").getValue().stringValue();
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
143 }
0
dwinter
parents:
diff changeset
144
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
145 String annotUser;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
146 if (user != null && !user.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
147 annotUser = user;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
148 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
149 annotUser = result.getBinding("author").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
150 }
0
dwinter
parents:
diff changeset
151
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
152 String textString = "";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
153 if (result.getBinding("text") != null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
154 textString = result.getBinding("text").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
155 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
156 Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), annotUser, result
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
157 .getBinding("created").getValue().stringValue(), textString, null, annotUri, result.getBinding("s")
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
158 .getValue().stringValue());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
159 retAnnots.add(annot);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
160 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
161 } catch (Exception e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
162 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
163 throw new TripleStoreSearchError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
164 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
165 // TODO Auto-generated method stub
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
166 return retAnnots;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
167 }
0
dwinter
parents:
diff changeset
168
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
169 public void deleteById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
170 if (id == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
171 return;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
172 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
173 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
174 // delete triples with id as subject
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
175 /* wish Virtuoso would speak SparQL1.1...
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
176 String queryString = String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
177 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
178 String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
0
dwinter
parents:
diff changeset
179
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
180 logger.debug("RDFSearcher:" + queryString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
181
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
182 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
183 th.querySPARQL(queryString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
184 } catch (Exception e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
185 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
186 throw new TripleStoreSearchError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
187 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
188 }
0
dwinter
parents:
diff changeset
189 }