📄 accountsessionbean.java
字号:
/* * JBoss, the OpenSource J2EE webOS * * Distributable under LGPL license. * See terms of license at gnu.org. */package transaction.session;import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;import java.rmi.RemoteException;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;import javax.ejb.EJBException;import javax.ejb.CreateException;import javax.ejb.FinderException;import javax.ejb.RemoveException;import javax.ejb.SessionContext;import transaction.interfaces.Account;import transaction.interfaces.AccountData;import transaction.interfaces.AccountHome;import transaction.interfaces.AccountPK;import transaction.interfaces.Constants;import transaction.interfaces.Transaction;import transaction.interfaces.TransactionHome;/** * The Session bean represents the account's business interface * * @author Andreas Schaefer * @version $Revision: 1.1 $ * * @ejb:bean name="bank/AccountSession" * display-name="Account Session" * type="Stateless" * view-type="remote" * jndi-name="ejb/bank/AccountSession" * * @ejb:interface extends="javax.ejb.EJBObject" * * @ejb:home extends="javax.ejb.EJBHome" * * @ejb:pk extends="java.lang.Object" * * @ejb:transaction type="Required" * * @ejb:ejb-ref ejb-name="bank/Account" * * @ejb:ejb-ref ejb-name="bank/Transaction" */public class AccountSessionBean extends SessionSupport{ // Constants ----------------------------------------------------- // Attributes ---------------------------------------------------- // Static -------------------------------------------------------- // Constructors -------------------------------------------------- // Public -------------------------------------------------------- /** * @ejb:interface-method view-type="remote" **/ public AccountData getAccount( String pAccountId ) throws FinderException, RemoteException { return getAccountHome().findByPrimaryKey( new AccountPK( pAccountId ) ).getData(); } /** * @ejb:interface-method view-type="remote" **/ public AccountData getAccount( String pCustomerId, int pType ) throws FinderException, RemoteException { return getAccountHome().findByCustomerAndType( pCustomerId, pType ).getData(); } /** * @ejb:interface-method view-type="remote" **/ public Collection getAccounts( String pCustomerId ) throws FinderException, RemoteException { Collection lAccounts = getAccountHome().findByCustomer( pCustomerId ); Collection lList = new ArrayList( lAccounts.size() ); Iterator i = lAccounts.iterator(); while( i.hasNext() ) { lList.add( ( (Account) i.next() ).getData() ); } return lList; } /** * @ejb:interface-method view-type="remote" **/ public Collection getTransactions( String pAccountId ) throws FinderException, RemoteException { Collection lTransactions = getTransactionHome().findByAccount( pAccountId ); Iterator i = lTransactions.iterator(); Collection lList = new ArrayList( lTransactions.size() ); while( i.hasNext() ) { lList.add( ( (Transaction) i.next() ).getData() ); } return lList; } /** * @ejb:interface-method view-type="remote" **/ public AccountData createAccount( String pCustomerId, int pType, float pInitialDeposit ) throws CreateException, RemoteException { AccountData lData = new AccountData(); lData.setCustomerId( pCustomerId ); lData.setType( pType ); lData.setBalance( pInitialDeposit ); Account lAccount = getAccountHome().create( lData ); AccountData lNew = lAccount.getData(); getTransactionHome().create( lNew.getId(), Constants.INITIAL_DEPOSIT, pInitialDeposit, "Account Creation" ); return lNew; } /** * @ejb:interface-method view-type="remote" **/ public void removeAccount( String pAccountId ) throws RemoveException, RemoteException { try { Account lAccount = getAccountHome().findByPrimaryKey( new AccountPK( pAccountId ) ); removeAccount( lAccount ); } catch( FinderException fe ) { // When not found then ignore it because account is already removed } catch( CreateException ce ) { throw new EJBException( ce ); } } /** * @ejb:interface-method view-type="remote" **/ public void removeAccount( String pCustomerId, int pType ) throws RemoveException, RemoteException { try { Account lAccount = getAccountHome().findByCustomerAndType( pCustomerId, pType ); removeAccount( lAccount ); } catch( FinderException fe ) { // When not found then ignore it because account is already removed } catch( CreateException ce ) { throw new EJBException( ce ); } } /** * @ejb:interface-method view-type="remote" **/ public void removeTransactions( String pAccountId ) throws RemoveException, RemoteException { try { Collection lTransactions = getTransactionHome().findByAccount( pAccountId ); Iterator i = lTransactions.iterator(); while( i.hasNext() ) { Transaction lTransaction = (Transaction) i.next(); lTransaction.remove(); } } catch( FinderException fe ) { } } private void removeAccount( Account pAccount ) throws RemoveException, CreateException, RemoteException { AccountData lData = pAccount.getData(); pAccount.remove(); getTransactionHome().create( lData.getId(), Constants.FINAL_WITHDRAW, lData.getBalance(), "Account Closure" ); } /** * @ejb:interface-method view-type="remote" **/ public void deposit( String pAccountId, float pAmount ) throws FinderException, RemoteException { Account lAccount = getAccountHome().findByPrimaryKey( new AccountPK( pAccountId ) ); AccountData lData = lAccount.getData(); lData.setBalance( lData.getBalance() + pAmount ); lAccount.setData( lData ); try { getTransactionHome().create( lData.getId(), Constants.DEPOSIT, pAmount, "Account Deposit" ); } catch( CreateException ce ) { throw new EJBException( ce ); } } /** * @ejb:interface-method view-type="remote" **/ public void withdraw( String pAccountId, float pAmount ) throws FinderException, RemoteException { Account lAccount = getAccountHome().findByPrimaryKey( new AccountPK( pAccountId ) ); AccountData lData = lAccount.getData(); lData.setBalance( lData.getBalance() - pAmount ); lAccount.setData( lData ); try { getTransactionHome().create( lData.getId(), Constants.WITHDRAW, pAmount, "Account Withdraw" ); } catch( CreateException ce ) { throw new EJBException( ce ); } } /** * @ejb:interface-method view-type="remote" **/ public void transfer( String pFromAccountId, String pToAccountId, float pAmount ) throws FinderException, RemoteException { try { withdraw( pFromAccountId, pAmount ); deposit( pToAccountId, pAmount ); } catch( RemoteException re ) { re.printStackTrace(); throw re; } } private AccountHome getAccountHome() { try { return (AccountHome) new InitialContext().lookup( AccountHome.COMP_NAME ); } catch( NamingException ne ) { throw new EJBException( ne ); } } private TransactionHome getTransactionHome() { try { return (TransactionHome) new InitialContext().lookup( TransactionHome.COMP_NAME ); } catch( NamingException ne ) { throw new EJBException( ne ); } } // SessionBean implementation ------------------------------------ public void setSessionContext(SessionContext context) { super.setSessionContext(context); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -