org.apache.commons.scaffold.sql
Class StorageBeanBase

java.lang.Object
  extended byorg.apache.commons.scaffold.util.ProcessBeanBase
      extended byorg.apache.commons.scaffold.sql.StorageBeanBase
All Implemented Interfaces:
ProcessBean, Storable, StorageBean
Direct Known Subclasses:
ScrollerBeanBase

public class StorageBeanBase
extends ProcessBeanBase
implements StorageBean

Implements StorageBean interface using StatementUtils and ResultSetUtils in this package. Specialty operations may be added using the same utilities.

To use this class, the developer must:

To specify a connection pool and load the SQL commands:

The ConnectionServlet in this package is designed to load ConnectionAdapters and resource (Properties) files.

Regarding the queries:

Version:
$Revision: 1.21 $ $Date: 2004/03/21 21:10:27 $

Field Summary
protected static java.util.Properties commands
          Our command string properties.
static java.lang.String DELETE
          [:TODO: Javadoc] purge
static java.lang.String DOT
          [:TODO: Javadoc]
static java.lang.String INSERT
          [:TODO: Javadoc]
static java.lang.String KEYS_INC
          [:TODO: Javadoc]
static java.lang.String KEYS_NEXT
          [:TODO: Javadoc]
static java.lang.String PARAM_SEP
          Default separator character for list of parameters [","] (comma).
static java.lang.String PARAMS
          [:TODO: Javadoc]
static java.lang.String RECYCLE
          [:TODO: Javadoc] delete
static java.lang.String RESTORE
          [:TODO: Javadoc]
static java.lang.String RETRIEVE
          [:TODO: Javadoc]
static java.lang.String TABLE
          [:TODO: Javadoc]
static java.lang.String UPDATE
          [:TODO: Javadoc]
 
Fields inherited from interface org.apache.commons.scaffold.util.ProcessBean
USER_PROFILE_KEY
 
Constructor Summary
StorageBeanBase()
           
 
Method Summary
 void allocateKey()
          Obtain a new primary key for this object.
static boolean blank(java.lang.Number key)
          Returns true if null or zero.
protected  boolean blank(java.lang.String s)
          Convenience method to check for null, empty String.
protected  boolean blankValue(java.lang.String s)
          Convenience method to check for null, empty String, or "0" String.
 java.lang.Object collection(java.lang.Object parameters)
          Retrieve a collection of objects from storage.
 java.lang.Object collectionResult(java.lang.String command)
           
 int count(java.lang.String command)
           
 int count(java.lang.String command, java.lang.Object parameter)
          Count of matching entries.
 java.lang.Object createKey(java.lang.String keyName)
          Returns next sequential key for given set of keys.
 void delete()
          Permenantly delete record.
 java.lang.Object delete(java.lang.Object parameters)
          [:TODO: Javadoc]
 java.lang.Object element(java.lang.Object command)
          Retrieve an element (record) from storage.
 ProcessResult elementResult(java.lang.String command)
           
 void executeUpdate(java.lang.String command)
          Lookup command and execute "update" query to create a table, seed it with data, et cetera.
 void executeUpdateRoot(java.lang.String command)
          Lookup command (sans prefix) and execute "update" query to create a table, seed it with data, et cetera.
 java.util.Collection findByProperty(java.lang.Object target, java.lang.String property, java.lang.String value)
          Select entries from data storage by indexed property..
 java.util.Collection findCollection(java.lang.Object target, java.lang.String command)
          Convenience method that calls findCollection(Object,String,Object[]) with appropriate parameters.
protected  java.util.Collection findCollection(java.lang.Object target, java.lang.String command, int parameter)
          Convenience method that calls findCollection(Object,String,Object[]) with appropriate parameters.
protected  java.util.Collection findCollection(java.lang.Object target, java.lang.String command, java.lang.Object parameter)
          Convenience method that calls findCollection(Object,String,Object[]) with appropriate parameters.
 java.util.Collection findCollection(java.lang.Object target, java.lang.String command, java.lang.Object[] parameters)
          Retrieve a collection of beans from data storage.
 boolean findElement(java.lang.Object target, java.lang.String command, java.lang.Object key)
          Retrieve object from data storage.
 void fixModified()
          If timestamp is null (or ConvertUtils.NULL_TIMESTAMP), set our modified property to the current time.
 java.lang.Short getMarked()
          Return the marked status.
 java.sql.Timestamp getModified()
          Return the edited timestamp.
 java.lang.Object[] getParameters(java.lang.String command)
          Return the runtime parameters for the given command.
 java.util.List getParamList(java.lang.String command)
          Return the runtime parameters for the given command.
 java.lang.String getPrefix()
          [:TODO: Javadoc]
 java.lang.Object getResult()
          Return the result object of an operation.
 int getResultCode()
          Return the result code from an operation.
 java.lang.Object getStorageKey()
          Return this object's primary key.
