velosurf.context
Class Instance

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

public class Instance
extends java.util.TreeMap
implements ReadOnlyMap

An Instance provides field values by their name.

Author:
Claude Brisson
See Also:
Serialized Form

Field Summary
protected  Database db
          the main database connection
protected  Entity entity
          this Instance's Entity
protected  boolean localized
          is there a column to localize ?
protected  java.lang.ThreadLocal<UserContext> userContext
          thread-local user context
 
Constructor Summary
Instance(Entity entity)
          Build an empty instance for the given entity
Instance(ReadOnlyMap values)
           
 
Method Summary
 boolean delete()
          Delete the row associated with this Instance.
 boolean equals(java.lang.Object o)
           
 java.lang.Object get(java.lang.Object 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: meant to be overloaded in ExternalObjectWrapper
 java.lang.Object getInternal(java.lang.Object key)
          Internal getter: first tries on the external object then on the Map interface
 java.util.List getKeys()
          Deprecated. As of Velosurf version 0.9, replaced by getPrimaryKey Returns an ArrayList of two-entries maps ('name' & 'value'), meant to be use in a #foreach loop to build form fields, like:

#foreach ($field in $product.keys)

  <input type=hidden name='$field.name' value='$field.value'> #end

 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, like: #foreach ($field in $product.primaryKey)   <input type=hidden name='$field.name' value='$field.value'> #end
protected  void handleSQLException(java.sql.SQLException sqle)
          handle an sql exception
 void initialize(Entity entity)
          Meant to be overloaded if needed
 boolean insert()
          Insert a new row corresponding to this Instance.
 java.lang.Object put(java.lang.Object key, java.lang.Object value)
          Generic setter
protected  void setUserContext(UserContext userContext)
          set this instance user context (thread local)
 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.
 boolean update(java.util.Map 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.
 boolean validate()
          validate this instance against declared contraints
 
Methods inherited from class java.util.TreeMap
clear, clone, comparator, containsKey, containsValue, entrySet, firstKey, 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 velosurf.sql.ReadOnlyMap
keySet
 
Methods inherited from interface java.util.Map
hashCode, isEmpty
 

Field Detail

userContext

protected java.lang.ThreadLocal<UserContext> userContext
thread-local user context


entity

protected Entity entity
this Instance's Entity


localized

protected boolean localized
is there a column to localize ?


db

protected 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(ReadOnlyMap values)
Method Detail

initialize

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


getEntity

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

Returns:
this Instance's Entity.

getKeys

public java.util.List getKeys()
Deprecated. As of Velosurf version 0.9, replaced by getPrimaryKey Returns an ArrayList of two-entries maps ('name' & 'value'), meant to be use in a #foreach loop to build form fields, like:

#foreach ($field in $product.keys)

  <input type=hidden name='$field.name' value='$field.value'> #end

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

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, like:

#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.Object 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.

Specified by:
get in interface java.util.Map
Specified by:
get in interface ReadOnlyMap
Overrides:
get in class java.util.TreeMap
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.Object key,
                            java.lang.Object value)
Generic setter

Specified by:
put in interface java.util.Map
Overrides:
put in class java.util.TreeMap
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)
Specified by:
equals in interface java.util.Map
Overrides:
equals in class java.util.AbstractMap

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.lastError can be checked).

update

public boolean update(java.util.Map 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.lastError 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.lastError 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.lastError can be checked).

validate

public boolean validate()
validate this instance against declared contraints


handleSQLException

protected void handleSQLException(java.sql.SQLException sqle)
handle an sql exception


setUserContext

protected void setUserContext(UserContext userContext)
set this instance user context (thread local)



~ooOoo~