
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import java.sql.SQLException;
import java.io.IOException;


/**
	An application that defines the graphical representation of the ER model. The
	application re-defines the ER model if it doesn't exist.
	model: "er"
	constructs: "erEntity", "erAttribute", "erRelationship", "erGeneralization" 
*/
public class DefineERGraphicalRepresentation implements Constants
{
  public static String modelName="erOld";
  public static String entityConstructName="entityOld";
  public static String attributeConstructName="attributeOld";
  public static String relationshipConstructName="relationshipOld";
  public static String generalizationConstructName="generalisationOld";

/* 
	public static void createERinMDR()
		throws AutoMedException, SQLException, IOException, InconsistentException
	{
		if (Model.exists(modelName)){
			Model.getModel(modelName).retract();
		}

		//create the ER model
		Model erModel = Model.createModel(modelName);

		//////////////////// ER ENTITY//////////////////////////////////
		Construct erEntity = 
			erModel.createConstruct(entityConstructName, CONSTRUCT_CLASS_NODAL, true);
		erEntity.createConstructScheme(SCHEME_TYPE_NAME, null, true, 
											SCHEME_HDM_TYPE_P1, 1, 1);
		
		//////////////////// ER ATTRIBUTE//////////////////////////////////
		Construct erAttribute =
			erModel.createConstruct(attributeConstructName, CONSTRUCT_CLASS_LINK_NODAL, true);
		erAttribute.createConstructScheme(SCHEME_TYPE_CONSTRUCT, erEntity,
					true, SCHEME_HDM_TYPE_P1, 1, 1);
		erAttribute.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_N1, 1, 1);

		//////////////////// ER RELATIONSHIP//////////////////////////////////
		Construct erRelationship =
			erModel.createConstruct(relationshipConstructName, CONSTRUCT_CLASS_LINK, true);
		erRelationship.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_N1, 1, 1);
		erRelationship.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_P1, 1, 1);
		erRelationship.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_P2, 1, 1);
//		erRelationship.createConstructScheme(SCHEME_TYPE_NAME, null,
//					true, SCHEME_HDM_TYPE_C, 1, 1);
//		erRelationship.createConstructScheme(SCHEME_TYPE_NAME, null,
//					true, SCHEME_HDM_TYPE_C, 1, 1);

		//////////////////// ER GENERALIZATION//////////////////////////////////
		Construct erGeneralization =
			erModel.createConstruct(generalizationConstructName, CONSTRUCT_CLASS_CONSTRAINT, true);
		erGeneralization.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_NAME, null,
					true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_C, 1, 1);
		erGeneralization.createConstructScheme(SCHEME_TYPE_CONSTRUCT,
					erEntity, true, SCHEME_HDM_TYPE_C, 1, 0);
	}
*/