static void init(java.util.Properties _commands)
          Set the Properties file to be used for SQL commands.
 boolean isCurrent(java.sql.Timestamp modified)
          Check to see if another modified timetamp matches the one for this instance.
 boolean isNew()
          Return whether this is a new record, or one that has already been stored.
static java.lang.String like(java.lang.String parameter)
          [:TODO: Javadoc]
protected  java.lang.String lookup(java.lang.String key)
          Retrieve command from commands Properties
protected  java.lang.String lookupRoot(java.lang.String key)
          Retrieve command from commands Properties
 void populate(java.util.Map parameters)
          Populate this bean from the entries on the provided map.
 void recycle()
          Mark entry for deletion.
 java.lang.Object recycle(java.lang.Object parameters)
          Mark this object for deletion.
 void restore()
          Unmark entry for deletion.
 java.lang.Object restore(java.lang.Object parameters)
          Update indicated entry in data storage.
 void retrieve()
          Retrieve record from storage.
 java.lang.Object retrieve(java.lang.Object parameters)
          Retrieve this object from storage.
 void setMarked(java.lang.Short marked)
          Set the marked status.
 void setModified(java.sql.Timestamp modified)
          Set the edited timestamp.
 void setParameters(java.lang.Object[] parameters)
          [:TODO: Javadoc]
 void setParamList(java.util.List paramList)
          [:TODO: Javadoc]
 void setPrefix(java.lang.String prefix)
          [:TODO: Javadoc]
 void setResult(java.lang.Object result)
          Cache the result object of an operation.
 void setResultCode(int resultCode)
          Set a result code for an operation.
 void setStorageKey(java.lang.Object storageKey)
          Set this object's primary key.
 void store()
          Commit record to storage.
 java.lang.Object store(java.lang.Object parameters)
          Commit record to storage.
 java.lang.Object update(java.lang.Object parameters)
           
 int updateResultCode(java.lang.String command)
           
 
Methods inherited from class org.apache.commons.scaffold.util.ProcessBeanBase
execute, execute, getLocale, getParameter, getRemoteAddr, getRemoteNode, getRemoteServer, setLocale, setParameter, setRemoteAddr, setRemoteNode, setRemoteServer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.commons.scaffold.util.ProcessBean
execute, execute, getLocale, getParameter, getRemoteNode, getRemoteServer, setLocale, setParameter, setRemoteNode, setRemoteServer
 

Field Detail

DOT

public static final java.lang.String DOT
[:TODO: Javadoc]

See Also:
Constant Field Values

commands

protected static java.util.Properties commands
Our command string properties. Can be loaded from an external properties file at startup.


INSERT

public static final java.lang.String INSERT
[:TODO: Javadoc]

See Also:
Constant Field Values

UPDATE

public static final java.lang.String UPDATE
[:TODO: Javadoc]

See Also:
Constant Field Values

RETRIEVE

public static final java.lang.String RETRIEVE
[:TODO: Javadoc]

See Also:
Constant Field Values

DELETE

public static final java.lang.String DELETE
[:TODO: Javadoc] purge

See Also:
Constant Field Values

RESTORE

public static final java.lang.String RESTORE
[:TODO: Javadoc]

See Also:
Constant Field Values

RECYCLE

public static final java.lang.String RECYCLE
[:TODO: Javadoc] delete

See Also:
Constant Field Values

TABLE

public static final java.lang.String TABLE
[:TODO: Javadoc]

See Also:
Constant Field Values

PARAMS

public static final java.lang.String PARAMS
[:TODO: Javadoc]

See Also:
Constant Field Values

PARAM_SEP

public static final java.lang.String PARAM_SEP
Default separator character for list of parameters [","] (comma).

See Also:
Constant Field Values

KEYS_NEXT

public static java.lang.String KEYS_NEXT
[:TODO: Javadoc]


