velosurf.context
Class Instance

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.TreeMap<java.lang.String,java.lang.Object>
          extended by velosurf.context.Instance
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<java.lang.String,java.lang.Object>, java.util.NavigableMap<java.lang.String,java.lang.Object>, java.util.SortedMap<java.lang.String,java.lang.Object>, HasParametrizedGetter
Direct Known Subclasses:
ExternalObjectWrapper

public class Instance
extends java.util.TreeMap<java.lang.String,java.lang.Object>
implements HasParametrizedGetter

An Instance provides field values by their name.

Author:
Claude Brisson
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Field Summary
protected  Database db
          The main database connection.
protected  java.util.List<java.lang.Boolean> dirtyFlags
          Keep a dirty flag per column
protected  Entity entity
          This Instance's Entity.
private  boolean localized
          Is there a column to localize?
 
Constructor Summary
Instance()
          Build an empty instance for the given entity.
Instance(Entity entity)
          Build an empty instance for the given entity.
Instance(java.util.Map<java.lang.String,java.lang.Object> values)
          Builds a generic instance using values.
Instance(java.util.Map<java.lang.String,java.lang.Object> values, Database db)
          Builds a generic instance using values.
 
Method Summary
 boolean containsKey(java.lang.Object key)
          Check for a key
 boolean delete()
          Delete the row associated with this Instance.
 boolean equals(java.lang.Object o)
          Test equality of two instances.
 java.lang.Object get(java.lang.Object k)
          Generic getter, used to access this instance properties by their name.
 EntityReference getEntity()
          Get this Instance's Entity.
 java.lang.Object getExternal(java.lang.Object key)
          External getter.
 java.lang.Object getInternal(java.lang.Object key)
          Internal getter.
 java.util.List getPrimaryKey()
          Returns an ArrayList of two-entries maps ('name' & 'value'), meant to be use in a #foreach loop to build form fields.
 java.lang.Object getWithParams(java.lang.String key, java.util.Map params)
          Default method handler, called by Velocity when it did not find the specified method.
private  void handleSQLException(java.sql.SQLException sqle)
          Handle an sql exception.
 void initialize(Entity entity)
          Initialization.
 boolean insert()
          Insert a new row corresponding to this Instance.
 java.lang.Object put(java.lang.String key, java.lang.Object value)
          Generic setter.
 java.lang.Object remove(java.lang.Object key)
          Removes an association
protected  java.lang.String resolveName(java.lang.String name)
           
 void setClean()
           
 void setColumnValues(java.util.Map<java.lang.String,java.lang.Object> values)
          Global setter that will only set values the correspond to actual columns (otherwise, use putAll(Map values)).
 java.lang.String toString()
          Inherit toString to avoid listing cached AttributeReference
 boolean update()
          Update the row associated with this Instance from passed values.
 boolean update(java.util.Map<java.lang.String,java.lang.Object> values)
          Update the row associated with this Instance from actual values.
 boolean upsert()
          Insert or update, depending on whether or not a value for the id key is present and does exist
 boolean upsert(java.util.Map<java.lang.String,java.lang.Object> values)
          Insert or update, depending on whether or not a value for the id key is present and does exist
 boolean validate()
          Validate this instance against declared contraints.
 
Methods inherited from class java.util.TreeMap
ceilingEntry, ceilingKey, clear, clone, comparator, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, putAll, size, subMap, subMap, tailMap, tailMap, values
 
Methods inherited from class java.util.AbstractMap
hashCode, isEmpty
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
hashCode, isEmpty
 

Field Detail

entity

protected Entity entity
This Instance's Entity.


localized

private boolean localized
Is there a column to localize?


db

protected Database db
The main database connection.


dirtyFlags

protected java.util.List<java.lang.Boolean> dirtyFlags
Keep a dirty flag per column

Constructor Detail

Instance

public Instance()
Build an empty instance for the given entity. The method initialize(Entity) should be called afterwards.


Instance

public Instance(Entity entity)
Build an empty instance for the given entity. This is the only constructor that will keep columns in their natural order (others will sort columns alphabetically)

Parameters:
entity - Entity this instance is a realisation of

Instance

public Instance(java.util.Map<java.lang.String,java.lang.Object> values)
Builds a generic instance using values.

Parameters:
values -

Instance

public Instance(java.util.Map<java.lang.String,java.lang.Object> values,
                Database db)
Builds a generic instance using values.

Parameters:
values -
db -
Method Detail

initialize

public void initialize(Entity entity)
Initialization. Meant to be overloaded if needed.

Parameters:
entity -

getEntity

public EntityReference getEntity()
Get this Instance's Entity.

