/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: UnivUpdateTag.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.2 2006/03/08 14:46:42 drmlipp
* Synchronized with 1.3.3p5.
*
* Revision 1.1.4.1 2005/12/17 12:49:11 drmlipp
* Added sql type support for parameters.
*
* Revision 1.1 2004/12/31 13:08:49 mlipp
* Added possibility to use UniversalPrepStmt in Jelly scripts.
*
*/
package de.danet.an.util.jellytags;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.jstl.sql.Result;
import org.apache.commons.jelly.JellyException;
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.jelly.tags.Resources;
import org.apache.commons.jelly.tags.sql.UpdateTag;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.danet.an.util.UniversalPrepStmt;
import de.danet.an.util.jellytags.ExtSqlParamTag.ParamAttributes;
/**
* This class provides an update tag that uses the {@link
* UniversalPrepStmt <code>universalPrepStmt} instead of the standard
* prepared statement.
*
* @author <a href="mailto:mnl@mnl.de">Michael N. Lipp</a>
* @version $Revision: 1607 $
*/
public class UnivUpdateTag extends UpdateTag {
/** The Log to which logging calls will be made. */
private static final Log logger = LogFactory.getLog(UnivUpdateTag.class);
/*
* Instance variables that are not for attributes
*/
private Connection conn;
private List parameters = null;
/**
* Creates an instance of <code>UnivUpdateTag</code>
* with all attributes initialized to default values.
*/
public UnivUpdateTag () {
}
/**
* Execute the SQL statement, set either through the <code>sql</code>
* attribute or as the body, and save the result as a variable
* named by the <code>var</code> attribute in the scope specified
* by the <code>scope</code> attribute, as an object that implements
* the Result interface.<p>
*
* The connection used to execute the statement comes either
* from the <code>DataSource</code> specified by the
* <code>dataSource</code> attribute, provided by a parent action
* element, or is retrieved from a JSP scope attribute
* named <code>javax.servlet.jsp.jstl.sql.dataSource</code>.<p>
*
* @param output the destination for the output
* @throws JellyTagException if an error occurs
*/
public void doTag(XMLOutput output) throws JellyTagException {
try {
conn = getConnection();
} catch (SQLException e) {
throw new JellyTagException(sql + ": " + e.getMessage(), e);
}
/*
* Use the SQL statement specified by the sql attribute, if any,
* otherwise use the body as the statement.
*/
String sqlStatement = null;
if (sql != null) {
sqlStatement = sql;
} else {
sqlStatement = getBodyText();
}
if (sqlStatement == null || sqlStatement.trim().length() == 0) {
throw new JellyTagException
(Resources.getMessage("SQL_NO_STATEMENT"));
}
Statement statement = null;
int result = 0;
try {
if (hasParameters()) {
PreparedStatement ps
= new UniversalPrepStmt (conn, sqlStatement);
statement = ps;
setParameters(ps);
result = ps.executeUpdate();
} else {
statement = conn.createStatement();
result = statement.executeUpdate(sqlStatement);
}
if (var != null) {
context.setVariable(var, new Integer(result));
}
// lets nullify before we close in case we get exceptions
// while closing, we don't want to try to close again
Statement tempStatement = statement;
statement = null;
tempStatement.close();
} catch (SQLException e) {
throw new JellyTagException
(sqlStatement + ": " + e.getMessage(), e);
} finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
logger.error
("Caught exception while closing statement: " + e, e);
}
}
if (conn != null && !isPartOfTransaction) {
try {
conn.close();
} catch (SQLException e) {
logger.error
("Caught exception while closing connection: " + e, e);
}
}
clearParameters();
}
}
/**
* Called by nested parameter elements to add PreparedStatement
* parameter values.
*/
public void addSQLParameter(Object o) {
if (parameters == null) {
parameters = new ArrayList();
}
parameters.add(o);
}
/**
* @return true if there are SQL parameters
*/
protected boolean hasParameters() {
return parameters != null && parameters.size() > 0;
}
protected void clearParameters() {
parameters = null;
}
protected void setParameters(PreparedStatement ps) throws SQLException {
for (int i = 0; i < parameters.size(); i++) {
Object param = parameters.get(i);
// The first parameter has index 1
if (!(param instanceof ParamAttributes)) {
if (param == null) {
ps.setNull(i + 1, Types.NULL);
} else {
ps.setObject(i + 1, param);
}
} else {
ps.setObject(i + 1, ((ParamAttributes)param).getValue(),
((ParamAttributes)param).getType());
}
}
}
}