view src/de/mpiwg/itgroup/annotationManager/RDFHandling/RDFSearcher.java @ 2:6888ae3287b8

changed to namespace handling for users
author dwinter
date Thu, 24 Nov 2011 11:39:12 +0100
parents 77530be3c747
children 0be9d53a6967
line wrap: on
line source

package de.mpiwg.itgroup.annotationManager.RDFHandling;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.tiles.context.ListAttribute;
import org.openrdf.query.BindingSet;
import org.openrdf.query.TupleQueryResult;
import org.restlet.Context;
import org.restlet.engine.component.ChildContext;

import sun.security.action.GetBooleanAction;

import de.mpiwg.itgroup.annotationManager.Errors.TripleStoreSearchError;
import de.mpiwg.itgroup.annotationManager.RDFHandling.Convert.Annotation;
import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler;

public class RDFSearcher {

	private String urlBase="http://ontologies.mpiwg-berlin.mpg.de/annotations/"; //TODO should go into config

	private TripleStoreHandler th;

	private String context;

	private Logger logger= Logger.getRootLogger();

	public RDFSearcher(String context) {
		this.context=context;
	}



	/** 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> search(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");
		
		th = new TripleStoreHandler("jdbc:virtuoso://virtuoso.mpiwg-berlin.mpg.de:1111", tripleStoreUser, tripleStorePW);//TODO should go into config
		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("?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();
				}
				
				
				Annotation annot = new Annotation(result.getBinding("xpointer").getValue().stringValue(), 
						annotUser, result.getBinding("created").getValue().stringValue(), 
						result.getBinding("text").getValue().stringValue(), null,
						annotUri,result.getBinding("s").getValue().stringValue());
				retAnnots.add(annot);
			}
		} catch (Exception e) {
			throw new TripleStoreSearchError();
		}
		// TODO Auto-generated method stub
		return retAnnots;
	}

}