/**
 * RecursiveXML.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;

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

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

  private static String sensorSrcSchemaName = "staff";
  private static String sensorSchemaName = "staff";
  
  /**
   * Creates a new instance of AutoMedPeerApplication
   */
  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 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);

      // 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);
      // 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);
    }
 
  }