KEYS_INC

public static java.lang.String KEYS_INC
[:TODO: Javadoc]

Constructor Detail

StorageBeanBase

public StorageBeanBase()
Method Detail

blank

protected boolean blank(java.lang.String s)
Convenience method to check for null, empty String.


blankValue

protected boolean blankValue(java.lang.String s)
Convenience method to check for null, empty String, or "0" String.


blank

public static boolean blank(java.lang.Number key)
Returns true if null or zero.


like

public static final java.lang.String like(java.lang.String parameter)
[:TODO: Javadoc]


lookup

protected java.lang.String lookup(java.lang.String key)
                           throws PropertiesException
Retrieve command from commands Properties

Throws:
PropertiesException

lookupRoot

protected java.lang.String lookupRoot(java.lang.String key)
                               throws PropertiesException
Retrieve command from commands Properties

Throws:
PropertiesException

init

public static final void init(java.util.Properties _commands)
Set the Properties file to be used for SQL commands. This can be called by main or in a servlet.init method at startup. If called more than once, the new commands are added to the existing store. If keys clash,the last one wins.


getPrefix

public java.lang.String getPrefix()
Description copied from interface: StorageBean
[:TODO: Javadoc]

Specified by:
getPrefix in interface StorageBean

setPrefix

public void setPrefix(java.lang.String prefix)
Description copied from interface: StorageBean
[:TODO: Javadoc]

Specified by:
setPrefix in interface StorageBean

getParameters

public java.lang.Object[] getParameters(java.lang.String command)
                                 throws ResourceException
Return the runtime parameters for the given command. The comma-delimited list of parameter properties can be provided as a ${command}.params element in the command resource. getParameters will automatically retrieve the *.params element and use it to create an array of the runtime values for each parameter. The sequence of runtime values can then be merged with a prepared statement (the command). Since the parameters are inserted into the command by position, the column names and parameter names do not need to match.

Specified by:
getParameters in interface StorageBean
Throws:
ResourceException - if the command resource or command cannot be found.

setParameters

public void setParameters(java.lang.Object[] parameters)
Description copied from interface: StorageBean
[:TODO: Javadoc]

Specified by:
setParameters in interface StorageBean

getParamList

public java.util.List getParamList(java.lang.String command)
                            throws ResourceException
Return the runtime parameters for the given command. The comma-delimited list of parameter properties can be provided as a ${command}.params element in the command resource. getParameters will automatically retrieve the *.params element and use it to create a list of the runtime values for each parameter. The sequence of runtime values can then be merged with a prepared statement (the command). Since the parameters are inserted into the command by position, the column names and parameter names do not need to match.

Specified by:
getParamList in interface StorageBean
Throws:
ResourceException - if the command resource or command cannot be found.

setParamList

public void setParamList(java.util.List paramList)
Description copied from interface: StorageBean
[:TODO: Javadoc]

Specified by:
setParamList in interface StorageBean

getMarked

public java.lang.Short getMarked()
Return the marked status.

Returns:
the marked status

setMarked

public void setMarked(java.lang.Short marked)
Set the marked status.

Parameters:
marked - The new marked status

populate

public void populate(java.util.Map parameters)
              throws java.lang.Exception
Description copied from interface: StorageBean
Populate this bean from the entries on the provided map. The base implementation uses BeanUtils to efficiently populate the bean through reflection.

Specified by:
populate in interface StorageBean
Throws:
java.lang.Exception

executeUpdate

public void executeUpdate(java.lang.String command)
                   throws ResourceException
Description copied from interface: StorageBean
Lookup command and execute "update" query to create a table, seed it with data, et cetera.

Specified by:
executeUpdate in interface StorageBean
Parameters:
command - Name of command to execute
Throws:
ResourceException

executeUpdateRoot

public void executeUpdateRoot(java.lang.String command)
                       throws ResourceException
Description copied from interface: StorageBean
Lookup command (sans prefix) and execute "update" query to create a table, seed it with data, et cetera.

Specified by:
executeUpdateRoot in interface StorageBean
Parameters:
command - Name of command to execute
Throws:
ResourceException

count

public int count(java.lang.String command)
          throws ResourceException
Throws:
ResourceException

count

public int count(java.lang.String command,
                 java.lang.Object parameter)
          throws ResourceException
Description copied from interface: StorageBean
Count of matching entries.

