/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library 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 any later version.
 * 
 * This library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

package org.objectweb.jonas.dbm;

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

/**
 * To compile this class, you must have the classes12.zip file
 * from Oracle (driver JDBC 2.0 with Oracle 8.1.6 version), 
 * and add the current file to the build mechanism (Makefile)
 */
public class JonasOracleXAStatement extends CoreStatement {
    
    private JonasOracleXAConnection	con;

    /**
     * Constructor.
     */
    JonasOracleXAStatement (JonasOracleXAConnection con) {
	this.con = con;
    } // constructor

    /** 
     * Pre-invokation of the delegation, in case of the Statement is
     * closed, we throw an exception
     */
    public synchronized void preInvoke() throws SQLException {

	Transaction ntx = null;
	if (con.tx == null) {
	    try {
		try {
		    ntx = con.transactionManager.getTransaction();
		    if (ntx != null) {
			con.tx = ntx;
			con.setAutoCommit(false);
			try {
			    con.tx.enlistResource(con.getXAResource());// enlist the xaResource in the transaction
			} catch (RollbackException n) {
			    throw new SQLException("JonasOracleXAStatement:preInvoke enlistResource exception: "+n.toString());
			}
		    }
		} catch (SystemException n) {
		    throw new SQLException("JonasOracleXAStatement:preInvoke getTransaction exception: "+n.toString());
		}
	    } catch (NullPointerException n) {
		// current is null: we are not in EJBServer.
		throw new SQLException("JonasOracleXAStatement:preInvoke should not be used outside an EJBServer: "+n.toString());
	    }
	}
	if (statement == null) {
	    statement = con.createStatement();
	}

    } // method preInvoke

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

} // class JonasOracleXAStatement
