com.hfg.sql.table.field.DatabaseDateField Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.sql.table.field;
import com.hfg.datetime.ThreadSafeDateFormat;
import com.hfg.sql.jdbc.JDBCException;
import com.hfg.sql.table.DatabaseCol;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.util.StringUtil;
import com.hfg.xml.XMLTag;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
//------------------------------------------------------------------------------
/**
Database field that manages a date value.
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class DatabaseDateField extends DatabaseField
{
private static ThreadSafeDateFormat sStdDateFormat = new ThreadSafeDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public DatabaseDateField(DatabaseCol inCol, Date inValue)
{
super(inCol, inValue);
}
//---------------------------------------------------------------------------
public DatabaseDateField(DatabaseCol inCol, Object inValue)
{
super(inCol, convertToDate(inValue));
}
//---------------------------------------------------------------------------
public DatabaseDateField(XMLTag inXMLTag, DatabaseTable inTable)
{
super(inXMLTag, inTable);
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public void setValueFromResultSet(ResultSet inResultSet)
{
try
{
Timestamp timestamp = inResultSet.getTimestamp(getCol().name());
setValue(inResultSet.wasNull() ? null : new Date(timestamp.getTime()));
}
catch (SQLException e)
{
try
{
throw new JDBCException("Problem mapping " + getCol().name() + " value: " + inResultSet.getString(getCol().name()) + "!", e);
}
catch (SQLException e2)
{
throw new JDBCException("Problem mapping " + getCol().name() + " value!", e);
}
}
}
//---------------------------------------------------------------------------
public void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex)
{
try
{
if (isNull())
{
inPreparedStatement.setNull(inIndex, Types.TIMESTAMP_WITH_TIMEZONE);
}
else
{
inPreparedStatement.setTimestamp(inIndex, new Timestamp(getValue().getTime()));
}
}
catch (SQLException e)
{
throw new JDBCException("Problem setting column " + getCol().name() + " value into PreparedStatement!", e);
}
}
//---------------------------------------------------------------------------
public void setValueFromString(String inValue)
{
setValue(convertToDate(inValue, getCol()));
}
//---------------------------------------------------------------------------
@Override
public String getSQLValue()
{
String valueString = null;
if (getValue() != null)
{
DateFormat formatter = getCol().getTable().getRDBMS().getSQLDateFormat(getCol().getType());
valueString = formatter.format(getValue());
}
return valueString != null ? "'" + StringUtil.replaceAll(valueString, "\'", "\'\'") + "'" : "null";
}
//###########################################################################
// PRIVATE METHODS
//###########################################################################
//---------------------------------------------------------------------------
private Date convertToDate(Object inValue, DatabaseCol inCol)
{
Date dateValue = null;
if (inValue != null)
{
dateValue = convertToDate(inValue);
if (null == dateValue
&& inValue instanceof String)
{
DateFormat formatter = getCol().getTable().getRDBMS().getSQLDateFormat(getCol().getType());
try
{
dateValue = formatter.parse((String) inValue);
}
catch (ParseException e)
{
try
{
dateValue = sStdDateFormat.parse((String) inValue);
}
catch (ParseException e2)
{
// Ignore
}
}
}
}
return dateValue;
}
//---------------------------------------------------------------------------
private static Date convertToDate(Object inValue)
{
Date dateValue = null;
if (inValue != null)
{
if (inValue instanceof Date)
{
dateValue = (Date) inValue;
}
else if (inValue instanceof java.sql.Date)
{
dateValue = new Date(((java.sql.Date) inValue).getTime());
}
else if (inValue instanceof String)
{
// TODO: Convert string to Date
}
}
return dateValue;
}
}