com.jfw.database
Class StoredProcedureGateway

java.lang.Object
  extended by com.jfw.database.StoredProcedureGateway
Direct Known Subclasses:
MySP

public abstract class StoredProcedureGateway
extends java.lang.Object

Used for execute stored procedures.

Example of subclass

 public class MySP extends StoredProcedureGateway
 {
   //implement one of the constructors present in StoredProcedureGateway 
   public MySP(HashMap inputParams)
   {
     super(inputParams);
   }
 
   //set stored procedure to use
   protected void setStoredProcedure()
   {
     setStoredProcedure("{call package.test(?)}");
   }
   
   //add the input parameters here
   protected void setInput() throws DatabaseException
   {
     try
     {
       CallableStatement callable = getCallableStatement();
       callable.setInt(1, Integer.valueOf((String)inputParams.get("id")).intValue());
     }
     catch (Throwable t)
     {
       JFWLogger.error(className, "setInput", "", "JFW", t.toString(), t);
       throw new DatabaseException(t.getMessage());
     }
   }
 
   //add the output parameters here
   protected void setOutput() throws DatabaseException
   {
     try
     {
       CallableStatement callable = getCallableStatement();
       callable.registerOutParameter(2, java.sql.Types.INTEGER);
     }
     catch (Throwable t)
     {
       JFWLogger.error(className, "setOutput", "", "JFW", t.toString(), t);
       throw new DatabaseException(t.getMessage());
     }
   }
 
   //execute stored procedure
   public void myExecute(HashMap outputData) throws DatabaseException
   {
     boolean commit = false;
 
     try
     {
       execute(outputData, false);
       commit = true;
       return outputData;
     }
     catch(DatabaseException dbe)
     {
       throw dbe;
     }
     catch(Throwable t)
     {
       JFWLogger.error(className, "myExecute", "", "JFW", t.toString(), t);
       throw new DatabaseException(t.getMessage());
     }
     finally
     {
       close(commit);
     }
   }
 }
 

An exampe of the caller class
 HashMap outputData = new HashMap();
 HashMap inputParams = new HashMap();
 //... add input params if needed
 MySP  mySP = new MySP(inputParams);
 mySP.myExecute(outputData);
 


Field Summary
protected  java.lang.String className
          Used for logging.
protected  java.util.HashMap inputParams
          Contiene i parametri di input.
protected  Profiler profiler
          Utilizzato per il profiling.
static java.lang.String RESULT_KEY
          The result of stored procedure execution.
 
Constructor Summary
StoredProcedureGateway(java.util.HashMap inputParams)
          Use the default db resource from the jfw.properties.
StoredProcedureGateway(java.util.HashMap inputParams, java.sql.Connection connection)
          Use the connection object gived.
StoredProcedureGateway(java.util.HashMap inputParams, java.lang.String resourceName)
          Use the parameter resourceName as db resource.
StoredProcedureGateway(java.util.HashMap inputParams, Transaction transaction)
          Use the Transaction for get the Connection to use.
 
Method Summary
protected  void close()
          Close the result set, the callable statement.
protected  void close(boolean doCommit)
          Call method commit() or rollback() and method close().
protected  boolean commit()
          Commit the stored procedure execution.
protected  java.lang.Object exec(boolean isQuery)
          Execute the stored procedure.
protected  void execute(java.util.HashMap outputdata)
          Call method execute(java.util.HashMap, boolean).
protected  void execute(java.util.HashMap outputdata, boolean isQuery)
          Call method exec(boolean).
 void finalize()
          Call super.finalize() and method close().
protected  void getAllData(java.sql.ResultSet resultSet, java.util.HashMap outputData)
          Call the other method getAllData(java.sql.ResultSet, java.util.HashMap, boolean) with default value false for parameter escape
protected  void getAllData(java.sql.ResultSet resultSet, java.util.HashMap outputData, boolean escape)
          Read the result set returned by the stpred procedure and save the data in a HashMap.
protected  java.sql.CallableStatement getCallableStatement()
          Return attribute callableStatement.
protected  java.sql.Connection getConnection()
          Return attribute connection.
protected  java.lang.String getInputParam(java.lang.String inputParam)
          Call other method getInputParam(java.lang.String, java.lang.Object) with default value null for parameter defaultValue
protected  java.lang.String getInputParam(java.lang.String inputParam, java.lang.Object defaultValue)
          Return the toString() of the object associate with key inputParam from inputParams.
protected  java.lang.String getStoredProcedure()
          Return attribute storedProcedure.
protected  void initialize()
          Call methods to initialize resources if init is false.
protected  boolean isClosed()
          Return attribute closed.
abstract  void myExecute(java.util.HashMap outputdata)
          The method called by the other classes for execute the stored procedure.
protected  boolean rollback()
          Rollback the stored procedure execution.
protected  void setInput()
          Default implementation if no input parameters must be set.
protected  void setOutput()
          Default implementation if no output parameters must be set.
protected abstract  void setStoredProcedure()
          Set attribute storedProcedure.
protected  void setStoredProcedure(java.lang.String storedProcedure)
          Set attribute storedProcedure.
 java.lang.String toString()
          Return data relative of stored procedure, input parameters and database resource.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

className

protected final java.lang.String className
Used for logging.


RESULT_KEY

public static final java.lang.String RESULT_KEY
The result of stored procedure execution. Used as key in the hash returned by execute(java.util.HashMap, boolean). The associated value is a Boolean object.

See Also:
Constant Field Values

profiler

protected Profiler profiler
Utilizzato per il profiling.


inputParams

protected java.util.HashMap inputParams
Contiene i parametri di input.

Constructor Detail

StoredProcedureGateway

public StoredProcedureGateway(java.util.HashMap inputParams)
Use the default db resource from the jfw.properties.

Parameters:
inputParams - the input parameters used by the stored procedure.

StoredProcedureGateway

public StoredProcedureGateway(java.util.HashMap inputParams,
                              java.lang.String resourceName)
Use the parameter resourceName as db resource.

Parameters:
inputParams - the input parameters used by the stored procedure.
resourceName - the name of a resource as specified in the jfw.properties.

StoredProcedureGateway

public StoredProcedureGateway(java.util.HashMap inputParams,
                              Transaction transaction)
Use the Transaction for get the Connection to use.

Parameters:
inputParams - the input parameters used by the stored procedure.
transaction - a Transaction object.

StoredProcedureGateway

public StoredProcedureGateway(java.util.HashMap inputParams,
                              java.sql.Connection connection)
Use the connection object gived.

Parameters:
inputParams - the input parameters used by the stored procedure.
connection - the Connection object to use.
Method Detail

initialize

protected void initialize()
                   throws DatabaseException
Call methods to initialize resources if init is false.
  1. setStoredProcedure(java.lang.String)
  2. setConnection(), if connection is null
  3. instantiate callableStatement
  4. setInput()
  5. setOutput()
  6. set init value at true

Throws:
DatabaseException

getCallableStatement

protected final java.sql.CallableStatement getCallableStatement()
Return attribute callableStatement.


getConnection

protected java.sql.Connection getConnection()
                                     throws DatabaseException
Return attribute connection.

Returns:
a Connection object.
Throws:
DatabaseException

commit

protected boolean commit()
Commit the stored procedure execution.

Returns:
true if commit is ok, false in other case.

rollback

protected boolean rollback()
Rollback the stored procedure execution.

Returns:
true if rollback is ok, false in other case.

close

protected void close()
Close the result set, the callable statement.
If attribute isTransaction is false then rollback and release the connection, if not nothing is done on the connection. Set value of attribute isClosed() at true.


close

protected void close(boolean doCommit)
Call method commit() or rollback() and method close().

Parameters:
doCommit - if true call method commit(), else call method rollback().

isClosed

protected final boolean isClosed()
Return attribute closed.

