velosurf.model
Class Entity

java.lang.Object
  extended by velosurf.model.Entity

public class Entity
extends java.lang.Object

The Entity class represents an entity in the data model.

Author:
Claude Brisson

Nested Class Summary
 class Entity.ColumnOrderComparator
           
(package private)  class Entity.ValidationError
           
 
Field Summary
private  java.util.Map<java.lang.String,Action> actionMap
          Action map.
private  java.util.Map<java.lang.String,java.lang.String> aliases
          Column by alias map.
private  java.util.Map<java.lang.String,Attribute> attributeMap
          Attribute map.
private  Cache cache
          The cache.
private  int cachingMethod
          The caching method.
private  java.util.List<java.lang.String> columns
          Column names in natural order.
private  java.util.Map<java.lang.String,java.util.List<FieldConstraint>> constraints
          Constraint by column name map.
private  Database db
          The database connection.
private  java.lang.String fetchQuery
          The SQL query used to fetch one instance of this entity.
private  java.lang.Class instanceClass
          The java class to use to realize this instance.
private  boolean[] keyColObfuscated
          Obfuscation status of key columns.
private  java.util.List<java.lang.String> keyCols
          Key column names in natural order.
private  java.util.List localizedColumns
          Localized columns.
private static int MAX_DATA_DISPLAY_LENGTH
          Truncate validation error messages to a maximum number of characters.
private  java.lang.String name
          Name.
private  boolean obfuscate
          Whether to obfuscate something.
private  java.util.List<java.lang.String> obfuscatedColumns
          Names of obfuscated columns.
private  boolean readOnly
          Whether this entity is read-only or not.
private  java.lang.String table
          Table.
private  java.util.Map<java.lang.String,java.lang.Integer> types
          Column types
private  java.util.List<java.lang.String> updatableCols
          Non-key column names in natural order
 
Constructor Summary
Entity(Database db, java.lang.String name, boolean readOnly, int cachingMethod)
          Constructor reserved for the framework.
 
Method Summary
 void addAction(Action action)
          Add an action.
 void addAlias(java.lang.String alias, java.lang.String column)
          Add a column alias.
 void addAttribute(Attribute attribute)
          Add a new attribute.
 void addColumn(java.lang.String colName, int sqlType)
          Add a column at the end of the sequential list of named columns.
 void addConstraint(java.lang.String column, FieldConstraint constraint)
          Add a constraint.
 void addPKColumn(java.lang.String colName)
          Add a key column to the sequential list of the key columns.
private  void buildFetchQuery()
          Build the SQL query used to fetch one instance of this query.
private  java.lang.Object buildKey(java.util.List<java.lang.Object> values)
          Build the key for the Cache from a List
private  java.lang.Object buildKey(java.util.Map<java.lang.String,java.lang.Object> values)
          Build the key for the Cache from a Map.
private  java.lang.Object buildKey(java.lang.Number value)
          Build the key for the Cache from a Number
protected  void clearCache()
          Clear the cache (not used for now).
 boolean delete(java.util.Map<java.lang.String,java.lang.Object> values)
          Delete a row based on (key) values.
 boolean delete(java.lang.Number keyValue)
          Delete a row based on the unique key string value.
 boolean delete(java.lang.String keyValue)
          Delete a row based on the unique key string value.
 java.lang.String deobfuscate(java.lang.Object value)
          De-obfuscate given value.
