package basic;

import java.sql.*;
import java.io.*;
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.modelmanagement.modeldef.SQLModelDef;
import uk.ac.ic.doc.automed.reps.*;
import uk.ac.ic.doc.automed.wrappers.*;

/*
 * An application to wrap bank integration
 * databases held in the sql92 datamodel.
 */
public class BankDatabaseWrapping {
	static String username="lab";
	static String password="lab";
	// static String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
	static String driver="org.postgresql.Driver";
	// static String
	// urlbase="jdbc:microsoft:sqlserver://sqlserver.doc.ic.ac.uk\\;databaseName=";
	static String urlbase="jdbc:postgresql://localhost/";
	static String cfgFile=null;

	static int features=SQLModelDef.PRIMARY_KEY+SQLModelDef.FOREIGN_KEY
			+SQLModelDef.NULL_CONSTRAINT+SQLModelDef.DATA_TYPE;

	static Model sql92=null;
	static Construct table=null;
	static Construct column=null;
	static Construct pk=null;
	static Construct fk=null;
	static SQLWrapperFactory wf;
	static boolean extra=false;
	static String schemaChoice=null;

	static void printHelp() {
		System.out.println("Usage: UniversityDatabaseWrapping [ options ]\n"
				+"  -debug <0-10>         set debug level\n"
				+"  -url <dbURL>          URL where DB located\n"
				+"  -driver <jdbcDriverClass>\n"+"  -user <user>\n"
				+"  -schema <schema name>\n"+"  -cfg <path_to_automed_ds_cfg>\n"
				+"  -password <password>\n"+"  -features <number>\n");
	}

	public static void readArgs(String[] args) {
		int i=0;

		try {
			while (i<args.length) {
				if (args[i].charAt(0)!='-') {
					System.out.println("Error at "+args[i]+"\n");
					printHelp();
					System.exit(0);
				} else if (args[i].startsWith("-debug"))
					STR.setDebugLevel(Integer.parseInt(args[++i]));
				else if (args[i].startsWith("-url"))
					urlbase=args[++i];
				else if (args[i].startsWith("-cfg"))
					cfgFile=args[++i];
				else if (args[i].startsWith("-help")) {
					printHelp();
					System.exit(0);
				} else if (args[i].startsWith("-driver"))
					driver=args[++i];
				else if (args[i].startsWith("-user"))
					username=args[++i];
				else if (args[i].startsWith("-schema"))
					schemaChoice=args[++i];
				else if (args[i].startsWith("-password"))
					password=args[++i];
				else if (args[i].startsWith("-features"))
					features=Integer.parseInt(args[++i]);

				++i;
			}

			if (STR.atDebugLevel(5))
				System.out.println("Finished reading arguments, "
						+"about to read database");

			if (cfgFile!=null)
				DSRConfiguration.setDefaultConfigFileName(cfgFile);

		} catch (ArrayIndexOutOfBoundsException e) {
			System.out.println("You didn't supply an argument to the "+args[i-1]
					+" option!");
			printHelp();
			System.exit(0);
		}
	}

	private static void close() {
		try {
			DSR.close();
		} catch (DSRException ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		readArgs(args);

		try {
			System.out.println("AutoMed Repository: "+"Wrapping of Bank Databases");

			// Use a common wrapper factory to wrap a set of sources that
			// are to be integrated, so that they have a consistent modelling
			// langauge.
			wf=new SQLWrapperFactory();
			wf.getModelDefinition().setFeatures(features);

			if (schemaChoice==null||"branch".equals(schemaChoice)) {
				Schema branch=AutoMedInANutshell.wrapSchema(username,password,driver,
						urlbase,wf,"lab_bank_branch","branch");
				branch.getSchemaObject("<<branch>>").setPosition(300,150);
				branch.getSchemaObject("<<account>>").setPosition(300,350);
				branch.getSchemaObject("<<movement>>").setPosition(300,550);

				Schema branch_src=Schema.getSchema("branch_src");
				branch_src.getSchemaObject("<<branch,3>>").setPosition(100,150);
				branch_src.getSchemaObject("<<account,5>>").setPosition(100,350);
				branch_src.getSchemaObject("<<movement,4>>").setPosition(300,550);
			}

			if (schemaChoice==null||"customer".equals(schemaChoice)) {
				Schema customer=AutoMedInANutshell.wrapSchema(username,password,driver,
						urlbase,wf,"lab_bank_customer","customer");

				customer.getSchemaObject("<<site>>").setPosition(100,150);
				customer.getSchemaObject("<<withdraw>>").setPosition(500,150);
				customer.getSchemaObject("<<account>>").setPosition(300,350);
				customer.getSchemaObject("<<customer>>").setPosition(500,350);
				// customer.getSchemaObject("<<movement>>").setPosition(300,550);
				customer.getSchemaObject("<<web_customer>>").setPosition(500,550);

				Schema customer_src=Schema.getSchema("customer_src");
				customer_src.getSchemaObject("<<site,3>>").setPosition(100,150);
				customer_src.getSchemaObject("<<withdraw,2>>").setPosition(500,150);
				customer_src.getSchemaObject("<<account,4>>").setPosition(100,350);
				customer_src.getSchemaObject("<<customer,5>>").setPosition(500,350);
				// customer_src.getSchemaObject("<<movement,4>>").setPosition(300,550);
				customer_src.getSchemaObject("<<web_customer,4>>").setPosition(500,550);
			}
			// Layout the schemas nicely

			sql92=wf.getAutoMedModel();
			table=sql92.getConstruct(SQLModelDef.AUTOMED_SQL_MODEL_TABLE);
			column=sql92.getConstruct(SQLModelDef.AUTOMED_SQL_MODEL_COLUMN);

			if (wf.getModelDefinition().isFeatureInUse(SQLModelDef.PRIMARY_KEY))
				pk=sql92.getConstruct(SQLModelDef.AUTOMED_SQL_MODEL_PRIMARY_KEY);

			if (wf.getModelDefinition().isFeatureInUse(SQLModelDef.FOREIGN_KEY))
				fk=sql92.getConstruct(SQLModelDef.AUTOMED_SQL_MODEL_FOREIGN_KEY);
		} catch (SQLException se) {
			se.printStackTrace();
		} catch (IOException ie) {
			ie.printStackTrace();
		} catch (AutoMedException ame) {
			ame.printStackTrace();
		} catch (Throwable t) {
			System.err.println(t.toString());
			t.printStackTrace();
		} finally {
			close();
			System.exit(0);
		}

	}
}