Returns:
the value of closed.

exec

protected java.lang.Object exec(boolean isQuery)
                         throws DatabaseException
Execute the stored procedure.

Parameters:
isQuery - true if the stored procedure return a result set, false in other cases.
Returns:
a ResultSet object (if paramater isQuery is true) or a Boolean object with the result of the execution.
Throws:
DatabaseException

execute

protected void execute(java.util.HashMap outputdata,
                       boolean isQuery)
                throws DatabaseException
Call method exec(boolean). If a ResultSet object is returned by exec(boolean) then the method getAllData(java.sql.ResultSet, java.util.HashMap, boolean) is called and is put in outputdata, at key RESULT_KEY, a Boolean object with value true. If a Boolean object is returned then the object is putted in the outputdata with key RESULT_KEY.

Parameters:
outputdata - contains the data returned by the stored procedure and other informations relative the database interrogation.
isQuery - true if the stored procedure return a result set, false in other cases.
Throws:
DatabaseException

execute

protected void execute(java.util.HashMap outputdata)
                throws DatabaseException
Call method execute(java.util.HashMap, boolean). The default value for parameter isQuery is true.

Throws:
DatabaseException

getAllData

protected void getAllData(java.sql.ResultSet resultSet,
                          java.util.HashMap outputData,
                          boolean escape)
                   throws DatabaseException
Read the result set returned by the stpred procedure and save the data in a HashMap.
Call the method ReadDbData.result2Hash(java.sql.ResultSet, java.util.HashMap, java.lang.String, boolean, java.lang.String, int, int) for read and save data.

Parameters:
resultSet - the result set to read.
escape - the escape param for method ReadDbData.result2Hash(java.sql.ResultSet, java.util.HashMap, java.lang.String, boolean, java.lang.String, int, int).
Throws:
DatabaseException

getAllData

protected void getAllData(java.sql.ResultSet resultSet,
                          java.util.HashMap outputData)
                   throws DatabaseException
Call the other method getAllData(java.sql.ResultSet, java.util.HashMap, boolean) with default value false for parameter escape

Throws:
DatabaseException

getInputParam

protected java.lang.String getInputParam(java.lang.String inputParam,
                                         java.lang.Object defaultValue)
Return the toString() of the object associate with key inputParam from inputParams.

Parameters:
inputParam - the key to read.
Returns:
the value of key inputParam or toString() of parameter defaultValue if the key not exist. If defaultValue is null return null.

getInputParam

protected java.lang.String getInputParam(java.lang.String inputParam)
Call other method getInputParam(java.lang.String, java.lang.Object) with default value null for parameter defaultValue


setInput

protected void setInput()
                 throws DatabaseException
Default implementation if no input parameters must be set.

Throws:
DatabaseException

setOutput

protected void setOutput()
                  throws DatabaseException
Default implementation if no output parameters must be set.

Throws:
DatabaseException

getStoredProcedure

protected java.lang.String getStoredProcedure()
Return attribute storedProcedure.

Returns:
the attribute storedProcedure.

setStoredProcedure

protected void setStoredProcedure(java.lang.String storedProcedure)
Set attribute storedProcedure.

Parameters:
storedProcedure - the stored procedure, example: package.test(?).

toString

public java.lang.String toString()
Return data relative of stored procedure, input parameters and database resource.

Overrides:
toString in class java.lang.Object

finalize

public void finalize()
              throws java.lang.Throwable
Call super.finalize() and method close().

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable

setStoredProcedure

protected abstract void setStoredProcedure()
Set attribute storedProcedure. Must be implement by subclass.


myExecute

public abstract void myExecute(java.util.HashMap outputdata)
                        throws java.lang.Exception
The method called by the other classes for execute the stored procedure. One of the methods execute(java.util.HashMap, boolean) or exec(boolean) must be called for execute the stored procedure and get the data.

Parameters:
outputdata - contains the data returned by the stored procedure and other informations relative the database interrogation.
Throws:
java.lang.Exception