/*
 * 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
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */

// PersonneEC.java

package org.objectweb.jonas.jtests.beans.annuaire;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import javax.ejb.CreateException;
import javax.ejb.DuplicateKeyException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.RemoveException;
import javax.ejb.EJBObject;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import javax.transaction.NotSupportedException;
import javax.transaction.Status;
import javax.transaction.SystemException;
import javax.transaction.UserTransaction;

import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.Logger;
import org.objectweb.util.monolog.api.BasicLevel;

/**
 *
 */
public class PersonneEC implements EntityBean {

    static private Logger logger = null;
    EntityContext ejbContext;
    boolean dirty = false;
    boolean isModifiedCalled = false;
    boolean ejbStoreCalled = false;

    // ------------------------------------------------------------------
    // State of the bean. 
    // They must be public for Container Managed Persistance.
    // ------------------------------------------------------------------
    public String nom;
    public String numero;

    // ------------------------------------------------------------------
    // isModified method
    // ------------------------------------------------------------------
    public boolean isModified() {
	isModifiedCalled = true;
	return dirty;
    }

    // ------------------------------------------------------------------
    // EntityBean implementation
    // ------------------------------------------------------------------

    /**
     * Set the associated entity context. The container invokes this method 
     * on an instance after the instance has been created.
     * This method is called in an unspecified transaction context. 
     * 
     * @param ctx - An EntityContext interface for the instance. The instance
     * should store the reference to the context in an instance variable.
     * @throws EJBException Thrown by the method to indicate a failure caused by a
     * system-level error.
     */
    public void setEntityContext(EntityContext ctx) {
        if (logger == null)
            logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
	logger.log(BasicLevel.DEBUG, "PersonneEC setEntityContext");
	ejbContext = ctx;
    }

    /**
     * Unset the associated entity context. The container calls this method
     * before removing the instance. 
     * This is the last method that the container invokes on the instance.
     * The Java garbage collector will eventually invoke the finalize() method
     * on the instance.
     * This method is called in an unspecified transaction context.
     *
     * @throws EJBException Thrown by the method to indicate a failure caused by a
     * system-level error.
     */
    public void unsetEntityContext() {
	logger.log(BasicLevel.DEBUG, "PersonneEC setEntityContext");
	ejbContext = null;
    }
    
    /**
     * A container invokes this method before it removes the EJB object
     * that is currently associated with the instance. This method is
     * invoked when a client invokes a remove operation on the enterprise Bean's
     * home interface or the EJB object's remote interface. This method
     * transitions the instance from the ready state to the pool of available
     * instances. 
     *
     * This method is called in the transaction context of the remove operation.
     * @throws RemoveException  The enterprise Bean does not allow destruction of the object.
     * @throws EJBException - Thrown by the method to indicate a failure caused by a system-level
     * error.
     */
    public void ejbRemove() throws RemoveException {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbRemove");
    }

    /**
     * A container invokes this method to instruct the instance to synchronize
     * its state by loading it state from the underlying database. 
     * This method always executes in the proper transaction context.
     *
     * @throws EJBException Thrown by the method to indicate a failure caused by
     * a system-level error.
     */
    public void ejbLoad() {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbLoad");
	dirty = false;
    }

    /**
     * A container invokes this method to instruct the instance to synchronize
     * its state by storing it to the underlying database. 
     * This method always executes in the proper transaction context.
     *
     * @throws EJBException Thrown by the method to indicate a failure caused by
     * a system-level error.
     */
    public void ejbStore() {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbStore");
	ejbStoreCalled = true;
	dirty = false;
    }
	
    /**
     * There must be an ejbPostCreate par ejbCreate method
     *
     * @throws CreateException Failure to create an entity EJB object.
     */
    public void ejbPostCreate(String nom, String numero) throws CreateException {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbPostCreate");
    }	
    public void ejbPostCreate(String nom, String numero, boolean t) throws CreateException {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbPostCreate");
    }
	

    public java.lang.String ejbCreate(String nom, String numero) throws CreateException, DuplicateKeyException {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbCreate nom numero");

	// Init here the bean fields
	this.nom = nom;
	this.numero = numero;

	// In CMP, should return null.
	return null;
    }

    public java.lang.String ejbCreate(String nom, String numero, boolean t) throws CreateException, DuplicateKeyException {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbCreate nom numero boolean");

	// Init here the bean fields
	this.nom = nom;
	this.numero = numero;

	// In CMP, should return null.
	return null;
    }

    /**
     * This method is called before the instance enters the "passive" state. 
     * The instance should release any resources that it can re-acquire later in the 
     * ejbActivate() method.
     * After the passivate method completes, the instance must be in a state that 
     * allows the container to use the Java Serialization protocol to externalize 
     * and store away the instance's state. 
     * This method is called with no transaction context. 
     *
     * @exception EJBException - Thrown if the instance could not perform the 
     * function requested by the container 
     */
    public void ejbPassivate() {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbPassivate");
    }

    /**
     * This method is called when the instance is activated from its "passive" state.
     * The instance should acquire any resource that it has released earlier in the 
     * ejbPassivate() method. 
     * This method is called with no transaction context.
     * 
     * @exception EJBException - Thrown if the instance could not perform the 
     * function requested by the container 
     */
    public void ejbActivate() {
	logger.log(BasicLevel.DEBUG, "PersonneEC ejbActivate");
    }
    
    // ------------------------------------------------------------------
    // Personne implementation
    // ------------------------------------------------------------------

    /**
     * getNumero
     */
    public String getNumero() {
	logger.log(BasicLevel.DEBUG, "PersonneEC getNumero");
	return numero;
    }

    /**
     * setNumero
     */
    public void setNumero(String s) {
	logger.log(BasicLevel.DEBUG, "PersonneEC setNumero");
	numero = s;
	dirty = true;
    }

    /**
     * setNom - Attention: on modifie la PK!!!
     */
    public void setNom(String s) {
	logger.log(BasicLevel.DEBUG, "PersonneEC setNom");
	nom = s;
	dirty = true;
    }

    /**
     * getNom
     */
    public String getNom() {
	logger.log(BasicLevel.DEBUG, "PersonneEC getNom");
	return nom;
    }

    public boolean isDirty() {
	logger.log(BasicLevel.DEBUG, "PersonneEC "+nom+" isDirty ="+dirty+" "+numero);
	return dirty;
    }

    public void reset() {
	logger.log(BasicLevel.DEBUG, "PersonneEC "+nom+" reset "+numero);
	isModifiedCalled = false;
	ejbStoreCalled = false;
    }

    public boolean isModifiedCalled() {
	logger.log(BasicLevel.DEBUG, "PersonneEC "+nom+" isModifiedCalled ="+isModifiedCalled+" "+numero);
	return isModifiedCalled;
    }

    public boolean ejbStoreCalled() {
	logger.log(BasicLevel.DEBUG, "PersonneEC "+nom+" ejbStoreCalled ="+ejbStoreCalled+" "+numero);
	return ejbStoreCalled;
    }
	
}
