<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">package usda.weru.nrmv;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import org.openide.util.Exceptions;

/**
 *
 * @author Benjamin.Todd
 */
public abstract class NrmvConverter {

    protected String fileToReadFrom, fileToWriteTo, name,
            runName, dailyWriteTo, monthlyWriteTo, yearlyWriteTo,
            date1, date2, myType, myPath;
    protected ArrayList&lt;String&gt; units, variables, previousAl;
    protected boolean shouldDeleteWarmUp,
            shouldContinue, secondDateNeedsToBeSet,
            secondDate, doubleFirstDate, shouldDoSummary = true;

    protected FileWriter fw;
    protected PrintWriter pw;
    protected FileReader fr;
    protected BufferedReader br;
    protected int linesToSkip;

    public void convert() {
        this.intitiateFileIo();
        this.setLinesToSkip();
        this.setVariables();
        this.setUnits();
        this.readLines(br);
        this.printHeader(name, name, pw, variables);
        this.printUnits(pw);

        this.readAsYouGo(pw, br);
        this.endPrint(pw, fw, fr, br);
    }
    
    //Abstract methods

    public abstract void setLinesToSkip();

    public abstract void setVariables();

    public abstract void setUnits();

    public abstract ArrayList&lt;String&gt; changeData(ArrayList&lt;String&gt; al);

    public abstract void printSummaryUnits(PrintWriter pw);

    public void printUnits(PrintWriter pw) {
        pw.println();

        pw.print("unit");
        this.printLineCommas(pw, units);
        pw.println();
    }

