biz.hammurapi.sql
Class DatabaseObject

java.lang.Object
  extended by biz.hammurapi.sql.DatabaseObject
All Implemented Interfaces:
Context, ContextConfigurable, DomConfigurable, ColumnChangeListener, IDatabaseObject, Attributable, Observable, Versioned, DomSerializable, java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
MeasuringDatabaseObject

public class DatabaseObject
extends java.lang.Object
implements DomSerializable, ColumnChangeListener, java.lang.Cloneable, ContextConfigurable, Context, DomConfigurable, Attributable, Versioned, Observable, IDatabaseObject, java.io.Serializable

SQLC-generated interface implementations implement this method to achieve differential update functionality - inserting and updating only modified fields.

Version:
$Revision: 1.11 $
Author:
Pavel Vlasov
See Also:
Serialized Form

Field Summary
protected  java.util.Collection columns
           
protected  int objectVersion
          Subclasses can choose to read object version from the database.
protected  int originalVersion
           
 
Constructor Summary
DatabaseObject()
          Default constructor
DatabaseObject(boolean force)
           
 
Method Summary
protected  void addColumn(Column column)
           
 void addObserver(Observer observer)
          Adds observer to a collection of observers.
protected  RelationshipList addRelationship(java.lang.String name, java.lang.String itemName, Relationship relationship)
           
 void clear()
          Sets all columns to default values and clears modified and deleted flags.
 java.lang.Object clone()
          Clones object, clears columns collection, clears isDeleted and isModified flags.
 void configure(Context context, CompositeConverter converter)
           
 void configure(org.w3c.dom.Node configNode, Context context)
          Reads configuration from Node
 void copy(DatabaseObject source)
          Copies values from source object to this object
 int delete(SQLProcessor processor, java.lang.String tableName)
          Deletes row in a table by primary key.
 boolean equals(java.lang.Object otherBean)
          Two objects are considered equal and all their fields are equal.
 void fromDom(org.w3c.dom.Element holder)
          Loads columns from XML element
 void fromDom(org.w3c.dom.Element holder, java.util.Properties nameMap)
           
 java.lang.Object get(java.lang.String name)
           
 java.lang.Object getAttribute(java.lang.Object key)
           
protected  Column getColumn(java.lang.String name)
           
 java.lang.Object getColumnAttribute(java.lang.String columnName, java.lang.Object key)
           
 int getObjectVersion()
           
protected  RelationshipList getRelationship(java.lang.String name)
           
protected  int getSqlType(java.lang.String columnName, int generatedType)
          Allows to override generated column types with <class name>.sqltypes resource.
 int hashCode()
           
 int insert(SQLProcessor processor, java.lang.String tableName)
           
 boolean isDeleted()
          isDeleted flag is cleared when primary key columns are modified
 boolean isModified()
           
protected  void loadRelationships(SQLProcessor processor)
          Use this method to eagerly load relationships in constructors.
 void onChange(Column column)
          Sets modified flag to true and increments version number.
 java.lang.Object removeAttribute(java.lang.Object key)
           
 java.lang.Object removeColumnAttribute(java.lang.String columnName, java.lang.Object key)
           
 void removeObserver(Observer observer)
          Removes observer from observers collection.
 void setAttribute(java.lang.Object key, java.lang.Object value)
           
 void setColumnAttribute(java.lang.String columnName, java.lang.Object key, java.lang.Object value)
           
 void setOriginal()
          Sets current values as original values in primary key columns.
 void toDom(org.w3c.dom.Element holder)
           
 void toDom(org.w3c.dom.Element holder, java.util.Properties nameMap, boolean originals)
          Serializes to DOM.
 java.lang.String toString()
           
 int update(SQLProcessor processor, java.lang.String tableName)
          Updates row in a table by primary key
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

columns

protected java.util.Collection columns

objectVersion

protected int objectVersion
Subclasses can choose to read object version from the database.


originalVersion

protected int originalVersion
Constructor Detail

DatabaseObject

public DatabaseObject()
Default constructor


DatabaseObject

public DatabaseObject(boolean force)
Parameters:
force - Forces columns to be marked as modified if setter method is invoked even if value being set equals to existing column value. Useful during inserts with non-nullable columns which map to primitive types and as such have default values.
Method Detail

getColumn

protected Column getColumn(java.lang.String name)

addColumn

protected void addColumn(Column column)

update

public int update(SQLProcessor processor,
                  java.lang.String tableName)
           throws java.sql.SQLException
Description copied from interface: IDatabaseObject
Updates row in a table by primary key

Specified by:
update in interface IDatabaseObject
Parameters:
processor - SQLProcessor
tableName - Target table
Returns:
Number of updates
Throws:
java.sql.SQLException

delete

public int delete(SQLProcessor processor,
                  java.lang.String tableName)
           throws java.sql.SQLException
Description copied from interface: IDatabaseObject
Deletes row in a table by primary key.

Specified by:
delete in interface IDatabaseObject
Parameters:
processor - SQLProcessor
tableName - Target table
Returns:
Number of updates
Throws:
java.sql.SQLException

insert

public int insert(SQLProcessor processor,
                  java.lang.String tableName)
           throws java.sql.SQLException
Specified by:
insert in interface IDatabaseObject
Throws:
java.sql.SQLException

fromDom

public void fromDom(org.w3c.dom.Element holder)
             throws ConfigurationException
Description copied from interface: IDatabaseObject
Loads columns from XML element

