Expresso 5-6

com.jcorporate.expresso.core.dataobjects
Class DefaultDataField

java.lang.Object
  extended bycom.jcorporate.expresso.core.dataobjects.DefaultDataField
All Implemented Interfaces:
DataField, Serializable

public class DefaultDataField
extends Object
implements DataField, Serializable

Default implementation of the DataField interface. This class provides some basic conversion capabilities between different Java types as well as provides all use change logging capabilities.

Since:
Expresso 5.0
Author:
Michael Rimov
See Also:
Serialized Form

Field Summary
static String ATTRIBUTE_ERROR
          Attribute String for if there's an error with the field.
static String ATTRIBUTE_ERROR_MESSAGE
          Attribute String for what message to display if there's an error with thie field.
protected  Map attributes
          A generic map of attribute keys to attribute values
protected  Object currentValue
          The Current Value
protected  boolean isChanged
          Flag to signify if the field is changed
protected  boolean isValueSet
          Flag to signify if the field value has been set - the retrieved value may be null
protected  DataFieldMetaData myMetaData
          A link to my metadata.
protected  Object originalValue
          The original value, used in comparison tests for 'isChanged' calculation; set by first retrieve(); reset after add() or update()
protected  DataObject owner
          A link to the DataObject that contains me.
 
Constructor Summary
protected DefaultDataField(DataFieldMetaData metaData, DataObject parentObject)
          Creates a new DefaultDataField object.
 
Method Summary
 BigDecimal asBigDecimal()
          Retrieve the wrapped object as a BigDecimal object or zero if we're unable to convert it.
 Boolean asBoolean()
          Retrieve the boolean object value.
 Date asDate()
          Retrieve the wrapped object as a Date object or possibly null if we can't convert it or the object is null.
 Double asDouble()
          Retrieve the wrapped object as a Double object or zero if we can't convert i
 Integer asInteger()
          Retrieves the wrapped object as an Integer or zero if it's an invalid format.
 InputStream asStream()
          Retrieve the field as a InputStream.
 String asString()
          Retrieves the wrapped object as a String
 void cacheIsChangedComparison()
          reset 'original' value and isChanged flags; call when add(), retrieve() or update() has occurred, and currentValue of data fields should be considered 'original value' for purposes of determining 'isChanged'
 void checkValue()
          Make sure the value of the field is valid.
 Map getAllAttributes()
          Returns a Read Only Map of all attributes in name-value pairs.
 Object getAttribute(String attributeName)
          Retrieves any user defined attributes for this field.
protected  String getBooleanFieldValue(Boolean theFieldValue)
          Internal refactoring for getting what a boolean field should be set to.
 DataFieldMetaData getFieldMetaData()
          Returns a handle to the Field MetaData object.
static DefaultDataField getInstance(DataFieldMetaData metaData, DataObject parentObject)
          create new object for this field
 Object getOriginalValue()
          Retrieve the last original value if the data has changed.
 DataObject getOwner()
          Returns a handle to the DataObject that is the container for this Data Field
protected  org.apache.oro.text.regex.PatternMatcher getPatternMatcher()
          Retrieve a thread local instance of the Perl5 pattern matcher.
 Object getSerializedForm()
          Returns the form in a way suitable for storage in a database or other data source.
protected  SimpleDateFormat getSimpleDateFormat(String pattern)
          Get a SimpleDateFormat object that is cached.
 Object getValue()
          retrieve value object for this field
 boolean isChanged()
          Used for change logging and updating only changed fields.
 boolean isNull()
          Returns true if the object is null.
 boolean isValueSet()
          Used for change logging.
 void release()
          set all internal data members to null;
 void removeAttribute(String attribute)
          Removes an attribute
 void resetChanged()
          Resets the isChanged and isValueSet flags and sets the original value field to null
 void setAttribute(String attributeName, Object value)
          Sets an attribute for this particular instance of the Data field
 void setFieldMetaData(DataFieldMetaData newMetadata)
          Sets the metadata object for this field.
 void setOwner(DataObject newOwner)
          Sets the owner of a given DataObject
 void setSerializedForm(Object o)
          Used for setting raw data from a JDBC connection.
 void setValue(Object newValue)
          Sets the wrapped object for the data field; also updates values for isValueSet and isChanged flags
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ATTRIBUTE_ERROR

