/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ejb.plugins.jaws.jdbc;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.jboss.logging.Logger;

/**
 * Abstract superclass for all JAWS Commands that issue JDBC queries
 * directly.
 * Provides a Template Method implementation for
 * <code>executeStatementAndHandleResult</code>.
 * 
 * @author <a href="mailto:justin@j-m-f.demon.co.uk">Justin Forder</a>
 * @version $Revision: 1.8.4.1 $
 */
public abstract class JDBCQueryCommand
   extends JDBCCommand
{
   // Constructors --------------------------------------------------

   private Logger log = Logger.getLogger(JDBCFindByCommand.class);
   
   /**
    * Pass the arguments on to the superclass constructor.
    */
   protected JDBCQueryCommand(JDBCCommandFactory factory, String name)
   {
      super(factory, name);
   }
   
   // Protected -----------------------------------------------------
   
   /**
    * Template Method that executes the PreparedStatement and calls
    * <code>handleResult</code> on the resulting ResultSet.
    *
    * @param stmt the prepared statement, with its parameters already set.
    * @param argOrArgs argument or array of arguments passed in from
    *  subclass execute method.
    * @return any result produced by the handling of the result of executing
    *  the prepared statement.
    * @throws Exception if execution or result handling fails.
    */
   protected Object executeStatementAndHandleResult(PreparedStatement stmt,
                                                    Object argOrArgs)
      throws Exception
   {
      ResultSet rs = null;
      Object result = null;
      
      try
      {
         rs = stmt.executeQuery();
         result = handleResult(rs, argOrArgs);
      } finally
      {
         if (rs != null)
         {
            try
            {
               rs.close();
            } catch (SQLException e)
            {
               log.debug("failed to close resultset", e);
            }
         }
      }
      
      return result;
   }
   
   /**
    * Handles the result of successful execution of the query.
    *
    * @param rs the result set from the query.
    * @param argOrArgs argument or array of arguments passed in from
    *  subclass execute method.
    * @return any result produced by the handling of the result of executing
    *  the prepared statement.
    * @throws Exception if execution or result handling fails.
    */
   protected abstract Object handleResult(ResultSet rs, Object argOrArgs) 
      throws Exception;
}