Specified by:
fromDom in interface IDatabaseObject
Throws:
ConfigurationException - If loading fails

fromDom

public void fromDom(org.w3c.dom.Element holder,
                    java.util.Properties nameMap)
             throws ConfigurationException
Specified by:
fromDom in interface IDatabaseObject
Throws:
ConfigurationException

toDom

public void toDom(org.w3c.dom.Element holder)
Specified by:
toDom in interface DomSerializable

toDom

public void toDom(org.w3c.dom.Element holder,
                  java.util.Properties nameMap,
                  boolean originals)
Description copied from interface: IDatabaseObject
Serializes to DOM.

Specified by:
toDom in interface IDatabaseObject
Parameters:
holder - Holder element
nameMap - Name map
originals - Output original values if any.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

onChange

public void onChange(Column column)
Sets modified flag to true and increments version number. Also broadcasts the change to observers. Changed column is passed as second argument of update() method. Override this method in subclasses to react on change events, but don't forget to invoke super.onChange().

Specified by:
onChange in interface ColumnChangeListener
Parameters:
column - Changed column

setOriginal

public void setOriginal()
Description copied from interface: IDatabaseObject
Sets current values as original values in primary key columns.

Specified by:
setOriginal in interface IDatabaseObject

isModified

public boolean isModified()
Specified by:
isModified in interface IDatabaseObject
Returns:
true if object was modified since last database operation.

isDeleted

public boolean isDeleted()
Description copied from interface: IDatabaseObject
isDeleted flag is cleared when primary key columns are modified

Specified by:
isDeleted in interface IDatabaseObject
Returns:
true if delete() method was executed.

equals

public boolean equals(java.lang.Object otherBean)
Two objects are considered equal and all their fields are equal.

Overrides:
equals in class java.lang.Object
Parameters:
otherBean - Other object
Returns:
true if object classes are equal and all member column values are equal.

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Clones object, clears columns collection, clears isDeleted and isModified flags. Subclasses shall add cloned columns.

Overrides:
clone in class java.lang.Object
Throws:
java.lang.CloneNotSupportedException

clear

public void clear()
Description copied from interface: IDatabaseObject
Sets all columns to default values and clears modified and deleted flags.

Specified by:
clear in interface IDatabaseObject

configure

public void configure(Context context,
                      CompositeConverter converter)
               throws ConfigurationException
Specified by:
configure in interface ContextConfigurable
Parameters:
context - Context to read values from
converter - Converter to convert values to proper type.
Throws:
ConfigurationException

get

public java.lang.Object get(java.lang.String name)
Specified by:
get in interface Context

configure

public void configure(org.w3c.dom.Node configNode,
                      Context context)
               throws ConfigurationException
Description copied from interface: DomConfigurable
Reads configuration from Node

Specified by:
configure in interface DomConfigurable
Throws:
ConfigurationException

getRelationship

protected RelationshipList getRelationship(java.lang.String name)

addRelationship

protected RelationshipList addRelationship(java.lang.String name,
                                           java.lang.String itemName,
                                           Relationship relationship)
Parameters:
name - Name for composite relationships to render in XML, null for shared relationships.
itemName - name of item element in XML.
itemClass - item class. This class shall have constructor from Element,boolean in order to load from XML docs.
relationship -

loadRelationships

protected void loadRelationships(SQLProcessor processor)
                          throws java.sql.SQLException
Use this method to eagerly load relationships in constructors.

Parameters:
processor -
Throws:
java.sql.SQLException

copy

public void copy(DatabaseObject source)
Description copied from interface: IDatabaseObject
Copies values from source object to this object

Specified by:
copy in interface IDatabaseObject

setAttribute

public void setAttribute(java.lang.Object key,
                         java.lang.Object value)
Specified by:
setAttribute in interface Attributable

getAttribute

public java.lang.Object getAttribute(java.lang.Object key)
Specified by:
getAttribute in interface Attributable

removeAttribute

public java.lang.Object removeAttribute(java.lang.Object key)
Specified by:
removeAttribute in interface Attributable

setColumnAttribute

public void setColumnAttribute(java.lang.String columnName,
                               java.lang.Object key,
                               java.lang.Object value)
Specified by:
setColumnAttribute in interface IDatabaseObject

getColumnAttribute

public java.lang.Object getColumnAttribute(java.lang.String columnName,
                                           java.lang.Object key)
Specified by:
getColumnAttribute in interface IDatabaseObject

removeColumnAttribute

public java.lang.Object removeColumnAttribute(java.lang.String columnName,
                                              java.lang.Object key)
Specified by:
removeColumnAttribute in interface IDatabaseObject

getSqlType

protected int getSqlType(java.lang.String columnName,
                         int generatedType)
Allows to override generated column types with <class name>.sqltypes resource. Subclasses shall use this method when dealing with Object columns.

Parameters:
columnName -
generatedType -
Returns:

getObjectVersion

public int getObjectVersion()
Specified by:
getObjectVersion in interface Versioned
Returns:
Object version. Each modification shall increment object version.

addObserver

public void addObserver(Observer observer)
Description copied from interface: Observable
Adds observer to a collection of observers.

Specified by:
addObserver in interface Observable

removeObserver

public void removeObserver(Observer observer)
Description copied from interface: Observable
Removes observer from observers collection.

Specified by:
removeObserver in interface Observable


Copyright © 2003 Hammurapi Group. All Rights Reserved.