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.apache.tiles.context.ListAttribute;
|
|
8 import org.openrdf.query.BindingSet;
|
|
9 import org.openrdf.query.TupleQueryResult;
|
|
10 import org.restlet.Context;
|
|
11 import org.restlet.engine.component.ChildContext;
|
|
12
|
|
13 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
|
|
14 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
|
|
15 import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
|
|
16 import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler;
|
|
17
|
|
18 public class RDFSearcher {
|
|
19
|
|
20 private String urlBase="http://ontologies.mpiwg-berlin.mpg.de/annotations/"; //TODO should go into config
|
|
21
|
|
22 private TripleStoreHandler th;
|
|
23
|
|
24 private String context;
|
|
25
|
|
26 private Logger logger= Logger.getRootLogger();
|
|
27
|
|
28 public RDFSearcher(String context) {
|
|
29 this.context=context;
|
|
30 }
|
|
31
|
|
32
|
|
33
|
|
34 public List<Annotation> search(String uri, String user, String limit,
|
|
35 String offset) throws TripleStoreHandlerException, TripleStoreSearchError {
|
|
36
|
|
37 List<Annotation> retAnnots = new ArrayList<Convert.Annotation>();
|
|
38 ChildContext context = (ChildContext)Context.getCurrent();
|
|
39 String tripleStoreUser = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser");
|
|
40 String tripleStorePW = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword");
|
|
41
|
|
42 th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", tripleStoreUser, tripleStorePW);//TODO should go into config
|
|
43 String queryString="";
|
|
44
|
|
45 String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
|
|
46 //whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
|
|
47
|
|
48 if(uri!=null && !uri.equals("")){
|
|
49 whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> <%s>.",uri);}
|
|
50 else {
|
|
51 whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?uri.");
|
|
52 }
|
|
53 whereString +=String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.");
|
|
54 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.");
|
|
55
|
|
56 if(user!=null && !user.equals("")){
|
|
57 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".",user);
|
|
58 } else {
|
|
59 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author.");
|
|
60 }
|
|
61
|
|
62 whereString +=String.format("?s <http://www.w3.org/2000/10/annotation-ns#created> ?created.");
|
|
63
|
|
64 whereString +=String.format("?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.");
|
|
65
|
|
66 queryString=String.format("select distinct * where {%s}",whereString);
|
|
67
|
|
68
|
|
69 logger.debug("RDFSearcher:"+queryString);
|
|
70
|
|
71 try {
|
|
72 TupleQueryResult results = th.querySPARQL(queryString);
|
|
73
|
|
74 while (results.hasNext()) {
|
|
75 BindingSet result = results.next();
|
|
76 String annotUri;
|
|
77 if(uri!=null && !uri.equals("")){
|
|
78 annotUri=uri;
|
|
79 } else {
|
|
80 annotUri = result.getBinding("uri").getValue().stringValue();
|
|
81 }
|
|
82
|
|
83 String annotUser;
|
|
84 if(user!=null && !user.equals("")){
|
|
85 annotUser=user;
|
|
86 } else {
|
|
87 annotUser = result.getBinding("author").getValue().stringValue();
|
|
88 }
|
|
89
|
|
90
|
|
91 Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(),
|
|
92 annotUser, result.getBinding("created").getValue().stringValue(),
|
|
93 result.getBinding("text").getValue().stringValue(), null,
|
|
94 annotUri);
|
|
95 retAnnots.add(annot);
|
|
96 }
|
|
97 } catch (Exception e) {
|
|
98 throw new TripleStoreSearchError();
|
|
99 }
|
|
100 // TODO Auto-generated method stub
|
|
101 return retAnnots;
|
|
102 }
|
|
103
|
|
104 }
|