org.formaria.data.pojo
Class PojoModel

java.lang.Object
  extended by org.formaria.aria.data.DataModel
      extended by org.formaria.data.pojo.PojoModel
Direct Known Subclasses:
PersistentPojoModel, PojoIterator

public class PojoModel
extends DataModel

A model node for interacting with POJOs

Copyright (c) Formaria Ltd., 2008
License: see license.txt


Field Summary
protected  PojoAdapter adapter
           
protected  PojoModel[] collectionElements
           
protected  PojoDataSource dataSource
           
protected  boolean dirty
           
protected  int elIdx
           
protected  java.lang.Class[] getterArgTypes
           
protected  java.lang.Object[] getterArgValues
           
protected  java.lang.Object pojo
           
protected  java.util.Hashtable properties
           
protected  java.lang.String propertyKey
           
protected  java.lang.String propertyName
           
 
Fields inherited from class org.formaria.aria.data.DataModel
parentModel, tagName
 
Constructor Summary
protected PojoModel(DataModel parent, java.lang.Object p, PojoDataSource ds)
          Creates a new instance of PojoModel
protected PojoModel(DataModel parent, java.lang.String subPath, PojoDataSource ds)
          Creates a new instance of PojoModel
 
Method Summary
 void append(DataModel childNode)
          Append a node
 java.lang.Object append(java.lang.String elementName)
          Append a new node with the specified name.
 void clear()
          Removes all POJOs from the tree rooted at this model node.
 java.lang.Object get()
          Gets the POJO encapsulated in this model node
 DataModel get(int i)
          Get the DataModel at element i
 java.lang.Object get(java.lang.String element)
          Get the value of the element located at the path in the element parameter.
 java.lang.String getAttribName(int i)
          Retrieve the name of the attribute at the specified index
 int getAttribute(java.lang.String attribName)
          Returns the index of the attribiteNames array whose value is the same as the attribName
 java.lang.Object getAttribValue(int i)
          Retrieve the value of the attribute at the specified index and return it.
 double getAttribValueAsDouble(int i)
          Deprecated. use getAttribValueAsDouble( i, decimalSeparator, groupingSeparator ) instead, if the locale is different from the locale used to write the values to the model, then the parsed value may be incorrect.
 double getAttribValueAsDouble(int i, char decimalSeparator, char groupingSeparator)
          Convert the attribute at the specified index to a double and return it
 int getAttribValueAsInt(int i)
          Retrieve the value of the attribute at the specified index, convert it to an int and return it.
 java.lang.String getAttribValueAsString(int i)
          Retrieve the value of the attribute at the specified index, convert it to a String and return it.
protected  PojoModel[] getCollectionElements()
          Gets the table containing pojo collection elements
 PojoDataSource getDataSource()
          Gets the data source object of this POJO model.
protected  int getElIdx()
          Gets the collection's position at which the pojo of this model node is located
protected  java.lang.Class[] getGetterArgTypes()
          Gets the argument types of the getter method associated with this model node.
protected  java.lang.Object[] getGetterArgValues()
          Gets the argument values of the getter method associated with this model node
 java.lang.String getId()
          Gets the value of the ID attribute
 int getNumAttributes()
          Gets the number of attributes of this node
 int getNumChildren()
          Gets the number of immediate children of this node
protected  java.lang.Object getPojo()
          Gets the underlying POJO
protected  java.lang.String getPropertyKey()
          Gets a String value that is used by the PojoAdapter associated with this node to store accessor methods.
protected  java.lang.String getPropertyName()
          Gets the property name of this model node
protected  java.lang.Object getPropertyValue(PojoModel childNode)
          Gets the value of the property represented by the specified model node.
 double getValueAsDouble(java.lang.String elementName)
          Retrieve the named child, convert it's value to a double and return it.
 int getValueAsInt(java.lang.String elementName)
          Retrieve the named child, convert it's value to an int and return it.
 java.lang.String getValueAsString(java.lang.String elementName)
          Retrieve the named child, convert it's value to a String and return it.
 void hasAutoId(boolean b)
          Used for elements which need a name assigned temporarily because one doesn't exist in the DataSource.
 int hashCode()
          The hashcode of this instance.
 boolean hasSetter()
          Indicates whether the value of the underlying property can be changed by invoking the relevant setter method on the parent object.
 boolean isAlwaysDirty()
          Is the POJO always treated as dirty?
 boolean isCollection()
          Tests whether the underlying POJO is an instance of a Collection
 boolean isDirty()
          Gets the dirty flag of this node.
 boolean isTransient()
          Tests whether the property wrapped by this model node is transient.
