ManagementResultSet.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;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

/**
 *
 * @author joelevin
 */
public class ManagementResultSet extends WepsResultSet {
    private static final Logger LOGGER = Logger.getLogger(ManagementResultSet.class.getName());
    public static final String NAME = "management";
    public static final String COLUMN_PREFIX = NAME + ".";
//    public static final String COLUMN_RUNID = COLUMN_PREFIX + "runid";
    public static final String COLUMN_DATE = COLUMN_PREFIX + "date";
    public static final String COLUMN_OPERATION = COLUMN_PREFIX + "operation";
    public static final String COLUMN_CROP = COLUMN_PREFIX + "crop";

    
//    private final WepsConnection c_con;
//    private boolean c_filled;

    // New member variables
    public String m_sDirectory;
    public String m_sUnits;

    public ManagementResultSet(WepsConnection con, String sDirectory, String sUnits) {
        super(con);
        m_sDirectory = sDirectory;
        m_sUnits     = sUnits;
        
//        c_con = con;
//        addColumn(COLUMN_RUNID, Types.INTEGER, 10, 0);        
        addColumn(COLUMN_DATE, Types.DATE, 0, 0);    
        addColumn(COLUMN_OPERATION, Types.VARCHAR, 255, 0);    
        addColumn(COLUMN_CROP, Types.VARCHAR, 255, 0);    
    }

    @Override
    public String getName() {
        return NAME;
    }
    
    public synchronized void fill() {
//        if (c_filled) {
//            return;
//        }
//        File[] files = c_con.getRunFiles(); 
        DateFormat harvestDateFormat = new SimpleDateFormat("dd/MM/y");
        
//        for (int runIndex = 0; runIndex < files.length; runIndex++) {
            File runDir = new File(m_sDirectory);
            File manageFile = new File(runDir, "mandate.out");
            
            
            if (manageFile.exists()){                                                                   
                BufferedReader in = null;
                try{
                    in = new BufferedReader(new FileReader(manageFile));                    
                    String line = in.readLine();    //first line is uncommented jibberish about number of years
                    while ((line = getLine(in)) != null ){
                        
                        Object[] row = createNewRow(false);
//                        setRowValue(row, COLUMN_RUNID, runIndex);
                       
                        String[] parts = line.split("\\|", -1);
                        
                        
                        //date
                        try{
                            String dateString = parts[0].trim();
                            Date harvestDate = harvestDateFormat.parse(dateString);
                            setRowValue(row, COLUMN_DATE, new java.sql.Date(harvestDate.getTime()));
                        }
                        catch(ParseException pe){
                            LOGGER.severe("Error parsing harvest date." + pe.getMessage());
                        }
                        
                        //operation name
                        try{
                            String operation = parts[1].trim();                                
                            setRowValue(row, COLUMN_OPERATION, operation);
                        }
                        catch(Exception e){
                            LOGGER.severe("Error reading crop name." + e.getMessage());
                        }
                        
                        //crop name
                        try{
                            String crop = parts[2].trim();                                
                            setRowValue(row, COLUMN_CROP, crop);
                        }
                        catch(Exception e){
                            LOGGER.severe("Error reading crop name." + e.getMessage());
                        }                        

                        addRow(row);
                    }
                }
                catch (IOException ioe) {
                    LOGGER.severe("Error reading mandate file: " + manageFile.getAbsolutePath() + ioe.getMessage());                    
                }                
                finally{
                    if (in != null){
                        try{
                            in.close();
                        }
                        catch(Exception e){
                            LOGGER.severe("Error closing mandate file: " + manageFile.getAbsolutePath() + e.getMessage());
                        }
                    }                    
                }
            }

                     
//        }                

//        c_filled = true;
    }
    
    //Skip comments and blank lines
    private String getLine(BufferedReader in) throws IOException {
        String temp;
        while ((temp = in.readLine()) != null) {
            temp = temp.trim();
            if (temp.length() == 0) {
                //blank line
                continue;
            }
            if (temp.charAt(0) != '#') {
                //not a comment
                return temp;
            }
        }
        return null;
    }

    
}