⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bankejbbean.java

📁 噶额外噶外骨骼感广泛高热感 就 啊啊
💻 JAVA
字号:
package itso.ejb.model.facade;

import itso.bank.model.*;
import itso.bank.exception.*;

import itso.ejb.model.entity.*;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Bean implementation class for Enterprise Bean: BankEJB
 */
public class BankEJBBean implements javax.ejb.SessionBean {
	
	// Context and home cache
	private Context context;
	private AccountLocalHome accountHome;
	private CustomerLocalHome customerHome;
	private TransRecordLocalHome transRecordHome;

	private javax.ejb.SessionContext mySessionCtx;
	/**
	 * getSessionContext
	 */
	public javax.ejb.SessionContext getSessionContext() {
		return mySessionCtx;
	}
	/**
	 * setSessionContext
	 */
	public void setSessionContext(javax.ejb.SessionContext ctx) {
		mySessionCtx = ctx;
	}
	/**
	 * ejbCreate
	 */
	public void ejbCreate() throws javax.ejb.CreateException {
	}
	/**
	 * ejbActivate
	 */
	public void ejbActivate() {
	}
	/**
	 * ejbPassivate
	 */
	public void ejbPassivate() {
	}
	/**
	 * ejbRemove
	 */
	public void ejbRemove() {
	}
	/**
	 * Returns the accountHome.
	 * @return AccountLocalHome
	 */
	private AccountLocalHome getAccountHome() {
		if (accountHome == null) 
			accountHome = (AccountLocalHome)lookup("ejb/Account");
		return accountHome;
	}

	/**
	 * Returns the context.
	 * @return Context
	 */
	private Context getContext() throws NamingException {
		if (context == null) context = new InitialContext();
		return context;
	}

	/**
	 * Returns the customerHome.
	 * @return CustomerLocalHome
	 */
	private CustomerLocalHome getCustomerHome() {
		if (customerHome == null)
			customerHome = (CustomerLocalHome)lookup("ejb/Customer");
		return customerHome;
	}

	/**
	 * Returns the transRecordHome.
	 * @return TransRecordLocalHome
	 */
	private TransRecordLocalHome getTransRecordHome() {
		if (transRecordHome == null)
			transRecordHome = (TransRecordLocalHome)lookup("ejb/TransRecord");
		return transRecordHome;
	}

	// helper metohd to lookup home interface
	private Object lookup(String referenceName) {
		try { return getContext().lookup("java:comp/env/" + referenceName);
		} catch (NamingException e) { throw new EJBException(e); }
	}
	
	
	// entity access methods
	private CustomerLocal getCustomerLocal(String customerID) 
							throws CustomerDoesNotExistException {
		try { return getCustomerHome().findByPrimaryKey
				(new CustomerKey(Integer.valueOf(customerID).intValue()));
		} catch (FinderException e) {
			throw new CustomerDoesNotExistException();
		}
	}
	
	private AccountLocal getAccountLocal(String accountID)
							throws AccountDoesNotExistException {
		try { return getAccountHome().findByPrimaryKey(accountID);
		} catch (FinderException e) {
			throw new AccountDoesNotExistException();
		}
	}
	
	// business methods
	public Customer getCustomer(String customerID)
								throws CustomerDoesNotExistException {
		CustomerLocal customer = getCustomerLocal(customerID);
		Customer customerDTO = new Customer();
		customerDTO.setId(customerID);
		customerDTO.setFirstName(customer.getFirstName());
		customerDTO.setLastName(customer.getLastName());
		customerDTO.setTitle(customer.getTitle());
		return customerDTO;
	}

	public Account getAccount(String accountID)
								throws AccountDoesNotExistException {
		AccountLocal account = getAccountLocal(accountID);
		Account accountDTO = new Account();
		accountDTO.setId(accountID);
		accountDTO.setType(account.getType());
		accountDTO.setBalance(account.getBalance());
		return accountDTO;
	}

	public Account[] getAccounts(String customerID)
									throws CustomerDoesNotExistException {
		CustomerLocal customer = getCustomerLocal(customerID);
		Collection accounts = customer.getAccounts();
		Iterator i = accounts.iterator();
		SortedSet dtoset = new TreeSet();
		while ( i.hasNext() ) {
			AccountLocal account = (AccountLocal) i.next();
			Account accountDTO = new Account();
			accountDTO.setId(account.getId());
			accountDTO.setType(account.getType());
			accountDTO.setBalance(account.getBalance());
			dtoset.add(accountDTO);
		}
		return (Account[])dtoset.toArray( new Account[dtoset.size()] );
	}

	public TransRecord[] getTransactions(String accountID)
									throws AccountDoesNotExistException {
		AccountLocal account = getAccountLocal(accountID);
		Collection transactions = account.getTransRecords();
		Iterator i = transactions.iterator();
		SortedSet dtoset = new TreeSet();
		while ( i.hasNext() ) {
			TransRecordLocal tr = (TransRecordLocal) i.next();
			TransRecord trDTO = new TransRecord();
			trDTO.setTimeStamp(tr.getTimeStamp());
			trDTO.setTransType(tr.getTransType());
			trDTO.setTransAmt(tr.getTransAmt());
			dtoset.add(trDTO);
		}
		return (TransRecord[])dtoset.toArray( new TransRecord[dtoset.size()] );
	}
	
	public BigDecimal deposit(String accountID, BigDecimal amount) 
					throws AccountDoesNotExistException, CreateException {
		AccountLocal account = getAccountLocal(accountID);
		account.deposit(amount);
		getTransRecordHome().create("C", amount, account);
		return account.getBalance();
	}

	public BigDecimal withdraw(String accountID, BigDecimal amount)
				throws InsufficientFundsException, AccountDoesNotExistException, CreateException {
		AccountLocal account = getAccountLocal(accountID);
		account.withdraw(amount);
		getTransRecordHome().create("D", amount, account);
		return account.getBalance();
	}

	public BigDecimal transfer(String account1, String account2, BigDecimal amount)
				throws InsufficientFundsException, AccountDoesNotExistException, CreateException {
		BigDecimal balance = withdraw(account1, amount);
		deposit (account2, amount);
		return balance;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -