WepsConnection.java [src/usda/weru/weps/reports/query] Revision: default  Date:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.weps.reports.query;
// Note: this code was moved from a WEPS package.
// This is a fairly major rewrite to produce a class that minimizes code changes
//   in the rest of the files taht were ported over.
//package usda.weru.weps.reports.query;

/**
 *
 * @author jplyon
 */

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import usda.weru.util.Util;
import usda.weru.weps.RunFileData;

/**
 * Workhorse class of the weps reporting.  This connection class extends the H2
 * databases's JdbcConnection and connects to the H2 engine as a memory only 
 * database.  This database is specific to this connection.  A single connection
 * may be used for more than one report.  This is often desirable because 
 * WepsConnection caches loaded data until closed.  
 * @author joelevin
 */
public class WepsConnection {

    private static final Logger LOGGER = Logger.getLogger(WepsConnection.class.getName());
    public static final String PROP_UNITS = "units";
    private String c_units = Util.SIUnits;

    ////////////////////////////////////////////////////////////////////////////
    // New member variables.
    
    // Hash map from the names of the parameters (columns) to their arrayed data.
    protected HashMap<String, ArrayList<Object>> m_ParamValues;

    ////////////////////////////////////////////////////////////////////////////

    public WepsConnection() {
        m_ParamValues = new HashMap<String, ArrayList<Object>> ();
    }

    private void checkParams(Properties info) {
        //check the units value
        if (info.containsKey(PROP_UNITS)) {
            String newUnits = info.getProperty(PROP_UNITS);
            if (Util.SIUnits.equals(newUnits) || Util.USUnits.equals(newUnits)) {
                c_units = newUnits;
            } else {
//                throw new SQLException("Unrecognized units value: " + newUnits);
            }
        }
    }

    public HashMap<String, ArrayList<Object>> getParamValues()
    {
        return m_ParamValues;
    }
    
    public void setUnits(String units) {
        String old = c_units;
        c_units = units;
    }

    public String getUnits() {
        return c_units;
    }

//    public void clearWepsData() {
//        synchronized (this) {
//            if (c_data != null) {
//                c_data.clear();
//            }
//        }
//    }
//
//    public void clearWepsData(String dataname) {
//        synchronized (this) {
//            if (c_data != null) {
//                c_data.remove(dataname);
//            }
//        }
//    }
//
    @Override
    public String toString() {
        StringBuffer buffer = new StringBuffer();

//        buffer.append(this.m_ParamValues.toString() + "\n");
        buffer.append("{\n");
        int i=0; int nParams = m_ParamValues.size();
        for (Map.Entry<String, ArrayList<Object>> entry : m_ParamValues.entrySet()) {
            String param = entry.getKey();
            buffer.append("\"" + param + "\":");
            ArrayList<Object> values = entry.getValue();
            buffer.append(values.toString());
            buffer.append((++i < nParams)? ",\n" : "\n");
        }       

        buffer.append("}\n");
        return buffer.toString();
    }

//    protected ResultSet getWepsData(String dataname, boolean fill) {
////        ReportContext.enter();
//        try {
//            synchronized (this) {
//                if (c_data == null) {
//                    c_data = new HashMap<String, WepsResultSet>();
//                }
//                WepsResultSet data = c_data.get(dataname);
//
//                if (data == null) {
//                    //No cached data so try to create a new resultset.
//                    if (RunsResultSet.NAME.equals(dataname)) {
//                        data = new RunsResultSet(this);
//                    } else if (StirEnergyResultSet.NAME.equals(dataname)) {
//                        data = new StirEnergyResultSet(this);
//                    } else if (CropRotationResultSet.NAME.equals(dataname)) {
//                        data = new CropRotationResultSet(this);
//                    } else if (SciEnergyResultSet.NAME.equals(dataname)) {
//                        data = new SciEnergyResultSet(this);
//                    } else if (NotesResultSet.NAME.equals(dataname)) {
//                        data = new NotesResultSet(this);
//                    } else if (SoilsResultSet.NAME.equals(dataname)) {
//                        data = new SoilsResultSet(this);
//                    } else if (BarriersResultSet.NAME.equals(dataname)) {
//                        data = new BarriersResultSet(this);
//                    } else if (OutputResultSet.NAME.equals(dataname)) {
//                        data = new OutputResultSet(this);
//                    } else if (HarvestsResultSet.NAME.equals(dataname)) {
//                        data = new HarvestsResultSet(this);
//                    } else if (ManagementResultSet.NAME.equals(dataname)) {
//                        data = new ManagementResultSet(this);
//                    } else if (OutputOpCropResultSet.NAME.equals(dataname)) {
//                        data = new OutputOpCropResultSet(this);
//                    } else if (ManagementNotesResultSet.NAME.equals(dataname)) {
//                        data = new ManagementNotesResultSet(this);
//                    }else if (FuelsResultSet.NAME.equals(dataname)) {
//                        data = new FuelsResultSet(this);
//                    } 
//                    else if (DatabaseCropsResultSet.NAME.equals(dataname)) {
//                        data = new DatabaseCropsResultSet(this);
//                    }
//                    else if (DatabaseOperationsResultSet.NAME.equals(dataname)) {
//                        data = new DatabaseOperationsResultSet(this);
//                    }
//                     else if (DatabaseManagementsResultSet.NAME.equals(dataname)) {
//                        data = new DatabaseManagementsResultSet(this);
//                    }
//                    else if (ConfidenceIntervalResultSet.NAME.equals(dataname)) {
//                        data = new ConfidenceIntervalResultSet(this);
//                    }
//                     //other resultsets go here
//                    else {
//                        //this isn't a known resultset
//                        throw new SQLException("Unknown WEPS ResultSet: " + dataname);
//                    }
//                    c_data.put(data.getName(), data);
//                }
//
//                if (fill) {
//                    data.fill();
//                }
//                return data;
//
//            }
//        } finally {
////            ReportContext.exit();
//        }
//    }
//
//    public synchronized File[] getRunFiles() {
//        if (c_runFiles == null) {
//            //create the list of runs            
//            c_runFiles = new LinkedList<File>();
//
//            if (c_rootFiles == null) {
//                String[] roots = c_rootString.split(",", -1);
//                File[] rootFiles = new File[roots.length];
//                for (int i = 0; i < roots.length; i++) {
//                    rootFiles[i] = new File(roots[i]);
//                }
//                buildFileList(c_runFiles, rootFiles);
//            } else {
//                buildFileList(c_runFiles, c_rootFiles);
//            }
//
//        }
//        return c_runFiles.toArray(new File[c_runFiles.size()]);
//    }
//
//    private void buildFileList(List<File> toKeep, File[] roots) {
//        if (roots == null) {
//            return;
//        }
//        for (File root : roots) {
//            if (accept(root) && !toKeep.contains(root)) {
//                toKeep.add(root.getCanOrAbsFile());
//            } else if (root.isDirectory()) {
//                buildFileList(toKeep, root.listFiles(File.getDefaultArchiveDetector()));
//            }
//        }
//    }
//
    private boolean accept(File file) {
        return file.isDirectory() && file.getName().toLowerCase().endsWith(RunFileData.RunSuffix);
    }

}