private  void extractColumnValues(java.util.Map<java.lang.String,java.lang.Object> source, java.util.Map<java.lang.String,java.lang.Object> target, boolean SQLNames)
          Extract column values from an input Map source and store result in target.
 Instance fetch(java.util.List<java.lang.Object> values)
          Fetch an instance from key values stored in a List in natural order.
 Instance fetch(java.util.Map<java.lang.String,java.lang.Object> values)
          Fetch an instance from key values stored in a Map.
 Instance fetch(java.lang.Number keyValue)
          Fetch an instance from its key value specified as a Number.
 Instance fetch(java.lang.String keyValue)
          Fetch an instance from its key value as a string.
 java.lang.Object filterID(java.lang.Long id)
          Obfuscate this id value if needed.
 java.lang.Object filterIncomingValue(java.lang.String column, java.lang.Object value)
           
 ExportedKey findExportedKey(Entity fkEntity, java.util.List<java.lang.String> fkCols)
          Check for the existence of an exported key with the same columns.
 ImportedKey findImportedKey(Entity pkEntity, java.util.List<java.lang.String> fkCols)
          Check for the existence of an imported key with the same columns.
 Action getAction(java.lang.String property)
          Get an action.
 java.util.Map<java.lang.String,Action> getActions()
           
 Attribute getAttribute(java.lang.String property)
          Get a named attribute.
 java.util.Map<java.lang.String,Attribute> getAttributes()
           
 int getColumnIndex(java.lang.String name)
           
 java.util.Comparator<java.lang.String> getColumnOrderComparator()
           
 java.util.List<java.lang.String> getColumns()
          Getter for the list of column names.
 int getColumnType(java.lang.String column)
          Get the SQL type for the specified column
 long getCount()
           
 long getCount(java.util.List refineCriteria)
           
 Database getDB()
          Get the database connection.
 java.lang.String getFetchQuery()
          Get the SQL query string used to fetch one instance of this query.
 java.lang.String getName()
          Getter for the name of this entity.
 java.util.List<java.lang.String> getPKCols()
          Getter for the list of key column names.
 java.lang.String getTableName()
          Get the name of the mapped table.
 int getUpdatableColumnIndex(java.lang.String name)
           
 java.util.List<java.lang.String> getUpdatableColumns()
           
 boolean hasLocalizedColumns()
          Does this entity have localized columns?
 boolean insert(java.util.Map<java.lang.String,java.lang.Object> values)
          Insert a new row based on values of a map.
 void invalidateInstance(java.util.Map<java.lang.String,java.lang.Object> instance)
          Invalidate an instance in the cache.
 boolean isColumn(java.lang.String name)
           
 boolean isLocalized(java.lang.String column)
          Returns whether the given column is obfuscated.
 boolean isObfuscated(java.lang.String column)
          Returns whether the given column is obfuscated.
 boolean isReadOnly()
          Is this entity read-only or read-write?
 boolean isRootEntity()
           
 Instance newInstance()
          Create a new realisation of this entity.
 Instance newInstance(java.util.Map<java.lang.String,java.lang.Object> values)
          Build a new instance from a Map object.
 Instance newInstance(java.util.Map<java.lang.String,java.lang.Object> values, boolean useSQLnames)
          Build a new instance from a Map object.
 java.lang.String obfuscate(java.lang.Object value)
          Obfuscate given value.
 RowIterator query()
          Issue a query to iterate though all instances of this entity.
 RowIterator query(java.util.List refineCriteria, java.lang.String order)
          Issue a query to iterate thought instances of this entity, with a facultative refining criteria and a facultative order by clause.
 java.lang.String resolveName(java.lang.String alias)
          Translates an alias to its column name.
 void reverseEnginered()
          Used by the framework to notify this entity that its reverse enginering is over.
 void setCachingMethod(int caching)
          Specify the caching method.
 void setInstanceClass(java.lang.String className)
          Specify a custom class to use when instanciating this entity.
 void setLocalized(java.util.List columns)
          Indicates a column as being localized.
 void setObfuscated(java.util.List<java.lang.String> columns)
          Indicates a column as being obfuscated.
 void setReadOnly(boolean readOnly)
          Set this entity to be read-only or read-write.
 void setTableName(java.lang.String table)
          Set the name of the table mapped by this entity.
 boolean update(java.util.Map<java.lang.String,java.lang.Object> values)
          Update a row based on a set of values that must contain key values.
 boolean upsert(java.util.Map<java.lang.String,java.lang.Object> values)
          Upsert a row based on a set of values (entity's primary key must be one column long - it can be omitted from provided values)
 boolean validate(java.util.Map<java.lang.String,java.lang.Object> row)
          Validate a set of values.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_DATA_DISPLAY_LENGTH

private static final int MAX_DATA_DISPLAY_LENGTH
Truncate validation error messages to a maximum number of characters.

See Also:
Constant Field Values

name

private java.lang.String name
Name.


table

private java.lang.String table
Table.


columns

private java.util.List<java.lang.String> columns
Column names in natural order.


types

private java.util.Map<java.lang.String,java.lang.Integer> types
Column types


keyCols

private java.util.List<java.lang.String> keyCols
Key column names in natural order.


updatableCols

private java.util.List<java.lang.String> updatableCols
Non-key column names in natural order


obfuscate

private boolean obfuscate
Whether to obfuscate something.


obfuscatedColumns

private java.util.List<java.lang.String> obfuscatedColumns
Names of obfuscated columns.


keyColObfuscated

private boolean[] keyColObfuscated
Obfuscation status of key columns.


localizedColumns

private java.util.List localizedColumns
Localized columns.


aliases

private java.util.Map<java.lang.String,java.lang.String> aliases
Column by alias map.


attributeMap

private java.util.Map<java.lang.String,Attribute> attributeMap
Attribute map.


actionMap

private java.util.Map<java.lang.String,Action> actionMap
Action map.


instanceClass

private java.lang.Class instanceClass
The java class to use to realize this instance.


fetchQuery

private java.lang.String fetchQuery
The SQL query used to fetch one instance of this entity.


readOnly

private boolean readOnly
Whether this entity is read-only or not.


db

private Database db
The database connection.


cachingMethod

private int cachingMethod
The caching method.


cache

private Cache cache
The cache.


constraints

private java.util.Map<java.lang.String,java.util.List<FieldConstraint>> constraints
Constraint by column name map.

Constructor Detail

Entity

public Entity(Database db,
              java.lang.String name,
              boolean readOnly,
              int cachingMethod)
Constructor reserved for the framework.

Parameters:
db - database connection
name - entity name
readOnly - access mode (read-write or read-only)
cachingMethod - caching method to be used
Method Detail

addColumn

public void addColumn(java.lang.String colName,
                      int sqlType)
Add a column at the end of the sequential list of named columns. Called during the reverse engeenering of the database.

Parameters:
colName - column name

addAlias

public void addAlias(java.lang.String alias,
                     java.lang.String column)
Add a column alias.

Parameters:
alias - alias
column - column

resolveName

public java.lang.String resolveName(java.lang.String alias)
Translates an alias to its column name.

Parameters:
alias - alias
Returns:
column name

addPKColumn

public void addPKColumn(java.lang.String colName)
Add a key column to the sequential list of the key columns. Called during the reverse-engeenering of the database.

Parameters:
colName - name of the key column

addAttribute

public void addAttribute(Attribute attribute)
Add a new attribute.

Parameters:
attribute - attribute

getAttribute

public Attribute getAttribute(java.lang.String property)
Get a named attribute.

Parameters:
property - attribute name
Returns:
the attribute

addAction

public void addAction(Action action)
Add an action.

Parameters:
action - action

getAction

public Action getAction(java.lang.String property)
Get an action.

Parameters:
property - action name
Returns:
the action

setInstanceClass

public void setInstanceClass(java.lang.String className)
Specify a custom class to use when instanciating this entity.

Parameters:
className - the java class name

setCachingMethod

public void setCachingMethod(int caching)
Specify the caching method. See Cache for allowed constants.

Parameters:
caching - Caching method

addConstraint

public void addConstraint(java.lang.String column,
                          FieldConstraint constraint)
Add a constraint.

Parameters:
column - column name
constraint - constraint

reverseEnginered

public void reverseEnginered()
Used by the framework to notify this entity that its reverse enginering is over.


clearCache

protected void clearCache()
Clear the cache (not used for now).


newInstance

public Instance newInstance()
Create a new realisation of 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

newInstance

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

Parameters:
values - the Map object containing the values
useSQLnames - map keys use SQL column names that must be translated to aliases
Returns:
the newly created instance

invalidateInstance

public void invalidateInstance(java.util.Map<java.lang.String,java.lang.Object> instance)
                        throws java.sql.SQLException
Invalidate an instance in the cache.

Parameters:
instance - instance
Throws:
java.sql.SQLException

extractColumnValues

private void extractColumnValues(java.util.Map<java.lang.String,java.lang.Object> source,
                                 java.util.Map<java.lang.String,java.lang.Object> target,
                                 boolean SQLNames)
                          throws java.sql.SQLException
Extract column values from an input Map source and store result in target.

Parameters:
source - Map source object
target - Map target object
SQLNames - the source uses SQL names
Throws:
java.sql.SQLException

buildKey

private java.lang.Object buildKey(java.util.Map<java.lang.String,java.lang.Object> values)
                           throws java.sql.SQLException
Build the key for the Cache from a Map.

Parameters:
values - the Map containing all values (unaliased)
Returns:
an array containing all key values
Throws:
java.sql.SQLException - the getter of the Map throws an SQLException

buildKey

private java.lang.Object buildKey(java.util.List<java.lang.Object> values)
                           throws java.sql.SQLException
Build the key for the Cache from a List

Parameters:
values - the Map containing all values (unaliased)
Returns:
an array containing all key values
Throws:
java.sql.SQLException - the getter of the Map throws an SQLException

buildKey

private java.lang.Object buildKey(java.lang.Number value)
                           throws java.sql.SQLException
Build the key for the Cache from a Number

Parameters:
values - the Map containing all values (unaliased)
Returns:
an array containing all key values
Throws:
java.sql.SQLException - the getter of the Map throws an SQLException

getName

public java.lang.String getName()
Getter for the name of this entity.

Returns:
the name of the entity

getPKCols

public java.util.List<java.lang.String> getPKCols()
Getter for the list of key column names.

Returns:
the list of key column names

getColumns

public java.util.List<java.lang.String> getColumns()
Getter for the list of column names.

Returns:
the list of column names

getUpdatableColumns

public java.util.List<java.lang.String> getUpdatableColumns()

isColumn

public boolean isColumn(java.lang.String name)

getColumnIndex

public int getColumnIndex(java.lang.String name)

getUpdatableColumnIndex

public int getUpdatableColumnIndex(java.lang.String name)

insert

public boolean insert(java.util.Map<java.lang.String,java.lang.Object> values)
               throws java.sql.SQLException
Insert a new row based on values of a map.

Parameters:
values - the Map object containing the values
Returns:
success indicator
Throws:
java.sql.SQLException

update

public boolean update(java.util.Map<java.lang.String,java.lang.Object> values)
               throws java.sql.SQLException
Update a row based on a set of values that must contain key values.

Parameters:
values - the Map object containing the values
Returns:
success indicator
Throws:
java.sql.SQLException

upsert

public boolean upsert(java.util.Map<java.lang.String,java.lang.Object> values)
               throws java.sql.SQLException
Upsert a row based on a set of values (entity's primary key must be one column long - it can be omitted from provided values)

Parameters:
values - the Map object containing the values
Returns:
success indicator
Throws:
java.sql.SQLException

delete

public boolean delete(java.util.Map<java.lang.String,java.lang.Object> values)
               throws java.sql.SQLException
Delete a row based on (key) values.

Parameters:
values - the Map containing the values
Returns:
success indicator
Throws:
java.sql.SQLException

delete

public boolean delete(java.lang.String keyValue)
               throws java.sql.SQLException
Delete a row based on the unique key string value.

Parameters:
keyValue - key value
Returns:
success indicator
Throws:
java.sql.SQLException

delete

public boolean delete(java.lang.Number keyValue)
               throws java.sql.SQLException
Delete a row based on the unique key string value.

Parameters:
keyValue - key value
Returns:
success indicator
Throws:
java.sql.SQLException

fetch

public Instance fetch(java.util.List<java.lang.Object> values)
               throws java.sql.SQLException
Fetch an instance from key values stored in a List in natural order.

Parameters:
values - the List containing the key values
Returns:
the fetched instance
Throws:
java.sql.SQLException

fetch

public Instance fetch(java.util.Map<java.lang.String,java.lang.Object> values)
               throws java.sql.SQLException
Fetch an instance from key values stored in a Map.

Parameters:
values - the Map containing the key values
Returns:
the fetched instance
Throws:
java.sql.SQLException

fetch

public Instance fetch(java.lang.String keyValue)
               throws java.sql.SQLException
Fetch an instance from its key value as a string.

Parameters:
keyValue - the key
Returns:
the fetched instance
Throws:
java.sql.SQLException

fetch

public Instance fetch(java.lang.Number keyValue)
               throws java.sql.SQLException
Fetch an instance from its key value specified as a Number.

Parameters:
keyValue - the key
Returns:
the fetched instance
Throws:
java.sql.SQLException

getFetchQuery

public java.lang.String getFetchQuery()
Get the SQL query string used to fetch one instance of this query.

Returns:
the SLQ query

buildFetchQuery

private void buildFetchQuery()
Build the SQL query used to fetch one instance of this query.


query

public RowIterator query()
                  throws java.sql.SQLException
Issue a query to iterate though all instances of this entity.

Returns:
the resulting RowIterator
Throws:
java.sql.SQLException

query

public RowIterator query(java.util.List refineCriteria,
                         java.lang.String order)
                  throws java.sql.SQLException
Issue a query to iterate thought instances of this entity, with a facultative refining criteria and a facultative order by clause.

Parameters:
refineCriteria - a refining criteria or null to get all instances
order - an 'order by' clause or null to get instances in their natural order
Returns:
the resulting RowIterator
Throws:
java.sql.SQLException

getCount

public long getCount()

getCount

public long getCount(java.util.List refineCriteria)

getDB

public Database getDB()
Get the database connection.

Returns:
the database connection

isReadOnly

public boolean isReadOnly()
Is this entity read-only or read-write?

Returns:
whether this entity is read-only or not

setReadOnly

public void setReadOnly(boolean readOnly)
Set this entity to be read-only or read-write.

Parameters:
readOnly - the mode to switch to : true for read-only, false for read-write

setTableName

public void setTableName(java.lang.String table)
Set the name of the table mapped by this entity.

Parameters:
table - the table mapped by this entity read-write

getTableName

public java.lang.String getTableName()
Get the name of the mapped table.

Returns:
name of the mapped table

setObfuscated

public void setObfuscated(java.util.List<java.lang.String> columns)
Indicates a column as being obfuscated.

Parameters:
columns - list of obfuscated columns

isObfuscated

public boolean isObfuscated(java.lang.String column)
Returns whether the given column is obfuscated.

Parameters:
column - the name of the column
Returns:
a boolean indicating whether this column is obfuscated

obfuscate

public java.lang.String obfuscate(java.lang.Object value)
Obfuscate given value.

Parameters:
value - value to obfuscate
Returns:
obfuscated value

filterID

public java.lang.Object filterID(java.lang.Long id)
Obfuscate this id value if needed.

Parameters:
id - id value
Returns:
filtered id value (that is, obfuscated if needed)

deobfuscate

public java.lang.String deobfuscate(java.lang.Object value)
De-obfuscate given value.

Parameters:
value - value to de-obfuscate
Returns:
obfuscated value

setLocalized

public void setLocalized(java.util.List columns)
Indicates a column as being localized.

Parameters:
columns - list of localized columns

isLocalized

public boolean isLocalized(java.lang.String column)
Returns whether the given column is obfuscated.

Parameters:
column - the name of the column
Returns:
a boolean indicating whether this column is obfuscated

hasLocalizedColumns

public boolean hasLocalizedColumns()
Does this entity have localized columns?


validate

public boolean validate(java.util.Map<java.lang.String,java.lang.Object> row)
                 throws java.sql.SQLException
Validate a set of values.

Throws:
java.sql.SQLException

getColumnOrderComparator

public java.util.Comparator<java.lang.String> getColumnOrderComparator()

findImportedKey

public ImportedKey findImportedKey(Entity pkEntity,
                                   java.util.List<java.lang.String> fkCols)
Check for the existence of an imported key with the same columns.

Parameters:
pkEntity - primary key entity
fkCols - foreign key columns
Returns:
previously defined imported key, if any

findExportedKey

public ExportedKey findExportedKey(Entity fkEntity,
                                   java.util.List<java.lang.String> fkCols)
Check for the existence of an exported key with the same columns.

Parameters:
fkEntity - foreign key entity
fkCols - foreign key columns
Returns:
previously defined exported key, if any

filterIncomingValue

public java.lang.Object filterIncomingValue(java.lang.String column,
                                            java.lang.Object value)

getAttributes

public java.util.Map<java.lang.String,Attribute> getAttributes()

getActions

public java.util.Map<java.lang.String,Action> getActions()

isRootEntity

public boolean isRootEntity()

getColumnType

public int getColumnType(java.lang.String column)
Get the SQL type for the specified column

Parameters:
column - column name
Returns:
the sql type


~ooOoo~