WepsReportData.java [tools/WepsReportData/src/wepsreportdata] Revision: c98511c63ebd28d7fde8e7ece29a41d0f3642520  Date: Mon May 08 13:38:30 MDT 2017
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wepsreportdata;

import usda.weru.util.Binaries;
import usda.weru.weps.RunFileData;
import usda.weru.weps.reports.query.*;

/**
 *
 * @author jplyon
 */
public class WepsReportData {
    
    ////////////////////////////////////////////////////////////
    // members and accessors
    
    /** Location of the input directory.
     * This utility will search for WEPS output files within this directory.
     */
    private String m_sInputDirectory = "";
    private static final String DETAIL_SOURCE = "/wepsreportdata/detail.xml";
    private static final String PATH_META = "tables/detail.xml";

    public boolean setInputDirectory(String sInputDirectory) {
        // @todo Check to see if this directory exists. If not return false.
        m_sInputDirectory = sInputDirectory;
        return true;
    }

    // The unit system to use for ouput.
    public String m_sUnits;
    
    // Fake database connection used by result set classes.
    WepsConnection m_con;

    ////////////////////////////////////////////////////////////
    
    public WepsReportData(WepsConnection con) {
        m_con = con;
    }
    
    public boolean loadFiles(String path,String stdfilename,String version){
        return loadFiles(path,stdfilename,version,1); // default is diesel so the conversionFactor should have no affect on the fuel
    }
    /**
     * Load WEPS output files into the data map.
     * @return true on success, false on failure.
     */
    public boolean loadFiles(String path,String stdfilename,String version,double conversionFactor) {
        try {
            
            // Try loading each of the output files in sequence.
            // Configuration is handled within each of these functions.
            // Failure will throw an exception.
            loadConfidenceIntervalResultSet();
            loadHarvestsResultSet(version);
            loadManagementResultSet();
            loadOutputResultSet(path);
            loadRunsResultSet(stdfilename);
            loadSciEnergyResultSet();
            loadStirEnergyResultSet(conversionFactor);
        }
        catch(Exception e) {
            
            return false;
        }
        
        return true;        
    }
    
    public boolean loadFiles(String path) {
        return loadFiles(path,RunFileData.StdOut,"",1);
    }

    /**
     * Load the WEPS output data from one of its text files.
     */
    public void loadConfidenceIntervalResultSet() {
        // @todo Check configuration options to see if we should load this file.
        ConfidenceIntervalResultSet resultset = new ConfidenceIntervalResultSet(m_con, m_sInputDirectory, m_sUnits);
        resultset.fill();        
    }

    /**
     * Load the WEPS output data from one of its text files.
     */
    public void loadHarvestsResultSet(String version) {
        // @todo Check configuration options to see if we should load this file.
        HarvestsResultSet resultset = new HarvestsResultSet(m_con, m_sInputDirectory, m_sUnits,version);
        resultset.fill();        
    }

    /**
     * Load the WEPS output data from one of its text files.
     */
    public void loadManagementResultSet() {
        // @todo Check configuration options to see if we should load this file.
        ManagementResultSet resultset = new ManagementResultSet(m_con, m_sInputDirectory, m_sUnits);
        resultset.fill();        
    }

    /**
     * Load the WEPS output data from one of its text files.
     * @param path
     */
    public void loadOutputResultSet(String path) {
        // @todo Check configuration options to see if we should load this file.
        try {
            Binaries.unpackResourceAbsolute(DETAIL_SOURCE, path +"/"+ PATH_META);
            OutputResultSet resultset = new OutputResultSet(m_con, m_sInputDirectory, m_sUnits);
            resultset.setWorkingDir(path);
            resultset.fill();        
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error parsing output result set in " + OutputResultSet.FILE_NAME);
            
        }
    }

    /**
     * Load the WEPS output data from one of its text files.
     */
    public void loadRunsResultSet(String stdfilename) {
        // @todo Check configuration options to see if we should load this file.
        RunsResultSet resultset = new RunsResultSet(m_con, m_sInputDirectory, m_sUnits,stdfilename);
        resultset.fill();        
    }

    /**
     * Load the WEPS output data from one of its text files.
     */
    public void loadSciEnergyResultSet() {
        // @todo Check configuration options to see if we should load this file.
        SciEnergyResultSet resultset = new SciEnergyResultSet(m_con, m_sInputDirectory, m_sUnits);
        resultset.fill();        
    }

    /**
     * Load the WEPS output data from one of its text files.
     * @param conversionFactor
     */
    public void loadStirEnergyResultSet(double conversionFactor) {
        // @todo Check configuration options to see if we should load this file.
        StirEnergyResultSet resultset = new StirEnergyResultSet(m_con, m_sInputDirectory, m_sUnits,conversionFactor);
        resultset.fill();        
    }

    ////////////////////////////////////////////////////////////
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        final String sUsage =
        "usage: wepsreportdata [options] infolder\n" +
        "Working dir path \n"+
        "\n";
        
        ////////////////////////////////////////////////////////////
        // Create and configure the translator.
        
        // Create a fake database connection object.
        WepsConnection con = new WepsConnection();
        
        // Create an instance of this class to do the actual loading.
        WepsReportData reportdata = new WepsReportData(con);
        
        ////////////////////////////////////////////////////////////
        // Get command line arguments.
        
        if (args.length < 2)
        {
            // Print a usage message and quit
            System.out.println(sUsage);
            return;
        }
        
        // Set the location of the folder to translate files in.
        String sInputDirectory = args[0];
        if (!reportdata.setInputDirectory(sInputDirectory)) {
            // Print a usage message and quit
            System.out.println("error: input directory doesn't exist.");
            return;
        }
        
        // @todo set configuration options.
        reportdata.m_sUnits = "US";
        
        // Load the file data.
        reportdata.loadFiles(args[1]);
        
        // Dump the loaded data as text for debugging purposes.
        System.out.println(con.toString());
        
    }
}