changeset 34:bd414fe235b5 default tip

now with 100% better sparql.
author casties
date Thu, 31 May 2012 21:32:30 +0200
parents e5f5848892a2
children
files src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java
diffstat 1 files changed, 28 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java	Thu May 31 19:08:48 2012 +0200
+++ b/src/main/java/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java	Thu May 31 21:32:30 2012 +0200
@@ -18,6 +18,22 @@
 
     private Logger logger = Logger.getRootLogger();
 
+    static final String baseGraph = " ?annotation <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Annotation>." 
+            + " ?annotation <" + NS.OAC_NS + "hasTarget> ?target." 
+            + " ?annotation <" + NS.OAC_NS + "hasBody> ?body." 
+            + " ?annotation <" + NS.DCTERMS_NS + "creator> ?creator."
+            + " ?annotation <" + NS.DCTERMS_NS + "created> ?created." 
+            + " ?body <" + NS.CNT_NS + "chars> ?text." 
+            + " ?target <" + NS.DCTERMS_NS + "isPartOf> ?uri.";
+    static final String optGraph = " OPTIONAL { ?body <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Body>}"
+            + " OPTIONAL { ?body <" + NS.RDF_NS + "type> <" + NS.CNT_NS + "ContentAsText>}"
+            + " OPTIONAL { ?target <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Target>}";
+    static final String nonOptGraph = " ?body <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Body>."
+            + " ?body <" + NS.RDF_NS + "type> <" + NS.CNT_NS + "ContentAsText>."
+            + " ?target <" + NS.RDF_NS + "type> <" + NS.OAC_NS + "Target>.";
+
+    static final String annotGraph = baseGraph + optGraph;
+
     public RDFSearcher(String context) {
         this.context = context;
     }
@@ -38,7 +54,6 @@
         List<Annotation> retAnnots = new ArrayList<Annotation>();
         TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
         String queryString = "";
-        // query for tuples with id as subject
         StringBuilder whereString = new StringBuilder();
         whereString.append(String.format("<%s> <http://www.openannotation.org/ns/hasTarget> ?target.", id));
         whereString.append("?target <http://www.purl.org/dc/terms/isPartOf> ?uri.");
@@ -46,7 +61,7 @@
         whereString.append("?body <http://www.w3.org/2011/content#chars> ?bodyText.");
         whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/creator> ?userUrl.", id));
         whereString.append(String.format("<%s> <http://www.purl.org/dc/terms/created> ?creationDate", id));
-        
+
         /*
          * StringBuilder whereString = new
          * StringBuilder(String.format("<%s> ?p <http://www.w3.org/2000/10/annotationType#Comment>.", id));
@@ -113,31 +128,21 @@
         TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
         String queryString = "";
 
-        String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
-        // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
+        String whereString = annotGraph;
 
         if (uri != null && !uri.equals("")) {
-            whereString = String.format("?target <http://www.purl.org/dc/terms/isPartOf> <%s>.", uri);
-        } else {
-            whereString = "?target <http://www.purl.org/dc/terms/isPartOf> ?uri.";
+            whereString += String.format("FILTER (?uri = <%s>)", uri);
         }
-        whereString += "?annotation <http://www.openannotation.org/ns/hasTarget> ?target.";
-        whereString += "?annotation <http://www.openannotation.org/ns/hasBody> ?body.";
-        whereString += "?body <http://www.w3.org/2011/content#chars> ?bodyText.";
 
         if (user != null && !user.equals("")) {
             if (user.startsWith("http")) {
-                whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> <%s>", user);
+                whereString += String.format("FILTER (?creator = <%s>)", user);
             } else {
-                whereString += String.format("?annotation <http://www.purl.org/dc/terms/creator> \"%s\".", user);
+                whereString += String.format("FILTER (?creator = \"%s\")", user);
             }
-        } else {
-            whereString += "?annotation <http://www.purl.org/dc/terms/creator> ?userUrl.";
         }
 
-        whereString += "?annotation <http://www.purl.org/dc/terms/created> ?creationDate";
-
-        queryString = String.format("select * from <%s> where {%s}", context, whereString);
+        queryString = String.format("SELECT * FROM <%s> WHERE {%s}", context, whereString);
 
         logger.debug("RDFSearcher:" + queryString);
 
@@ -152,20 +157,18 @@
                 } else {
                     annotatedUri = result.getBinding("uri").getValue().stringValue();
                 }
-
                 String annotUser;
                 if (user != null && !user.equals("")) {
                     annotUser = user;
                 } else {
-                    annotUser = result.getBinding("userUrl").getValue().stringValue();
+                    annotUser = result.getBinding("creator").getValue().stringValue();
                 }
-
                 String textString = "";
-                if (result.getBinding("bodyText") != null) {
-                    textString = result.getBinding("bodyText").getValue().stringValue();
+                if (result.getBinding("text") != null) {
+                    textString = result.getBinding("text").getValue().stringValue();
                 }
                 String xpointer = result.getBinding("target").getValue().stringValue();
-                String time = result.getBinding("creationDate").getValue().stringValue();
+                String time = result.getBinding("created").getValue().stringValue();
                 String annotationUri = result.getBinding("annotation").getValue().stringValue();
                 Annotation annot = new Annotation(xpointer, annotUser, time, textString, null, annotatedUri, annotationUri);
                 retAnnots.add(annot);
@@ -188,7 +191,8 @@
          * wish Virtuoso would speak SparQL1.1... String queryString =
          * String.format("WITH <%s> DELETE { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
          */
