org.apache.cocoon.acting.modular
Class DatabaseAction

java.lang.Object
  |
  +--org.apache.avalon.framework.logger.AbstractLoggable
        |
        +--org.apache.cocoon.acting.AbstractAction
              |
              +--org.apache.cocoon.acting.AbstractConfigurableAction
                    |
                    +--org.apache.cocoon.acting.ConfigurableComposerAction
                          |
                          +--org.apache.cocoon.acting.AbstractComplementaryConfigurableAction
                                |
                                +--org.apache.cocoon.acting.modular.DatabaseAction
All Implemented Interfaces:
Action, org.apache.avalon.framework.component.Component, org.apache.avalon.framework.component.Composable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.activity.Disposable, org.apache.avalon.framework.logger.Loggable
Direct Known Subclasses:
DatabaseAddAction, DatabaseDeleteAction, DatabaseQueryAction, DatabaseSelectAction, DatabaseUpdateAction

public abstract class DatabaseAction
extends AbstractComplementaryConfigurableAction
implements org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.activity.Disposable

Abstract action for common function needed by database actions. The difference to the other Database*Actions is, that the actions in this package use additional components ("modules") for reading and writing parameters. In addition the descriptor format has changed to accomodate the new features.

This action is heavily based upon the original DatabaseAddActions.

Modes have to be configured in cocoon.xconf. Mode names from descriptor.xml file are looked up in the component service. Default mode names can only be set during action setup.

The number of affected rows is returned to the sitemap with the "row-count" parameter if at least one row was affected.

All known column types can be found in JDBCTypeConversions.

Configuration options (setup):
input default mode name for reading values
autoincrement default mode name for obtaining values from autoincrement columns
Configuration options (setup and per invocation):
throw-exception throw an exception when an error occurs (default: false)
descriptor file containing database description
table-set table-set name to work with
output mode name for writing values

Version:
CVS $Id: DatabaseAction.java,v 1.8.2.2 2002/11/17 19:10:07 haul Exp $
Author:
Christian Haul
See Also:
org.apache.cocoon.components.modules.input, org.apache.cocoon.components.modules.output, org.apache.cocoon.components.modules.database, JDBCTypeConversions

Inner Class Summary
protected  class DatabaseAction.CacheHelper
          Structure that takes all processed data for a table depending on current default modes
protected  class DatabaseAction.Column
          Structure that takes all processed data for one column.
protected  class DatabaseAction.LookUpKey
          Structure that takes up both current mode types for database operations and table configuration data.
 
Field Summary
protected  HashMap cachedQueryData
           
protected  org.apache.avalon.framework.component.ComponentSelector dbselector
           
protected  java.util.Map defaultModeNames
           
 
Fields inherited from class org.apache.cocoon.acting.ConfigurableComposerAction
manager
 
Fields inherited from class org.apache.cocoon.acting.AbstractConfigurableAction
settings
 
Fields inherited from class org.apache.cocoon.acting.AbstractAction
EMPTY_MAP
 
Fields inherited from interface org.apache.cocoon.acting.Action
ROLE
 
Constructor Summary
DatabaseAction()
           
 
Method Summary
 java.util.Map act(Redirector redirector, SourceResolver resolver, java.util.Map objectModel, java.lang.String source, org.apache.avalon.framework.parameters.Parameters param)
          Add a record to the database.
 void compose(org.apache.avalon.framework.component.ComponentManager manager)
          Compose the Actions so that we can select our databases.
 void configure(org.apache.avalon.framework.configuration.Configuration conf)
          Configures the Action.
 void dispose()
          dispose
protected  void fillModes(org.apache.avalon.framework.configuration.Configuration[] conf, boolean isKey, java.util.Map defaultModeNames, java.util.Map modeTypes, DatabaseAction.CacheHelper set)
          Setup parsed attribute configuration object
protected  java.lang.Object[] getColumnValue(org.apache.avalon.framework.configuration.Configuration tableConf, DatabaseAction.Column column, java.util.Map objectModel)
           