protected  boolean isTransient(java.lang.String propertyName)
          Tests whether the specified property of this model node is transient.
protected  void markSubtreeAsAlwaysDirty(boolean isAlwaysDirty)
          Marks the children of this node as dirty.
protected  void markSubtreeAsDirty()
          Marks the children of this node as dirty.
protected  PojoModel retrieveModelNode(java.lang.String subPath)
          Gets the previously created model node located at the specified subpath.
protected  void saveModelNode(java.lang.String subPath, PojoModel model)
          Stores the specified model node
 void set(java.lang.Object value)
          Sets the model value
 void set(java.lang.String elementName, java.lang.Object newObject)
          Set the named attribute value of this DataModel node.
 void setAlwaysDirty(boolean state)
          Set the POJO as always dirty?
 void setAttribValue(int i, java.lang.Object value)
          Sets the attribute value
 void setAttribValue(int i, java.lang.String attribName, java.lang.Object value)
          Sets the attribute name and value
 void setDirty(boolean isDirty)
          Sets the dirty state of this node.
 void setId(java.lang.String newId)
          Sets the ID attribute
 void setNumChildren(int num)
          Set the number of children of this node
protected  void setPojo(java.lang.Object newPojo)
          Sets the new POJO of this model node
protected  void setPropertyValue(PojoModel childNode, java.lang.Object value)
          Sets the given POJO of the given PojoModel object by invoking the setter method.
protected  void sync()
          Synchronizes the underlying pojo by invoking the getter on the parent object.
 
Methods inherited from class org.formaria.aria.data.DataModel
addModelListener, fireModelUpdated, getParent, getTagName, hasAutoId, prefixOutputPath, removeChildren, reset, resetAttributes, setNumAttributes, setParent, setTagName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

adapter

protected PojoAdapter adapter

dataSource

protected PojoDataSource dataSource

propertyName

protected java.lang.String propertyName

getterArgValues

protected java.lang.Object[] getterArgValues

getterArgTypes

protected java.lang.Class[] getterArgTypes

propertyKey

protected java.lang.String propertyKey

elIdx

protected int elIdx

pojo

protected java.lang.Object pojo

collectionElements

protected PojoModel[] collectionElements

properties

protected java.util.Hashtable properties

dirty

protected boolean dirty
Constructor Detail

PojoModel

protected PojoModel(DataModel parent,
                    java.lang.String subPath,
                    PojoDataSource ds)
Creates a new instance of PojoModel

Parameters:
parent - the parent model node
subPath - String in the format: propertyName(property arguments)[collection idx]

PojoModel

protected PojoModel(DataModel parent,
                    java.lang.Object p,
                    PojoDataSource ds)
Creates a new instance of PojoModel

Parameters:
parent - the parent model node
p - the root pojo
ds - the data source object
Method Detail

setPojo

protected void setPojo(java.lang.Object newPojo)
Sets the new POJO of this model node

Parameters:
newPojo - the new POJO

getDataSource

public PojoDataSource getDataSource()
Gets the data source object of this POJO model.

Returns:
PojoDataSource object

getPropertyKey

protected java.lang.String getPropertyKey()
Gets a String value that is used by the PojoAdapter associated with this node to store accessor methods.

Returns:
String identifying this node.

getPropertyName

protected java.lang.String getPropertyName()
Gets the property name of this model node

Returns:
the property name

getGetterArgValues

protected java.lang.Object[] getGetterArgValues()
Gets the argument values of the getter method associated with this model node

Returns:
table containing argument values

getGetterArgTypes

protected java.lang.Class[] getGetterArgTypes()
Gets the argument types of the getter method associated with this model node.

Returns:
table containing argument types.

getElIdx

protected int getElIdx()
Gets the collection's position at which the pojo of this model node is located

Returns:
pojo's position in a collection

markSubtreeAsDirty

protected void markSubtreeAsDirty()
Marks the children of this node as dirty.


setDirty

