//Copyright Acelet Corp. 1999-2005. All rights reserved.
//You can use, modify, distrubute this file.
//This is an example only. It is not optimized in any way.

package examples;

import java.io.*;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.management.*;
import javax.management.j2ee.*;

import com.acelet.logging.Logging;
import com.acelet.opensource.globalEnvironment.GlobalLong;
import com.acelet.opensource.logging.Alog;

public class CasinoExampleEJB implements CasinoExampleConstants, SessionBean, Serializable {
  SessionContext sessionContext = null;
  Connection connection;

  public void ejbActivate() {
  }

  public void ejbCreate() {
    Logging.log("Hi everyone!", 0, "examples.CasinoExampleEJB", 
      "ejbCreate", "CasinoExampleEJB.java", 27);
  }

  public void ejbPassivate() {
  }
  
  public void ejbRemove() {
  }

  public int getNumberOfDigits() {
    try {
      Long numberOfDigitsLong = GlobalLong.get(connection, numberOfDigitKey);
      return (int) numberOfDigitsLong.longValue();
    } catch (Exception ex) {
      return 8;
    }
  }

  public String getResult() {
    try {
      int numberOfDigits = getNumberOfDigits();
      int winningNumber = getWinningNumber();

      try {
        int lastNumber = (int) GlobalLong.get(connection, numberKey).longValue();
        if (lastNumber == winningNumber)
          return "You won: " + winningNumber;
      } catch (Exception ex) {
      }

      double d = (Math.pow(10., (double) numberOfDigits));
      Logging.log("d=" + d, 0, "examples.CasinoExampleEJB", 
        "getResult", "CasinoExampleEJB.java", 59);

      int nextNumber = (int) (Math.random() * d);
      Alog.log("nextNumber=" + nextNumber + " winningNumber=" + winningNumber, 0,
        "examples.CasinoExampleEJB", "getResult", 
        "CasinoExampleEJB.java", 63);
      
      GlobalLong.put(connection, numberKey, nextNumber);

      if (nextNumber == winningNumber) {
        try {
          notifyMbean();
        } catch (Exception e) {
        }
        return "You won: " + winningNumber;
      }

      return nextNumber + "";
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new EJBException(ex);
    }
  }

  public int getWinningNumber() {
    try {
      Long winningNumberLong = GlobalLong.get(connection, winningNumberKey);
      return (int) winningNumberLong.longValue();
    } catch (Exception ex) {
      return -1;
    }
  }

  void notifyMbean() throws Exception {
    ObjectName casinoObjectName = CasinoExampleMBean.getObjectName();
    Management mejb = CasinoExampleMBean.getManagement();
    Attribute attribute = new Attribute("WinningState", new Boolean(true));
    mejb.setAttribute(casinoObjectName, attribute);
  }

  public void reset() throws RemoteException {
    setNumberOfDigits(8);
    setWinningNumber(-1);
  }

  public void setNumberOfDigits(int numberOfDigits) {
    try {
      if (numberOfDigits >= 1 || numberOfDigits < 8)
        GlobalLong.put(connection, numberOfDigitKey, numberOfDigits);
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new EJBException(ex);
    }
  }

  public void setSessionContext(SessionContext sc) {
    this.sessionContext = sc;

    try {
      connection = CasinoExampleMBean.getConnection();
      setupMbean();
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new EJBException(ex);
    }
  }

  void setupMbean() throws Exception {
    new CasinoExampleMBean().register();
  }

  public void setWinningNumber(int winningNumber) {
    try {
      GlobalLong.put(connection, winningNumberKey, winningNumber);
      GlobalLong.put(connection, numberKey, winningNumber + 1);
    } catch (Exception ex) {
      ex.printStackTrace();
      throw new EJBException(ex);
    }
  }

  public void unregisterMBean() throws RemoteException {
    new CasinoExampleMBean().unregisterMBean();
  }
    
  public static void main(String[] args) {
    System.out.println("CasinoExampleEJB");
  }
}


