package caise05;

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.wrappers.*;
import uk.ac.ic.doc.automed.wrappers.YattaWrapperFactory;
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,true);
    YattaWrapperFactory yw=new YattaWrapperFactory();
    Model yatta=yw.getModel();

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

    // Now build the schema
    SchemaObject root = s.createSchemaObject(rootnode,new Object[]{"root","list"});
    SchemaObject student = s.createSchemaObject(yattanode,new Object[]{root, "ug", "set", "+"});
    SchemaObject name = s.createSchemaObject(yattanode,new Object[]{student, "name", "string", "k"});
    SchemaObject tutor = s.createSchemaObject(yattanode,new Object[]{student, "tutor", "string", ""});
    SchemaObject nt = s.createSchemaObject(sequence,new Object[]{"",name,tutor});
    SchemaObject course = s.createSchemaObject(yattanode,new Object[]{student, "course", "set", "*"});
    SchemaObject tc = s.createSchemaObject(sequence,new Object[]{"",tutor,course});
    SchemaObject code = s.createSchemaObject(yattanode,new Object[]{course, "code", "string", "k"});
    SchemaObject dept = s.createSchemaObject(yattanode,new Object[]{course, "dept", "string", ""});
    SchemaObject cd = s.createSchemaObject(sequence,new Object[]{"",code,dept});
    SchemaObject lecturer = s.createSchemaObject(yattanode,new Object[]{course, "lecturer", "string", ""});
    SchemaObject dl = s.createSchemaObject(sequence,new Object[]{"",dept,lecturer});
    SchemaObject year = s.createSchemaObject(yattanode,new Object[]{course, "year", "integer", ""});
    SchemaObject ly = s.createSchemaObject(sequence,new Object[]{"",lecturer,year});
    SchemaObject grade = s.createSchemaObject(yattanode,new Object[]{course, "grade", "string", "?"});
    SchemaObject yg = s.createSchemaObject(sequence,new Object[]{"",year,grade});

    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,true);
    YattaWrapperFactory yw=new YattaWrapperFactory();
    Model yatta=yw.getModel();

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

    // Now build the schema
    SchemaObject root = s.createSchemaObject(rootnode,new Object[]{"root","list"});
    SchemaObject course = s.createSchemaObject(yattanode,new Object[]{root, "course", "set", "+"});
    SchemaObject code = s.createSchemaObject(yattanode,new Object[]{course, "code", "string", "k"});
    SchemaObject dept = s.createSchemaObject(yattanode,new Object[]{course, "dept", "string", ""});
    SchemaObject cd = s.createSchemaObject(sequence,new Object[]{"",code,dept});
    SchemaObject building = s.createSchemaObject(yattanode,new Object[]{course, "building", "string", ""});
    SchemaObject db = s.createSchemaObject(sequence,new Object[]{"",dept,building});
    SchemaObject student = s.createSchemaObject(yattanode,new Object[]{course, "ug_pg", "set", "*"});
    SchemaObject bs = s.createSchemaObject(sequence,new Object[]{"",building,student});
    SchemaObject name = s.createSchemaObject(yattanode,new Object[]{student, "name", "string", "k"});
    SchemaObject level = s.createSchemaObject(yattanode,new Object[]{student, "level", "string", ""});
    SchemaObject nl = s.createSchemaObject(sequence,new Object[]{"",name,level});
    SchemaObject year = s.createSchemaObject(yattanode,new Object[]{student, "year", "integer", ""});
    SchemaObject ly = s.createSchemaObject(sequence,new Object[]{"",level,year});
    SchemaObject grade = s.createSchemaObject(yattanode,new Object[]{student, "grade", "string", "?"});
    SchemaObject yg = s.createSchemaObject(sequence,new Object[]{"",year,grade});

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

    return s;
  }

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

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

      String yattaS1 = "ug";
      if(Schema.exists(yattaS1))
	  Schema.retract(yattaS1);

      Schema s1=createYATTAS1("ug");
      s1.listSchema(System.out,Schema.LIST_SCHEMA_NORMAL,true);

      String yattaS2 = "student";
      if(Schema.exists(yattaS2))
	  Schema.retract(yattaS2);

      Schema s2=createYATTAS2("student");
      s2.listSchema(System.out,Schema.LIST_SCHEMA_NORMAL,true);

      //create accessMethod
      Protocol p;
      String protocolName="yatta";
      if(Protocol.exists(protocolName))
      {
	  p=Protocol.getProtocol(protocolName);
      }
      else
      {
	  p=Protocol.createProtocol(protocolName);
      }
      String driver="uk.ac.ic.doc.automed.wrappers.YattaWrapper";
      AccessMethod amy1=AccessMethod.createAccessMethod(s1,
        "http://www.doc.ic.ac.uk/~sk297/examples/CAISE05/F1", 
        p, driver, "",null,driver,0);
      AccessMethod amy2=AccessMethod.createAccessMethod(s2, 
        "http://www.doc.ic.ac.uk/~sk297/examples/CAISE05/F2", 
        p,driver, "",null,driver,0 );

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