public void setDirty(boolean isDirty)
Sets the dirty state of this node.

Parameters:
isDirty - the new dirty state

isDirty

public boolean isDirty()
Gets the dirty flag of this node.

Returns:
true if the pojo stored in this node is out of sync, false otherwise

getPojo

protected java.lang.Object getPojo()
Gets the underlying POJO

Returns:
POJO

getCollectionElements

protected PojoModel[] getCollectionElements()
Gets the table containing pojo collection elements

Returns:
collection elements if the underlying pojo is an instance of Collection, null otherwise

getId

public java.lang.String getId()
Gets the value of the ID attribute

Specified by:
getId in class DataModel
Returns:
the ID attribute

setId

public void setId(java.lang.String newId)
Sets the ID attribute

Overrides:
setId in class DataModel
Parameters:
newId - the new name

set

public void set(java.lang.String elementName,
                java.lang.Object newObject)
Set the named attribute value of this DataModel node. If the attribName is null then this node's value is updated.

Specified by:
set in class DataModel
Parameters:
elementName - The path to the DataModel in the format 'base/foo
newObject - The new value of the DataModel

getAttribute

public int getAttribute(java.lang.String attribName)
Returns the index of the attribiteNames array whose value is the same as the attribName

Specified by:
getAttribute in class DataModel
Parameters:
attribName - The name of the attribute we are trying to locate
Returns:
The index of the attributeNames array containg the name

setAttribValue

public void setAttribValue(int i,
                           java.lang.Object value)
Sets the attribute value

Specified by:
setAttribValue in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
value - the value object

setAttribValue

public void setAttribValue(int i,
                           java.lang.String attribName,
                           java.lang.Object value)
Sets the attribute name and value

Specified by:
setAttribValue in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
attribName - the name of the attribute
value - the value of the object

sync

protected void sync()
Synchronizes the underlying pojo by invoking the getter on the parent object.


get

public DataModel get(int i)
Get the DataModel at element i

Specified by:
get in class DataModel
Parameters:
i - The index of the values array
Returns:
The DataModel at location i

get

public java.lang.Object get()
Gets the POJO encapsulated in this model node

Specified by:
get in class DataModel
Returns:
the POJO

isTransient

protected boolean isTransient(java.lang.String propertyName)
Tests whether the specified property of this model node is transient.

Parameters:
propertyName - the name of the property
Returns:
true if the property is transient, false otherwise

isTransient

public boolean isTransient()
Tests whether the property wrapped by this model node is transient.

Returns:
true if the property is transient, false otherwise

isCollection

public boolean isCollection()
Tests whether the underlying POJO is an instance of a Collection


getPropertyValue

protected java.lang.Object getPropertyValue(PojoModel childNode)
Gets the value of the property represented by the specified model node.

Parameters:
childNode - the model node of the property whose value is to be retrieved
Returns:
the value of the property

set

public void set(java.lang.Object value)
Sets the model value

Specified by:
set in class DataModel
Parameters:
value - the new value

clear

public void clear()
Removes all POJOs from the tree rooted at this model node.

Overrides:
clear in class DataModel

hasSetter

public boolean hasSetter()
Indicates whether the value of the underlying property can be changed by invoking the relevant setter method on the parent object.

Returns:
true if the property has a setter, false otherwise.

setPropertyValue

protected void setPropertyValue(PojoModel childNode,
                                java.lang.Object value)
Sets the given POJO of the given PojoModel object by invoking the setter method.

Parameters:
childNode - model node of the property which value is to be set.
value - the new value of the property

retrieveModelNode

protected PojoModel retrieveModelNode(java.lang.String subPath)
Gets the previously created model node located at the specified subpath.

Parameters:
subPath - path to the requested model node
Returns:
DataModel node

saveModelNode

protected void saveModelNode(java.lang.String subPath,
                             PojoModel model)
Stores the specified model node

Parameters:
subPath - the path at which the model will be located
model - the model node to be saved

get

public java.lang.Object get(java.lang.String element)
Get the value of the element located at the path in the element parameter. The element name parameter can include an attribute name by appending '@attributeName' to the path, where attributeName is the name of the attribute.

Overrides:
get in class DataModel
Parameters:
element - The path to the DataModel required
Returns:
The value of the DataModel or the attribute