public static final String ATTRIBUTE_ERROR
Attribute String for if there's an error with the field.

See Also:
Constant Field Values

ATTRIBUTE_ERROR_MESSAGE

public static final String ATTRIBUTE_ERROR_MESSAGE
Attribute String for what message to display if there's an error with thie field.

See Also:
Constant Field Values

myMetaData

protected transient DataFieldMetaData myMetaData
A link to my metadata.


owner

protected transient DataObject owner
A link to the DataObject that contains me.


attributes

protected Map attributes
A generic map of attribute keys to attribute values


currentValue

protected Object currentValue
The Current Value


originalValue

protected Object originalValue
The original value, used in comparison tests for 'isChanged' calculation; set by first retrieve(); reset after add() or update()


isChanged

protected boolean isChanged
Flag to signify if the field is changed


isValueSet

protected boolean isValueSet
Flag to signify if the field value has been set - the retrieved value may be null

Constructor Detail

DefaultDataField

protected DefaultDataField(DataFieldMetaData metaData,
                           DataObject parentObject)
Creates a new DefaultDataField object.

Parameters:
metaData -
parentObject -
Method Detail

getInstance

public static DefaultDataField getInstance(DataFieldMetaData metaData,
                                           DataObject parentObject)
create new object for this field

Parameters:
metaData -
parentObject -
Returns:
new instance of this field

release

public void release()
set all internal data members to null;


setSerializedForm

public void setSerializedForm(Object o)
Used for setting raw data from a JDBC connection. Allows for special processing such as decryption.

Parameters:
o - the object to set.

getSerializedForm

public Object getSerializedForm()
                         throws DataException
Returns the form in a way suitable for storage in a database or other data source.

Returns:
java.lang.Object
Throws:
DataException - upon error

getValue

public Object getValue()
retrieve value object for this field

Specified by:
getValue in interface DataField
Returns:
Object which contains value

asString

public String asString()
Retrieves the wrapped object as a String

Specified by:
asString in interface DataField
Returns:
a String value representing the object as a string

asStream

public InputStream asStream()
Retrieve the field as a InputStream. If the object's native type is an input stream, it returns that. If it is a serializable object, it returns a ByteArrayInputStream that was created using a ByteArrayOutputStream as well as a ObjectOutputStream.

Specified by:
asStream in interface DataField
Returns:
java.io.InputSTream or null if there was an IO Exception getting the value, the value was null, of if the native object is not serializable

asInteger

public Integer asInteger()
Retrieves the wrapped object as an Integer or zero if it's an invalid format.

Specified by:
asInteger in interface DataField
Returns:
a java.lang.Integer object, or null if the object is null.

asDate

public Date asDate()
Retrieve the wrapped object as a Date object or possibly null if we can't convert it or the object is null.

Specified by:
asDate in interface DataField
Returns:
a properly instantiated Date Object if we're able to convert it.

getSimpleDateFormat

protected SimpleDateFormat getSimpleDateFormat(String pattern)
Get a SimpleDateFormat object that is cached. Make sure you have dateConvertFormatMap already locked before calling this function or you'll have a race condition.

Parameters:
pattern - the format pattern to look up
Returns:
an instantiated SimpleDateFormat object. SimpleDateFormat is NOT threadsafe, so make sure you do your parsing while still in the synchronized block. Perhaps in the future a keyed Object pool will be better.

asBigDecimal

public BigDecimal asBigDecimal()
Retrieve the wrapped object as a BigDecimal object or zero if we're unable to convert it.

Specified by:
asBigDecimal in interface DataField
Returns:
a properly instantiated BigDecimal Object if we're able to convert it.

asDouble

public Double asDouble()
Retrieve the wrapped object as a Double object or zero if we can't convert i

Specified by:
asDouble in interface DataField
Returns:
a properly instantiated Double Object if we're able to convert it.

asBoolean

