import java.sql.*;
import java.io.*;
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import uk.ac.ic.doc.automed.editor.UserActionResult;
import uk.ac.ic.doc.automed.editor.GUITool;

/*
 * What ever is under test in the
 * Automed API
 */
public class Test {
  // Generic reinitialise of a repository, to clear the main repositories,
  // call this twice, once with "MDR" and once with "STR"
  public static void initialiseRepository(String sourceName)  
  throws DSRException, SQLException, IOException {
    String sa[]=new String[]{"",sourceName};

    DSR.doDropdbCommand(sa);
    DSR.doCreatedbCommand(sa);
    System.out.println("Repository "+sourceName+" initialised");
  }

  public static UserActionResult addSchema(Object[] o) {
    try {
      Schema.createSchema("test",false);
    }
    catch (Exception e) {
      GUITool.reportException(e);
    }

    return null;
  }
  public static UserActionResult addPlanePid(Object[] o) {
    try {
      SchemaObject so=(SchemaObject)o[0];
      Schema s=so.getSchema();
      Model er=Model.getModel("er");
      Construct ent=er.getConstruct("attribute");
      s.createSchemaObject(ent,new Object[]{so,"pid","key"});
    }
    catch (Exception e) {
      GUITool.reportException(e);
    }

    return null;
  }

  public static UserActionResult addPlane(Object[] o) {
    try {
      Schema s=(Schema)o[0];
      Model er=Model.getModel("er");
      Construct ent=er.getConstruct("entity");
      s.createSchemaObject(ent,new Object[]{"plane"});
    }
    catch (Exception e) {
      GUITool.reportException(e);
    }

    return null;
  }

  public static UserActionResult deletePlane(Object[] o) {
    try {
      Schema s=(Schema)o[0];
      s.getSchemaObject("<<plane>>").remove();
    }
    catch (Exception e) {
      GUITool.reportException(e);
    }

    return null;
  }

  public static void main(String[] args) {
    try {
      System.out.println("AutoMed MDR Reinitialisation");

      // Set this to 2 to see SQL executed
      STR.setDebugLevel(0);
      Model m[]=Model.getModels();

      for (int i=0;i<m.length;i++) {
	m[i].retract();
      }

      if (1==1) 
	return;

      initialiseRepository(MDR.getDataSourceName());
      initialiseRepository(STR.getDataSourceName());
      //initialiseRepository(SchemaModelingRepository.getDataSourceName());
      //initialiseRepository(TemplateExecutionRepository.getDataSourceName());
      //initialiseRepository(TemplateTransformationRepository.getDataSourceName());
      //initialiseRepository(TemplateTransformationSyntaxRepository.getDataSourceName());
    }
    catch (SQLException se) { 
      se.printStackTrace();
    }
    catch (IOException ie) {
      ie.printStackTrace();
    }
    catch (AutoMedException ame) {
      ame.printStackTrace();
    }      
  }
}

























