/**
 * CVSTest.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.reps.*;
import uk.ac.bbk.dcs.automed.qproc.*;
import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.bbk.dcs.automed.qproc.reformulate.LAVQueryReformulationException;

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.wrappers.CSVWrapperFactory;
//import utils.AutoMedUtils;

public class CSVTest {
  
  // Location of CSV file
  //private static String xmlFile = "file:/homes/acs203/DevAutoMed/automed/java/examples/data/sensorData.xml";
  private static String csvFile = "file:D:/netbeansprogs/automed/java/examples/data/csvfile.txt";

  // Dummy constants for username and password
  private static String username = "";
  private static String password = "";  

  private static String csvSchemaName = "csvSchema";
  
  /**
   * Creates a new instance of CSV test
   */
  public CSVTest() {
  }


  public static void main(String args[]) throws Exception {
    
    Debugger.setSectionDebugLevel(args);
    DSRConfiguration.setDefaultConfigFileName(args);
      // Create an CSV wrapper factory
      CSVWrapperFactory xwf=new CSVWrapperFactory();
      // Wrap the CSV file
      String driver = "uk.ac.ic.doc.automed.wrappers.CSVWrapper";
      if (Schema.exists(csvSchemaName)) Schema.retract(csvSchemaName);
      AutoMedWrapper swxml=
        AutoMedWrapper.selectNewAutoMedWrapper(username,password,null,driver,
                                           csvFile,csvSchemaName,xwf);
      
    Schema sourceXMLSchema=swxml.getSchema();
    // Generate an AutoMed oriented schema
    Schema csvSchema=swxml.newAutoMedSchema(csvSchemaName);

    // must reload all models for constructs in new model
    Model.reloadModels();

    csvSchema.listSchema();

        Schema extSchemas[] = csvSchema.getAttachedSchemas(Schema.DATA_SOURCE_TYPE);
        String scheme = "<<root,element1>>";
        String integrationSemantics = "++";
        QueryProcessorConfiguration qpc = null;
        try {
            ASG asg = new ASG(scheme);
            QueryProcessor qp = new QueryProcessor();
            qpc = new QueryProcessorConfiguration();
            qp.process(asg, csvSchema, extSchemas, integrationSemantics, qpc);
            System.out.println("Extent of " + scheme + " in " + csvSchema.getName() + " is "+ asg.formattedString());
        } catch (QProcException e) {
            if ((e.getCause() instanceof LAVQueryReformulationException) &&
                (qpc.getReformulationSemantics() ==
                QueryProcessorConfiguration.REFORMULATION_LAV_INV_RULES)){
                // LAV query reformulation may not succeed always.
                // Its a valid limitation of LAV reformulation
                System.out.println("###LAV Issue:");
            } else {
                // This error should not occur.
                System.out.println("###Some Other Issue:");
            }
            qpc.printConfiguration();
            e.printStackTrace();
        }
    }
    

  }
