RunsResultSet.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.Timestamp;
import java.sql.Types;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;
import usda.weru.util.ConversionCalculator;
import usda.weru.util.Util;
import usda.weru.weps.RunFileData;
/**
*
* @author joelevin
*/
public class RunsResultSet extends WepsResultSet {
private static final Logger LOGGER = Logger.getLogger(RunsResultSet.class.getName());
private String stdFileName;
// Names of the table and columns in the SQL data model.
public static final String NAME = "runs";
public static final String COLUMN_PREFIX = NAME + ".";
// public static final String COLUMN_RUNID = COLUMN_PREFIX + "runid";
public static final String COLUMN_NAME = COLUMN_PREFIX + "name";
public static final String COLUMN_NAMEEXT = COLUMN_PREFIX + "nameext";
public static final String COLUMN_LOCATION = COLUMN_PREFIX + "location";
public static final String COLUMN_CLIENT = COLUMN_PREFIX + "client";
public static final String COLUMN_FARM = COLUMN_PREFIX + "farm";
public static final String COLUMN_TRACT = COLUMN_PREFIX + "tract";
public static final String COLUMN_FIELD = COLUMN_PREFIX + "field";
public static final String COLUMN_MANAGE = COLUMN_PREFIX + "manage";
public static final String COLUMN_SOIL = COLUMN_PREFIX + "soil";
public static final String COLUMN_FIELDIMAGE = COLUMN_PREFIX + "fieldimage";
public static final String COLUMN_RUNDATE = COLUMN_PREFIX + "rundate";
public static final String COLUMN_MODE = COLUMN_PREFIX + "mode";
public static final String COLUMN_CYCLESCOUNT = COLUMN_PREFIX + "cyclescount";
public static final String COLUMN_STATE = COLUMN_PREFIX + "state";
public static final String COLUMN_COUNTY = COLUMN_PREFIX + "county";
public static final String COLUMN_SITE = COLUMN_PREFIX + "site";
public static final String COLUMN_LATITUDE = COLUMN_PREFIX + "latitude";
public static final String COLUMN_LONGITUDE = COLUMN_PREFIX + "longitude";
public static final String COLUMN_ELEVATION = COLUMN_PREFIX + "elevation";
public static final String COLUMN_FIELDSHAPE = COLUMN_PREFIX + "fieldshape";
public static final String COLUMN_FIELDXLENGTH= COLUMN_PREFIX + "fieldxlength";
public static final String COLUMN_FIELDYLENGTH = COLUMN_PREFIX + "fieldylength";
public static final String COLUMN_FIELDRADIUS = COLUMN_PREFIX + "fieldradius";
public static final String COLUMN_FIELDORIENTATION = COLUMN_PREFIX + "fieldorientation";
public static final String COLUMN_CLIGEN = COLUMN_PREFIX + "cligen";
public static final String COLUMN_WINDGEN = COLUMN_PREFIX + "windgen";
// private final WepsConnection c_con;
// private boolean c_filled;
// New member variables
public String m_sDirectory;
public String m_sUnits;
public RunsResultSet(WepsConnection con, String sDirectory, String sUnits,String stdfilename) {
// c_con = con;
super(con);
m_sDirectory = sDirectory;
m_sUnits = sUnits;
stdFileName = stdfilename;
// addColumn(COLUMN_RUNID, Types.INTEGER, 10, 0);
addColumn(COLUMN_NAME, Types.VARCHAR, 50, 0);
addColumn(COLUMN_NAMEEXT, Types.VARCHAR, 50, 0);
addColumn(COLUMN_LOCATION, Types.VARCHAR, 255, 0);
addColumn(COLUMN_CLIENT, Types.VARCHAR, 100, 0);
addColumn(COLUMN_FARM, Types.VARCHAR, 100, 0);
addColumn(COLUMN_TRACT, Types.VARCHAR, 50, 0);
addColumn(COLUMN_FIELD, Types.VARCHAR, 50, 0);
addColumn(COLUMN_MANAGE, Types.VARCHAR, 255, 0);
addColumn(COLUMN_SOIL, Types.VARCHAR, 255, 0);
addColumn(COLUMN_CLIGEN, Types.VARCHAR, 255, 0);
addColumn(COLUMN_WINDGEN, Types.VARCHAR, 255, 0);
addColumn(COLUMN_FIELDIMAGE, Types.BINARY, 0, 0);
addColumn(COLUMN_RUNDATE, Types.TIMESTAMP, 0, 0);
addColumn(COLUMN_MODE, Types.VARCHAR, 25, 0);
addColumn(COLUMN_CYCLESCOUNT, Types.INTEGER, 10, 0);
addColumn(COLUMN_STATE, Types.VARCHAR, 50, 0);
addColumn(COLUMN_COUNTY, Types.VARCHAR, 50, 0);
addColumn(COLUMN_SITE, Types.VARCHAR, 50, 0);
addColumn(COLUMN_LATITUDE, Types.DOUBLE, 10, 3);
addColumn(COLUMN_LONGITUDE, Types.DOUBLE, 10, 3);
addColumn(COLUMN_ELEVATION, Types.DOUBLE, 10, 3);
addColumn(COLUMN_FIELDSHAPE, Types.VARCHAR, 50, 0);
addColumn(COLUMN_FIELDXLENGTH, Types.DOUBLE, 10, 3);
addColumn(COLUMN_FIELDYLENGTH, Types.DOUBLE, 10, 3);
addColumn(COLUMN_FIELDRADIUS, Types.DOUBLE, 10, 3);
addColumn(COLUMN_FIELDORIENTATION, Types.DOUBLE, 10, 3);
}
@Override
public String getName() {
return NAME;
}
protected boolean isUSUnits(){
return Util.USUnits.equals(m_sUnits);
}
public synchronized void fill() /*throws SQLException*/ {
// if (c_filled) {
// return;
// }
// File[] files = c_con.getRunFiles();
// DrawField fieldPainter = null;
// RepBarPanel rbp = null;
int i = 0;
// for (int i = 0; i < files.length; i++) {
File runDir = new File(m_sDirectory);
Object[] row = createNewRow(false);
// setRowValue(row, COLUMN_RUNID, i);
//name with extension
String name = runDir.getName();
setRowValue(row, COLUMN_NAMEEXT, name);
//name with out extension
name = name.substring(0, name.length() - RunFileData.RunSuffix.length());
setRowValue(row, COLUMN_NAME, name);
RunFileData data = new RunFileData(runDir.getAbsolutePath(), false);
setRowValue(row, COLUMN_CLIENT, data.getData(RunFileData.UserName));
setRowValue(row, COLUMN_FARM, data.getData(RunFileData.FarmId));
setRowValue(row, COLUMN_TRACT, data.getData(RunFileData.TractId));
setRowValue(row, COLUMN_FIELD, data.getData(RunFileData.FieldId));
setRowValue(row, COLUMN_MANAGE, data.getData(RunFileData.ManageFile));
setRowValue(row, COLUMN_SOIL, data.getData(RunFileData.SoilFile));
// Station cligen = data.getBean().getCligenStation();
// setRowValue(row, COLUMN_CLIGEN, cligen != null ? cligen.getDisplayName() : null);
setRowValue(row, COLUMN_CLIGEN, data.getData("CliGenStationName"));
// Station windgen = data.getBean().getWindgenStation();
// setRowValue(row, COLUMN_WINDGEN, windgen != null ? windgen.getDisplayName() : null);
setRowValue(row, COLUMN_WINDGEN, data.getData("WindgenStationName"));
//run mode
setRowValue(row, COLUMN_MODE, data.getData(RunFileData.RunTypeDisp));
try{
Integer cyclescount = Integer.valueOf(data.getData(RunFileData.CycleCount));
setRowValue(row, COLUMN_CYCLESCOUNT, cyclescount);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing cylescount value.");
}
//site
// try{
// String buffer = null;
//
// for (Site site = data.getBean().getSite(); site != null; site = site.getParent()){
//
// if(buffer == null){
// buffer = site.getDisplayName();
//
// }
// else{
// buffer = site.getDisplayName() + ", " + buffer;
// }
// }
//
// setRowValue(row, COLUMN_SITE, buffer != null ? buffer : "");
// }
// catch(Exception e){
// LOGGER.severe("Error parsing site in file: " + runDir.getAbsolutePath());
// }
setRowValue(row, COLUMN_SITE, data.getData(RunFileData.Site));
DecimalFormat twoDecimalFormat = new DecimalFormat("#.##");
//lat/lon
String latlong = data.getData(RunFileData.LatLong);
String[] latlongParts = latlong.split(";", 2);
try{
Double lat = Double.valueOf(twoDecimalFormat.format(Double.valueOf(latlongParts[0].trim())));
setRowValue(row, COLUMN_LATITUDE, lat);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable able to parse latitude for file: " + runDir.getAbsolutePath());
}
try{
Double lon = Double.valueOf(twoDecimalFormat.format(Double.valueOf(latlongParts[1].trim())));
setRowValue(row, COLUMN_LONGITUDE, lon);
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable able to parse latitude for file: " + runDir.getAbsolutePath());
}
setRowValue(row, COLUMN_FIELDSHAPE, data.getData(RunFileData.Shape));
//radius
try{
Double radius = Double.valueOf(data.getData(RunFileData.Radius));
if (isUSUnits()){
radius = ConversionCalculator.convert(radius, "m", "ft");
}
setRowValue(row, COLUMN_FIELDRADIUS, radius);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing radius." + nfe.getMessage());
}
catch(ConversionCalculator.ConversionNotFoundException cnfe){
LOGGER.severe("Error converting radius value." + cnfe.getMessage());
}
catch(ConversionCalculator.UnitNotFoundException unfe){
LOGGER.severe("Error converting radius value." + unfe.getMessage());
}
//x length
try{
Double xLength = Double.valueOf(data.getData(RunFileData.XLength));
if (isUSUnits()){
xLength = ConversionCalculator.convert(xLength, "m", "ft");
}
setRowValue(row, COLUMN_FIELDXLENGTH, xLength);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing xlength." + nfe.getMessage());
}
catch(ConversionCalculator.ConversionNotFoundException cnfe){
LOGGER.severe("Error converting xlength value." + cnfe.getMessage());
}
catch(ConversionCalculator.UnitNotFoundException unfe){
LOGGER.severe("Error converting xlength value." + unfe.getMessage());
}
//y length
try{
Double yLength = Double.valueOf(data.getData(RunFileData.YLength));
if (isUSUnits()){
yLength = ConversionCalculator.convert(yLength, "m", "ft");
}
setRowValue(row, COLUMN_FIELDYLENGTH, yLength);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing ylength." + nfe.getMessage());
}
catch(ConversionCalculator.ConversionNotFoundException cnfe){
LOGGER.severe("Error converting ylength value." + cnfe.getMessage());
}
catch(ConversionCalculator.UnitNotFoundException unfe){
LOGGER.severe("Error converting ylength value." + unfe.getMessage());
}
//elevation
try{
Double elevation = Double.valueOf(data.getData(RunFileData.Elevation));
if (isUSUnits()){
elevation = ConversionCalculator.convert(elevation, "m", "ft");
}
setRowValue(row, COLUMN_ELEVATION, elevation);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing elevation." + nfe.getMessage());
}
catch(ConversionCalculator.ConversionNotFoundException cnfe){
LOGGER.severe("Error converting elevation value." + cnfe.getMessage());
}
catch(ConversionCalculator.UnitNotFoundException unfe){
LOGGER.severe("Error converting elevation value." + unfe.getMessage());
}
//orientation
try{
Double orientation = Double.valueOf(data.getData(RunFileData.RegionAngle));
setRowValue(row, COLUMN_FIELDORIENTATION, orientation);
}
catch(NumberFormatException nfe){
LOGGER.severe("Error parsing orientation." + nfe.getMessage());
}
//add special fields
//location of run
// String location = getRunLocation(ConfigData.getDefault().getData(ConfigData.DefaultRunsLocation),runDir);
String location = m_sDirectory;
setRowValue(row, COLUMN_LOCATION, location);
// //field image
// if (fieldPainter == null){
// fieldPainter = new DrawField();
// fieldPainter.setSize(200, 200);
// fieldPainter.setBackground(Color.WHITE);
//
//
// }
// rbp=new RepBarPanel();
// rbp.addPropertyChangeListener(fieldPainter);
// fieldPainter.reset();
// data.fireAll(rbp, fieldPainter);
//
// BufferedImage image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
// fieldPainter.paintComponent(image.getGraphics());
// byte[] bytes = getBytes(image);
// setRowValue(row, COLUMN_FIELDIMAGE, bytes);
// //end image
//
//date of weps run, have to read the stdout.txt
File stdout = new File(runDir, stdFileName);
if (stdout.exists()){
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(stdout));
String line = getLine(reader); //First line
line = line.trim();
while(line != null){
if (line.startsWith("Date of WEPS Run:")){
//found the line
String dateText = line;
dateText = line.substring(18, line.length());
try{
//Each thread creates its own because formats are not thread safe.
DateFormat format = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");
Date date = format.parse(dateText);
setRowValue(row, COLUMN_RUNDATE, new Timestamp(date.getTime()));
}
catch(NumberFormatException nfe){
LOGGER.warning("Unable to parse date line: \"" + line + "\" in file: " + stdout.getAbsolutePath() + nfe.getMessage());
}
catch(ParseException pe){
LOGGER.warning("Unable to parse date line: \"" + line + "\" in file: " + stdout.getAbsolutePath() + pe.getMessage());
}
break;
}
line = getLine(reader); //next line
}
}
catch(IOException ioe){
LOGGER.severe("Error reading file: " + stdout.getAbsolutePath() + ioe.getMessage());
}
finally{
if (reader != null){
try{
reader.close();
}
catch(Exception e){
LOGGER.severe("Error closing file: " + stdout.getAbsolutePath() + e.getMessage());
}
}
}
}
// }
addRow(row);
// c_filled = true;
}
// public byte [] getBytes(RenderedImage image) {
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// try {
// ImageIO.write(image, "JPEG", baos);
// } catch (IOException ex) {
// throw new RuntimeException(ex);
// }
// return baos.toByteArray();
// }
//
// private String getRunLocation(String defaultLocation, File run) {
// if (defaultLocation == null) {
// return run.getParentFile().getPath();
// }
//
// File defaultLocationFile = new File(defaultLocation);
//
// File parent = new File(run.getParentFile());
//
// //Is the direct parent a project file?
// if (parent.getName().endsWith(RunFileData.ProjectSuffix)) {
// return parent.getName().replace(RunFileData.ProjectSuffix, "");
// }
//
// while (parent != null) {
// if (parent.equals(defaultLocationFile)) {
// String pathToIgnore = defaultLocationFile.getParentFile().getAbsolutePath();
// String fullPath = run.getParentFile().getAbsolutePath();
// fullPath = fullPath.replace(pathToIgnore, "");
// if (fullPath.startsWith(File.separator)) {
// fullPath = fullPath.substring(1);
// }
// return fullPath;
// } else {
// try {
// parent = new File(parent.getParentFile());
// } catch (NullPointerException ex) {
// return run.getParentFile().getPath();
// }
// }
// }
// return run.getParentFile().getPath();
// }
//
//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;
}
}