/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.test.testbeancluster.bean;

import java.rmi.RemoteException;
import java.rmi.dgc.VMID;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.ejb.CreateException;
import javax.ejb.SessionContext;
import javax.ejb.SessionBean;
import javax.ejb.FinderException;

import org.apache.log4j.Logger;
import org.jboss.test.testbean.interfaces.AComplexPK;
import org.jboss.test.testbeancluster.interfaces.EntityPK;
import org.jboss.test.testbeancluster.interfaces.EntityPKHome;
import org.jboss.test.testbeancluster.interfaces.NodeAnswer;

/**  A stateful session which access an entity bean used in testing the CIF.
 * @author Scott.Stark@jboss.org
 * @version $Revision: 1.2 $
 */ 
public class SessionToEntityBean implements SessionBean
{
   private static Logger log = Logger.getLogger(SessionToEntityBean.class);
   private static VMID nodeID = new VMID();

   private int accessCount;
   private AComplexPK theKey;

   public void ejbCreate(AComplexPK key) throws CreateException
   {
      log.debug("ejbCreate(AComplexPK) called, nodeID="+nodeID);
      this.theKey = key;
   }
   public void ejbActivate()
   {
      log.debug("ejbActivate() called, nodeID="+nodeID);
   }
   public void ejbPassivate()
   {
      log.debug("ejbPassivate() called, nodeID="+nodeID);
   }
   public void ejbRemove()
   {
      log.debug("ejbRemove() called, nodeID="+nodeID);
      try
      {
         InitialContext ctx = new InitialContext();
         Context enc = (Context) ctx.lookup("java:comp/env");
         EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
         home.remove(theKey);
      }
      catch(Exception e)
      {
         log.error("Failed to remove EntityPK", e);
      }
   }
   public void setSessionContext(SessionContext context)
   {
   }

   public String createEntity()
      throws CreateException
   {
      String msg = null;
      EntityPKHome home = null;
      log.info("Enter createEntity, theKey="+theKey);
      try
      {
         InitialContext ctx = new InitialContext();
         Context enc = (Context) ctx.lookup("java:comp/env");
         home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
         EntityPK bean = home.findByPrimaryKey(theKey);
         msg = "Found EntityPK, bean="+bean;
         log.info(msg);
      }
      catch(FinderException e)
      {
         EntityPK bean = home.create(theKey.aBoolean, theKey.anInt, theKey.aLong,
            theKey.aDouble, theKey.aString);
         msg = "Created EntityPK, bean="+bean;
         log.info(msg);
      }
      catch(Exception e)
      {
         log.error("Failed to create EntityPK", e);
         throw new CreateException("Failed to create EntityPK: "+e.getMessage());
      }
      return msg;
   }
   public NodeAnswer accessEntity()
   {
      accessCount ++;
      log.debug("Enter accessEntity(), accessCount="+accessCount);
      int beanCount = 0;
      try
      {
         InitialContext ctx = new InitialContext();
         Context enc = (Context) ctx.lookup("java:comp/env");
         EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
         EntityPK bean = home.findByPrimaryKey(theKey);
         bean.setOtherField(accessCount);
         log.debug("Set EntityPK.OtherField to: "+accessCount);
         beanCount = bean.getOtherField();
      }
      catch(Exception e)
      {
         log.debug("failed", e);
      }
      log.debug("Exit accessEntity()");
      return new NodeAnswer(nodeID, new Integer(beanCount));
   }

   public int getAccessCount()
   {
      return accessCount;
   }
   public NodeAnswer validateAccessCount(int count)
      throws RemoteException
   {
      if( accessCount != count )
         throw new RemoteException("AccessCount: " + accessCount + " != " + count);
      
      int beanCount = 0;
      try
      {
         InitialContext ctx = new InitialContext();
         Context enc = (Context) ctx.lookup("java:comp/env");
         EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
         EntityPK bean = home.findByPrimaryKey(theKey);
         beanCount = bean.getOtherField();
         if( beanCount != count )
            throw new RemoteException("BeanCount: " + beanCount + " != " + count);
      }
      catch(RemoteException e)
      {
         log.error("Failed to validate EntityPK", e);
         throw e;
      }
      catch(Exception e)
      {
         log.error("Failed to validate EntityPK", e);
         throw new RemoteException("Failed to validate EntityPK");
      }
      return new NodeAnswer(nodeID, new Integer(beanCount));
   }
}