Strabon

changeset 880:79951cf9c1e1 temporals

Strabon endpoint now supports storing quads both from direct and URI input.
author Panayiotis Smeros <psmeros@di.uoa.gr>
date Tue Feb 19 20:09:27 2013 +0200 (2013-02-19)
parents 4762a336fc31
children 8b8c2efcc293
files endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java
line diff
     1.1 --- a/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Fri Feb 15 19:58:29 2013 +0200
     1.2 +++ b/endpoint/src/main/java/eu/earthobservatory/org/StrabonEndpoint/StrabonBeanWrapper.java	Tue Feb 19 20:09:27 2013 +0200
     1.3 @@ -11,20 +11,26 @@
     1.4  
     1.5  import eu.earthobservatory.utils.Format;
     1.6  
     1.7 +import java.io.ByteArrayInputStream;
     1.8  import java.io.IOException;
     1.9 +import java.io.InputStream;
    1.10  import java.io.OutputStream;
    1.11  import java.io.StringReader;
    1.12  import java.net.URL;
    1.13 +import java.text.ParseException;
    1.14  import java.util.ArrayList;
    1.15 +import java.util.Collection;
    1.16  import java.util.Iterator;
    1.17  import java.util.List;
    1.18  import java.util.regex.Matcher;
    1.19  import java.util.regex.Pattern;
    1.20  
    1.21  import org.openrdf.model.Resource;
    1.22 +import org.openrdf.model.Statement;
    1.23  import org.openrdf.query.MalformedQueryException;
    1.24  import org.openrdf.query.QueryEvaluationException;
    1.25  import org.openrdf.query.TupleQueryResultHandlerException;
    1.26 +import org.openrdf.query.algebra.evaluation.function.temporal.stsparql.relation.TemporalConstants;
    1.27  import org.openrdf.repository.RepositoryException;
    1.28  import org.openrdf.repository.sail.SailRepositoryConnection;
    1.29  import org.openrdf.rio.RDFFormat;
    1.30 @@ -32,6 +38,8 @@
    1.31  import org.slf4j.LoggerFactory;
    1.32  
    1.33  import eu.earthobservatory.org.StrabonEndpoint.StrabonBeanWrapperConfiguration;
    1.34 +import eu.earthobservatory.runtime.generaldb.NQuadsParser;
    1.35 +import eu.earthobservatory.runtime.generaldb.NQuadsTranslator;
    1.36  import eu.earthobservatory.runtime.generaldb.Strabon;
    1.37  
    1.38  
    1.39 @@ -259,16 +267,60 @@
    1.40  
    1.41  		try {
    1.42  			// store data
    1.43 +			URL source=null;
    1.44 +			
    1.45  			if (url) {
    1.46 -				URL source = new URL(source_data);
    1.47 +				source = new URL(source_data);
    1.48  				if (source.getProtocol().equalsIgnoreCase(FILE_PROTOCOL)) {
    1.49  					// it would be a security issue if we read from the server's filesystem
    1.50  					throw new IllegalArgumentException("The protocol of the URL should be one of http or ftp.");
    1.51 -				} 
    1.52 -				conn.add(source, "", format, new Resource[1]);
    1.53 -
    1.54 -			} else {
    1.55 -				conn.add(new StringReader(source_data), "", format, new Resource[1]);
    1.56 +				}
    1.57 +			}
    1.58 +				
    1.59 +			if(!format.equals(RDFFormat.NQUADS))
    1.60 +			{
    1.61 +				if (url) {				
    1.62 +					conn.add(source, "", format, new Resource[1]);
    1.63 +	
    1.64 +				} else {
    1.65 +					conn.add(new StringReader(source_data), "", format, new Resource[1]);
    1.66 +				}			
    1.67 +			}
    1.68 +			else
    1.69 +			{
    1.70 +				InputStream in=null;
    1.71 +				if (url) {				
    1.72 +					in= source.openStream();
    1.73 +				} else {
    1.74 +					in= new ByteArrayInputStream(source_data.getBytes());
    1.75 +				}
    1.76 +				//ByteArrayInputStream in = new ByteArrayInputStream();
    1.77 +				NQuadsTranslator translator = new NQuadsTranslator();
    1.78 +							 
    1.79 +				Collection<Statement> statements = translator.translate(in, "");
    1.80 +				for(Statement st: statements)
    1.81 +				{
    1.82 +					String cont = st.getContext().toString();
    1.83 +					 String validPeriod= cont;
    1.84 +					 if(!cont.contains(","))
    1.85 +					 {
    1.86 +						 int i = cont.indexOf('"')+1;
    1.87 +						 int j = cont.lastIndexOf('"');
    1.88 +						 validPeriod = "\"[" + cont.substring(i,j) + "," + cont.substring(i,j) + "]\"^^<"+TemporalConstants.PERIOD; 
    1.89 +						 //validPeriod = cont.replace("]",","+cont.substring(i, j)+"]");		 
    1.90 +					 }
    1.91 +					 
    1.92 +					try {
    1.93 +						Resource newContext = new NQuadsParser().createValidTimeURI(validPeriod);
    1.94 +						conn.add(st.getSubject(), st.getPredicate(), st.getObject(), newContext);
    1.95 +					} catch (ParseException e) {
    1.96 +						logger.error(this.getClass().toString()+": error when constructing the new context");
    1.97 +						e.printStackTrace();
    1.98 +					}
    1.99 +		
   1.100 +				}
   1.101 +				StringReader quadGraphReader = new StringReader(translator.getHandledTriples().toString());
   1.102 +				conn.add(quadGraphReader, "", RDFFormat.NTRIPLES);
   1.103  			}
   1.104  			
   1.105  			logger.info("[StrabonEndpoint] STORE was successful.");