import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import uk.ac.ic.doc.automed.templates.*;
import uk.ac.ic.doc.automed.templates.wrapper.*;

public class DefineTransformationsUsingTemplates{

	public static Schema testTemplate(Schema s1)
	    throws NotFoundException,TemplateDefinitionException,Exception {
		CompositeTransformation template = CompositeTransformation.getCompositeTransformation("test runtime");
TTemplateInputArgumentList argumentList = new TTemplateInputArgumentList(template);
argumentList.addArgument(s1);
argumentList.addArgument("hello");
		CompositeTransformationRecord record = argumentList.executeTransformation();
		return record.getFinalSchema();

	}

		
	public static Schema transformrels1Torels2(Schema s1)
	    throws NotFoundException,TemplateDefinitionException,Exception {
		SchemaObject person=s1.getSchemaObject("<<person>>");
		SchemaObject personsex = s1.getSchemaObject("<<person,sex>>");
		SchemaObject personid = s1.getSchemaObject("<<person,id>>");

		
		CompositeTransformation template = CompositeTransformation.getCompositeTransformation(MandatoryColumnToHierarchyEquivalence.getName());
		TTemplateInputArgumentList argumentList = new TTemplateInputArgumentList(template);
		argumentList.addArgument(s1);
		argumentList.addArgument(person);
		argumentList.addArgument(personsex);
		argumentList.addArgument(personid);
		argumentList.addArgument(new String[]{"male","female"});
		argumentList.addArgument(new String[]{"M","F"});
		CompositeTransformationRecord record = argumentList.executeTransformation();
		return record.getFinalSchema();
	}

	
	public static Schema transformrels2Torels3(Schema s2)
	    throws NotFoundException,TemplateDefinitionException,Exception {
		SchemaObject person = s2.getSchemaObject("<<person>>");
		SchemaObject personid = s2.getSchemaObject("<<person,name>>");
		SchemaObject male = s2.getSchemaObject("<<male>>");
		SchemaObject female = s2.getSchemaObject("<<female>>");

		CompositeTransformation template = CompositeTransformation.getCompositeTransformation(ColumnMovedDownHierarchy.getName());
		TTemplateInputArgumentList argumentList = new TTemplateInputArgumentList(template);
		argumentList.addArgument(s2);
		argumentList.addArgument(person);
		argumentList.addArgument(personid);
		argumentList.addArgument(new SchemaObject[]{male, female});	
		CompositeTransformationRecord record = argumentList.executeTransformation();
		return record.getFinalSchema();			
	}	
	

	public static Schema transforms1Tos2(Schema s1)
	    throws NotFoundException,TemplateDefinitionException,Exception {
    	SchemaObject staff=s1.getSchemaObject("<<staff>>");
    	SchemaObject staffsex=s1.getSchemaObject("<<staff,sex>>");


		CompositeTransformation template = CompositeTransformation.getCompositeTransformation(AttributeToGeneralisationEquivalence.getName());	
		TTemplateInputArgumentList argumentList = new TTemplateInputArgumentList(template);
		argumentList.addArgument(s1);
		argumentList.addArgument(staff);
		argumentList.addArgument(staffsex);
		argumentList.addArgument(new String[] {"male","female"});
		argumentList.addArgument(new String[] {"M","F"});
		argumentList.addArgument("sex");
		CompositeTransformationRecord record = argumentList.executeTransformation();
		return record.getFinalSchema();
	}

	public static Schema transforms2Tos3(Schema s2)
	    throws NotFoundException,TemplateDefinitionException,Exception {
    	SchemaObject staff=s2.getSchemaObject("<<staff>>");
    	SchemaObject staffdept=s2.getSchemaObject("<<staff,dept>>");


		CompositeTransformation template = CompositeTransformation.getCompositeTransformation(AttributeToEntityEquivalence.getName());	
		TTemplateInputArgumentList argumentList = new TTemplateInputArgumentList(template);
		argumentList.addArgument(s2);
		argumentList.addArgument(staff);
		argumentList.addArgument(staffdept);
		argumentList.addArgument("dept");
		argumentList.addArgument("dname");
		argumentList.addArgument("worksin");
		CompositeTransformationRecord record = argumentList.executeTransformation();
		return record.getFinalSchema();
	}

	public static void main(String[] args) {
		try {
			System.out.println("AutoMed Repository: Template Transformation Demo");

			// level 5 prints all the old debug information, 0 makes it silent
			STR.setDebugLevel(0);
	
			Schema er_s1=Schema.getSchema("templates_er_s1");
			Schema er_s2=transforms1Tos2(er_s1);
			er_s2.listSchema();
			Schema er_s3=transforms2Tos3(er_s2);
			er_s3.listSchema();
	
	
			Schema rel_s1 = Schema.getSchema("templates_rel_s1");
			Schema rel_s2 = transformrels1Torels2(rel_s1);
			rel_s2.listSchema();
			Schema rel_s3 = transformrels2Torels3(rel_s2);
			rel_s3.listSchema();


//			Schema er_s1 = Schema.getSchema("templates_er_s1");
//			Schema res = testTemplate(er_s1);
//			res.listSchema();
		}
		catch(AutoMedException ame){
			ame.printStackTrace();
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}

}
