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.SortedMap<java.lang.String,java.lang.Object>
Direct Known Subclasses:
ExternalObjectWrapper

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

An Instance provides field values by their name.

Author:
Claude Brisson
See Also:
Serialized Form

Field Summary
private  Database db
          The main database connection.
private  Entity entity
          This Instance's Entity.
private  boolean localized
          Is there a column to localize?
 
Constructor Summary
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.
 
Method Summary
 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.String key)
          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.
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.
 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 validate()
          Validate this instance against declared contraints.
 
Methods inherited from class java.util.TreeMap
clear, clone, comparator, containsKey, containsValue, entrySet, firstKey, get, headMap, keySet, lastKey, putAll, remove, size, subMap, tailMap, values
 
Methods inherited from class java.util.AbstractMap
hashCode, isEmpty, toString
 
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

private Entity entity
This Instance's Entity.


localized

private boolean localized
Is there a column to localize?


db

private Database db
The main database connection.

Constructor Detail

Instance

public Instance(Entity entity)
Build an empty instance for the given entity.

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 -
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

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

get

public java.lang.Object get(java.lang.String key)

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.

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

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

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.


handleSQLException

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



~ooOoo~