
/**
 * DBLPXMLtoSQLTransform.java
 *
 *
 *
 * @author Andrew Charles Smith
 *
 * Created on 09 March 2006
 * Department of Computing, Imperial College
 */



//~--- non-JDK imports --------------------------------------------------------

import uk.ac.ic.doc.automed.*;
import uk.ac.ic.doc.automed.reps.Construct;
import uk.ac.ic.doc.automed.reps.HDMToHighLevel;
import uk.ac.ic.doc.automed.reps.HDMTransformations;
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.Transformation;
import uk.ac.ic.doc.automed.util.Debugger;
import uk.ac.ic.doc.automed.util.Logger;
import uk.ac.ic.doc.automed.wrappers.AutoMedString;
import uk.ac.ic.doc.automed.wrappers.AutoMedType;
import uk.ac.ic.doc.automed.wrappers.AutoMedWrapper;
import uk.ac.ic.doc.automed.wrappers.SQLWrapperFactory;
import uk.ac.ic.doc.automed.wrappers.XMLSchemaWrapperFactory;

//~--- classes ----------------------------------------------------------------

public class DBLPXMLtoSQLTransform {

    // Location of XML file
    // private static String xmlFile = "file:/homes/acs203/DevAutoMed/automed/java/examples/publicationsP2PDemo/dblpsmall.xml";
    private static String xmlFile =
        "file:c:/netbeansprojs/automed/java/examples/publicationsP2PDemo/dblpsmall.xml";

    // private static String xmlFile = "file:c:/netbeansprojs/automed/java/examples/publicationsP2PDemo/dblpimproved.xml";

    private static String username            = "automed";
    private static String sensorSrcSchemaName = "dblp";
    private static String sensorSchemaName    = "dblp";
    private static String password            = "automed";

    //~--- constructors -------------------------------------------------------

    /**
     * Creates a new instance of the demo
     */
    public DBLPXMLtoSQLTransform() {}

    //~--- methods ------------------------------------------------------------

    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 DBLP XML schema
        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();

        // integrate two schemas
        Schema intSchema = null;

        intSchema = transformXML2SQL(xmlSchema);
    }

    // integrate 2 compatible schemas
    public static Schema transformXML2SQL(Schema xmlSchema)
            throws AutoMedException {

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

        System.out.println("HDM version of XML Schema");
        hdmSchema.listSchema();

        // Transform the HDM from hierarchical to two level
        HDMTransformations trans       = new HDMTransformations(hdmSchema);
        SchemaObject       tempObject1 =
            hdmSchema.getSchemaObject("<<HDMdblp>>");
        SchemaObject       tempObject2 =
            hdmSchema.getSchemaObject("<<_,HDMdblp,HDMdblparticle>>");

        hdmSchema   = trans.inclusionExpand(tempObject1, tempObject2);
        trans       = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMdblparticle>>");
        tempObject2 = hdmSchema.getSchemaObject(
            "<<_,HDMdblparticle,HDMdblparticleauthor>>");
        hdmSchema   = trans.inclusionExpand(tempObject1, tempObject2);
        trans       = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMdblparticle>>");
        tempObject2 = hdmSchema.getSchemaObject("<<HDMdblparticlekey>>");
        hdmSchema   = trans.moveInclusion(tempObject1, tempObject2);
        trans       = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMdblp>>");
        tempObject2 =
            hdmSchema.getSchemaObject("<<_,HDMdblp,HDMdblpinproceedings>>");
        hdmSchema   = trans.inclusionExpand(tempObject1, tempObject2);
        trans       = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMdblpinproceedings>>");
        tempObject2 = hdmSchema.getSchemaObject(
            "<<_,HDMdblpinproceedings,HDMdblpinproceedingsauthor>>");
        hdmSchema = trans.inclusionExpand(tempObject1, tempObject2);

//      trans = new HDMTransformations(hdmSchema);
//      tempObject1 = hdmSchema.getSchemaObject("<<HDMdblp>>");
//      tempObject2 = hdmSchema.getSchemaObject("<<_,HDMdblp,HDMdblpproceedings>>");
//      hdmSchema = trans.inclusionExpand(tempObject1, tempObject2);
        trans       = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMdblp>>");
        tempObject2 = hdmSchema.getSchemaObject("<<_,HDMdblp,HDMdblpwww>>");
        hdmSchema   = trans.inclusionExpand(tempObject1, tempObject2);
        trans       = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMdblpinproceedings>>");
        tempObject2 = hdmSchema.getSchemaObject(
            "<<_,HDMdblpinproceedings,HDMdblpinproceedingscite>>");
        hdmSchema   = trans.inclusionExpand(tempObject1, tempObject2);
        trans       = new HDMTransformations(hdmSchema);
        tempObject1 = hdmSchema.getSchemaObject("<<HDMdblpinproceedings>>");
        tempObject2 = hdmSchema.getSchemaObject("<<HDMdblpinproceedingskey>>");
        hdmSchema   = trans.moveInclusion(tempObject1, tempObject2);
        System.out.println("Transformed HDM schema");
        hdmSchema.listSchema();

        // Now convert the HDM schema to SQL
        HDMToHighLevel hdmToSQL  = new HDMToHighLevel(hdmSchema);
        Schema         sqlSchema = hdmToSQL.hdmToRelational();

        System.out.println("New SQL Schema");
        sqlSchema.listSchema();

        return sqlSchema;
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
