/**
 * CVSTest.java
 *
 *
 *
 * @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.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;

public class CSVTest {
  
  // Location of CSV file
  //private static String csvFile = "file:/homes/acs203/automed/java/examples/data/csvfile.xml";
  private static String csvFile = "file:c:/netbeansprojs/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 swcsv=
    AutoMedWrapper.selectNewAutoMedWrapper(username,password,null,driver,
                                           csvFile,csvSchemaName,xwf);
      
    Schema sourceCSVSchema=swcsv.getSchema();
    // Generate an AutoMed oriented schema
    Schema csvSchema=swcsv.newAutoMedSchema(csvSchemaName);

    csvSchema.listSchema();

    // Do a test query on the CSV schema
    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();
    }
  }
  
}