protected  org.apache.avalon.excalibur.datasource.DataSourceComponent getDataSource(org.apache.avalon.framework.configuration.Configuration conf, org.apache.avalon.framework.parameters.Parameters parameters)
          Get the Datasource we need.
protected  org.apache.avalon.framework.configuration.Configuration getMode(org.apache.avalon.framework.configuration.Configuration conf, java.lang.String type)
          Choose a mode configuration based on its name.
protected  java.lang.String getOutputName(org.apache.avalon.framework.configuration.Configuration tableConf, org.apache.avalon.framework.configuration.Configuration columnConf)
          compose name for output a long the lines of "table.column"
protected  java.lang.String getOutputName(org.apache.avalon.framework.configuration.Configuration tableConf, org.apache.avalon.framework.configuration.Configuration columnConf, int rowIndex)
          compose name for output a long the lines of "table.column[row]" or "table.column" if rowIndex is -1.
protected abstract  DatabaseAction.CacheHelper getQuery(org.apache.avalon.framework.configuration.Configuration table, java.util.Map modeTypes, java.util.Map defaultModeNames)
          Get the String representation of the PreparedStatement.
protected abstract  boolean honourAutoIncrement()
          determine whether autoincrement columns should be honoured by this operation.
protected  boolean isLargeObject(java.lang.String type)
          Return whether a type is a Large Object (BLOB/CLOB).
protected abstract  int processRow(java.util.Map objectModel, java.sql.Connection conn, java.sql.PreparedStatement statement, java.lang.String outputMode, org.apache.avalon.framework.configuration.Configuration table, DatabaseAction.CacheHelper queryData, java.lang.Object[][] columnValues, int rowIndex, java.util.Map results)
          set all necessary ?
protected  int processTable(org.apache.avalon.framework.configuration.Configuration table, java.sql.Connection conn, java.util.Map objectModel, java.util.Map results, java.util.Map modeTypes)
          Inserts a row or a set of rows into the given table based on the request parameters
protected abstract  java.lang.String selectMode(boolean isAutoIncrement, java.util.Map modes)
          determine which mode to use as default mode This method is intended to be overridden by classes that implement other operations e.g. delete
protected  void setColumn(java.util.Map objectModel, java.lang.String outputMode, java.util.Map results, org.apache.avalon.framework.configuration.Configuration table, org.apache.avalon.framework.configuration.Configuration column, int rowIndex, java.lang.Object value, java.sql.PreparedStatement statement, int position)
          set a column in a statement using the appropriate JDBC setXXX method and propagate the value to the output module and results map if present.
protected  void setColumn(java.sql.PreparedStatement statement, int position, org.apache.avalon.framework.configuration.Configuration entry, java.lang.Object value)
          set a column in a statement using the appropriate JDBC setXXX method.
protected  void setOutput(java.util.Map objectModel, java.lang.String outputMode, java.util.Map results, org.apache.avalon.framework.configuration.Configuration table, org.apache.avalon.framework.configuration.Configuration column, int rowIndex, java.lang.Object value)
          create a unique name using the getOutputName method and write the value to the output module and the results map if present.
protected  void setOutputAttribute(java.util.Map objectModel, java.lang.String outputMode, java.lang.String key, java.lang.Object value)
          Store a key/value pair in the output attributes.
 
Methods inherited from class org.apache.cocoon.acting.AbstractComplementaryConfigurableAction
getConfiguration, getConfiguration, getConfiguration
 
Methods inherited from class org.apache.avalon.framework.logger.AbstractLoggable
getLogger, setLogger, setupLogger, setupLogger, setupLogger
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dbselector

protected org.apache.avalon.framework.component.ComponentSelector dbselector

defaultModeNames

protected java.util.Map defaultModeNames

cachedQueryData

protected final HashMap cachedQueryData
Constructor Detail

DatabaseAction

public DatabaseAction()
Method Detail

configure

public void configure(org.apache.avalon.framework.configuration.Configuration conf)
               throws org.apache.avalon.framework.configuration.ConfigurationException
Description copied from class: AbstractConfigurableAction
Configures the Action. Takes the children from the Configuration and stores them them as key (configuration name) and value (configuration value) in settings.
This automates parsing of flat string-only configurations. For nested configurations, override this function in your action.
Specified by:
configure in interface org.apache.avalon.framework.configuration.Configurable
Overrides:
configure in class AbstractConfigurableAction

compose

public void compose(org.apache.avalon.framework.component.ComponentManager manager)
             throws org.apache.avalon.framework.component.ComponentException
Compose the Actions so that we can select our databases.
Overrides:
compose in class ConfigurableComposerAction

dispose

public void dispose()
dispose
Specified by:
dispose in interface org.apache.avalon.framework.activity.Disposable

getDataSource

protected org.apache.avalon.excalibur.datasource.DataSourceComponent getDataSource(org.apache.avalon.framework.configuration.Configuration conf,
                                                                                   org.apache.avalon.framework.parameters.Parameters parameters)
                                                                            throws org.apache.avalon.framework.component.ComponentException
Get the Datasource we need.

isLargeObject

protected final boolean isLargeObject(java.lang.String type)
Return whether a type is a Large Object (BLOB/CLOB).

setOutputAttribute

protected void setOutputAttribute(java.util.Map objectModel,
                                  java.lang.String outputMode,
                                  java.lang.String key,
                                  java.lang.Object value)
Store a key/value pair in the output attributes. We prefix the key with the name of this class to prevent potential name collisions.

processTable

protected int processTable(org.apache.avalon.framework.configuration.Configuration table,
                           java.sql.Connection conn,
                           java.util.Map objectModel,
                           java.util.Map results,
                           java.util.Map modeTypes)
                    throws java.sql.SQLException,
                           org.apache.avalon.framework.configuration.ConfigurationException,
                           java.lang.Exception
Inserts a row or a set of rows into the given table based on the request parameters
Parameters:
table - the table's configuration
conn - the database connection
objectModel - the objectModel

getMode

protected org.apache.avalon.framework.configuration.Configuration getMode(org.apache.avalon.framework.configuration.Configuration conf,
                                                                          java.lang.String type)
                                                                   throws org.apache.avalon.framework.configuration.ConfigurationException
