/*
 * Created on Jul 10, 2003
 *
 * ConnectionImpl.java is used to test the J2EE Connector
 * as implemented by JOnAS. 
 *  
 */
package ersatz.resourceadapter;

import javax.resource.ResourceException;
import javax.resource.NotSupportedException;
import javax.resource.cci.Connection;
import javax.resource.cci.Interaction;
import javax.resource.cci.ConnectionMetaData;
import javax.resource.cci.LocalTransaction;
import javax.resource.cci.ResultSetInfo;
import javax.resource.spi.ManagedConnection;
import javax.resource.spi.ConnectionEvent;
import javax.resource.spi.ConnectionRequestInfo;

/**
 * @author Bob Kruse
 *
 * Ersatz Resource Adapter
 *
 * used to test the J2EE Connector as implemented by JOnAS.
 * 
 */
public class ConnectionImpl 
        implements Connection
{
    public String product="Ersatz EIS"; // see J2eeConnectorTestBeanClient
    String version="1.1";
    String UserName="Ersatz";  //
    private ManagedConnectionImpl mc;
    public ConnectionRequestInfoImpl crii = null;
    public LocalTransactionImpl lt;
    boolean closed = false;  // connection is closed
    String cName = "ConnectionImpl";
    private boolean autoCommitMode = false;

    public ConnectionImpl(ManagedConnectionImpl mc) { // constructor for Connection
        Utility.log(cName+".constructor");
        this.mc=mc;
    }
    //
    // close connection handle
    //
    public void close() throws ResourceException 
    {
        Utility.log(cName
            +".close with ConnectionEvent.CONNECTION_CLOSED="
            +ConnectionEvent.CONNECTION_CLOSED);
        closed = true;
        
        if (mc != null) {
            try {
                mc.sendEvent(ConnectionEvent.CONNECTION_CLOSED, null, this);
                mc=null;
                Utility.log(cName
                       +".close sendevent 'CONNECTION_CLOSED'");
            } catch (ResourceException e) {
                Utility.log(cName+".close error: unable to close");
                throw e; 
            }
        } else {
            Utility.log(cName+".close error: mc=null");
        }
        return;
    }
    //
    // close physical connection
    //
    // The connectionErrorOccurred method indicates that the associated 
    // ManagedConnection instance is now invalid and unusable.
    // mc.sendEvent method makes the call to connectionErrorOccurred
    //
    public void close(int eType) throws ResourceException 
    {
        Utility.log(cName+".close("+eType+")");
        closed = true;
        
        if (mc != null) {
            try {
                mc.sendEvent(eType, null, this);
                mc=null;
                Utility.log(cName
                       +".close(CONNECTION_ERROR_OCCURRED="
                       +ConnectionEvent.CONNECTION_ERROR_OCCURRED+") to "
                       +"close physical connection");
            } catch (ResourceException e) {
                Utility.log(cName
                       +".close error: unable to close physical connection"
                       +" with 'CONNECTION_ERROR_OCCURRED'");
                throw e; 
            }
        } else {
            Utility.log(cName+".close error: mc=null already");
        }
        return;
    }
    public Interaction createInteraction() throws ResourceException 
    {
      Utility.log(cName+".createInteraction");
      InteractionImpl gi=new InteractionImpl(this, mc);
      // TODO Interaction gi = new Interaction(this, this.mc, lw);
      return gi;
    }
    public ConnectionMetaData getMetaData() throws ResourceException 
    {
        Utility.log(cName+".getMetaData");
        ConnectionMetaDataImpl eisInfo = new ConnectionMetaDataImpl(this, mc);
        return eisInfo;
    }
    public LocalTransaction getLocalTransaction() 
                throws ResourceException
    {
        try {
            lt = (LocalTransactionImpl)mc.getLocalTransaction(true);
            Utility.log(cName+".getLocalTransaction lt="+lt);
        } catch (Exception e) {
            Utility.log(cName+".getLocalTransaction error: "
                +e.getMessage()+" <<<<<<<<<<<<");
        }
        return (lt);
    }
    public ResultSetInfo getResultSetInfo() throws ResourceException
    {
        Utility.log(cName+".getResultSetInfo");
        NotSupportedException nse = new NotSupportedException("getResultSetInfo is not supported");
        throw nse;
    }
    public void associateConnection(ManagedConnectionImpl mc) throws IllegalStateException
    {
        Utility.log(cName+".associateConnection");
        this.mc=mc;
    }
    public ManagedConnectionImpl getMC()  throws ResourceException
    {
        if (mc==null) 
            Utility.log(cName+".getMC mc="+this.mc);
        else
            Utility.log(cName+".getMC mc="+this.mc+" mc.xar="+mc.xar+", mc.xari="+mc.xari);
        return this.mc;
    }
    public void setAutoCommit(boolean a) {
        autoCommitMode=a;
    }
    public boolean getAutoCommit() {
        return autoCommitMode;
    }
}