Returns:
this Instance's Entity.

getPrimaryKey

public java.util.List getPrimaryKey()

Returns an ArrayList of two-entries maps ('name' & 'value'), meant to be use in a #foreach loop to build form fields.

Example:

#foreach ($field in $product.primaryKey)
  <input type=hidden name='$field.name' value='$field.value'>
#end

Please note that this method won't be of any help if you are using column aliases.

Returns:
an ArrayList of two-entries maps ('name' & 'value')

get

public java.lang.Object get(java.lang.Object k)

Generic getter, used to access this instance properties by their name.

Asked property is first searched in the Map, then among Attributes defined for the entity.

Specified by:
get in interface java.util.Map<java.lang.String,java.lang.Object>
Overrides:
get in class java.util.TreeMap<java.lang.String,java.lang.Object>
Parameters:
k - key of the property to be returned
Returns:
a String, an Instance, an AttributeReference or null if an error occurs

getWithParams

public java.lang.Object getWithParams(java.lang.String key,
                                      java.util.Map params)
Default method handler, called by Velocity when it did not find the specified method.

Specified by:
getWithParams in interface HasParametrizedGetter
Parameters:
key - asked key
params - passed parameters
See Also:
HasParametrizedGetter

put

public java.lang.Object put(java.lang.String key,
                            java.lang.Object value)
Generic setter.

Specified by:
put in interface java.util.Map<java.lang.String,java.lang.Object>
Overrides:
put in class java.util.TreeMap<java.lang.String,java.lang.Object>
Parameters:
key - key of the property to be set
value - corresponding value
Returns:
previous value, or null

setClean

public void setClean()

setColumnValues

public void setColumnValues(java.util.Map<java.lang.String,java.lang.Object> values)
Global setter that will only set values the correspond to actual columns (otherwise, use putAll(Map values)).

Parameters:
values - corresponding values

getInternal

public java.lang.Object getInternal(java.lang.Object key)
Internal getter. First tries on the external object then on the Map interface.

Parameters:
key - key of the property to be returned
Returns:
a String, an Instance, an AttributeReference or null if not found or if an error occurs

getExternal

public java.lang.Object getExternal(java.lang.Object key)
External getter. Meant to be overloaded in ExternalObjectWrapper.

Parameters:
key - key of the property to be returned
Returns:
a String, an Instance, an AttributeReference or null if not found or if an error occurs

equals

public boolean equals(java.lang.Object o)
Test equality of two instances.

Specified by:
equals in interface java.util.Map<java.lang.String,java.lang.Object>
Overrides:
equals in class java.util.AbstractMap<java.lang.String,java.lang.Object>
Parameters:
o - other instance
Returns:
equality status

update

public boolean update()

Update the row associated with this Instance from passed values.

Velosurf will ensure all key columns are specified, to avoid an accidental massive update.

Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

update

public boolean update(java.util.Map<java.lang.String,java.lang.Object> values)

Update the row associated with this Instance from actual values.

Velosurf will ensure all key columns are specified, to avoid an accidental massive update.

Parameters:
values - values to be used for the update
Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

delete

public boolean delete()

Delete the row associated with this Instance.

Velosurf will ensure all key columns are specified, to avoid an accidental massive update.

Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

insert

public boolean insert()
Insert a new row corresponding to this Instance.

Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

validate

public boolean validate()
Validate this instance against declared contraints.

Returns:
a boolean stating whether this instance data are valid in regard to declared constraints

handleSQLException

private void handleSQLException(java.sql.SQLException sqle)
Handle an sql exception.


resolveName

protected java.lang.String resolveName(java.lang.String name)

containsKey

public boolean containsKey(java.lang.Object key)
Check for a key

Specified by:
containsKey in interface java.util.Map<java.lang.String,java.lang.Object>
Overrides:
containsKey in class java.util.TreeMap<java.lang.String,java.lang.Object>
Returns:
whether or not this key is present

remove

public java.lang.Object remove(java.lang.Object key)
Removes an association

Specified by:
remove in interface java.util.Map<java.lang.String,java.lang.Object>
Overrides:
remove in class java.util.TreeMap<java.lang.String,java.lang.Object>
Returns:
the removed object, or null

toString

public java.lang.String toString()
Inherit toString to avoid listing cached AttributeReference

Overrides:
toString in class java.util.AbstractMap<java.lang.String,java.lang.Object>

upsert

public boolean upsert()
Insert or update, depending on whether or not a value for the id key is present and does exist

Returns:
success flag

upsert

public boolean upsert(java.util.Map<java.lang.String,java.lang.Object> values)
Insert or update, depending on whether or not a value for the id key is present and does exist



~ooOoo~