changeset 16:667d98fd28bd

working on search and update.
author casties
date Thu, 22 Mar 2012 10:54:56 +0100
parents 6c7c4140630d
children b0ef5c860464
files src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java src/de/mpiwg/itgroup/annotationManager/restlet/AddAndReadAnnotations.java src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java src/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.java
diffstat 5 files changed, 107 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java	Wed Mar 21 18:12:45 2012 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java	Thu Mar 22 10:54:56 2012 +0100
@@ -10,6 +10,7 @@
 import org.restlet.engine.component.ChildContext;
 
 import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
+import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreStoreError;
 import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
 import de.mpiwg.itgroup.triplestoremanager.exceptions.TripleStoreHandlerException;
 import de.mpiwg.itgroup.triplestoremanager.owl.TripleStoreHandler;
@@ -30,7 +31,98 @@
 
 
 
-	/** Sucht im Triplestore nach Annotationen
+        /** Sucht im Triplestore nach Annotationen.
+         * 
+         * @param id id der Annotation
+         * @return
+         * @throws TripleStoreHandlerException
+         * @throws TripleStoreSearchError
+         */
+        public List<Annotation> searchById(String id) throws TripleStoreHandlerException, TripleStoreSearchError {
+
+                List<Annotation> retAnnots = new ArrayList<Convert.Annotation>();
+                ChildContext context = (ChildContext)Context.getCurrent();
+                String tripleStoreUser = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser");
+                String tripleStorePW = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword");
+                String connectionURL = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreConnectionURL");
+                if (tripleStoreUser == null || tripleStorePW == null || connectionURL == null) {
+                    logger.error(String.format("Missing triplestore parameters! (user=%s pw=%s url=%s)", tripleStoreUser, tripleStorePW, connectionURL));
+                    throw new TripleStoreStoreError();
+                }
+                
+                th = new TripleStoreHandler(connectionURL, tripleStoreUser, tripleStorePW);
+                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 uri = null;
+                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.");
+
+/*                String user = null;
+                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;
+        }
+
+        /** 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
@@ -39,15 +131,20 @@
 	 * @throws TripleStoreHandlerException
 	 * @throws TripleStoreSearchError
 	 */
-	public List<Annotation> search(String uri, String user, String limit,
+	public List<Annotation> searchByUriUser(String uri, String user, String limit,
 			String offset) throws TripleStoreHandlerException, TripleStoreSearchError {
 
 		List<Annotation> retAnnots = new ArrayList<Convert.Annotation>();
 		ChildContext context = (ChildContext)Context.getCurrent();
 		String tripleStoreUser = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUser");
 		String tripleStorePW = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreUserPassword");
+                String connectionURL = context.getParameters().getFirstValue("de.mpiwg.itgroup.annotationManager.virtuoso.tripleStoreConnectionURL");
+                if (tripleStoreUser == null || tripleStorePW == null || connectionURL == null) {
+                    logger.error(String.format("Missing triplestore parameters! (user=%s pw=%s url=%s)", tripleStoreUser, tripleStorePW, connectionURL));
+                    throw new TripleStoreStoreError();
+                }
 		
-		th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", tripleStoreUser, tripleStorePW);//TODO should go into config
+		th = new TripleStoreHandler(connectionURL, tripleStoreUser, tripleStorePW);
 		String queryString="";
 
 		String whereString = "?s ?p <http://www.w3.org/2000/10/annotationType#Comment>.";
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndReadAnnotations.java	Wed Mar 21 18:12:45 2012 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AddAndReadAnnotations.java	Thu Mar 22 10:54:56 2012 +0100
@@ -100,7 +100,7 @@
 				"<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>";
 		try {
 			
-			List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
+			List<Convert.Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
 
 			for (Convert.Annotation annot:annots){
 				
@@ -200,7 +200,7 @@
 		JSONArray ja;
 		try {
 			
-			List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
+			List<Convert.Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
 
 			ja = new JSONArray();
 			for (Convert.Annotation annot:annots){
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java	Wed Mar 21 18:12:45 2012 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorAnnotations.java	Thu Mar 22 10:54:56 2012 +0100
@@ -73,7 +73,7 @@
 
         JSONArray results;
         try {
-            List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
+            List<Convert.Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset);
 
             results = new JSONArray();
             for (Convert.Annotation annot : annots) {
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java	Wed Mar 21 18:12:45 2012 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/AnnotatorSearch.java	Thu Mar 22 10:54:56 2012 +0100
@@ -65,7 +65,7 @@
         JSONArray ja;
         try {
 
-            List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
+            List<Convert.Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset);
 
             ja = new JSONArray();
             for (Convert.Annotation annot : annots) {
@@ -141,7 +141,7 @@
                 + "<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>";
         try {
 
-            List<Convert.Annotation> annots = searcher.search(uri, user, limit, offset);
+            List<Convert.Annotation> annots = searcher.searchByUriUser(uri, user, limit, offset);
 
             for (Convert.Annotation annot : annots) {
 
--- a/src/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.java	Wed Mar 21 18:12:45 2012 +0100
+++ b/src/de/mpiwg/itgroup/annotationManager/restlet/SearchAnnotations.java	Thu Mar 22 10:54:56 2012 +0100
@@ -97,7 +97,7 @@
 				"<td><a href=\"%s\">%s</a></td><td>%s</td><td>%s</td><td><a href=\"%s\">%s</a></td><td><a href=\"%s\">%s</a></td></div>";
 		try {
 			
-			List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
+			List<Convert.Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
 
 			for (Convert.Annotation annot:annots){
 				
@@ -196,7 +196,7 @@
 		JSONArray ja;
 		try {
 			
-			List<Convert.Annotation> annots=searcher.search(uri,user,limit,offset);
+			List<Convert.Annotation> annots=searcher.searchByUriUser(uri,user,limit,offset);
 
 			ja = new JSONArray();
 			for (Convert.Annotation annot:annots){