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