HarvestsResultSet.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;
import usda.weru.util.Util;
/**
*
* @author joelevin
*/
public class HarvestsResultSet extends WepsResultSet {
private static final Logger LOGGER = Logger.getLogger(HarvestsResultSet.class.getName());
public static final String NAME = "harvests";
public static final String COLUMN_PREFIX = NAME + ".";
// public static final String COLUMN_RUNID = COLUMN_PREFIX + "runid";
public static final String COLUMN_CYCLENUMBER = COLUMN_PREFIX + "cyclenumber";
public static final String COLUMN_HARVESTDATE = COLUMN_PREFIX + "harvestdate";
public static final String COLUMN_CROPINDEX = COLUMN_PREFIX + "cropindex";
public static final String COLUMN_CROP = COLUMN_PREFIX + "crop";
public static final String COLUMN_DRYYIELD = COLUMN_PREFIX + "dryyield";
public static final String COLUMN_RESIDUE = COLUMN_PREFIX + "residue";
public static final String COLUMN_INDEX= COLUMN_PREFIX + "index";
public static final String COLUMN_YIELD= COLUMN_PREFIX + "yield";
public static final String COLUMN_YIELDUNITS= COLUMN_PREFIX + "yieldunits";
public static final String COLUMN_MOISTURE= COLUMN_PREFIX + "moisture";
// private final WepsConnection c_con;
// private boolean c_filled;
private String version;
// New member variables
public String m_sDirectory;
public String m_sUnits;
public HarvestsResultSet(WepsConnection con, String sDirectory, String sUnits,String version) {
// c_con = con;
super(con);
m_sDirectory = sDirectory;
m_sUnits = sUnits;
this.version = version;
// addColumn(COLUMN_RUNID, Types.INTEGER, 10, 0);
addColumn(COLUMN_CYCLENUMBER, Types.INTEGER, 10, 0);
addColumn(COLUMN_HARVESTDATE, Types.DATE, 10, 0);
addColumn(COLUMN_CROPINDEX, Types.INTEGER, 10, 0);
addColumn(COLUMN_CROP, Types.VARCHAR, 255, 0);
addColumn(COLUMN_DRYYIELD, Types.DOUBLE, 10, 3);
addColumn(COLUMN_RESIDUE, Types.DOUBLE, 10, 3);
addColumn(COLUMN_INDEX, Types.DOUBLE, 10, 3);
addColumn(COLUMN_YIELD, Types.DOUBLE, 10, 3);
addColumn(COLUMN_YIELDUNITS, Types.VARCHAR, 255, 0);
addColumn(COLUMN_MOISTURE, Types.DOUBLE, 10, 3);
}
@Override
public String getName() {
return NAME;
}
protected boolean isUSUnits(){
return Util.USUnits.equals(m_sUnits);
}
public synchronized void fill() {
if(version.contains("1.3"))
fill_V1_3_9();
else
if(version.contains("1.5"))
fill_V1_5_1();
else // default
fill_V1_3_9();
}
private synchronized void fill_V1_3_9(){
DateFormat harvestDateFormat = new SimpleDateFormat("dd/MM/y");
File runDir = new File(m_sDirectory);
String harvestFileName = isUSUnits() ? "harvest_en.out" : "harvest_si.out";
File harvestFile = new File(runDir, harvestFileName);
if (harvestFile.exists()){
BufferedReader in = null;
try{
in = new BufferedReader(new FileReader(harvestFile));
int cycleNumber = 0;
String line;
while ((line = getLine(in)) != null ){
//each line is a new cycle
cycleNumber++;
String[] parts = line.split("\\|", -1);
//each crop is 12 columns
int cropCount = (parts.length - 1) / 12;
//loop over each crop
for (int cropIndex = 0; cropIndex < cropCount; cropIndex++){
int cropOffset = cropIndex * 12;
Object[] row = createNewRow(false);
// setRowValue(row, COLUMN_RUNID, runIndex);
setRowValue(row, COLUMN_CYCLENUMBER, cycleNumber);
setRowValue(row, COLUMN_CROPINDEX, cropIndex);
//harvest date
try{
String dateString = parts[cropOffset + 0].trim();
Date harvestDate = harvestDateFormat.parse(dateString);
setRowValue(row, COLUMN_HARVESTDATE, new java.sql.Date(harvestDate.getTime()));
}
catch(ParseException pe){
LOGGER.severe("Error parsing harvest date." + pe.getMessage());
}
//crop name
try{
String cropName = parts[cropOffset + 1].trim();
setRowValue(row, COLUMN_CROP, cropName);
}
catch(Exception e){
LOGGER.severe("Error reading crop name." + e.getMessage());
}
//dry yield
try{
Double dryYield = Double.valueOf(parts[cropOffset + 2].trim());
setRowValue(row, COLUMN_DRYYIELD, dryYield);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing dry yield." + nfe.getMessage());
}
//residue
try{
Double residue = Double.valueOf(parts[cropOffset + 4].trim());
setRowValue(row, COLUMN_RESIDUE, residue);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing residue." + nfe.getMessage());
}
//harvest index
try{
Double harvestIndex = Double.valueOf(parts[cropOffset + 6].trim());
setRowValue(row, COLUMN_INDEX, harvestIndex);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing harvest index." + nfe.getMessage());
}
//yield
try{
Double yield = Double.valueOf(parts[cropOffset + 8].trim());
setRowValue(row, COLUMN_YIELD, yield);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing harvest yield." + nfe.getMessage());
}
//yield units
try{
String yieldUnits = parts[cropOffset + 9].trim();
setRowValue(row, COLUMN_YIELDUNITS, yieldUnits);
}
catch(Exception e){
LOGGER.severe("Error reading yield units." + e.getMessage());
}
//% water
try{
Double mositure = Double.valueOf(parts[cropOffset + 10].trim());
setRowValue(row, COLUMN_MOISTURE, mositure);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing % moisture." + nfe.getMessage());
}
addRow(row);
}
}
}
catch (IOException ioe) {
LOGGER.severe("Error reading harvest file: " + harvestFile.getAbsolutePath() + ioe.getMessage());
}
finally{
if (in != null){
try{
in.close();
}
catch(Exception e){
LOGGER.severe("Error closing harvest file: " + harvestFile.getAbsolutePath() + e.getMessage());
}
}
}
}
}
//The index offset changed
private synchronized void fill_V1_5_1(){
DateFormat harvestDateFormat = new SimpleDateFormat("dd/MM/y");
File runDir = new File(m_sDirectory);
String harvestFileName = isUSUnits() ? "harvest_en.out" : "harvest_si.out";
File harvestFile = new File(runDir, harvestFileName);
if (harvestFile.exists()){
BufferedReader in = null;
try{
in = new BufferedReader(new FileReader(harvestFile));
int cycleNumber = 0;
String line;
while ((line = getLine(in)) != null ){
//each line is a new cycle
cycleNumber++;
String[] parts = line.split("\\|", -1);
//each crop is 12 columns
int cropCount = (parts.length - 1) / 13;
//loop over each crop
for (int cropIndex = 0; cropIndex < cropCount; cropIndex++){
int cropOffset = cropIndex * 13;
Object[] row = createNewRow(false);
// setRowValue(row, COLUMN_RUNID, runIndex);
setRowValue(row, COLUMN_CYCLENUMBER, cycleNumber);
setRowValue(row, COLUMN_CROPINDEX, cropIndex);
//harvest date
try{
String dateString = parts[cropOffset + 0].trim();
Date harvestDate = harvestDateFormat.parse(dateString);
setRowValue(row, COLUMN_HARVESTDATE, new java.sql.Date(harvestDate.getTime()));
}
catch(ParseException pe){
LOGGER.severe("Error parsing harvest date." + pe.getMessage());
}
//crop name
try{
String cropName = parts[cropOffset + 2].trim();
setRowValue(row, COLUMN_CROP, cropName);
}
catch(Exception e){
LOGGER.severe("Error reading crop name." + e.getMessage());
}
//dry yield
try{
Double dryYield = Double.valueOf(parts[cropOffset + 3].trim());
setRowValue(row, COLUMN_DRYYIELD, dryYield);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing dry yield." + nfe.getMessage());
}
//residue
try{
Double residue = Double.valueOf(parts[cropOffset + 5].trim());
setRowValue(row, COLUMN_RESIDUE, residue);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing residue." + nfe.getMessage());
}
//harvest index
try{
Double harvestIndex = Double.valueOf(parts[cropOffset + 7].trim());
setRowValue(row, COLUMN_INDEX, harvestIndex);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing harvest index." + nfe.getMessage());
}
//yield
try{
Double yield = Double.valueOf(parts[cropOffset + 9].trim());
setRowValue(row, COLUMN_YIELD, yield);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing harvest yield." + nfe.getMessage());
}
//yield units
try{
String yieldUnits = parts[cropOffset + 10].trim();
setRowValue(row, COLUMN_YIELDUNITS, yieldUnits);
}
catch(Exception e){
LOGGER.severe("Error reading yield units." + e.getMessage());
}
//% water
try{
Double mositure = Double.valueOf(parts[cropOffset + 11].trim());
setRowValue(row, COLUMN_MOISTURE, mositure);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing % moisture." + nfe.getMessage());
}
addRow(row);
}
}
}
catch (IOException ioe) {
LOGGER.severe("Error reading harvest file: " + harvestFile.getAbsolutePath() + ioe.getMessage());
}
finally{
if (in != null){
try{
in.close();
}
catch(Exception e){
LOGGER.severe("Error closing harvest file: " + harvestFile.getAbsolutePath() + e.getMessage());
}
}
}
}
}
//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;
}
}