annotate 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
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
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
10 import de.mpiwg.itgroup.annotationManager.Constants.NS;
0
dwinter
parents:
diff changeset
11 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
5
0be9d53a6967 editor for annotations
dwinter
parents: 2
diff changeset
12 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
0be9d53a6967 editor for annotations
dwinter
parents: 2
diff changeset
13 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
0
dwinter
parents:
diff changeset
14
dwinter
parents:
diff changeset
15 public class RDFSearcher {
dwinter
parents:
diff changeset
16
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
17 private String context;
0
dwinter
parents:
diff changeset
18
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
19 private Logger logger = Logger.getRootLogger();
0
dwinter
parents:
diff changeset
20
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
21 public RDFSearcher(String context) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
22 this.context = context;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
23 }
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
24
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
25 /**
30
b37487b756ac still working on new annotations...
casties
parents: 23
diff changeset
26 * Retrieves Annotations by ID.
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
27 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
28 * @param id
30
b37487b756ac still working on new annotations...
casties
parents: 23
diff changeset
29 * id of the Annotation
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
30 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
31 * @throws TripleStoreHandlerException
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
32 * @throws TripleStoreSearchError
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
33 */
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
34 public List<Annotation> searchById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
35 if (id == null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
36 return null;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
37 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
38 List<Annotation> retAnnots = new ArrayList<Annotation>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
39 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
40 String queryString = "";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
41 // query for tuples with id as subject
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
42 StringBuilder whereString = new StringBuilder();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
43 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasTarget> ?target.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
44 whereString.append("?target <http://www.purl.org/dc/terms/isPartOf> ?uri.");
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
45 whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasBody> ?body.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
46 whereString.append("?body <http://www.w3.org/2011/content#chars> ?bodyText.");
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
47 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/creator> ?userUrl.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
48 whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/created> ?creationDate", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
49
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
50 /*
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
51 * StringBuilder whereString = new
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
52 * StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
53 * whereString.append(String
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
54 * .format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
55 * whereString.append(String.format("<%s> <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.",
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
56 * id)); whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
57 * whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#author> ?author.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
58 * whereString.append(String.format("<%s> <http://www.w3.org/2000/10/annotation-ns#created> ?created.", id));
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
59 * whereString.append(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
60 */
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
61
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
62 queryString = String.format("select * from <%s> where {%s}", context, whereString);
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
63
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
64 logger.debug("RDFSearcher:" + queryString);
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
65
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
66 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
67 TupleQueryResult results = th.querySPARQL(queryString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
68 while (results.hasNext()) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
69 BindingSet result = results.next();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
70 String annotUri = result.getBinding("uri").getValue().stringValue();
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
71 String annotUser = result.getBinding("userUrl").getValue().stringValue();
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
72 String textString = "";
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
73 if (result.getBinding("bodyText") != null) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
74 textString = result.getBinding("bodyText").getValue().stringValue();
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
75 }
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
76
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
77 String xpointer = result.getBinding("target").getValue().stringValue();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
78 String created = result.getBinding("creationDate").getValue().stringValue();
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
79 Annotation annot = new Annotation(xpointer, annotUser, created, textString, null, annotUri, id);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
80 retAnnots.add(annot);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
81 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
82 } catch (Exception e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
83 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
84 throw new TripleStoreSearchError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
85 }
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
86 return retAnnots;
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
87 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
88
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
89 /**
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
90 * Sucht im Triplestore nach Annotationen
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
91 *
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
92 * select * from <file:///annotations2> where { ?target <http://www.purl.org/dc/terms/isPartOf>
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
93 * <file:///Volumes/Schlachteplatte/Users/casties/Library/Eclipse/annotator/dev.html>. ?ann
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
94 * <http://www.openannotation.org/ns/hasTarget> ?target. ?ann <http://www.openannotation.org/ns/hasBody> ?body. ?body
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
95 * <http://www.w3.org/2011/content#chars> ?bodyText. }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
96 *
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
97 * @param uri
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
98 * Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
99 * oder NULL oder leer
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
100 * @param user
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
101 * Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
102 * wurden.
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
103 * @param limit
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
104 * @param offset
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
105 * @return
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
106 * @throws TripleStoreHandlerException
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
107 * @throws TripleStoreSearchError
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
108 */
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
109 public List<Annotation> searchByUriUser(String uri, String user, String limit, String offset)
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
110 throws TripleStoreHandlerException, TripleStoreSearchError {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
111
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
112 List<Annotation> retAnnots = new ArrayList<Annotation>();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
113 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
114 String queryString = "";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
115
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
116 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
117 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
118
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
119 if (uri != null && !uri.equals("")) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
120 whereString = String.format("?target <http://www.purl.org/dc/terms/isPartOf> <%s>.", uri);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
121 } else {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
122 whereString = "?target <http://www.purl.org/dc/terms/isPartOf> ?uri.";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
123 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
124 whereString += "?annotation <http://www.openannotation.org/ns/hasTarget> ?target.";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
125 whereString += "?annotation <http://www.openannotation.org/ns/hasBody> ?body.";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
126 whereString += "?body <http://www.w3.org/2011/content#chars> ?bodyText.";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
127
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
128 if (user != null && !user.equals("")) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
129 if (user.startsWith("http")) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
130 whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> <%s>", user);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
131 } else {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
132 whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> \"%s\".", user);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
133 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
134 } else {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
135 whereString += "?annotation <http://www.purl.org/dc/terms/creator> ?userUrl.";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
136 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
137
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
138 whereString += "?annotation <http://www.purl.org/dc/terms/created> ?creationDate";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
139
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
140 queryString = String.format("select * from <%s> where {%s}", context, whereString);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
141
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
142 logger.debug("RDFSearcher:" + queryString);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
143
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
144 try {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
145 TupleQueryResult results = th.querySPARQL(queryString);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
146
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
147 while (results.hasNext()) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
148 BindingSet result = results.next();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
149 String annotatedUri;
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
150 if (uri != null && !uri.equals("")) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
151 annotatedUri = uri;
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
152 } else {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
153 annotatedUri = result.getBinding("uri").getValue().stringValue();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
154 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
155
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
156 String annotUser;
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
157 if (user != null && !user.equals("")) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
158 annotUser = user;
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
159 } else {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
160 annotUser = result.getBinding("userUrl").getValue().stringValue();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
161 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
162
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
163 String textString = "";
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
164 if (result.getBinding("bodyText") != null) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
165 textString = result.getBinding("bodyText").getValue().stringValue();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
166 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
167 String xpointer = result.getBinding("target").getValue().stringValue();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
168 String time = result.getBinding("creationDate").getValue().stringValue();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
169 String annotationUri = result.getBinding("annotation").getValue().stringValue();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
170 Annotation annot = new Annotation(xpointer, annotUser, time, textString, null, annotatedUri, annotationUri);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
171 retAnnots.add(annot);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
172 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
173 } catch (Exception e) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
174 e.printStackTrace();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
175 throw new TripleStoreSearchError();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
176 }
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
177 // TODO Auto-generated method stub
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
178 return retAnnots;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
179 }
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
180
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
181 public void deleteById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
182 if (id == null) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
183 return;
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
184 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
185 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
186 // delete triples with id as subject
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
187 /*
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
188 * wish Virtuoso would speak SparQL1.1... String queryString =
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
189 * String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
190 */
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
191 String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
192
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
193 logger.debug("RDFSearcher:" + queryString);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
194
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
195 try {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
196 th.querySPARQL(queryString);
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
197 } catch (Exception e) {
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
198 e.printStackTrace();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
199 throw new TripleStoreSearchError();
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
200 }
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
201 }
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
202 /**
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
203 * Sucht im Triplestore nach Annotationen
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
204 *
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
205 * @param uri
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
206 * 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
207 * oder NULL oder leer
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
208 * @param user
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
209 * 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
210 * wurden.
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
211 * @param limit
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
212 * @param offset
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
213 * @return
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
214 * @throws TripleStoreHandlerException
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
215 * @throws TripleStoreSearchError
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
216 */
33
e5f5848892a2 new annotation model basically works.
casties
parents: 30
diff changeset
217 public List<Annotation> searchByUriUserAlt(String uri, String user, String limit, String offset)
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
218 throws TripleStoreHandlerException, TripleStoreSearchError {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
219
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
220 List<Annotation> retAnnots = new ArrayList<Annotation>();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
221 TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
222 String queryString = "";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
223
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
224 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
225 // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
226
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
227 if (uri != null && !uri.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
228 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
229 uri);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
230 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
231 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
232 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
233 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
234 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
235
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
236 if (user != null && !user.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
237 if (user.startsWith("http")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
238 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
239 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
240 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
241 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
242 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
243 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
244 }
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
245
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
246 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
247
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
248 whereString += String
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
249 .format(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
250
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
251 queryString = String.format("select distinct * where {%s}", whereString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
252
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
253 logger.debug("RDFSearcher:" + queryString);
0
dwinter
parents:
diff changeset
254
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
255 try {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
256 TupleQueryResult results = th.querySPARQL(queryString);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
257
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
258 while (results.hasNext()) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
259 BindingSet result = results.next();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
260 String annotUri;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
261 if (uri != null && !uri.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
262 annotUri = uri;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
263 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
264 annotUri = result.getBinding("uri").getValue().stringValue();
16
667d98fd28bd working on search and update.
casties
parents: 7
diff changeset
265 }
0
dwinter
parents:
diff changeset
266
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
267 String annotUser;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
268 if (user != null && !user.equals("")) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
269 annotUser = user;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
270 } else {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
271 annotUser = result.getBinding("author").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
272 }
0
dwinter
parents:
diff changeset
273
17
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
274 String textString = "";
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
275 if (result.getBinding("text") != null) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
276 textString = result.getBinding("text").getValue().stringValue();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
277 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
278 Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), annotUser, result
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
279 .getBinding("created").getValue().stringValue(), textString, null, annotUri, result.getBinding("s")
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
280 .getValue().stringValue());
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
281 retAnnots.add(annot);
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
282 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
283 } catch (Exception e) {
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
284 e.printStackTrace();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
285 throw new TripleStoreSearchError();
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
286 }
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
287 // TODO Auto-generated method stub
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
288 return retAnnots;
b0ef5c860464 updating and deleting annotations works now!
casties
parents: 16
diff changeset
289 }
0
dwinter
parents:
diff changeset
290
dwinter
parents:
diff changeset
291 }