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


/**
	An application that defines the "ExtendField" template transformation.
	This transformation should be applied on a schema of the "sql92" model.
	It extends an existing table with a field.

	The arguments for this transformation are:
	1. initial schema ID
	2. existing table ID
	3. new field name
	4. new field ID (returned value)
	5. final schema ID (returned value)	
*/
public class ExtendField{
  private static String modelName="sql92";
  private static String tableConstructName="table";
  private static String fieldConstructName="field";
  private static String primaryKeyConstructName="primary_key";
  private static String foreignKeyConstructName="foreign_key";

	private static String templateName = "ExtendField";

	public static void defineTemplate()
		throws TemplateTransformationRepositoryException, RepositoryAccessException, TemplateTransformationSyntaxRepositoryException, AutoRepException, AutoMedException
	{
		try{
			CompositeTransformation.removeCompositeTransformation(templateName);
		}
		catch(TemplateTransformationRepositoryException exc){
			System.out.println("new transformation "+templateName);
		}


		Model rel = Model.getModel(modelName);
		Construct table = rel.getConstruct(tableConstructName);
		Construct field = rel.getConstruct(fieldConstructName);
		Construct pk = rel.getConstruct(primaryKeyConstructName);
		Construct fk = rel.getConstruct(foreignKeyConstructName);

		TemplateArgumentSequence newFieldArgs = new TemplateArgumentSequence();
		newFieldArgs.createArgument(CompositeTransformation.ARG_CID_IN_NEWOBJECT, String.valueOf(field.getCID()));
		newFieldArgs.createArgument(CompositeTransformation.ARG_VALUE_IN_NEWOBJECT, 3);
		newFieldArgs.createArgument(CompositeTransformation.ARG_ISTEMP_IN_NEWOBJECT, "0");
		TemplateArgumentSequence newFieldScheme = new TemplateArgumentSequence();
		newFieldScheme.createArgument(1, 2);
		newFieldScheme.createArgument(2, newFieldArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		newFieldArgs.createArgument(CompositeTransformation.ARG_SCHEME_IN_NEWOBJECT, String.valueOf(newFieldScheme.getSequenceId())); 

		TemplateArgumentSequence extendNewFieldArgs = new TemplateArgumentSequence();
		extendNewFieldArgs.createArgument(CompositeTransformation.ARG_ACTION_IN_PRIMITIVE, "ext");
		extendNewFieldArgs.createArgument(CompositeTransformation.ARG_FROMOBJ_IN_PRIMITIVE, "-1");
		extendNewFieldArgs.createArgument(CompositeTransformation.ARG_TOOBJ_IN_PRIMITIVE, newFieldArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		extendNewFieldArgs.createArgument(CompositeTransformation.ARG_FROMSCHEMA_IN_PRIMITIVE, 1);
		extendNewFieldArgs.createArgument(CompositeTransformation.ARG_TOSCHEMA_IN_PRIMITIVE, newFieldArgs, CompositeTransformation.ARG_SID_IN_NEWOBJECT);
		extendNewFieldArgs.createArgument(CompositeTransformation.ARG_FUNCTION_IN_PRIMITIVE, "");
		extendNewFieldArgs.createArgument(CompositeTransformation.ARG_CONSTRAINTS_IN_PRIMITIVE, "");


		TemplateArgumentSequence returnedArgs = new TemplateArgumentSequence();
		returnedArgs.createArgument(4, newFieldArgs, CompositeTransformation.ARG_OBID_IN_NEWOBJECT);
		returnedArgs.createArgument(5, newFieldArgs, CompositeTransformation.ARG_SID_IN_NEWOBJECT);	

		CompositeTransformation trans = new CompositeTransformation(templateName, "A new field is created", true);

		trans.createArgumentDefinition(1, "schema", "SID", "initial schema", false);
		trans.createArgumentDefinition(2, "object", "OBID", "existing table ID", false);
		trans.createArgumentDefinition(3, "object", "value", "new field name", false);
		trans.createArgumentDefinition(4, "object", "OBID", "new field ID", true);
		trans.createArgumentDefinition(5, "schema", "SID", "final schema ID", true);

		trans.createReturnedArgumentsDefinition(returnedArgs);

		trans.createStatementExecution(1, Statement.getStatement(CompositeTransformation.NEW_OBJECT_STATEMENT), newFieldArgs);
		trans.createStatementExecution(2, Statement.getStatement(CompositeTransformation.PRIMITIVE_TRANSFORMATION_STATEMENT), extendNewFieldArgs);
	}

	public static void main(String[] args){
		try{
			System.out.println("Re-creating template transformation ExtendField...");	
			ExtendField.defineTemplate();
		}
		catch(Exception e){
			e.printStackTrace();
		}	 
	} 
}

