## ============================================================================ ## Filename : FWJavaBeanAbstractStoredProcedureTemplate.vm ## Note(s) : This template is used to generate the base class for all ## Java Bean Stored Procedure classes. It is intended to be ## used along with the JavaBeanStoredProcedureTemplate.vm file. ## ## Copyright (c) 2008 ThinkUI Software Inc. All rights reserved. ## ============================================================================ ## ## **************************************************************************** ## The following variables are available in this template. For more ## information on any of the following variable, please refer to the user guide. ## **************************************************************************** ## $projectName ## $authorName ## $headerText ## $className ## $packageName ## $subPackageName ## $prjPkgName ## $prjClassPrefix ## $superClassName ## $date ## $codeGen ## #if ($packageName) package $packageName; #end #parse( "ClassHeaderInclude.vm" ) import java.math.BigDecimal; import java.math.BigInteger; import java.sql.CallableStatement; import java.sql.Connection; import java.sql.SQLException; import java.sql.Timestamp; /** * Abstract class stored procedure. */ public abstract class ${className} { private String prepareCallSQL; /** * ${className} constructor. */ public ${className}(String prepareCallSQL) { this.prepareCallSQL = prepareCallSQL; } public final String getPrepareCallSQL() { return prepareCallSQL; } /** * Prepare and return a CallableStatement to execute the specified SQL query. */ protected final CallableStatement prepareCallable(Connection conn, String sql) throws Exception { //System.out.println(getClass().getName() + ".prepareCallable() - sql=" + sql); return conn.prepareCall(sql); } protected final String getString(CallableStatement cstmt, int parameterIndex) throws SQLException { return cstmt.getString(parameterIndex); } protected final void setString(CallableStatement cstmt, int parameterIndex, String value) throws SQLException { if (value != null) { cstmt.setString(parameterIndex, value); } else { cstmt.setNull(parameterIndex, java.sql.Types.VARCHAR); } } protected final Character getCharacter(CallableStatement cstmt, int parameterIndex) throws SQLException { String value = cstmt.getString(parameterIndex); Character charValue = null; if (value != null) { charValue = new Character(value.charAt(0)); } return charValue; } protected final void setCharacter(CallableStatement cstmt, int parameterIndex, Character value) throws SQLException { if (value != null) { cstmt.setString(parameterIndex, String.valueOf(value.charValue())); } else { cstmt.setNull(parameterIndex, java.sql.Types.VARCHAR); } } protected final Integer getInteger(CallableStatement cstmt, int parameterIndex) throws SQLException { int value = cstmt.getInt(parameterIndex); Integer intValue = null; if (!cstmt.wasNull()) { intValue = new Integer(value); } return intValue; } protected final void setInteger(CallableStatement cstmt, int parameterIndex, Integer value) throws SQLException { if (value != null) { cstmt.setInt(parameterIndex, value.intValue()); } else { cstmt.setNull(parameterIndex, java.sql.Types.NUMERIC); } } protected final Long getLong(CallableStatement cstmt, int parameterIndex) throws SQLException { long value = cstmt.getLong(parameterIndex); Long longValue = null; if (!cstmt.wasNull()) { longValue = new Long(value); } return longValue; } protected final void setLong(CallableStatement cstmt, int parameterIndex, Long value) throws SQLException { if (value != null) { cstmt.setLong(parameterIndex, value.longValue()); } else { cstmt.setNull(parameterIndex, java.sql.Types.NUMERIC); } } protected final Timestamp getTimestamp(CallableStatement cstmt, int parameterIndex) throws SQLException { return cstmt.getTimestamp(parameterIndex); } protected final void setTimestamp(CallableStatement cstmt, int parameterIndex, java.sql.Timestamp value) throws SQLException { if (value != null) { cstmt.setTimestamp(parameterIndex, value); } else { cstmt.setNull(parameterIndex, java.sql.Types.TIMESTAMP); } } protected final java.sql.Date getDate(CallableStatement cstmt, int parameterIndex) throws SQLException { return cstmt.getDate(parameterIndex); } protected final void setDate(CallableStatement cstmt, int parameterIndex, java.sql.Date value) throws SQLException { if (value != null) { cstmt.setDate(parameterIndex, value); } else { cstmt.setNull(parameterIndex, java.sql.Types.DATE); } } protected final BigDecimal getBigDecimal(CallableStatement cstmt, int parameterIndex) throws SQLException { return cstmt.getBigDecimal(parameterIndex); } protected final BigInteger getBigInteger(CallableStatement cstmt, int parameterIndex) throws SQLException { return AbstractDAO.toBigInteger(cstmt.getBigDecimal(parameterIndex)); } /** * Registers the out parameters (if any) for the stored procedure. * * @param stmt * the CallableStatement for the stored procedure. * @throws SQLException * if a database error is encountered. */ protected void registerOutParameters(CallableStatement stmt) throws SQLException { // Empty. } /** * Bind arguments (if any) to the callable statement. * * @param stmt * the CallableStatement for the stored procedure. * @throws SQLException * if a database error is encountered. */ protected void bindArguments(CallableStatement stmt) throws SQLException { // Empty. } /** * Retrieve the out parameters values (if any) from the callable statement after it has been * successfully executed. * * @param stmt * the CallableStatement for the stored procedure. * @throws SQLException * if a database error is encountered. */ protected void getReturnValues(CallableStatement stmt) throws SQLException { // Empty. } /** * Execute the stored procedure. * * @param conn * @throws Exception */ public final void execute(Connection conn) throws Exception { CallableStatement stmt = conn.prepareCall(prepareCallSQL); try { registerOutParameters(stmt); bindArguments(stmt); stmt.execute(); getReturnValues(stmt); } finally { stmt.close(); } } }