package it.unibz.krdb.sql;

import it.unibz.krdb.obda.io.ModelIOManager;
import it.unibz.krdb.obda.model.OBDADataFactory;
import it.unibz.krdb.obda.model.OBDAModel;
import it.unibz.krdb.obda.model.impl.OBDADataFactoryImpl;
import it.unibz.krdb.obda.owlrefplatform.core.Quest;
import it.unibz.krdb.obda.owlrefplatform.core.QuestConstants;
import it.unibz.krdb.obda.owlrefplatform.core.QuestPreferences;
import it.unibz.krdb.obda.owlrefplatform.owlapi3.QuestOWL;
import it.unibz.krdb.obda.owlrefplatform.owlapi3.QuestOWLConnection;
import it.unibz.krdb.obda.owlrefplatform.owlapi3.QuestOWLFactory;
import it.unibz.krdb.obda.owlrefplatform.owlapi3.QuestOWLResultSet;
import it.unibz.krdb.obda.owlrefplatform.owlapi3.QuestOWLStatement;
import it.unibz.krdb.obda.sesame.r2rml.R2RMLReader;

import java.io.File;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;
import org.semanticweb.owlapi.reasoner.SimpleConfiguration;

import com.google.common.base.Joiner;

/**
 *
 * @author dimitris
 */
public class ADPOntopTest {
	
	final String owlfile = "src/test/resources/adp/npd-ql.owl";
	final String obdafile = "src/test/resources/adp/mapping-fed.obda";
	final String queryfile = "src/test/resources/adp/01.q";
	final String r2rmlfile = "src/test/resources/adp/mapping-fed.ttl";

	public void runQuery() throws Exception {

		  Class.forName("madgik.adp.federatedjdbc.AdpDriver");
		  
		  String conString="jdbc:adp:http://whale.di.uoa.gr:9090/datasets/npd-dataset";

		  Connection conn2 = DriverManager.getConnection(
		    		conString,
		            "adp",
		            "adp");
		    
		  Statement adpSt=conn2.createStatement();
		  adpSt.executeQuery("addFederatedEndpoint(NPD, jdbc:mysql://whale.di.uoa.gr:3306/newnpd, com.mysql.jdbc.Driver, optique, gray769watt724)");

		/*
		 * Load the ontology from an external .owl file.
		 */
		OWLOntologyManager manager = OWLManager.createOWLOntologyManager();
		OWLOntology ontology = manager.loadOntologyFromOntologyDocument(new File(owlfile));

		/*
		 * Load the OBDA model from an external .obda file
		 */
		
		OBDADataFactory fac = OBDADataFactoryImpl.getInstance();
		OBDAModel obdaModel = fac.getOBDAModel();
		ModelIOManager ioManager = new ModelIOManager(obdaModel);
		ioManager.load(obdafile);
		
		
		/*
		 * Prepare the configuration for the Quest instance. The example below shows the setup for
		 * "Virtual ABox" mode
		 */
		QuestPreferences preferences = new QuestPreferences();
		preferences.setCurrentValueOf(QuestPreferences.ABOX_MODE, QuestConstants.VIRTUAL);

		
		/*
		 * Create the instance of Quest OWL reasoner.
		 */
		QuestOWLFactory factory = new QuestOWLFactory();
		factory.setOBDAController(obdaModel);
		factory.setPreferenceHolder(preferences);
		QuestOWL reasoner = (QuestOWL) factory.createReasoner(ontology, new SimpleConfiguration());

		/*
		 * Prepare the data connection for querying.
		 */
		QuestOWLConnection conn = reasoner.getConnection();
		QuestOWLStatement st = conn.createStatement();

		String sparqlQuery = Joiner.on("\n").join( 
				Files.readAllLines(Paths.get(queryfile), Charset.defaultCharset())); 
				
				

		try {
			QuestOWLResultSet rs = st.executeTuple(sparqlQuery);
			int columnSize = rs.getColumCount();
			while (rs.nextRow()) {
				for (int idx = 1; idx <= columnSize; idx++) {
					OWLObject binding = rs.getOWLObject(idx);
					System.out.print(binding.toString() + ", ");
				}
				System.out.print("\n");
			}
			rs.close();

			/*
			 * Print the query summary
			 */
			QuestOWLStatement qst = (QuestOWLStatement) st;
			String sqlQuery = qst.getUnfolding(sparqlQuery);

			System.out.println();
			System.out.println("The input SPARQL query:");
			System.out.println("=======================");
			System.out.println(sparqlQuery);
			System.out.println();
			
			System.out.println("The output SQL query:");
			System.out.println("=====================");
			System.out.println(sqlQuery);
			
		} finally {
			
			/*
			 * Close connection and resources
			 */
			if (st != null && !st.isClosed()) {
				st.close();
			}
			if (conn != null && !conn.isClosed()) {
				conn.close();
			}
			reasoner.dispose();
		}
	}

  public static void main(String[] args) throws Exception {
	  new ADPOntopTest().runQuery();
	    
  }
}

@@ -799,16 +799,43 @@
 o.File;%0A
+import java.io.FileReader;%0A
 import j
@@ -1353,16 +1353,57 @@
 .Joiner;
+%0Aimport com.google.common.io.CharStreams;
 %0A%0A/**%0A *
@@ -1418,16 +1418,8 @@
 hor 
-dimitris
 %0A */
@@ -1754,460 +1754,10 @@
 %0A%0A%09%09
-  Class.forName(%22madgik.adp.federatedjdbc.AdpDriver%22);%0A%09%09  %0A%09%09  String conString=%22jdbc:adp:http://whale.di.uoa.gr:9090/datasets/npd-dataset%22;%0A%0A%09%09  Connection conn2 = DriverManager.getConnection(%0A%09%09    %09%09conString,%0A%09%09            %22adp%22,%0A%09%09            %22adp%22);%0A%09%09    %0A%09%09  Statement adpSt=conn2.createStatement();%0A%09%09  adpSt.executeQuery(%22addFederatedEndpoint(NPD, jdbc:mysql://whale.di.uoa.gr:3306/newnpd, com.mysql.jdbc.Driver, optique, gray769watt724)%22);
 %0A
+%09
 %0A%09%09/
@@ -3010,94 +3010,107 @@
 oin(
- 
 %0A%09%09%09%09
-File
+CharStream
 s.read
-All
 Lines(
-Paths.get(queryfile), Charset.defaultCharset())
+new FileReader(queryfile))); %0A%09%09%0A%09%09//System.out.println(sparqlQuery
 );
- 
 %0A%09%09
-%09%09%0A%09%09%09%09%0A
 %0A%09%09t

update test