	public static void createERinSMR()
		throws AutoMedException, SchemaModelingRepositoryException
	{
		// create modeling statements
		ModelingStatement this_getValue = new ModelingStatement("this.getValue");
		ModelingStatement this_getLocation = new ModelingStatement("this.getLocation");
		ModelingStatement this_getManager = new ModelingStatement("this.getManager");
		ModelingStatement getGraphics = new ModelingStatement("getGraphics");
		ModelingStatement setGraphics = new ModelingStatement("setGraphics");
		ModelingStatement setShapeLocation = new ModelingStatement("setShapeLocation");
		ModelingStatement setText = new ModelingStatement("setText");
		ModelingStatement this_setShape = new ModelingStatement("this.setShape");
		ModelingStatement draw = new ModelingStatement("draw");
		ModelingStatement box = new ModelingStatement("uk.ac.ic.doc.automed.gui.SchemaManager.createBox");
		ModelingStatement line = new ModelingStatement("uk.ac.ic.doc.automed.gui.SchemaManager.createLine");
		ModelingStatement rhombus = new ModelingStatement("uk.ac.ic.doc.automed.gui.SchemaManager.createRhombus");
		ModelingStatement this_getReferencedConstruct = new ModelingStatement("this.getReferencedConstruct");	
		ModelingStatement this_getReferencedConstructs= new ModelingStatement("this.getReferencedConstructs");	
		ModelingStatement getGraphicObject = new ModelingStatement("getGraphicObject");	
		ModelingStatement getGraphicObject_n = new ModelingStatement("getGraphicObject -n");	
		ModelingStatement getShape = new ModelingStatement("getShape");
		ModelingStatement this_getShape = new ModelingStatement("this.getShape");
		ModelingStatement setPoint1 = new ModelingStatement("setPoint1");
		ModelingStatement setPoint2 = new ModelingStatement("setPoint2");
		ModelingStatement setPoint2_n = new ModelingStatement("setPoint2 -n");
		ModelingStatement this_getSpecialConstructs = new ModelingStatement("this.getSpecialConstructs");
		ModelingStatement getGenEdgePosition = new ModelingStatement("getGenEdgePosition");
		ModelingStatement getEdgePosition = new ModelingStatement("getEdgePosition");
		ModelingStatement getSpecEdgePosition = new ModelingStatement("getSpecEdgePosition");


		//create modeling programs for each ER construct
		ModelingProgram erEntityModeling = new ModelingProgram();
		erEntityModeling.add(this_getValue);
		erEntityModeling.add(this_getLocation);
		erEntityModeling.add(this_getManager);
		erEntityModeling.add(getGraphics);
		erEntityModeling.add(box);
		erEntityModeling.add(setGraphics);
		erEntityModeling.add(setShapeLocation);
		erEntityModeling.add(setText);
		erEntityModeling.add(this_setShape);
		erEntityModeling.add(draw);
		
		ModelingProgram erAttributeModeling = new ModelingProgram();
		erAttributeModeling.add(this_getValue);
		erAttributeModeling.add(this_getLocation);
		erAttributeModeling.add(this_getManager);
		erAttributeModeling.add(getGraphics);
		erAttributeModeling.add(new ModelingStatement("uk.ac.ic.doc.automed.gui.SchemaManager.createArc"));
		erAttributeModeling.add(setGraphics);
		erAttributeModeling.add(setShapeLocation);
		erAttributeModeling.add(setText);
		erAttributeModeling.add(this_setShape);
		erAttributeModeling.add(this_getLocation);
		erAttributeModeling.add(this_getReferencedConstruct);
		erAttributeModeling.add(this_getManager);
		erAttributeModeling.add(getGraphicObject);
		erAttributeModeling.add(getShape);
		erAttributeModeling.add(getEdgePosition);
		erAttributeModeling.add(this_getLocation);
		erAttributeModeling.add(this_getReferencedConstruct);
		erAttributeModeling.add(this_getManager);
		erAttributeModeling.add(getGraphicObject);
		erAttributeModeling.add(getShape);
		erAttributeModeling.add(getEdgePosition);
		erAttributeModeling.add(this_getShape);
		erAttributeModeling.add(getEdgePosition);
		erAttributeModeling.add(this_getShape);
		erAttributeModeling.add(draw);
		erAttributeModeling.add(this_getManager);
		erAttributeModeling.add(getGraphics);
		erAttributeModeling.add(line);
		erAttributeModeling.add(setGraphics);
		erAttributeModeling.add(setPoint1);
		erAttributeModeling.add(setPoint2);
		erAttributeModeling.add(draw);
		
		ModelingProgram erRelationshipModeling = new ModelingProgram();
		erRelationshipModeling.add(this_getValue);
		erRelationshipModeling.add(this_getLocation);
		erRelationshipModeling.add(this_getManager);
		erRelationshipModeling.add(getGraphics);
		erRelationshipModeling.add(rhombus);
		erRelationshipModeling.add(setGraphics);
		erRelationshipModeling.add(setShapeLocation);
		erRelationshipModeling.add(setText);
		erRelationshipModeling.add(this_setShape);
		erRelationshipModeling.add(draw);
		erRelationshipModeling.add(this_getLocation);
		erRelationshipModeling.add(this_getReferencedConstructs);
		erRelationshipModeling.add(this_getManager);
		erRelationshipModeling.add(getGraphicObject_n);
		erRelationshipModeling.add(getShape);
		erRelationshipModeling.add(getEdgePosition);
		erRelationshipModeling.add(this_getLocation);
		erRelationshipModeling.add(this_getReferencedConstructs);
		erRelationshipModeling.add(this_getManager);
		erRelationshipModeling.add(getGraphicObject_n);
		erRelationshipModeling.add(getShape);
		erRelationshipModeling.add(getEdgePosition);
		erRelationshipModeling.add(this_getShape);
		erRelationshipModeling.add(new ModelingStatement("getEntityEdgePosition -n"));
		erRelationshipModeling.add(this_getManager);
		erRelationshipModeling.add(getGraphics);
		erRelationshipModeling.add(this_getReferencedConstructs);
		erRelationshipModeling.add(new ModelingStatement("uk.ac.ic.doc.automed.gui.SchemaManager.createLine -n"));
		erRelationshipModeling.add(setGraphics);
		erRelationshipModeling.add(new ModelingStatement("setPoint1 -n"));
		erRelationshipModeling.add(setPoint2_n);
		erRelationshipModeling.add(draw);
			
		ModelingProgram erGeneralizationModeling = new ModelingProgram();
		erGeneralizationModeling.add(this_getManager);
		erGeneralizationModeling.add(new ModelingStatement("getGeneralisationExtend"));
		erGeneralizationModeling.add(this_getValue);
		erGeneralizationModeling.add(this_getLocation);
		erGeneralizationModeling.add(this_getManager);
		erGeneralizationModeling.add(getGraphics);
		erGeneralizationModeling.add(rhombus);
		erGeneralizationModeling.add(setGraphics);
		erGeneralizationModeling.add(setShapeLocation);
		erGeneralizationModeling.add(setText);
		erGeneralizationModeling.add(new ModelingStatement("setExtend"));
		erGeneralizationModeling.add(this_setShape);
		erGeneralizationModeling.add(draw);
		erGeneralizationModeling.add(this_getShape);
		erGeneralizationModeling.add(getGenEdgePosition);
		erGeneralizationModeling.add(new ModelingStatement("this.getGeneralConstruct"));
		erGeneralizationModeling.add(this_getManager);
		erGeneralizationModeling.add(getGraphicObject);
		erGeneralizationModeling.add(getShape);
		erGeneralizationModeling.add(getEdgePosition);
		erGeneralizationModeling.add(this_getShape);
		erGeneralizationModeling.add(getGenEdgePosition);
		erGeneralizationModeling.add(this_getManager);
		erGeneralizationModeling.add(getGraphics);
		erGeneralizationModeling.add(line);
		erGeneralizationModeling.add(setGraphics);
		erGeneralizationModeling.add(setPoint1);
		erGeneralizationModeling.add(setPoint2);
		erGeneralizationModeling.add(draw);
		erGeneralizationModeling.add(this_getShape);
		erGeneralizationModeling.add(getSpecEdgePosition);
		erGeneralizationModeling.add(this_getSpecialConstructs);
		erGeneralizationModeling.add(this_getManager);
		erGeneralizationModeling.add(getGraphicObject_n);
		erGeneralizationModeling.add(getShape);
		erGeneralizationModeling.add(getEdgePosition);
		erGeneralizationModeling.add(this_getShape);
		erGeneralizationModeling.add(getSpecEdgePosition);
		erGeneralizationModeling.add(this_getManager);
		erGeneralizationModeling.add(getGraphics);
		erGeneralizationModeling.add(this_getSpecialConstructs);
		erGeneralizationModeling.add(new ModelingStatement("uk.ac.ic.doc.automed.gui.SchemaManager.createLine -n"));
		erGeneralizationModeling.add(setGraphics);
		erGeneralizationModeling.add(setPoint1);
		erGeneralizationModeling.add(setPoint2_n);
		erGeneralizationModeling.add(draw);
	
		Model er = Model.getModel(modelName);
		ConstructWithModelingProgram erEntity = (ConstructWithModelingProgram)er.getConstruct(entityConstructName);
		ConstructWithModelingProgram erAttribute = (ConstructWithModelingProgram)er.getConstruct(attributeConstructName);
		ConstructWithModelingProgram erRelationship = (ConstructWithModelingProgram)er.getConstruct(relationshipConstructName);
		ConstructWithModelingProgram erGeneralization = (ConstructWithModelingProgram)er.getConstruct(generalizationConstructName);

		erEntity.removeModelingProgram();
		erAttribute.removeModelingProgram();
		erRelationship.removeModelingProgram();
		erGeneralization.removeModelingProgram();

		erEntity.setModelingProgram(erEntityModeling);
		erAttribute.setModelingProgram(erAttributeModeling);
		erRelationship.setModelingProgram(erRelationshipModeling);
		erGeneralization.setModelingProgram(erGeneralizationModeling);
	}

	public static void main(String[] args){
		try{
			if(!Model.exists(DefineModels.modelName)){
				System.out.println("Re-defining the ER model...");
				DefineModels.createERinMDR();
			}
			System.out.println("Defining the ER model's graphical representation...");
			DefineERGraphicalRepresentation.createERinSMR();
		}
		catch(InconsistentException aie){
			aie.printStackTrace();
		}	
		catch(Exception e){
			e.printStackTrace();
		}
	}
} 
 
