net.sf.spif.persistence
Class TypePersister

java.lang.Object
  |
  +--net.sf.spif.persistence.TypePersister

public class TypePersister
extends java.lang.Object

Ignore - public for testing only.

A class which knows how to persist a certain type. This is used from the StorePersister to handle requests regarding a certain type.

This class caches discovered information on the fields of the type and the db table used to persist it, to achieve dynamic persistence mapping and high speed.

A typepersister may hold on to other type persisters which are used to persist dependent objects of this persister.

Version:
$Id: TypePersister.java,v 1.5 2003/04/03 20:43:42 bratseth Exp $
Author:
Jon S Bratseth

Field Summary
protected  net.sf.spif.persistence.DependentsPersister dependents
          The dependent persisters of this type persister
protected static java.util.logging.Logger log
           
protected  MatchProperties matchProperties
          The properties involved in matching at this type
protected  net.sf.spif.persistence.PersistentProperties persistentProperties
          The persistent properties of this type, or null if it does not have it's own table
protected  net.sf.spif.persistence.PersisterStatements statements
          The sql statements used by this class, or null if nothing should be persisted for *this* class (but maybe for children)
 
Constructor Summary
protected TypePersister()
          Must call initialize after this.
  TypePersister(java.lang.Class type, java.util.Map availableProperties, net.sf.spif.persistence.DependentPersister parent, java.sql.Connection connection)
          Creates a type persister.
 
Method Summary
protected  void addMatchedValues(java.lang.Object object, java.util.Map values)
          Adds values for the matchedProperties to the given map
 void bindValues(Id id, java.lang.Object source, java.sql.PreparedStatement statement, java.util.Map unmatchedValues, boolean update)
           
 java.lang.Object get(Id id, java.lang.Object receiver, java.util.Map leftoverValues, java.sql.Connection connection)
          Reads this object and it dependents from persistent storage.
 MatchProperties getMatchProperties()
          For testing
protected  net.sf.spif.persistence.DependentPersister getParent()
           
protected  java.sql.ResultSet getRows(Id id, java.sql.Connection connection)
           
protected  java.lang.Class getType()
          Returns the type this persists
protected  void initialize(java.lang.Class type, java.util.Map availableProperties, net.sf.spif.persistence.DependentPersister parent, java.sql.Connection connection)
          Does initializing which is obligatory for subclasses as well.
 java.lang.Boolean isEmpty()
          Returns whether this persister has anything to persist
protected  void populateFrom(java.sql.ResultSet row, java.util.Map leftoverValues, java.lang.Object object)
          Populates an object with setters matching the result set column names.
protected  boolean populateFromLeftovers(java.util.Map values, java.lang.Object object)
          Populates an object with setters matching the property names
 void put(Id id, java.lang.Object object, java.util.Map unmatchedValues, java.sql.Connection connection)
           
 boolean remove(Id id, java.lang.Class type, java.sql.Connection connection)
           
protected  boolean rowToObject(Id id, java.lang.Object receiver, java.util.Map leftoverValues, java.sql.ResultSet row)
          Creates an instance of the type given by the id and populates setters with matching names in the resultset
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected static java.util.logging.Logger log

statements

protected net.sf.spif.persistence.PersisterStatements statements
The sql statements used by this class, or null if nothing should be persisted for *this* class (but maybe for children)


dependents

protected net.sf.spif.persistence.DependentsPersister dependents
The dependent persisters of this type persister


persistentProperties

protected net.sf.spif.persistence.PersistentProperties persistentProperties
The persistent properties of this type, or null if it does not have it's own table


matchProperties

protected MatchProperties matchProperties
The properties involved in matching at this type

Constructor Detail

TypePersister

protected TypePersister()
Must call initialize after this. One constructor must throw an exception and the other not, that's why we need to separate out the throwing part in an initializer


TypePersister

public TypePersister(java.lang.Class type,
                     java.util.Map availableProperties,
                     net.sf.spif.persistence.DependentPersister parent,
                     java.sql.Connection connection)
              throws java.sql.SQLException
Creates a type persister.

Parameters:
type - the class this persists
availableProperties - the unmatched persistent properties of the owners table, which we may find a match for here, or null if there is no such properties
parent - the parent (owner) of this object, or null if this is a top-level object
connection - a db connection, which the type persister will not hold on to, or null if we are running tests and should not consult a db
Method Detail

initialize

protected final void initialize(java.lang.Class type,
                                java.util.Map availableProperties,
                                net.sf.spif.persistence.DependentPersister parent,
                                java.sql.Connection connection)
                         throws java.sql.SQLException
Does initializing which is obligatory for subclasses as well. See parameter description of constructor. TODO: I would like to move this into the constructor, but that must work with the CollectionTypePersister subclass.

Throws:
java.sql.SQLException - if there is no matching table

get

public java.lang.Object get(Id id,
                            java.lang.Object receiver,
                            java.util.Map leftoverValues,
                            java.sql.Connection connection)
                     throws java.sql.SQLException
Reads this object and it dependents from persistent storage.

java.sql.SQLException

put

public void put(Id id,
                java.lang.Object object,
                java.util.Map unmatchedValues,
                java.sql.Connection connection)
         throws java.sql.SQLException
java.sql.SQLException

remove

public boolean remove(Id id,
                      java.lang.Class type,
                      java.sql.Connection connection)
               throws java.sql.SQLException
java.sql.SQLException

isEmpty

public java.lang.Boolean isEmpty()
Returns whether this persister has anything to persist

Returns:
true if it has, false if it hasn't, or null if we don't know yet

getMatchProperties

public MatchProperties getMatchProperties()
For testing


addMatchedValues

protected void addMatchedValues(java.lang.Object object,
                                java.util.Map values)
Adds values for the matchedProperties to the given map


rowToObject

protected boolean rowToObject(Id id,
                              java.lang.Object receiver,
                              java.util.Map leftoverValues,
                              java.sql.ResultSet row)
                       throws java.sql.SQLException
Creates an instance of the type given by the id and populates setters with matching names in the resultset

Returns:
true if we read something, false otherwise
java.sql.SQLException

populateFrom

protected void populateFrom(java.sql.ResultSet row,
                            java.util.Map leftoverValues,
                            java.lang.Object object)
                     throws java.sql.SQLException
Populates an object with setters matching the result set column names. Result set values which has no corresponding setter will be silently ignored

java.sql.SQLException

populateFromLeftovers

protected boolean populateFromLeftovers(java.util.Map values,
                                        java.lang.Object object)
                                 throws java.sql.SQLException
Populates an object with setters matching the property names

java.sql.SQLException

getRows

protected java.sql.ResultSet getRows(Id id,
                                     java.sql.Connection connection)
                              throws java.sql.SQLException
java.sql.SQLException

bindValues

public void bindValues(Id id,
                       java.lang.Object source,
                       java.sql.PreparedStatement statement,
                       java.util.Map unmatchedValues,
                       boolean update)
                throws java.sql.SQLException
java.sql.SQLException

getType

protected java.lang.Class getType()
Returns the type this persists


getParent

protected net.sf.spif.persistence.DependentPersister getParent()

toString

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


Spif 1.01 (2003-04-03). http://spif.sf.net