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 import org.restlet.Context;
|
|
10 import org.restlet.engine.component.ChildContext;
|
|
11
|
|
12 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
|
13 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
|
5
|
14 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
|
|
15 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
|
0
|
16
|
|
17 public class RDFSearcher {
|
|
18
|
|
19 private String urlBase="http://ontologies.mpiwg-berlin.mpg.de/annotations/"; //TODO should go into config
|
|
20
|
|
21 private TripleStoreHandler th;
|
|
22
|
|
23 private String context;
|
|
24
|
|
25 private Logger logger= Logger.getRootLogger();
|
|
26
|
|
27 public RDFSearcher(String context) {
|
|
28 this.context=context;
|
|
29 }
|
|
30
|
|
31
|
|
32
|
2
|
33 /** Sucht im Triplestore nach Annotationen
|
|
34 * @param uri Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource oder NULL oder leer
|
|
35 * @param user Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt wurden.
|
|
36 * @param limit
|
|
37 * @param offset
|
|
38 * @return
|
|
39 * @throws TripleStoreHandlerException
|
|
40 * @throws TripleStoreSearchError
|
|
41 */
|
0
|
42 public List<Annotation> search(String uri, String user, String limit,
|
|
43 String offset) throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
44
|
|
45 List<Annotation> retAnnots = new ArrayList<Convert.Annotation>();
|
|
46 ChildContext context = (ChildContext)Context.getCurrent();
|
|
47 String tripleStoreUser = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser");
|
|
48 String tripleStorePW = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword");
|
|
49
|
|
50 th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", tripleStoreUser, tripleStorePW);//TODO should go into config
|
|
51 String queryString="";
|
|
52
|
|
53 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
|
|
54 //whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
|
|
55
|
|
56 if(uri!=null && !uri.equals("")){
|
2
|
57 whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.",uri);}
|
0
|
58 else {
|
2
|
59 whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.");
|
0
|
60 }
|
|
61 whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.");
|
|
62 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.");
|
|
63
|
|
64 if(user!=null && !user.equals("")){
|
2
|
65 if (user.startsWith("http")){
|
|
66 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> <%s>.",user);
|
|
67 } else {
|
|
68 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".",user);
|
|
69 }
|
0
|
70 } else {
|
|
71 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author.");
|
|
72 }
|
|
73
|
|
74 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#created> ?created.");
|
|
75
|
5
|
76 whereString +=String.format(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
|
0
|
77
|
|
78 queryString=String.format("select distinct * where {%s}",whereString);
|
|
79
|
|
80
|
|
81 logger.debug("RDFSearcher:"+queryString);
|
|
82
|
|
83 try {
|
|
84 TupleQueryResult results = th.querySPARQL(queryString);
|
|
85
|
|
86 while (results.hasNext()) {
|
|
87 BindingSet result = results.next();
|
|
88 String annotUri;
|
|
89 if(uri!=null && !uri.equals("")){
|
|
90 annotUri=uri;
|
|
91 } else {
|
|
92 annotUri = result.getBinding("uri").getValue().stringValue();
|
|
93 }
|
|
94
|
|
95 String annotUser;
|
|
96 if(user!=null && !user.equals("")){
|
|
97 annotUser=user;
|
|
98 } else {
|
|
99 annotUser = result.getBinding("author").getValue().stringValue();
|
|
100 }
|
|
101
|
5
|
102 String textString="";
|
|
103 if (result.getBinding("text")!=null){
|
|
104 textString= result.getBinding("text").getValue().stringValue();
|
|
105 }
|
0
|
106 Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(),
|
|
107 annotUser, result.getBinding("created").getValue().stringValue(),
|
5
|
108 textString, null,
|
2
|
109 annotUri,result.getBinding("s").getValue().stringValue());
|
0
|
110 retAnnots.add(annot);
|
|
111 }
|
|
112 } catch (Exception e) {
|
5
|
113 e.printStackTrace();
|
0
|
114 throw new TripleStoreSearchError();
|
|
115 }
|
|
116 // TODO Auto-generated method stub
|
|
117 return retAnnots;
|
|
118 }
|
|
119
|
|
120 }
|