view src/de/mpiwg/itgroup/nimanager/persons/PersonService.java @ 2:e3ecb88314a5

minor bugs ontologies added
author dwinter
date Fri, 02 Dec 2011 08:37:03 +0100
parents 1384a0d382fa
children f986e74583eb
line wrap: on
line source

package de.mpiwg.itgroup.nimanager.persons;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.lucene.index.CorruptIndexException;
import org.openrdf.repository.RepositoryException;
import org.restlet.Context;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.engine.component.ChildContext;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.Options;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;
import org.restlet.resource.Get;

import de.mpiwg.itgroup.nimanager.exceptions.TripleStoreHandlerException;
import de.mpiwg.itgroup.nimanager.luceneIndices.Importer;
import de.mpiwg.itgroup.nimanager.owl.MetaDataHandler;
import de.mpiwg.itgroup.nimanager.owl.TripleStoreHandler;
import de.mpiwg.itgroup.nimanager.owl.Util;

public class PersonService extends ServerResource {
		private  String NAMED_ENTITIES_ONTOLOGIE_URL = "http://ontologies.mpiwg-berlin.mpg.de/authorities/namedIdentities";
		private  String VIRTUOSO_PW;
		private  String VIRTUOSO_USER;
		private  String virtuoso_server_url;
		private  String TRIPLE_INDEX_PATH;
		private Logger rl = Logger.getRootLogger();
		private MetaDataHandler mh;
		private TripleStoreHandler th;
		
		
		
		public PersonService(){
			//mh = MetaDataHandler.getInstance();
			ChildContext context = (ChildContext)Context.getCurrent();
			TRIPLE_INDEX_PATH = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.index");
			virtuoso_server_url = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.url");
			VIRTUOSO_PW = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.pw");
			VIRTUOSO_USER = context.getParameters().getFirstValue("de.mpwig.itgroup.personSearch.virtuoso.user");
			
			try {
				th = new TripleStoreHandler(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW);
			} catch (TripleStoreHandlerException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		@Options
		public void doOptions(Representation entity) {
			Form responseHeaders = (Form) getResponse().getAttributes().get(
					"org.restlet.http.headers");
			if (responseHeaders == null) {
				responseHeaders = new Form();
				getResponse().getAttributes().put("org.restlet.http.headers",
						responseHeaders);
			}
			responseHeaders.add("Access-Control-Allow-Origin", "*");
			responseHeaders.add("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
			responseHeaders.add("Access-Control-Allow-Headers", "Content-Type");
			responseHeaders.add("Access-Control-Allow-Credentials", "false");
			responseHeaders.add("Access-Control-Max-Age", "60");
		}
		
		@Get("XML")
		public Representation getXML() throws UnsupportedEncodingException{
			String id = (String)getRequest().getAttributes().get("personId");
			id = URLDecoder.decode(id,"utf-8");
			if (id.startsWith("dbpedia:")){
				String[] splitted=id.split(":");
				return handleDBPedia(splitted[1]);
			} else if (id.startsWith(NAMED_ENTITIES_ONTOLOGIE_URL)){
				String[] splitted=id.split(":");
				return handlePerson(id);
			}
			getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
			return new StringRepresentation("<error>not supported</error>",MediaType.TEXT_XML);
			
		}

		private Representation handlePerson(String id) {
			String personString =getPerson(id);
			return new StringRepresentation(personString, MediaType.TEXT_XML);
			
		}

	

		private Representation handleDBPedia(String id) {
			String personString = getPerson("<http://dbpedia.org/resource/"+id+">");
			return new StringRepresentation(personString, MediaType.TEXT_XML);
			
	
		}
		
		
		private String getPerson(String id) {
			Map<String, Object> values=null;
			try {
				//Collection props = mh.getDirectPropsForClass("http://xmlns.com/foaf/0.1/Person");
				Collection props = mh.getDirectPropsForClass("http://dbpedia.org/ontology/Person",true);
				values = th.getJenaRDFValues(props, id, mh.getOwlModel());
			} catch (RepositoryException e) {
				e.printStackTrace();
				return null;
			}
			
			String xmlVersion = Util.rdf2xml(values);
			
			
			return xmlVersion;
		}
		
		/**
		 * different modes :
		 *
		 *Indizierte alle werte des triples mit "personId"
		 *Im Body kann eine Liste von Predicate Ÿbegeben werden die indiziert werden sollen und sich im triplestore befinden mŸssen.
		 * Wird kein body Ÿbergeben ist das predicate="http://xmlns.com/foaf/0.1/name".
		 * Wird graphId zusŠtzlich Ÿbergeben, wird der Eintrag aus diesem Graphen indiziert, default ist: "file://newpersonsFromProjects"
		 * @param entity
		 * @return
		 */
		@Put
		public Representation doPut(Representation entity){
			String id = (String) getRequest().getAttributes().get("personId");
			String graphId = (String) getRequest().getAttributes().get("graphId");
			
			try {
				id = URLDecoder.decode(id,"utf-8");
			
			} catch (UnsupportedEncodingException e1) {
				setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
				return new StringRepresentation("not a valid url:"+id);
				
			}
			if (graphId==null || graphId.equals("")){
				graphId="file://newpersonsFromProjects";
			} else {
				try {
					graphId = URLDecoder.decode(graphId,"utf-8");
				} catch (UnsupportedEncodingException e) {
					setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
					return new StringRepresentation("not a valid graph Id:"+graphId);
					
				}
			}
			String cnt;
			try {
				cnt = entity.getText();
				rl.info(cnt);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				cnt="";
			}
			
			if (cnt.equals("")){
				cnt="http://xmlns.com/foaf/0.1/name";
			}
			String[] predicates = cnt.split("\n");
			
			int indexedTriples =doIndexing(graphId,id,predicates);
			if (indexedTriples<0){
				setStatus(Status.SERVER_ERROR_INTERNAL);
				return new StringRepresentation("Konnte nicht indizieren!");
			}
			return new StringRepresentation(String.valueOf(indexedTriples));
	
			//setStatus(Status.CLIENT_ERROR_NOT_ACCEPTABLE);
			//return new StringRepresentation("Mode not known!");
		}
		

		private int doIndexing(String graphId,String identifier, String[] predicates) {
			TripleStoreHandler th;
			try {
				th = new TripleStoreHandler(virtuoso_server_url, VIRTUOSO_USER, VIRTUOSO_PW);
			} catch (TripleStoreHandlerException e) {
				rl.error("indexing",e);
				return 0;
			}
			Importer im = new Importer(graphId, predicates, th, TRIPLE_INDEX_PATH); // open the indexer
			try {
				im.initializeIndexer(false); //initialize
			} catch (IOException e1) {
				rl.error("indexing",e1);
				return 0;
			} 
			int count=0;
			try {
				count = im.writeStatementsToIndex(identifier);
			} catch (RepositoryException e) {
				rl.error("do Import",e);
				 count=-1;
			} catch (CorruptIndexException e) {
				rl.error("do Import",e);
				count=-1;
			} catch (IOException e) {
				rl.error("do Import",e);
				count=-1;
			}
			
			try {
				im.close();
			} catch (CorruptIndexException e) {
				rl.error("do Import",e);
				count=-1;
			} catch (IOException e) {
				rl.error("do Import",e);
				count=-1;
			}
			
			return count;
			
		}
}