
import uk.ac.ic.doc.automed.templates.wrapper.*;
import uk.ac.ic.doc.automed.reps.*;
import uk.ac.ic.doc.automed.wrappers.SQLWrapperFactory;
import uk.ac.ic.doc.automed.InconsistentException;
import uk.ac.ic.doc.automed.IntegrityException;


public class ColumnMovedDownHierarchy extends TemplateCompilerSimulator{
  private static Model defaultModel;
  private static String name;

  static {
	try{
	SQLWrapperFactory wf = new SQLWrapperFactory();
	wf.setFeatures(SQLWrapperFactory.PRIMARY_KEY);
	defaultModel = wf.getAutoMedModel();
	name = "Parent Column to Specialisations Equivalence ("+defaultModel.getName()+" model)";
	}
	catch(Exception e){
		e.printStackTrace();
		System.exit(-1);
	}
  } 

	public ColumnMovedDownHierarchy() throws Exception{
		super();
	}
	
	public static String getName(){
		return name;
	}
	

	protected String templateName(){
		return name;
	}

	protected String templateDescription(){
		return "A column is moved from the parent table to the specialisations";
	}

	protected String defaultModel(){
		return defaultModel.getName();
	}

	protected void defineTemplate() throws Exception{
		Model sql = defaultModel;
		Construct table = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_TABLE);
		Construct column = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_COLUMN);
		Construct primarykey = sql.getConstruct(SQLWrapperFactory.AUTOMED_SQL_MODEL_PRIMARY_KEY);

		INPUTS();
			OBJECT parentTable = askForObject("Existing parent table", table);
			OBJECT parentColumn = askForObject("Column to specialise", column);
			OBJECTLIST specialisationTables = askForObjectList("Specialisation tables to move column", table);
			
		START();
			FOREACH(); OBJECT specialisationTable = IN (specialisationTables);
				OBJECT parentTableClone = VARIES_WITH(parentTable);
				OBJECT parentColumnClone = VARIES_WITH(parentColumn);
			DO();
				ALIAS(specialisationTable, "specialisationTable");
				ALIAS(parentTableClone, "parentTable");
				ALIAS(parentColumnClone, "parentColumn");

				FUNCTION newColumnExtent = DEFINE_FUNCTION("[ {x,y} | {x}<-@specialisationTable?scheme ; {x1}<-@parentTable?scheme ; {x2,y}<-@parentColumn?scheme ; (=) x1 x2 ; (=) x x1]");
				NAME newColumnName = DEFINE_NAME("@parentColumn?name");
				OBJECT newColumn = ADD( CONSTRUCT.IS(column),
									   SCHEME.IS(new Object[]{specialisationTable, my(newColumnName)}),
									   FUNCTION.IS(newColumnExtent)
								     );

				ALIAS(newColumn, "newColumn");
				STRING partialQuery = DEFINE_STRING("[ {x,y} | {x}<-@parentTable?scheme ; {x1,y}<-@newColumn?scheme  ; (=) x x1]");
				STRINGLIST partialQueriesForParentColumn = (STRINGLIST) COLLECTS(partialQuery);

			ENDFOREACH();

			STRING query = DEFINE_STRING(partialQueriesForParentColumn, "++");
			ALIAS(query,"query");
			FUNCTION parentColumnExtent = DEFINE_FUNCTION("@query");
			parentColumn.DELETE(parentColumnExtent);	
		END();
	}	

	public static void main(String[] args){
		try{
				new ColumnMovedDownHierarchy();
		}
		catch(Exception e){
			e.printStackTrace();
		}	

	}

}