Specified by:
count in interface StorageBean
Parameters:
command - Name of command to execute
parameter - A parameter to be used with command, if any
Returns:
Count of matching entries
Throws:
ResourceException

findElement

public boolean findElement(java.lang.Object target,
                           java.lang.String command,
                           java.lang.Object key)
                    throws ResourceException
Description copied from interface: StorageBean
Retrieve object from data storage.

NOTE that the precursor to this inteface, the AccessBase class reversed the command and key parameters. This was inconsistent with how the parameters were used elsewhere. If you are converting from AccessBase, be sure to submit the parameters in the correct order.

Specified by:
findElement in interface StorageBean
Parameters:
target - Object to use as factory when populating (e.g. this)
key - The primary key of the entry
command - The name of the data access command collection
Returns:
True if object is found
Throws:
ResourceException - if SQL error occurs

findCollection

public java.util.Collection findCollection(java.lang.Object target,
                                           java.lang.String command)
                                    throws ResourceException
Convenience method that calls findCollection(Object,String,Object[]) with appropriate parameters.

Throws:
ResourceException

findCollection

protected final java.util.Collection findCollection(java.lang.Object target,
                                                    java.lang.String command,
                                                    int parameter)
                                             throws ResourceException
Convenience method that calls findCollection(Object,String,Object[]) with appropriate parameters.

Throws:
ResourceException

findCollection

protected final java.util.Collection findCollection(java.lang.Object target,
                                                    java.lang.String command,
                                                    java.lang.Object parameter)
                                             throws ResourceException
Convenience method that calls findCollection(Object,String,Object[]) with appropriate parameters.

Throws:
ResourceException

findCollection

public java.util.Collection findCollection(java.lang.Object target,
                                           java.lang.String command,
                                           java.lang.Object[] parameters)
                                    throws ResourceException
Description copied from interface: StorageBean
Retrieve a collection of beans from data storage.

Specified by:
findCollection in interface StorageBean
Parameters:
target - Object to use as factory when populating (e.g. this)
command - Name of the data access command collection
parameters - An array of parameters to be used with command
Returns:
Collection with entry or empty collection
Throws:
ResourceException

findByProperty

public java.util.Collection findByProperty(java.lang.Object target,
                                           java.lang.String property,
                                           java.lang.String value)
                                    throws ParameterException,
                                           PopulateException,
                                           ResourceException
Description copied from interface: StorageBean
Select entries from data storage by indexed property..

Specified by:
findByProperty in interface StorageBean
Parameters:
target - Object to use as factory when populating collection (e.g. this)
property - Field to search
value - Term to match
Returns:
Collection with record or empty Collection
Throws:
ParameterException
PopulateException
ResourceException

getStorageKey

public java.lang.Object getStorageKey()
Description copied from interface: Storable
Return this object's primary key.

Specified by:
getStorageKey in interface Storable

setStorageKey

public void setStorageKey(java.lang.Object storageKey)
Description copied from interface: Storable
Set this object's primary key.

Specified by:
setStorageKey in interface Storable

getModified

public java.sql.Timestamp getModified()
Return the edited timestamp.

Returns:
the edited timestamp.

setModified

public void setModified(java.sql.Timestamp modified)
Set the edited timestamp.


isCurrent

public boolean isCurrent(java.sql.Timestamp modified)
Check to see if another modified timetamp matches the one for this instance. Returns true if the timestamps match.


getResultCode

public int getResultCode()
Description copied from interface: Storable
Return the result code from an operation.

Specified by:
getResultCode in interface Storable

setResultCode

public void setResultCode(int resultCode)
Description copied from interface: Storable
Set a result code for an operation.

Specified by:
setResultCode in interface Storable

getResult

public java.lang.Object getResult()
Description copied from interface: Storable
Return the result object of an operation.

Specified by:
getResult in interface Storable

setResult

public void setResult(java.lang.Object result)
Description copied from interface: Storable
Cache the result object of an operation.

Specified by:
setResult in interface Storable

fixModified

public void fixModified()
If timestamp is null (or ConvertUtils.NULL_TIMESTAMP), set our modified property to the current time. This emulates how MySQL treats the first null Timestamp in a record.


updateResultCode

public int updateResultCode(java.lang.String command)
                     throws ResourceException
Throws:
ResourceException

update

