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 existing models
 */
public class DefineSchemas_nnyt98 {
  public static Schema createS1() 
    throws NotFoundException,TypeMismatchException,IntegrityException {
      Schema s = Schema.createSchema("s1",true);

      Model er=Model.getModel("er");
      Construct ent=er.getConstruct("entity");
      Construct att=er.getConstruct("attribute");
      Construct rel=er.getConstruct("relationship");

      SchemaObject s1_person = s.createSchemaObject(ent,new Object[]{"person"});
      s.createSchemaObject(att,new Object[]{s1_person,"id","key"});
      s.createSchemaObject(att,new Object[]{s1_person,"name","notnull"});
      s.createSchemaObject(att,new Object[]{s1_person,"sex","notnull"});
      System.out.println("Create schema s1 successfully");
      return s;
  }

  public static Schema createS2() 
    throws NotFoundException,TypeMismatchException,IntegrityException {
      Schema s = Schema.createSchema("s2",true);

      Model er=Model.getModel("er");

      Construct ent=er.getConstruct("entity");
      Construct att=er.getConstruct("attribute");
      Construct rel=er.getConstruct("relationship");
      Construct gen=er.getConstruct("generalisation");

      SchemaObject s2_employee = s.createSchemaObject(ent,new Object[]{"employee"});
      s.createSchemaObject(att,new Object[]{s2_employee,"id","key"});
      s.createSchemaObject(att,new Object[]{s2_employee,"name","notnull"});
      s.createSchemaObject(att,new Object[]{s2_employee,"sex","notnull"});
      SchemaObject s2_division = s.createSchemaObject(ent,new Object[]{"division"});
      s.createSchemaObject(att,new Object[]{s2_division,"dname","key"});
      s.createSchemaObject(att,new Object[]{s2_division,"location","null"});
      s.createSchemaObject(rel,new Object[]{"worksin",s2_employee,s2_division,"1:1","1:N"});
      System.out.println("Create schema s2 successfully");
      return s;
  }

  public static Schema createS3() 
    throws NotFoundException,TypeMismatchException,IntegrityException {
      Schema s = Schema.createSchema("s3",true);

      Model er=Model.getModel("er");
      Construct ent=er.getConstruct("entity");
      Construct att=er.getConstruct("attribute");
      Construct rel=er.getConstruct("relationship");
      Construct gen=er.getConstruct("generalisation");

      SchemaObject s3_person = s.createSchemaObject(ent,new Object[]{"person"});
      s.createSchemaObject(att,new Object[]{s3_person,"id","key"});
      s.createSchemaObject(att,new Object[]{s3_person,"name","notnull"});
      s.createSchemaObject(att,new Object[]{s3_person,"dname","notnull"});
      s.createSchemaObject(att,new Object[]{s3_person,"year","notnull"});
      SchemaObject s3_male = s.createSchemaObject(ent,new Object[]{"male"});
      SchemaObject s3_female = s.createSchemaObject(ent,new Object[]{"female"});
      s.createSchemaObject(gen,new Object[]{"sex","total",s3_person,s3_male,s3_female});
      System.out.println("Create schema s3 successfully");
      return s;
  }

  public static Schema createS4() 
    throws NotFoundException,TypeMismatchException,IntegrityException {
      Schema s = Schema.createSchema("s4",true);

      Model er=Model.getModel("er");
      Construct ent=er.getConstruct("entity");
      Construct att=er.getConstruct("attribute");
      Construct rel=er.getConstruct("relationship");

      SchemaObject s4_person = s.createSchemaObject(ent,new Object[]{"person"});
      s.createSchemaObject(att,new Object[]{s4_person,"id","key"});
      s.createSchemaObject(att,new Object[]{s4_person,"name","notnull"});
      SchemaObject s4_branch = s.createSchemaObject(ent,new Object[]{"branch"});
      s.createSchemaObject(att,new Object[]{s4_branch,"bname","key"});
      SchemaObject s4_dept = s.createSchemaObject(ent,new Object[]{"dept"});
      s.createSchemaObject(att,new Object[]{s4_dept,"dname","key"});
      s.createSchemaObject(rel,new Object[]{"basedat",s4_person,s4_branch,"1:1","1:N"});
      s.createSchemaObject(rel,new Object[]{"within",s4_branch,s4_dept,"1:1","1:N"});
      System.out.println("Create schema s4 successfully");
      return s;
  }

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

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

      Schema.retract("s1");
      Schema s1=createS1();
      s1.listSchema();

      Schema.retract("s2");
      Schema s2=createS2();
      s2.listSchema();

      Schema.retract("s3");
      Schema s3=createS3();
      s3.listSchema();

      Schema.retract("s4");
      Schema s4=createS4();
      s4.listSchema();
    }
    catch (AutoMedException ame) {
      ame.printStackTrace();
    }
  }
}

