velosurf.context
Class EntityReference

java.lang.Object
  extended by velosurf.context.EntityReference
All Implemented Interfaces:
java.lang.Iterable

public class EntityReference
extends java.lang.Object
implements java.lang.Iterable

Context wrapper for an entity.

Author:
Claude Brisson

Field Summary
private  Entity entity
          The wrapped entity.
private  java.lang.String order
          Specified order.
private  java.util.List<java.lang.String> refineCriteria
          Specified refining criteria.
 
Constructor Summary
EntityReference(Entity entity)
          Builds a new EntityReference.
 
Method Summary
 void clearRefinement()
          Clears any refinement made on this entity.
 boolean delete(java.util.Map<java.lang.String,java.lang.Object> values)
          Detele a row from this entity's table.
 boolean delete(java.lang.Number keyValue)
          Detele a row from this entity's table, specifying the value of its unique key column.
 boolean delete(java.lang.String keyValue)
          Detele a row from this entity's table, specifying the value of its unique key column.
 Instance fetch(java.util.List<java.lang.Object> values)
          Fetch an Instance of this entity, specifying the values of its key columns in their natural order.
 Instance fetch(java.util.Map<java.lang.String,java.lang.Object> values)
          Fetch an Instance of this entity, specifying the values of its key columns in the map.
 Instance fetch(java.lang.Number keyValue)
          Fetch an Instance of this entity, specifying the value of its unique key column as an integer
 Instance fetch(java.lang.String keyValue)
          Fetch an Instance of this entity, specifying the value of its unique key column as a string
 java.util.List getColumns()
          Getter for the list of column names.
 long getCount()
           
 java.lang.Object getLastInsertID()
          Returns the ID of the last inserted row (obfuscated if needed).
 java.lang.String getName()
          gets the name of the wrapped entity
 java.util.List getRows()
          Get all the rows in a list of maps.
 boolean insert(java.util.Map<java.lang.String,java.lang.Object> values)
          Insert a new row in this entity's table.
 java.util.Iterator iterator()
          Called by the #foreach directive.
 Instance newInstance()
          Create a new instance for this entity.
 Instance newInstance(java.util.Map<java.lang.String,java.lang.Object> values)
          Build a new instance from a Map object.
 void refine(java.lang.String criterium)
          Refines this entity reference querying result.
 void setOrder(java.lang.String order)
          Specify an 'order by' clause for this attribute reference result.
 java.lang.String toString()
          toString, used for debugging
 boolean update(java.util.Map<java.lang.String,java.lang.Object> values)
          Update a row in this entity's table.
 boolean upsert(java.util.Map<java.lang.String,java.lang.Object> values)
          Upsert a row in this entity's table.
 boolean validate(java.util.Map<java.lang.String,java.lang.Object> values)
          Validate values of this instance.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

entity

private Entity entity
The wrapped entity.


order

private java.lang.String order
Specified order.


refineCriteria

private java.util.List<java.lang.String> refineCriteria
Specified refining criteria.

Constructor Detail

EntityReference

public EntityReference(Entity entity)
Builds a new EntityReference.

Parameters:
entity - the wrapped entity
Method Detail

getName

public java.lang.String getName()
gets the name of the wrapped entity


insert

public boolean insert(java.util.Map<java.lang.String,java.lang.Object> values)
Insert a new row in this entity's table.

Parameters:
values - col -> value map
Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

getLastInsertID

public java.lang.Object getLastInsertID()
Returns the ID of the last inserted row (obfuscated if needed).

Returns:
last insert ID

update

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

Update a row in this entity's table.

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

Parameters:
values - col -> value map
Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

upsert

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

Upsert a row in this entity's table.

Primary key must be a single column.

Parameters:
values - col -> value map
Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

delete

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

Detele a row from this entity's table.

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

Parameters:
values - col -> value map
Returns:
true if successfull, false if an error occurs (in which case $db.error can be checked).

delete

public boolean delete(java.lang.String keyValue)

Detele a row from this entity's table, specifying the value of its unique key column.

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

delete

public boolean delete(java.lang.Number keyValue)

Detele a row from this entity's table, specifying the value of its unique key column.

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

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

fetch

public Instance fetch(java.util.List<java.lang.Object> values)
Fetch an Instance of this entity, specifying the values of its key columns in their natural order.

Parameters:
values - values of the key columns
Returns:
an Instance, or null if an error occured (in which case $db.error can be checked)

fetch

public Instance fetch(java.util.Map<java.lang.String,java.lang.Object> values)
Fetch an Instance of this entity, specifying the values of its key columns in the map.

Parameters:
values - key=>value map
Returns:
an Instance, or null if an error occured (in which case $db.error can be checked)

fetch

public Instance fetch(java.lang.String keyValue)
Fetch an Instance of this entity, specifying the value of its unique key column as a string

Parameters:
keyValue - value of the key column
Returns:
an Instance, or null if an error occured (in which case $db.error can be checked)

fetch

public Instance fetch(java.lang.Number keyValue)
Fetch an Instance of this entity, specifying the value of its unique key column as an integer

Parameters:
keyValue - value of the key column
Returns:
an Instance, or null if an error occured (in which case $db.error can be checked)

iterator

public java.util.Iterator iterator()
Called by the #foreach directive.

Specified by:
iterator in interface java.lang.Iterable
Returns:
a RowIterator on all instances of this entity, possibly previously refined or ordered.

getRows

public java.util.List getRows()
Get all the rows in a list of maps.

Returns:
a list of all the rows

refine

public void refine(java.lang.String criterium)

Refines this entity reference querying result. The provided criterium will be added to the 'where' clause (or a 'where' clause will be added).

This method can be called several times, thus allowing a field-by-field handling of an html search form.

All criteria will be merged with the sql 'and' operator (if there is an initial where clause, it is wrapped into parenthesis).

Example: if we issue the following calls from inside the template:

$person.refine("age>30")
$person.refine("salary>3000")

the resulting query that will be issed is:

select * from person where (age>30) and (salary>3000)

Parameters:
criterium - a valid sql condition

clearRefinement

public void clearRefinement()
Clears any refinement made on this entity.


setOrder

public void setOrder(java.lang.String order)

Specify an 'order by' clause for this attribute reference result.

If an 'order by' clause is already present in the original query, the ew one is appended (but successive calls to this method overwrite previous ones).

postfix " DESC " to a column for descending order.

Pass it null or an empty string to clear any ordering.

Parameters:
order - valid sql column names (separated by commas) indicating the desired order

newInstance

public Instance newInstance()
Create a new instance for this entity.

Returns:
the newly created instance

newInstance

public Instance newInstance(java.util.Map<java.lang.String,java.lang.Object> values)
Build a new instance from a Map object.

Parameters:
values - the Map object containing the values
Returns:
the newly created instance

validate

public boolean validate(java.util.Map<java.lang.String,java.lang.Object> values)
Validate values of this instance.

Parameters:
values -
Returns:
true if values are valid with respect to defined column constraints

getColumns

public java.util.List getColumns()
Getter for the list of column names.

Returns:
the list of column names

getCount

public long getCount()

toString

public java.lang.String toString()
toString, used for debugging

Overrides:
toString in class java.lang.Object


~ooOoo~