/**
 * RecursiveXMLSchema2.java
 *
 *
 *
 * @author Andrew Charles Smith
 *
 * Created on 09 March 2006
 * Department of Computing, Imperial College
 */
import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.util.Debugger;
import uk.ac.ic.doc.automed.util.Logger;

import uk.ac.ic.doc.automed.wrappers.AutoMedWrapper;

import uk.ac.ic.doc.automed.reps.Construct;
import uk.ac.ic.doc.automed.reps.HDMToHighLevel;
import uk.ac.ic.doc.automed.reps.HighLevelToHDM;
import uk.ac.ic.doc.automed.reps.Model;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;
import uk.ac.ic.doc.automed.reps.HDMTransformations;
import uk.ac.ic.doc.automed.reps.Transformation;
import uk.ac.ic.doc.automed.wrappers.AutoMedString;
import uk.ac.ic.doc.automed.wrappers.AutoMedType;
import uk.ac.ic.doc.automed.wrappers.SQLWrapperFactory;
import uk.ac.ic.doc.automed.wrappers.XMLSchemaWrapperFactory;
//import utils.AutoMedUtils;

public class RecursiveXMLSchema2 {
  
  // Location of XML file
  //private static String xmlFile = "file:/homes/acs203/DevAutoMed/automed/java/examples/data/sensorData.xml";
  private static String xmlFile = "file:c:/netbeansprojs/automed/java/examples/data/staffnr.xml";

  private static String username = "automed";
  private static String password = "automed";  

  private static String sensorSrcSchemaName = "staff2";
  private static String sensorSchemaName = "staff2";
  
  /**
   * Creates a new instance of AutoMedPeerApplication
   */
  public RecursiveXMLSchema2() {
  }


  public static void main(String args[]) throws Exception {
    
        Debugger.setSectionDebugLevel(args);
        DSRConfiguration.setDefaultConfigFileName(args);
        // Create an XMLSchema wrapper factory
        XMLSchemaWrapperFactory xwf=new XMLSchemaWrapperFactory();
        // Wrap the XML schema from vehicle A
        String driver = "uk.ac.ic.doc.automed.wrappers.XMLSchemaWrapper";
        if (Schema.exists(sensorSrcSchemaName)) Schema.retract(sensorSrcSchemaName);
        AutoMedWrapper swxml=
        AutoMedWrapper.selectNewAutoMedWrapper(username,password,null,driver,
                                           xmlFile,sensorSrcSchemaName,xwf);
      
        Schema sourceXMLSchema=swxml.getSchema();
        // Generate an AutoMed oriented schema
        Schema xmlSchema=swxml.newAutoMedSchema(sensorSchemaName);

        //xmlSchema.listSchema(); 

        // Convert the XML Schema to HDM and then to SQL so we can match it
        HighLevelToHDM hltohdm = new HighLevelToHDM(xmlSchema);
        Schema hdmSchema = hltohdm.TransformToHDM("xmlschema",0);
        HDMTransformations trans = new HDMTransformations(hdmSchema);
        SchemaObject tempObject1 = hdmSchema.getSchemaObject("<<HDMstaffdept>>");
        SchemaObject tempObject2 = hdmSchema.getSchemaObject("<<HDMstaffdeptperson>>");
        hdmSchema = trans.removeRecursion(tempObject1, tempObject2, "HDMperson", "HDMpperson");
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMstaff>>");
        tempObject2 = hdmSchema.getSchemaObject("<<_,HDMstaff,HDMstaffdept>>");
        hdmSchema = trans.inclusionExpand(tempObject1, tempObject2);
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMstaff>>");
        hdmSchema = trans.idNodeExpand(tempObject1);
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMstaff>>");
        tempObject2 = hdmSchema.getSchemaObject("<<HDMstaff_pk>>");
        hdmSchema = trans.moveInclusion(tempObject1, tempObject2);
        
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMstaffdept>>");
        tempObject2 = hdmSchema.getSchemaObject("<<_,HDMstaffdept,HDMstaffdeptcountry>>");
        hdmSchema = trans.inclusionExpand(tempObject1, tempObject2);
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMstaffdept>>");
        hdmSchema = trans.idNodeExpand(tempObject1);
        // Move the inclusion constraint to depts primary key
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMstaffdept>>");
        tempObject2 = hdmSchema.getSchemaObject("<<HDMstaffdept_pk>>");
        hdmSchema = trans.moveInclusion(tempObject1, tempObject2);
        
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMpperson>>");
        tempObject2 = hdmSchema.getSchemaObject("<<_,HDMpperson,HDMperson>>");
        hdmSchema = trans.inclusionExpand(tempObject1, tempObject2);
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMpperson>>");
        hdmSchema = trans.idNodeExpand(tempObject1);
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMpperson>>");
        tempObject2 = hdmSchema.getSchemaObject("<<HDMpperson_pk>>");
        hdmSchema = trans.moveInclusion(tempObject1, tempObject2);
        
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMperson>>");
        hdmSchema = trans.idNodeExpand(tempObject1);
        trans = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMperson>>");
        tempObject2 = hdmSchema.getSchemaObject("<<HDMperson_pk>>");
        hdmSchema = trans.moveInclusion(tempObject1, tempObject2);
        // Now convert the HDM schema to SQL
        HDMToHighLevel hdmToSQL = new HDMToHighLevel(hdmSchema);
        Schema sqlSchema = hdmToSQL.hdmToRelational();
        System.out.println("New SQL Schema");
        sqlSchema.listSchema();
    }
       
  }
