package xml;

import org.w3c.dom.Document;

import uk.ac.bbk.dcs.automed.xml.XMLConstants;
import uk.ac.bbk.dcs.automed.xml.sra.SchemaRestructuringAlgorithm;
import uk.ac.bbk.dcs.automed.xml.sra.SchemaRestructuringException;
import uk.ac.bbk.dcs.automed.xml.utilities.PrintUtility;
import uk.ac.bbk.dcs.automed.xml.wrappers.DOMWrapper;
import uk.ac.bbk.dcs.automed.xml.wrappers.DOMWrapperFactory;
import uk.ac.bbk.dcs.automed.xml.utilities.XMLUtilities;
import uk.ac.ic.doc.automed.reps.Schema;

/**
 * Class Task Description
 * 
 * @author <a href="mailto:lucas@dcs.bbk.ac.uk">Lucas Zamboulis</a>
 */
public class XMLBankIntegration {

	private static void printSource(String label, Document doc, Document schemaUIDDoc, Schema schema) {
		System.out.println();
		PrintUtility.prettyPrint(doc, label + " XML file:");
		System.out.println();

		System.out.println();
		PrintUtility.prettyPrint(schemaUIDDoc, label + " XML DataSource Schema file:");
		System.out.println();

		PrintUtility.title(label + " XML DataSource Schema");
		schema.listSchema();
		PrintUtility.banner();
		System.out.println();
	}

	private static void deleteOldSchema(String schemaName) {
		try {
			System.out.print("Deleting old schema named " + schemaName + "...");
			Schema sourceSchema = Schema.getSchema(schemaName);
			sourceSchema.removeAccessMethods();
			sourceSchema.retract();
			System.out.println("done.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Schema sourceSchema1 = null, sourceSchema2 = null, targetSchema = null;
		Document source1Doc = null, source2Doc = null, targetDoc = null;
		Document source1SchemaUIDDoc = null, source2SchemaUIDDoc = null, targetSchemaUIDDoc = null;

		String URI1 = "http://www.dcs.bbk.ac.uk/~lucas/automed/examples/bank1.xml";
		String URI2 = "http://www.dcs.bbk.ac.uk/~lucas/automed/examples/bank2.xml";
		String URI3 = "http://www.dcs.bbk.ac.uk/~lucas/automed/examples/bank3.xml";
		String targetSchemaName = "bank1";
		String source1SchemaName = "bank2";
		String source2SchemaName = "bank3";

		// Extract XML DataSource Schema and create target (global) schema
		try {
			if (Schema.exists(targetSchemaName))
				deleteOldSchema(targetSchemaName);

			DOMWrapperFactory xwfTarget = new DOMWrapperFactory();
			DOMWrapper xwTarget = (DOMWrapper) DOMWrapper.selectNewAutoMedWrapper("", "", null, XMLConstants.driverDOM, URI1,
					targetSchemaName, xwfTarget);
			targetDoc = xwTarget.getInputDoc();
			targetSchemaUIDDoc = xwTarget.getSchemaUIDDoc();
			targetSchema = xwTarget.getSchema();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Extract XML DataSource Schema and create the first source schema
		try {
			if (Schema.exists(source1SchemaName))
				deleteOldSchema(source1SchemaName);
			if (Schema.exists(source2SchemaName))
				deleteOldSchema(source2SchemaName);

			DOMWrapperFactory xwfSource = new DOMWrapperFactory();

			DOMWrapper xwSource1 = (DOMWrapper) DOMWrapper.selectNewAutoMedWrapper("", "", null, XMLConstants.driverDOM, URI2,
					source1SchemaName, xwfSource);
			source1Doc = xwSource1.getInputDoc();
			source1SchemaUIDDoc = xwSource1.getSchemaUIDDoc();
			sourceSchema1 = xwSource1.getSchema();

			DOMWrapper xwSource2 = (DOMWrapper) DOMWrapper.selectNewAutoMedWrapper("", "", null, XMLConstants.driverDOM, URI3,
					source2SchemaName, xwfSource);
			source2Doc = xwSource2.getInputDoc();
			source2SchemaUIDDoc = xwSource2.getSchemaUIDDoc();
			sourceSchema2 = xwSource2.getSchema();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// Print input XML files, XML Datasource Schema files, and AutoMed schemas
		printSource(source1SchemaName, source1Doc, source1SchemaUIDDoc, sourceSchema1);
		printSource(source2SchemaName, source2Doc, source2SchemaUIDDoc, sourceSchema2);
		printSource(targetSchemaName, targetDoc, targetSchemaUIDDoc, targetSchema);

		// Transformation

		try {
			SchemaRestructuringAlgorithm t1 = new SchemaRestructuringAlgorithm();
			t1.restructure(true, sourceSchema1, source1SchemaUIDDoc, targetSchema, targetSchemaUIDDoc,null,null);

			SchemaRestructuringAlgorithm t2 = new SchemaRestructuringAlgorithm();
			t2.restructure(true, sourceSchema2, source2SchemaUIDDoc, targetSchema, targetSchemaUIDDoc,null,null);
		} catch (SchemaRestructuringException e) {
			e.printStackTrace();
		}
	}
}