public java.lang.Object update(java.lang.Object parameters)
                        throws java.lang.Exception
Throws:
java.lang.Exception

elementResult

public ProcessResult elementResult(java.lang.String command)
                            throws java.lang.Exception
Throws:
java.lang.Exception

element

public java.lang.Object element(java.lang.Object command)
                         throws java.lang.Exception
Retrieve an element (record) from storage. The element is retrieved is determined by what command has been set to the parameter property.

This signature is designed for compatibilty with the Executable interface.

Throws:
java.lang.Exception

collectionResult

public java.lang.Object collectionResult(java.lang.String command)
                                  throws java.lang.Exception
Throws:
java.lang.Exception

collection

public java.lang.Object collection(java.lang.Object parameters)
                            throws java.lang.Exception
Retrieve a collection of objects from storage. The objects retrieved is determined by what command has been set to the parameter property.

This signature is designed for compatibilty with the Executable interface.

Throws:
java.lang.Exception

isNew

public boolean isNew()
Description copied from interface: StorageBean
Return whether this is a new record, or one that has already been stored.

Specified by:
isNew in interface StorageBean
Returns:
"new" status for this object.

createKey

public java.lang.Object createKey(java.lang.String keyName)
                           throws ResourceException
Description copied from interface: StorageBean
Returns next sequential key for given set of keys.

Specified by:
createKey in interface StorageBean
Parameters:
keyName - The name of the key set to use to generate the key
Returns:
An object representing the allocated key
Throws:
ResourceException - if data access error occurs

allocateKey

public void allocateKey()
                 throws java.lang.Exception
Description copied from interface: Storable
Obtain a new primary key for this object.

Specified by:
allocateKey in interface Storable
Throws:
java.lang.Exception

store

public void store()
           throws java.lang.Exception
Description copied from interface: Storable
Commit record to storage.

Specified by:
store in interface Storable
Throws:
java.lang.Exception

store

public java.lang.Object store(java.lang.Object parameters)
                       throws java.lang.Exception
Commit record to storage. If storageKey is null, new storage for this object is created. Otherwise, an existing object is updated.

Specified by:
store in interface StorageBean
Returns:
ProcessResult with messages and this object as data
Throws:
ResourceException - if data access error occurs
java.lang.Exception

retrieve

public void retrieve()
              throws java.lang.Exception
Description copied from interface: Storable
Retrieve record from storage.

Specified by:
retrieve in interface Storable
Throws:
java.lang.Exception

retrieve

public java.lang.Object retrieve(java.lang.Object parameters)
                          throws java.lang.Exception
Description copied from interface: StorageBean
Retrieve this object from storage.

This signature is designed for compatibilty with the Executable interface.

Specified by:
retrieve in interface StorageBean
Throws:
java.lang.Exception

delete

public void delete()
            throws java.lang.Exception
Description copied from interface: Storable
Permenantly delete record.

Specified by:
delete in interface Storable
Throws:
java.lang.Exception

delete

public java.lang.Object delete(java.lang.Object parameters)
                        throws java.lang.Exception
[:TODO: Javadoc]

Specified by:
delete in interface StorageBean
Throws:
java.lang.Exception

recycle

public void recycle()
             throws java.lang.Exception
Description copied from interface: Storable
Mark entry for deletion.

Specified by:
recycle in interface Storable
Throws:
java.lang.Exception

recycle

public java.lang.Object recycle(java.lang.Object parameters)
                         throws java.lang.Exception
Description copied from interface: StorageBean
Mark this object for deletion.

This signature is designed for compatibilty with the Executable interface.

Specified by:
recycle in interface StorageBean
Throws:
java.lang.Exception

restore

public void restore()
             throws java.lang.Exception
Unmark entry for deletion. Returns copy of restored entry in target parameter.

Specified by:
restore in interface Storable
Returns:
0 if fails
Throws:
ResourceException - if data access error occurs
java.lang.Exception

restore

public java.lang.Object restore(java.lang.Object parameters)
                         throws java.lang.Exception
Update indicated entry in data storage. Return confirmation message in a ProcessResult object.

Specified by:
restore in interface StorageBean
Parameters:
parameters - The map or other object to use with this operation
Throws:
ParameterException - if article not found in parameters
ResourceException - if SQLException or other data exception
java.lang.Exception


Copyright © 2002-2005 The Apache Software Foundation. All Rights Reserved.