import java.sql.*;
import java.io.*;
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.*;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * An application to demonstrate how to create schemas in YATTA model
 */

public class DefineSchemas {
  public static Schema createYATTAS1(String schema_name) 
    throws AutoMedException,SQLException,IOException  {
    Schema s=Schema.createSchema(schema_name,false);
    Model yatta=Model.getModel("yatta");

    Construct rootnode=yatta.getConstruct("rootnode");
    Construct yattanode=yatta.getConstruct("yattanode");

    // Now build the schema
    SchemaObject root = s.createSchemaObject(rootnode,new Object[]{"root","list"});
    SchemaObject student = s.createSchemaObject(yattanode,new Object[]{"student", "list", "*", "1", root});
    SchemaObject name = s.createSchemaObject(yattanode,new Object[]{"name", "string", "-", "1", student});
    SchemaObject course = s.createSchemaObject(yattanode,new Object[]{"course", "list", "+", "2", student});
    SchemaObject code = s.createSchemaObject(yattanode,new Object[]{"code", "string", "-", "1", course});
    SchemaObject year = s.createSchemaObject(yattanode,new Object[]{"year", "integer", "", "2", course});
    SchemaObject grade = s.createSchemaObject(yattanode,new Object[]{"grade", "string", "", "3", course});

    System.out.println("Created schema "+schema_name+" succesfully");

    return s;
  }

  public static Schema createYATTAS2(String schema_name) 
    throws AutoMedException,SQLException,IOException  {
    Schema s=Schema.createSchema(schema_name,false);
    Model yatta=Model.getModel("yatta");

    Construct rootnode=yatta.getConstruct("rootnode");
    Construct yattanode=yatta.getConstruct("yattanode");

    // Now build the schema
    SchemaObject root = s.createSchemaObject(rootnode,new Object[]{"root","list"});
    SchemaObject course = s.createSchemaObject(yattanode,new Object[]{"course", "list", "*", "1", root});
    SchemaObject code = s.createSchemaObject(yattanode,new Object[]{"code", "string", "-", "1", course});
    SchemaObject student = s.createSchemaObject(yattanode,new Object[]{"student", "list", "+", "2", course});
    SchemaObject name = s.createSchemaObject(yattanode,new Object[]{"name", "string", "-", "1", student});
    SchemaObject year = s.createSchemaObject(yattanode,new Object[]{"year", "integer", "", "2", student});
    SchemaObject grade = s.createSchemaObject(yattanode,new Object[]{"grade", "string", "", "3", student});

    System.out.println("Created schema "+schema_name+" successfully");

    return s;
  }

  public static void main(String[] args) {
    try {
      System.out.println("AutoMed Repository: Creating Schemas in YATTA model");
      //Transformation.syncWithRepository();

      // level 5 prints all the old debug information, 0 makes it silent
      //SchemaTransformationRepository.setDebugLevel(0);

      Schema.retract("y1");
      Schema y1=createYATTAS1("y1");
      y1.listSchema(System.out,Schema.LIST_SCHEMA_NORMAL,true);

      Schema.retract("y2");
      Schema y2=createYATTAS2("y2");
      y2.listSchema(System.out,Schema.LIST_SCHEMA_NORMAL,true);

      //create datasource for schemas
      Protocol p;
      String protocolName="yatta";
      if(Protocol.exists(protocolName))
      {
	  p=Protocol.getProtocol(protocolName);
      }
      else
      {
	  p=Protocol.createProtocol(protocolName);
      }

      AccessMethod amy1=AccessMethod.createAccessMethod(y1, "source_y1", p, "uk.ac.ic.doc.automed.wrapper.YattaWrapper", "");
      AccessMethod amy2=AccessMethod.createAccessMethod(y2, "source_y2", p, "uk.ac.ic.doc.automed.wrapper.YattaWrapper", "");


    }
    catch (SQLException se) { 
      se.printStackTrace();
    }
    catch (IOException ie) {
      ie.printStackTrace();
    }
    catch (AutoMedException ame) {
      ame.printStackTrace();
    }
  }
}












