/*-----------------------------------------------------------------------------
 *
 * Enhydra JdbcS
 * The Initial Developer of the Original Code is Lutris Technologies Inc.
 * Portions created by Lutris are Copyright 1997-2000 Lutris Technologies Inc.
 * All Rights Reserved.
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 * www.enhydra.org/license/epl.html
 *
 * Software distributed under the License is distributed on an "ASIS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * -----------------------------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------------------------
 */
package org.enhydra.jdbc.standard;

import java.sql.SQLException;
import java.sql.CallableStatement;
import javax.sql.ConnectionEvent;
import org.enhydra.jdbc.core.CoreCallableStatement;
import javax.transaction.Transaction;
import javax.transaction.SystemException;
import javax.transaction.RollbackException;

public class StandardXACallableStatement extends CoreCallableStatement {
    
    private StandardXAConnectionHandle	con;	// the StandardConnectionHandle that created this object
    private boolean                     closed; // true when the Statement has been closed
    private String sql;
    private int resultSetType;
    private int resultSetConcurrency;

    /**
     * Constructor.
     */
    StandardXACallableStatement (StandardXAConnectionHandle con, String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
	this.con = con;
	this.sql = sql;
	this.resultSetType = resultSetType;
	this.resultSetConcurrency = resultSetConcurrency;
	//cs = newStatement();
    } // constructor

    private CallableStatement newStatement() throws SQLException {
	if (resultSetType == 0 && resultSetConcurrency == 0) {
	    return con.con.prepareCall(sql);
	} else {
	    return con.con.prepareCall(sql, resultSetType, resultSetConcurrency);
	}
    }

    /**
     * Close this statement.
     */
    public synchronized void close() throws SQLException {
	super.close();	// we do not reuse the Statement, we have to close it
	closed = true;
    } // method close
    
    /** 
     * Pre-invokation of the delegation, in case of the Statement is
     * closed, we throw an exception
     */
    public synchronized void preInvoke() throws SQLException {
	if (closed)
	    throw new SQLException ("Prepare Statement is closed");

	Transaction ntx = null;
	if (con.tx == null) {
	    try {
		try {
		    ntx = (con.transactionManager!=null)?con.transactionManager.getTransaction():null;
		    if (ntx != null) {
			con.tx = ntx;
			con.setAutoCommit(false);
			try {
			    con.tx.enlistResource(con.xacon.getXAResource());// enlist the xaResource in the transaction
			    if (cs != null) {
				cs.close();
				cs = null;
			    }
			} catch (RollbackException n) {
			    throw new SQLException("StandardXAStatement:preInvoke enlistResource exception: "+n.toString());
			}
		    }
	        //else con.setAutoCommit(true);

		} catch (SystemException n) {
		    throw new SQLException("StandardXAStatement:preInvoke getTransaction exception: "+n.toString());
		}
	    } catch (NullPointerException n) {
		// current is null: we are not in EJBServer.
		throw new SQLException("StandardXAStatement:preInvoke should not be used outside an EJBServer: "+n.toString());
	    }
	}
	if (cs == null) {
	    cs = newStatement();
	}

    } // method preInvoke

    /**
     * Exception management : catch or throw the exception
     */
    public void catchInvoke(SQLException sqlException) throws SQLException {
	//ConnectionEvent event = new ConnectionEvent (con.pooledCon);
	//con.pooledCon.connectionErrorOccurred(event);
	throw(sqlException);
    } // method catchInvoke

} // class StandardXAStatement