    public void readLines(BufferedReader br) {
        try {
            for (int i = 1; i &lt;= linesToSkip; i++) {
                br.readLine();
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    public void intitiateFileIo() {
        try {
            fr = new FileReader(fileToReadFrom);
        } catch (FileNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        }
        br = new BufferedReader(fr);
        try {
            fw = new FileWriter(this.dailyWriteTo);
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        pw = new PrintWriter(fw);
    }

    /**
     *
     * @param pw
     * @param aList
     */
    public void printLineCommas(PrintWriter pw, ArrayList&lt;String&gt; aList) {
        for (String word : aList) {
            pw.print("," + word);
        }
    }

    /**
     *
     * @param name
     * @param type
     * @param pw
     * @param variables
     */
    public void printHeader(String name, String type, PrintWriter pw, ArrayList&lt;String&gt; variables) {
        myType = type;
        pw.println("@T, " + type);
        pw.println("type, " + type);
        pw.print("@H");
        printLineCommas(pw, variables);
    }

    /**
     *
     * @param pw
     * @param fw
     * @param fr
     * @param br
     */
    public void endPrint(PrintWriter pw, FileWriter fw, FileReader fr, BufferedReader br) {
        pw.close();
        try {
            fr.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        try {
            br.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        try {
            fw.close();
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    /**
     *
     * @param name
     */
    public void setRunName(String name) {
        runName = name;
        //System.out.println("run name is " + runName);
    }

    /**
     *
     * @param path
     * @param name
     */
    public void setFileToReadFrom(String path, String name) {
        fileToReadFrom = path + "/" + name;
        //System.out.println("File to read from is " + fileToReadFrom);
        //Calendar ca;
    }

    /**
     *
     * @param path
     * @param n
     */
    public void setFileToWriteTo(String path, String n) {
        name = n;
        myPath = path + "/";
        fileToWriteTo = path + "/" + name;
        dailyWriteTo = path + "/" + name + "-daily.csv";
        monthlyWriteTo = path + "/" + name + "-monthly.csv";
        yearlyWriteTo = path + "/" + name + "-yearly.csv";
        // System.out.println("File to Write to is " + fileToWriteTo);
    }
    //This method is intended if the variables need to be read in from a line of code
    //Currently most of the conversion files do not use this method

    /**
     *
     * @param s
     */
    /* public void setVariables(String s) {
     StringTokenizer st;
     variables = new ArrayList&lt;String&gt;();
     st = new StringTokenizer(s);
     while (st.hasMoreTokens()) {
     variables.add(st.nextToken());
     }
     }
     */
    /**
     *
     * @param number
     * @return
     */
    public String convertNumber(int number) {
        number--;
        GregorianCalendar ca = new GregorianCalendar();
        ca.set(Calendar.YEAR, 2001);
        ca.set(Calendar.DAY_OF_YEAR, 1);
        ca.add(Calendar.DAY_OF_MONTH, number);
        SimpleDateFormat df = new SimpleDateFormat();
        df.applyPattern("MM/dd/yyyy");
        //df.applyPattern("yyyy-MM-dd");

        return df.format(ca.getTime());
    }

    /**
     *
     * @param st
     * @return
     */
    public boolean doneReadingWarmUp(ArrayList&lt;String&gt; st) {
        String myDate = st.get(0);

        if (myDate.equalsIgnoreCase(date2)) {
            secondDate = true;
        }
        if (secondDate) {
            if (myDate.equalsIgnoreCase(date1)) {
                doubleFirstDate = true;
            }
        }
        if (doubleFirstDate) {
            return true;
        }

        return false;
    }

    /**
     *
     */
    public void doNotContinue() {
        shouldContinue = false;
    }

    /**
     *
     * @return
     */
    public boolean returnSecond() {
        return secondDateNeedsToBeSet;
    }

    /**
     * This method reads every line from a certain point forward
     * This method also eliminates the warm up by setting flags for 2 equivelent dates
     * This method does not read in blank lines getting rid of spaces as rows
     * the change data methjod in the middle must be written by the user in order to manipulate the data
     *
     * @param al
     */
    public void correctDoubleDecimal(ArrayList&lt;String&gt; al) {
        for (int j = 0; j &lt; al.size(); j++) {
            String checkString = al.get(j);
            if (checkDoubleDecimal(checkString)) {
                String first = checkString.substring(0, checkString.lastIndexOf(".") - 1);
                String second = checkString.substring(checkString.lastIndexOf(".") - 1);
                al.set(j, first);
                al.add(j + 1, second);
            } else if (checkString.contains("*")) {
                al.set(j, checkString.substring(0, checkString.indexOf("*")));
                al.add(j + 1, "0");
            }
        }
    }

    public boolean checkDoubleDecimal(String str) {
        String[] tokens = str.split("\\.");
        return tokens.length == 3;
    }

    public void readAsYouGo(PrintWriter pw, BufferedReader br) {

        AverageConverter av = new AverageConverter(this, this.variables, this.myType, myPath);
        LayerAverageConverter lc = new LayerAverageConverter(this, this.variables, this.monthlyWriteTo,
                this.yearlyWriteTo, this.myType, myPath);

        shouldContinue = true;
        date1 = "";
        date2 = "";
        shouldDeleteWarmUp = true;
        String str;
        secondDateNeedsToBeSet = true;
        boolean firstRead = true;
        try {
            previousAl = new ArrayList&lt;String&gt;();
            ArrayList&lt;String&gt; doublePreviousAl = new ArrayList&lt;String&gt;();
            while ((str = br.readLine()) != null) {

                ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;();
                StringTokenizer st = new StringTokenizer(str);

                if (st.countTokens() == 0) {
                } else {

                    while (st.hasMoreTokens()) {
                        al.add(st.nextToken());

                    }
                    //The change data method should be different for almost all classes
                    //this manipulates the data after it is read right before it goes out
                    al = changeData(al);
                    correctDoubleDecimal(al);
                    if (shouldContinue) {
                        if (firstRead) {
                            date1 = al.get(0);
                            firstRead = false;
                        }
                        if (secondDateNeedsToBeSet) {
                            String firstString = al.get(0);
                            if (!firstString.equalsIgnoreCase(date1)) {
                                date2 = firstString;
                                secondDateNeedsToBeSet = false;

                                if (this.hasMoreThanOneLayer()) {
                                    setLayerAmount(doublePreviousAl);
                                }
                                if (addToLayers() &amp;&amp; shouldDoSummary) {
                                    lc.createConverters(getLayerAmount());
                                }
                            }
                        }
                        if (doneReadingWarmUp(al) || !shouldDeleteWarmUp) {
                            if (addToSum() &amp;&amp; shouldDoSummary) {
                                av.addToSum(al);
                            }
                            if (addToLayers() &amp;&amp; shouldDoSummary) {
                                lc.addToSum(al);
                            }
                            this.printLineCommas(pw, al);
                            pw.println();
                        }
                    }
                }
                doublePreviousAl = new ArrayList&lt;String&gt;(previousAl);
                previousAl = new ArrayList&lt;String&gt;(al);
            }

        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        }
        if (addToSum() &amp;&amp; shouldDoSummary) {
            av.finish();
            av.printOut(this.monthlyWriteTo, this.yearlyWriteTo);
        }
        if (addToLayers() &amp;&amp; shouldDoSummary) {
            lc.printOut();
        }

    }
    /*
     * These boolean statements should be overwritten depending on the 
     * file that  is being converted
     */

    public boolean addToSum() {
        return true;
    }

    public boolean addToLayers() {
        return false;
    }

    public void setLayerAmount(ArrayList&lt;String&gt; al) {
    }

    public int getLayerAmount() {
        return 1;
    }

    public boolean hasMoreThanOneLayer() {
        return false;
    }

    public void setSummaryOnOff(boolean b) {
        shouldDoSummary = b;
    }
}
</pre></body></html>