/**
 * RecursiveXML.java
 *
 * Demo to show how a recursive XML Schema can be transformed into SQL
 *
 * @author Andrew Charles Smith
 *
 * Created on 09 March 2006
 * Department of Computing, Imperial College
 */
package test;

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;

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

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

  private static String recursiveSrcSchemaName = "staff";
  private static String recursiveSchemaName = "staff";
  
  /**
   * Creates a new instance of the class
   */
  public RecursiveXML() {
  }

  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
    String driver = "uk.ac.ic.doc.automed.wrappers.XMLSchemaWrapper";
    if (Schema.exists(recursiveSrcSchemaName)) Schema.retract(recursiveSrcSchemaName);
    AutoMedWrapper swxml=
        AutoMedWrapper.selectNewAutoMedWrapper(username,password,null,driver,
                                       xmlFile,recursiveSrcSchemaName,xwf);

    Schema sourceXMLSchema=swxml.getSchema();
    // Generate an AutoMed oriented schema
    Schema xmlSchema=swxml.newAutoMedSchema(recursiveSchemaName);

    // Convert the XML Schema to HDM and then to SQL
    HighLevelToHDM hltohdm = new HighLevelToHDM(xmlSchema);

    Schema hdmSchema = hltohdm.TransformToHDM("xmlschema",0);
    // Transform the HDM from hierarchical to two level
    HDMTransformations trans = new HDMTransformations(hdmSchema);
    SchemaObject tempObject1 = hdmSchema.getSchemaObject("<<HDMstaffdept>>");
    SchemaObject tempObject2 = hdmSchema.getSchemaObject("<<HDMstaffdeptperson>>");
    hdmSchema = trans.removeRecursion(tempObject1, tempObject2,"HDMperson","HDMperson_container");
    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("<<HDMperson_container>>");
    tempObject2 = hdmSchema.getSchemaObject("<<_,HDMperson_container,HDMperson>>");
    hdmSchema = trans.inclusionExpand(tempObject1,tempObject2);
    
    // Now convert the HDM schema to SQL
    HDMToHighLevel hdmToSQL  = new HDMToHighLevel(hdmSchema);
    Schema         sqlSchema = hdmToSQL.hdmToRelational();
  }
 
}