public Boolean asBoolean()
Retrieve the boolean object value.

Specified by:
asBoolean in interface DataField
Returns:
Boolean object. Null if the object's value is null

getOriginalValue

public Object getOriginalValue()
Retrieve the last original value if the data has changed. Will return null if isChanged() is false

Specified by:
getOriginalValue in interface DataField
Returns:
the original object or null

isChanged

public boolean isChanged()
Used for change logging and updating only changed fields. change is true IF the field has been set once (after object is born or reset), and subsequently the value is changed in the field.

Specified by:
isChanged in interface DataField
Returns:
true if the field has changed since the last reset (i.e. since object was constructed or reset)

isValueSet

public boolean isValueSet()
Used for change logging.

Specified by:
isValueSet in interface DataField
Returns:
true if the field has had the value set since the last reset

checkValue

public void checkValue()
                throws DataException
Make sure the value of the field is valid. Checks mask and for null.

Specified by:
checkValue in interface DataField
Throws:
DataException - if the field value is not valid

resetChanged

public void resetChanged()
Resets the isChanged and isValueSet flags and sets the original value field to null

Specified by:
resetChanged in interface DataField

setValue

public void setValue(Object newValue)
Sets the wrapped object for the data field; also updates values for isValueSet and isChanged flags

Specified by:
setValue in interface DataField
Parameters:
newValue - a new Object to set the value to

isNull

public boolean isNull()
Returns true if the object is null.

Specified by:
isNull in interface DataField
Returns:
true if the object is null

setAttribute

public void setAttribute(String attributeName,
                         Object value)
Sets an attribute for this particular instance of the Data field

Specified by:
setAttribute in interface DataField
Parameters:
attributeName - the name of the attribute to set
value - the value to set it to

removeAttribute

public void removeAttribute(String attribute)
Removes an attribute

Parameters:
attribute - The attribute key to remove

getAttribute

public Object getAttribute(String attributeName)
Retrieves any user defined attributes for this field.

Specified by:
getAttribute in interface DataField
Parameters:
attributeName - the name of the attribute to retrieve
Returns:
an object or null if the attribute doesn't exist

getAllAttributes

public Map getAllAttributes()
Returns a Read Only Map of all attributes in name-value pairs. If there are no attributes then getAllAttributes will return a null map.

Specified by:
getAllAttributes in interface DataField
Returns:
java.util.map

getOwner

public DataObject getOwner()
Returns a handle to the DataObject that is the container for this Data Field

Specified by:
getOwner in interface DataField
Returns:
DataObject the containing data object.

setOwner

public void setOwner(DataObject newOwner)
Sets the owner of a given DataObject

Specified by:
setOwner in interface DataField
Parameters:
newOwner - The new parent object.

getFieldMetaData

public DataFieldMetaData getFieldMetaData()
Returns a handle to the Field MetaData object.

Specified by:
getFieldMetaData in interface DataField
Returns:
DataFieldMetaData

setFieldMetaData

public void setFieldMetaData(DataFieldMetaData newMetadata)
Sets the metadata object for this field.

Specified by:
setFieldMetaData in interface DataField
Parameters:
newMetadata - the new Field Meta data object... for example DBField

getBooleanFieldValue

protected String getBooleanFieldValue(Boolean theFieldValue)
Internal refactoring for getting what a boolean field should be set to.

Parameters:
theFieldValue - The target value
Returns:
the string value
Throws:
IllegalArgumentException - If we can't get the data context

getPatternMatcher

protected org.apache.oro.text.regex.PatternMatcher getPatternMatcher()
Retrieve a thread local instance of the Perl5 pattern matcher. Allows for optimization of # of instances of pattern matcher vs synchronization.

Returns:
PatternMatcher

cacheIsChangedComparison

public void cacheIsChangedComparison()
reset 'original' value and isChanged flags; call when add(), retrieve() or update() has occurred, and currentValue of data fields should be considered 'original value' for purposes of determining 'isChanged'

Specified by:
cacheIsChangedComparison in interface DataField

Expresso 5-6

Please see www.jcorporate.com for information about new Expresso releases.