Package de.danet.an.util.jellytags

Source Code of de.danet.an.util.jellytags.UnivQueryTag

/*
* 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: UnivQueryTag.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* 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.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.jsp.jstl.sql.Result;

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.QueryTag;
import org.apache.commons.jelly.tags.sql.ResultImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import de.danet.an.util.UniversalPrepStmt;

/**
* This class provides a query 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 UnivQueryTag extends QueryTag {

    /** The Log to which logging calls will be made. */
    private static final Log logger = LogFactory.getLog(UnivQueryTag.class);
   
    /*
     * Instance variables that are not for attributes
     */
    private Connection conn;

    /**
     * Creates an instance of <code>UnivQueryTag</code>
     * with all attributes initialized to default values.
     */
    public UnivQueryTag () { 
    }
   
    /**
     * 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.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 {
        if (!maxRowsSpecified) {
            Object obj = context.getVariable
    ("org.apache.commons.jelly.sql.maxRows");
            if (obj != null) {
                if (obj instanceof Integer) {
                    maxRows = ((Integer) obj).intValue();
                } else if (obj instanceof String) {
                    try {
                        maxRows = Integer.parseInt((String) obj);
                    } catch (NumberFormatException nfe) {
                        throw new JellyTagException
          (Resources.getMessage("SQL_MAXROWS_PARSE_ERROR",
              (String) obj), nfe);
                    }
                } else {
                    throw new JellyTagException
      (Resources.getMessage("SQL_MAXROWS_INVALID"));
                }
            }
        }
 
        Result result = null;
        String sqlStatement = null;

        logger.debug( "About to lookup connection" );

        ResultSet rs = null;
        Statement statement = null;
        try {
            conn = getConnection();

            /*
             * Use the SQL statement specified by the sql attribute, if any,
             * otherwise use the body as the statement.
             */
            if (sql != null) {
                sqlStatement = sql;
            } else {
                sqlStatement = getBodyText();
            }
            if (sqlStatement == null || sqlStatement.trim().length() == 0) {
                throw new JellyTagException
        (Resources.getMessage("SQL_NO_STATEMENT"));
            }
            // We shouldn't have a negative startRow or illegal maxrows
            if ((startRow < 0) || (maxRows < -1)) {
                throw new JellyTagException
        (Resources.getMessage("PARAM_BAD_VALUE"));
            }

            /*
             * Note! We must not use the setMaxRows() method on the
             * the statement to limit the number of rows, since the
             * Result factory must be able to figure out the correct
             * value for isLimitedByMaxRows(); there's no way to check
             * if it was from the ResultSet.
             */
            if (logger.isDebugEnabled()) {
                logger.debug( "About to execute query: " + sqlStatement );
            }

            if (hasParameters()) {
                PreparedStatement ps
        = new UniversalPrepStmt (conn, sqlStatement);
                statement = ps;
                setParameters(ps);
                rs = ps.executeQuery();
            } else {
                statement = conn.createStatement();
                rs = statement.executeQuery(sqlStatement);
            }

            result = new ResultImpl(rs, startRow, maxRows);
            context.setVariable(var, result);

            // always close the result set first since it may be closed by
            // JDBC 3 when closing statements

            // lets nullify before we close in case we get exceptions
            // while closing, we don't want to try to close again
            ResultSet tempRs = rs;
            rs = null;
            tempRs.close();
            Statement tempStatement = statement;
            statement = null;
            tempStatement.close();
        } catch (SQLException e) {
            throw new JellyTagException
    (sqlStatement + ": " + e.getMessage(), e);
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    logger.error
      ("Caught exception while closing result set: " + e, e);
                }
            }
            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);
                }
                conn = null;
            }
            clearParameters();
        }
    }
}
TOP

Related Classes of de.danet.an.util.jellytags.UnivQueryTag

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.