
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import uk.ac.ic.doc.automed.wrappers.*;


public class DefineMatchedSchemas{

	public static Schema createSQL18S1(String name)
		throws NotFoundException, TypeMismatchException, IntegrityException{
		Schema s = Schema.createSchema(name, true);
		SQLWrapperFactory	wf = new SQLWrapperFactory();
      	wf.setFeatures(SQLWrapperFactory.PRIMARY_KEY);
      	wf.setFeatures(SQLWrapperFactory.FOREIGN_KEY);
		wf.setFeatures(SQLWrapperFactory.DATA_TYPE);
		Model sql= wf.getAutoMedModel();

		Construct table = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_TABLE);
		Construct column = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_COLUMN);
		Construct pk = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_PRIMARY_KEY);
		Construct fk = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_FOREIGN_KEY);

		
		SchemaObject date = s.createSchemaObject(table, new Object[] {"date"});
		SchemaObject date_id = s.createSchemaObject(column, new Object[]{date, "date_id", "int"});
		SchemaObject day = s.createSchemaObject(column, new Object[]{date, "day", "int"});
		SchemaObject month = s.createSchemaObject(column, new Object[]{date, "month", "int"});
		SchemaObject year = s.createSchemaObject(column, new Object[]{date, "year", "int"});
		s.createSchemaObject(pk, new Object[]{"date_pk", date, date_id});

		SchemaObject student = s.createSchemaObject(table, new Object[] {"student"});
		SchemaObject student_login = s.createSchemaObject(column, new Object[]{student, "login", "text"});
		SchemaObject student_dateid = s.createSchemaObject(column, new Object[]{student, "date_id", "int"});
		s.createSchemaObject(pk, new Object[]{"student_pk", student, student_login});
		s.createSchemaObject(fk, new Object[]{"studentdateid_fk", student, student_dateid, date, date_id});

		SchemaObject ug = s.createSchemaObject(table, new Object[]{"ug"});
		SchemaObject ug_login = s.createSchemaObject(column, new Object[]{ug, "ug_login", "text"});
		s.createSchemaObject(column, new Object[]{ug, "ug_name", "text"});
		s.createSchemaObject(pk, new Object[]{"ug_pk", ug, ug_login});
		s.createSchemaObject(fk, new Object[]{"ug_isa_student", ug, ug_login, student, student_login});

		SchemaObject pg = s.createSchemaObject(table, new Object[]{"pg"});
		SchemaObject pg_login = s.createSchemaObject(column, new Object[]{pg, "pg_login", "text"});
		s.createSchemaObject(column, new Object[]{pg, "pg_name", "text"});
		s.createSchemaObject(pk, new Object[]{"pg_pk", pg, pg_login});
		s.createSchemaObject(fk, new Object[]{"pg_isa_student", pg, pg_login, student, student_login});
		
		SchemaObject staff = s.createSchemaObject(table, new Object[]{"staff"});
		SchemaObject slogin = s.createSchemaObject(column, new Object[]{staff, "slogin", "text"});
		s.createSchemaObject(column, new Object[]{staff, "sname", "text"});
		s.createSchemaObject(pk, new Object[]{"staff_pk", staff, slogin});

		SchemaObject course = s.createSchemaObject(table, new Object[]{"course"});
		SchemaObject course_id = s.createSchemaObject(column, new Object[]{course, "course_id", "int"});
		s.createSchemaObject(column, new Object[]{course, "cname", "text"});
		s.createSchemaObject(pk, new Object[]{"course_pk", course, course_id});
		
		SchemaObject registered = s.createSchemaObject(table, new Object[]{"registered"});
		SchemaObject registered_login = s.createSchemaObject(column, new Object[]{registered, "ug_login", "text"});
		SchemaObject registered_course = s.createSchemaObject(column, new Object[]{registered, "course_id", "int"});
		s.createSchemaObject(pk, new Object[]{"registered_pk", registered, registered_login, registered_course});
		s.createSchemaObject(fk, new Object[]{"registered_login_fk", registered, registered_login, ug, ug_login});
		s.createSchemaObject(fk, new Object[]{"registered_course_fk", registered, registered_course, course, course_id});

		SchemaObject teaches = s.createSchemaObject(table, new Object[]{"teaches"});
		SchemaObject teaches_login = s.createSchemaObject(column, new Object[]{teaches, "slogin", "text"});
		SchemaObject teaches_course = s.createSchemaObject(column, new Object[]{teaches, "course_id", "int"});
		s.createSchemaObject(pk, new Object[]{"teaches_pk", teaches, teaches_login, teaches_course});
		s.createSchemaObject(fk, new Object[]{"teaches_login_fk", teaches, teaches_login, staff, slogin});
		s.createSchemaObject(fk, new Object[]{"teaches_course_fk", teaches, teaches_course, course, course_id});

		SchemaObject tutored_by = s.createSchemaObject(table, new Object[]{"tutored_by"});
		SchemaObject tutoredby_uglogin = s.createSchemaObject(column, new Object[]{tutored_by, "ug_login", "text"});
		SchemaObject tutoredby_slogin = s.createSchemaObject(column, new Object[]{tutored_by, "slogin", "text"});
		s.createSchemaObject(pk, new Object[]{"tutoredby_pk", tutored_by, tutoredby_uglogin, tutoredby_slogin});
		s.createSchemaObject(fk, new Object[]{"tutoredby_uglogin_fk", tutored_by, tutoredby_uglogin, ug, ug_login});
		s.createSchemaObject(fk, new Object[]{"tutoredby_slogin_fk", tutored_by, tutoredby_slogin, staff, slogin});

		SchemaObject supervised_by = s.createSchemaObject(table, new Object[]{"supervised_by"});
		SchemaObject supervisedby_pglogin = s.createSchemaObject(column, new Object[]{supervised_by, "pg_login", "text"});
		SchemaObject supervisedby_slogin = s.createSchemaObject(column, new Object[]{supervised_by, "slogin", "text"});
		s.createSchemaObject(pk, new Object[]{"supervisedby_pk", supervised_by, supervisedby_pglogin, supervisedby_slogin});
		s.createSchemaObject(fk, new Object[]{"supervisedby_pglogin_fk", supervised_by, supervisedby_pglogin, pg, pg_login});
		s.createSchemaObject(fk, new Object[]{"supervisedby_slogin_fk", supervised_by, supervisedby_slogin, staff, slogin});

		
		return s;
	}

	public static Schema createSQL18S2(String name)
		throws NotFoundException, TypeMismatchException, IntegrityException{
		Schema s = Schema.createSchema(name, false);
		SQLWrapperFactory	wf = new SQLWrapperFactory();
      	wf.setFeatures(SQLWrapperFactory.PRIMARY_KEY);
      	wf.setFeatures(SQLWrapperFactory.FOREIGN_KEY);
		wf.setFeatures(SQLWrapperFactory.DATA_TYPE);
		Model sql= wf.getAutoMedModel();

		Construct table = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_TABLE);
		Construct column = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_COLUMN);
		Construct pk = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_PRIMARY_KEY);
		Construct fk = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_FOREIGN_KEY);

		
		SchemaObject date = s.createSchemaObject(table, new Object[] {"date"});
		SchemaObject date_id = s.createSchemaObject(column, new Object[]{date, "date_id", "int"});
		SchemaObject day = s.createSchemaObject(column, new Object[]{date, "day", "int"});
		SchemaObject month = s.createSchemaObject(column, new Object[]{date, "month", "int"});
		SchemaObject year = s.createSchemaObject(column, new Object[]{date, "year", "int"});
		s.createSchemaObject(pk, new Object[]{"date_pk", date, date_id});

		SchemaObject postgrad = s.createSchemaObject(table, new Object[]{"postgrad"});
		SchemaObject postgrad_login = s.createSchemaObject(column, new Object[]{postgrad, "postgrad_login", "text"});
		s.createSchemaObject(pk, new Object[]{"postgrad_pk", postgrad, postgrad_login});
		
		SchemaObject staff = s.createSchemaObject(table, new Object[]{"staff"});
		SchemaObject staff_login = s.createSchemaObject(column, new Object[]{staff, "staff_login", "text"});
		s.createSchemaObject(column, new Object[]{staff, "staff_name", "text"});
		s.createSchemaObject(pk, new Object[]{"staff_pk", staff, staff_login});

		SchemaObject course = s.createSchemaObject(table, new Object[]{"course"});
		SchemaObject course_id = s.createSchemaObject(column, new Object[]{course, "course_id", "int"});
		s.createSchemaObject(column, new Object[]{course, "cname", "text"});
		s.createSchemaObject(pk, new Object[]{"course_pk", course, course_id});
		
		SchemaObject assistance = s.createSchemaObject(table, new Object[]{"assistance"});
		SchemaObject assistance_stafflogin = s.createSchemaObject(column, new Object[]{assistance, "staff_login", "text"});
		SchemaObject assistance_postgradlogin = s.createSchemaObject(column, new Object[]{assistance, "postgrad_login", "text"});
		SchemaObject assistance_courseid = s.createSchemaObject(column, new Object[]{assistance, "course_id", "int"});
		SchemaObject assistance_dateid = s.createSchemaObject(column, new Object[]{assistance, "date_id", "int"});
		s.createSchemaObject(pk, new Object[]{"assistance_pk", assistance, assistance_stafflogin, assistance_postgradlogin, assistance_courseid, assistance_dateid});
		s.createSchemaObject(fk, new Object[]{"assistance_stafflogin_fk", assistance, assistance_stafflogin, staff, staff_login});
		s.createSchemaObject(fk, new Object[]{"assistance_postgradlogin_fk", assistance, assistance_postgradlogin, postgrad, postgrad_login});
		s.createSchemaObject(fk, new Object[]{"assistance_courseid_fk", assistance, assistance_courseid, course, course_id});
		s.createSchemaObject(fk, new Object[]{"assistance_dateid_fk", assistance, assistance_dateid, date, date_id});

		
		return s;
	}
	public static void main(String[] args){
		try{
			System.out.println("AutoMed Repository: Creating Matched Schemas");
			STR.setDebugLevel(0);

			Schema.retract("s1_tomatch");
			Schema s1 = createSQL18S1("s1_tomatch");
			s1.listSchema();
			
			System.out.println("======================================================");
			
			Schema.retract("s2_tomatch");
			Schema s2 = createSQL18S2("s2_tomatch");
			s2.listSchema();
		}
		catch(AutoMedException ame){
			ame.printStackTrace();
		}
				
	}
}

