package semistructured.xml.biomap;

import uk.ac.bbk.dcs.automed.xml.relational.XMLDSSFromRDB;
import uk.ac.bbk.dcs.automed.xml.wrappers.DOMWrapperFactory;
import uk.ac.ic.doc.automed.reps.Schema;
import uk.ac.ic.doc.automed.reps.SchemaObject;

/**
 * Class Task Description
 * 
 * @author <a href="mailto:lucas@dcs.bbk.ac.uk">Lucas Zamboulis </a>
 */
public class CATHWrapping {
    public static void main(String[] args) {
        System.out.println("Starting wrapping CATH data source: " + System.currentTimeMillis());
        String cathUsername = "cath";
        String password = "sqlex";
        String driver = "oracle.jdbc.driver.OracleDriver";
        String cathSchemaName = cathUsername.toUpperCase();
        String urlbase = "jdbc:oracle:thin:";
        String url = urlbase + '@' + "193.61.29.5:1521:teach";

        try {
            if (Schema.exists(cathSchemaName)) {
                System.out.print("Deleting old schema " + cathSchemaName + "...");
                Schema.retract(cathSchemaName);
                System.out.println("done.");
            }
            DOMWrapperFactory xwf = new DOMWrapperFactory();
            xwf.setFeatures(DOMWrapperFactory.WHITESPACE_COLLAPSE);

            XMLDSSFromRDB cathTool = new XMLDSSFromRDB(XMLDSSFromRDB.oracle, cathUsername, password, driver, url, cathSchemaName);

            manualTransformations(cathTool);
            
            Schema temp = cathTool.getXMLDSSSchema();
            temp.setName("CATH_final2");
            cathTool.setXMLDSSSchema(temp);
            
            System.out.println("Before list schema: " + System.currentTimeMillis());
            System.out.println("\nFinal Schema " + cathSchemaName + "\n");
            cathTool.getXMLDSSSchema().listSchema();
            System.out.println("After list schema: " + System.currentTimeMillis());
            System.out.println("Finished wrapping CATH data source: " + System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void manualTransformations(XMLDSSFromRDB tool) throws Exception {
        Schema localCopy = tool.getXMLDSSSchema();
        SchemaObject toBeRenamed;
        //rename cath_list.topology_id cath_topology_id
        toBeRenamed = localCopy.getSchemaObject("<<CATH$TOPOLOGY_ID$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "CATH$CATH_TOPOLOGY_ID$1" });
        //rename cath_list.superfam_id cath_superfam_id
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SUPERFAM_ID$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SUPERFAM_ID$1>>" });
        //rename cath_list.class_code cath_class_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$CLASS_CODE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_CLASS_CODE$1>>" });
        //rename cath_list.arch_code cath_arch_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$ARCH_CODE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_ARCH_CODE$1>>" });
        //rename cath_list.topology_code cath_topology_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$TOPOLOGY_CODE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_TOPOLOGY_CODE$1>>" });
        //rename cath_list.homology_code cath_homology_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$HOMOLOGY_CODE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_HOMOLOGY_CODE$1>>" });
        //rename cath_list.s35_code cath_s35_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$S35_CODE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_S35_CODE$1>>" });
        //rename cath_list.s95_code cath_s95_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$S95_CODE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_S95_CODE$1>>" });
        //rename cath_list.s100_code cath_s100_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$S100_CODE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_S100_CODE$1>>" });
        //rename cath_list.s100_count cath_s100_count
        toBeRenamed = localCopy.getSchemaObject("<<CATH$S100_COUNT$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_S100_COUNT$1>>" });
        //rename cath_list.domain_length cath_domain_length
        toBeRenamed = localCopy.getSchemaObject("<<CATH$DOMAIN_LENGTH$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_DOMAIN_LENGTH$1>>" });
        //rename cath_list.structure_resolution structure_resolution
        toBeRenamed = localCopy.getSchemaObject("<<CATH$STRUCTURE_RESOLUTION$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_STRUCTURE_RESOLUTION$1>>" });
        //rename cath_domains.protein_name cath_protein_name
        toBeRenamed = localCopy.getSchemaObject("<<CATH$PROTEIN_NAME$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_PROTEIN_NAME$1>>" });
        //rename cath_domains.protein_source cath_protein_source
        toBeRenamed = localCopy.getSchemaObject("<<CATH$PROTEIN_SOURCE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_PROTEIN_SOURCE$1>>" });
        //rename cath_domains.domain_length cath_domain_length
        toBeRenamed = localCopy.getSchemaObject("<<CATH$DOMAIN_LENGTH$2>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_DOMAIN_LENGTH$1>>" });
        //rename cath_domains.segment_number cath_segment_number
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SEGMENT_NUMBER$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SEGMENT_NUMBER$1>>" });
        //rename cath_segments.segment_number cath_segment_index
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SEGMENT_NUMBER$2>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SEGMENT_NUMBER$1>>" });
        //rename cath_segments.segment_range_start cath_segment_range_start
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SEGMENT_RANGE_START$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SEGMENT_RANGE_START$1>>" });
        //rename cath_segments.segment_range_stop cath_segment_range_stop
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SEGMENT_RANGE_STOP$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SEGMENT_RANGE_STOP$1>>" });
        //rename cath_segments.segment_length cath_segment_length
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SEGMENT_LENGTH$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SEGMENT_LENGTH$1>>" });
        //rename cath_segments.segment_sequence_header cath_segment_sequence_header
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SEGMENT_SEQUENCE_HEADER$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SEGMENT_SEQUENCE_HEADER$1>>" });
        //rename cath_segments.segment_aa_sequence cath_segment_aa_sequence
        toBeRenamed = localCopy.getSchemaObject("<<CATH$SEGMENT_AA_SEQUENCE$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_SEGMENT_AA_SEQUENCE$1>>" });
        //rename cath_names.class_code cath_class_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$CLASS_CODE$2>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_CLASS_CODE$1>>" });
        //rename cath_names.arch_code cath_arch_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$ARCH_CODE$2>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_ARCH_CODE$1>>" });
        //rename cath_names.topology_code cath_topology_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$TOPOLOGY_CODE$2>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_TOPOLOGY_CODE$1>>" });
        //rename cath_names.homology_code cath_homology_code
        toBeRenamed = localCopy.getSchemaObject("<<CATH$HOMOLOGY_CODE$2>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_HOMOLOGY_CODE$1>>" });
        //rename cath_names.name cath_name
        toBeRenamed = localCopy.getSchemaObject("<<CATH$NAME$1>>");
        localCopy = localCopy.applyRenameTransformation(toBeRenamed, new Object[] { "<<CATH$CATH_NAME$1>>" });

        //Set the copy in the tool to be the local copy
        tool.setXMLDSSSchema(localCopy);
    }
}