hasAutoId

public void hasAutoId(boolean b)
Used for elements which need a name assigned temporarily because one doesn't exist in the DataSource.

Overrides:
hasAutoId in class DataModel
Parameters:
b - true if there was no name in the DataSource

getAttribName

public java.lang.String getAttribName(int i)
Retrieve the name of the attribute at the specified index

Specified by:
getAttribName in class DataModel
Parameters:
i - The index of the attributeNames array whose value we want
Returns:
The string value of the attributeNames array at position i

getAttribValueAsString

public java.lang.String getAttribValueAsString(int i)
Retrieve the value of the attribute at the specified index, convert it to a String and return it.

Specified by:
getAttribValueAsString in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribValue

public java.lang.Object getAttribValue(int i)
Retrieve the value of the attribute at the specified index and return it.

Specified by:
getAttribValue in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The string value of the attributeValues array at position i

getAttribValueAsDouble

public double getAttribValueAsDouble(int i)
Deprecated. use getAttribValueAsDouble( i, decimalSeparator, groupingSeparator ) instead, if the locale is different from the locale used to write the values to the model, then the parsed value may be incorrect.

Retrieve the value of the attribute at the specified index, convert it to a double and return it.

Specified by:
getAttribValueAsDouble in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The double value of the attributeValues array at position i

getAttribValueAsDouble

public double getAttribValueAsDouble(int i,
                                     char decimalSeparator,
                                     char groupingSeparator)
Convert the attribute at the specified index to a double and return it

Specified by:
getAttribValueAsDouble in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
decimalSeparator - the decimal separator
groupingSeparator - the grouping (thousands) separator
Returns:
The double value of the attributeValues array at position i

getAttribValueAsInt

public int getAttribValueAsInt(int i)
Retrieve the value of the attribute at the specified index, convert it to an int and return it.

Specified by:
getAttribValueAsInt in class DataModel
Parameters:
i - The index of the attributeValues array whose value we want
Returns:
The int value of the attributeValues array at position i

getValueAsDouble

public double getValueAsDouble(java.lang.String elementName)
Retrieve the named child, convert it's value to a double and return it.

Specified by:
getValueAsDouble in class DataModel
Parameters:
elementName - The name of the child whose value is required
Returns:
the value as a double

getValueAsInt

public int getValueAsInt(java.lang.String elementName)
Retrieve the named child, convert it's value to an int and return it.

Specified by:
getValueAsInt in class DataModel
Parameters:
elementName - The name of the child to be retrieved
Returns:
the value as an int

getValueAsString

public java.lang.String getValueAsString(java.lang.String elementName)
Retrieve the named child, convert it's value to a String and return it.

Specified by:
getValueAsString in class DataModel
Parameters:
elementName - The name of the child whose value is required
Returns:
the value as a string

hashCode

public int hashCode()
The hashcode of this instance. Based on the ID String

Specified by:
hashCode in class DataModel
Returns:
The hashcode of this instance

getNumChildren

public int getNumChildren()
Gets the number of immediate children of this node

Overrides:
getNumChildren in class DataModel
Returns:
the number of children

getNumAttributes

public int getNumAttributes()
Gets the number of attributes of this node

Overrides:
getNumAttributes in class DataModel
Returns:
the number of attributes

setNumChildren

public void setNumChildren(int num)
Set the number of children of this node

Overrides:
setNumChildren in class DataModel
Parameters:
num - the new number of children

append

public void append(DataModel childNode)
Append a node

Specified by:
append in class DataModel
Parameters:
childNode - the child node to be appended

append

public java.lang.Object append(java.lang.String elementName)
Append a new node with the specified name. This method does not replace any existing nodes.

Specified by:
append in class DataModel
Parameters:
elementName - The immediate path to the DataModel required
Returns:
The value of the DataModel or the attribute

isAlwaysDirty

public boolean isAlwaysDirty()
Is the POJO always treated as dirty?

Returns:
true if the POJO always calls its getter

setAlwaysDirty

public void setAlwaysDirty(boolean state)
Set the POJO as always dirty?

Parameters:
state - true if the POJO always calls its getter

markSubtreeAsAlwaysDirty

protected void markSubtreeAsAlwaysDirty(boolean isAlwaysDirty)
Marks the children of this node as dirty.