-        String queryString = String.format("DELETE FROM <%s> { <%s> ?p ?o } WHERE { <%s> ?p ?o }", context, id, id);
+        String whereString = annotGraph + String.format(" FILTER(?annotation = <%s>)", id);
+        String queryString = String.format("DELETE FROM <%s> {%s} WHERE {%s}", context, baseGraph+nonOptGraph, whereString);
 
         logger.debug("RDFSearcher:" + queryString);
 
@@ -199,93 +203,6 @@
             throw new TripleStoreSearchError();
         }
     }
-    /**
-     * Sucht im Triplestore nach Annotationen
-     * 
-     * @param uri
-     *            Adresse der Annotierten Ressource, in der Regel nicht mit dem xpointer, sonder die URI der kompletten Ressource
-     *            oder NULL oder leer
-     * @param user
-     *            Author der Annotationen, entweder als uri der Person oder ein Username, je nachdem wie die Annotatinen angelegt
-     *            wurden.
-     * @param limit
-     * @param offset
-     * @return
-     * @throws TripleStoreHandlerException
-     * @throws TripleStoreSearchError
-     */
-    public List<Annotation> searchByUriUserAlt(String uri, String user, String limit, String offset)
-            throws TripleStoreHandlerException, TripleStoreSearchError {
 
-        List<Annotation> retAnnots = new ArrayList<Annotation>();
-        TripleStoreHandler th = TripleStoreConnection.newTripleStoreHandler();
-        String queryString = "";
-
-        String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
-        // whereString +="?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/docuviewerText> ?link.";
-
-        if (uri != null && !uri.equals("")) {
-            whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> <%s>.",
-                    uri);
-        } else {
-            whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/annotatesDocuviewerText> ?uri.");
-        }
-        whereString += String.format("?s <http://ontologies.mpiwg-berlin.mpg.de/annotations/textSelection> ?xpointer.");
-        whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#body> ?annotText.");
-
-        if (user != null && !user.equals("")) {
-            if (user.startsWith("http")) {
-                whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> <%s>.", user);
-            } else {
-                whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> \"%s\".", user);
-            }
-        } else {
-            whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#author> ?author.");
-        }
-
-        whereString += String.format("?s <http://www.w3.org/2000/10/annotation-ns#created> ?created.");
-
-        whereString += String
-                .format(" OPTIONAL {?annotText <http://ontologies.mpiwg-berlin.mpg.de/annotations/containsText> ?text.}");
-
-        queryString = String.format("select distinct * where {%s}", whereString);
-
-        logger.debug("RDFSearcher:" + queryString);
-
-        try {
-            TupleQueryResult results = th.querySPARQL(queryString);
-
-            while (results.hasNext()) {
-                BindingSet result = results.next();
-                String annotUri;
-                if (uri != null && !uri.equals("")) {
-                    annotUri = uri;
-                } else {
-                    annotUri = result.getBinding("uri").getValue().stringValue();
-                }
-
-                String annotUser;
-                if (user != null && !user.equals("")) {
-                    annotUser = user;
-                } else {
-                    annotUser = result.getBinding("author").getValue().stringValue();
-                }
-
-                String textString = "";
-                if (result.getBinding("text") != null) {
-                    textString = result.getBinding("text").getValue().stringValue();
-                }
-                Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), annotUser, result
-                        .getBinding("created").getValue().stringValue(), textString, null, annotUri, result.getBinding("s")
-                        .getValue().stringValue());
-                retAnnots.add(annot);
-            }
-        } catch (Exception e) {
-            e.printStackTrace();
-            throw new TripleStoreSearchError();
-        }
-        // TODO Auto-generated method stub
-        return retAnnots;
-    }
 
 }