com.hfg.sql.table.field.DatabaseField 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 java.lang.reflect.Constructor;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.hfg.exception.ProgrammingException;
import com.hfg.sql.jdbc.JDBCException;
import com.hfg.sql.table.DatabaseCol;
import com.hfg.sql.table.DatabaseTable;
import com.hfg.sql.table.DatabaseXML;
import com.hfg.xml.XMLTag;
//------------------------------------------------------------------------------
/**
Base class for a database field.
@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 abstract class DatabaseField implements Cloneable
{
private DatabaseCol mCol;
private T mValue;
private boolean mIsDirty;
//###########################################################################
// CONSTRUCTORS
//###########################################################################
//---------------------------------------------------------------------------
public DatabaseField(DatabaseCol inCol, T inValue)
{
mCol = inCol;
mValue = inValue;
}
//---------------------------------------------------------------------------
public DatabaseField(XMLTag inXMLTag, DatabaseTable inTable)
{
inXMLTag.verifyTagName(DatabaseXML.FIELD);
mCol = inTable.getCol(inXMLTag.getAttributeValue(DatabaseXML.NAME_ATT));
if (! inXMLTag.hasAttribute(DatabaseXML.IS_NULL_ATT))
{
setValueFromString(inXMLTag.getUnescapedContent());
}
}
//###########################################################################
// PUBLIC METHODS
//###########################################################################
//---------------------------------------------------------------------------
public static DatabaseField instantiate(XMLTag inXMLTag, DatabaseTable inTable)
{
DatabaseField field;
try
{
Class clazz = Class.forName(inXMLTag.getAttributeValue(DatabaseXML.CLASS_ATT));
Constructor constructor = clazz.getConstructor(XMLTag.class, DatabaseTable.class);
field = (DatabaseField) constructor.newInstance(inXMLTag, inTable);
}
catch (Exception e)
{
throw new JDBCException(e);
}
return field;
}
//---------------------------------------------------------------------------
@Override
public String toString()
{
return isNull() ? "null" : getValue().toString();
}
//---------------------------------------------------------------------------
public XMLTag toXMLTag()
{
XMLTag tag = new XMLTag(DatabaseXML.FIELD);
tag.setAttribute(DatabaseXML.CLASS_ATT, getClass().getName());
tag.setAttribute(DatabaseXML.NAME_ATT, getCol().name());
if (isNull())
{
tag.setAttribute(DatabaseXML.IS_NULL_ATT, true);
}
else
{
tag.setContent(getStringValue());
}
return tag;
}
//---------------------------------------------------------------------------
protected String getStringValue()
{
return (getValue() != null ? getValue().toString() : null);
}
//---------------------------------------------------------------------------
@Override
public DatabaseField clone()
{
DatabaseField cloneObj;
try
{
cloneObj = (DatabaseField) super.clone();
}
catch (CloneNotSupportedException e)
{
throw new ProgrammingException(e);
}
return cloneObj;
}
//---------------------------------------------------------------------------
public DatabaseCol getCol()
{
return mCol;
}
//---------------------------------------------------------------------------
public abstract void setValueFromResultSet(ResultSet inResultSet);
//---------------------------------------------------------------------------
public abstract void setValueInPreparedStatement(PreparedStatement inPreparedStatement, int inIndex);
//---------------------------------------------------------------------------
public abstract void setValueFromString(String inValue);
//---------------------------------------------------------------------------
public void setValue(T inValue)
{
if ((null == inValue
&& mValue != null)
|| (inValue != null
&& ! inValue.equals(mValue)))
{
mIsDirty = true;
}
mValue = inValue;
}
//---------------------------------------------------------------------------
public T getValue()
{
return mValue;
}
//---------------------------------------------------------------------------
public String getSQLValue()
{
return mValue != null ? mValue.toString() : "null";
}
//---------------------------------------------------------------------------
public boolean isNull()
{
return null == mValue;
}
//---------------------------------------------------------------------------
public boolean isDirty()
{
return mIsDirty;
}
//---------------------------------------------------------------------------
public DatabaseField setIsDirty(boolean inValue)
{
mIsDirty = inValue;
return this;
}
}