Choose a mode configuration based on its name.
Parameters:
conf - Configuration (i.e. a column's configuration) that might have several children configurations named "mode".
type - desired type (i.e. every mode has a type attribute), find the first mode that has a compatible type. Special mode "all" matches all queried types.
Returns:
configuration that has desired type or type "all" or null.

getOutputName

protected java.lang.String getOutputName(org.apache.avalon.framework.configuration.Configuration tableConf,
                                         org.apache.avalon.framework.configuration.Configuration columnConf)
compose name for output a long the lines of "table.column"

getOutputName

protected java.lang.String getOutputName(org.apache.avalon.framework.configuration.Configuration tableConf,
                                         org.apache.avalon.framework.configuration.Configuration columnConf,
                                         int rowIndex)
compose name for output a long the lines of "table.column[row]" or "table.column" if rowIndex is -1. If the section of the sitemap corresponding to the action contains false the name for output is "column[row]" If the section of the sitemap corresponding to the action contains false the name for output is "column"

getColumnValue

protected java.lang.Object[] getColumnValue(org.apache.avalon.framework.configuration.Configuration tableConf,
                                            DatabaseAction.Column column,
                                            java.util.Map objectModel)
                                     throws org.apache.avalon.framework.configuration.ConfigurationException,
                                            org.apache.avalon.framework.component.ComponentException

fillModes

protected void fillModes(org.apache.avalon.framework.configuration.Configuration[] conf,
                         boolean isKey,
                         java.util.Map defaultModeNames,
                         java.util.Map modeTypes,
                         DatabaseAction.CacheHelper set)
                  throws org.apache.avalon.framework.configuration.ConfigurationException
Setup parsed attribute configuration object

setOutput

protected void setOutput(java.util.Map objectModel,
                         java.lang.String outputMode,
                         java.util.Map results,
                         org.apache.avalon.framework.configuration.Configuration table,
                         org.apache.avalon.framework.configuration.Configuration column,
                         int rowIndex,
                         java.lang.Object value)
create a unique name using the getOutputName method and write the value to the output module and the results map if present.

setColumn

protected void setColumn(java.sql.PreparedStatement statement,
                         int position,
                         org.apache.avalon.framework.configuration.Configuration entry,
                         java.lang.Object value)
                  throws java.lang.Exception
set a column in a statement using the appropriate JDBC setXXX method.

setColumn

protected void setColumn(java.util.Map objectModel,
                         java.lang.String outputMode,
                         java.util.Map results,
                         org.apache.avalon.framework.configuration.Configuration table,
                         org.apache.avalon.framework.configuration.Configuration column,
                         int rowIndex,
                         java.lang.Object value,
                         java.sql.PreparedStatement statement,
                         int position)
                  throws java.lang.Exception
set a column in a statement using the appropriate JDBC setXXX method and propagate the value to the output module and results map if present. Effectively combines calls to setColumn and setOutput.

act

public java.util.Map act(Redirector redirector,
                         SourceResolver resolver,
                         java.util.Map objectModel,
                         java.lang.String source,
                         org.apache.avalon.framework.parameters.Parameters param)
                  throws java.lang.Exception
Add a record to the database. This action assumes that the file referenced by the "descriptor" parameter conforms to the AbstractDatabaseAction specifications.
Following copied from interface: org.apache.cocoon.acting.Action
Parameters:
resolver - The SourceResolver in charge
objectModel - The Map with object of the calling environment which can be used to select values this controller may need (ie Request, Response).
source - A source String to the Action
parameters - The Parameters for this invocation
Returns:
Map The returned Map object with sitemap substitution values which can be used in subsequent elements attributes like src= using a xpath like expression: src="mydir/{myval}/foo" If the return value is null the processing inside the element of the sitemap will be skipped.
Throws:
java.lang.Exception - Indicates something is totally wrong

processRow

protected abstract int processRow(java.util.Map objectModel,
                                  java.sql.Connection conn,
                                  java.sql.PreparedStatement statement,
                                  java.lang.String outputMode,
                                  org.apache.avalon.framework.configuration.Configuration table,
                                  DatabaseAction.CacheHelper queryData,
                                  java.lang.Object[][] columnValues,
                                  int rowIndex,
                                  java.util.Map results)
                           throws java.sql.SQLException,
                                  org.apache.avalon.framework.configuration.ConfigurationException,
                                  java.lang.Exception
set all necessary ?s and execute the query return number of rows processed This method is intended to be overridden by classes that implement other operations e.g. delete

selectMode

protected abstract java.lang.String selectMode(boolean isAutoIncrement,
                                               java.util.Map modes)
determine which mode to use as default mode This method is intended to be overridden by classes that implement other operations e.g. delete

honourAutoIncrement

protected abstract boolean honourAutoIncrement()
determine whether autoincrement columns should be honoured by this operation. This is usually snsible only for INSERTs. This method is intended to be overridden by classes that implement other operations e.g. delete

getQuery

protected abstract DatabaseAction.CacheHelper getQuery(org.apache.avalon.framework.configuration.Configuration table,
                                                       java.util.Map modeTypes,
                                                       java.util.Map defaultModeNames)
                                                throws org.apache.avalon.framework.configuration.ConfigurationException,
                                                       org.apache.avalon.framework.component.ComponentException
Get the String representation of the PreparedStatement. This is mapped to the Configuration object itself, so if it doesn't exist, it will be created. This method is intended to be overridden by classes that implement other operations e.g. delete
Parameters:
table - the table's configuration object
Returns:
the insert query as a string


Copyright � 1999-2002 Apache Software Foundation. All Rights Reserved.