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

📄 txcontrollerbean.java

📁 本教程介绍j2ee企业开发方面的知识
💻 JAVA
字号:
/* * * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved. *  * This software is the proprietary information of Sun Microsystems, Inc.   * Use is subject to license terms. *  */package com.sun.ebank.ejb.tx;import java.sql.*;import javax.sql.*;import java.util.*;import javax.ejb.*;import javax.naming.*;import java.rmi.RemoteException;import com.sun.ebank.ejb.account.AccountHome;import com.sun.ebank.ejb.account.Account;import com.sun.ebank.ejb.exception.TxNotFoundException;import com.sun.ebank.ejb.exception.AccountNotFoundException;import com.sun.ebank.ejb.exception.InsufficientFundsException;import com.sun.ebank.ejb.exception.InsufficientCreditException;import com.sun.ebank.ejb.exception.IllegalAccountTypeException;import com.sun.ebank.util.Debug;import com.sun.ebank.util.TxDetails;import com.sun.ebank.util.DBHelper;import com.sun.ebank.util.EJBGetter;import com.sun.ebank.util.CodedNames;import com.sun.ebank.util.DomainUtil;public class TxControllerBean implements SessionBean {    private TxHome txHome;    private AccountHome accountHome;    private Connection con;    private SessionContext context;    public ArrayList getTxsOfAccount(java.util.Date startDate,         java.util.Date endDate, String accountId)         throws RemoteException {        Debug.print("TxControllerBean  getTxsOfAccount");        Collection txIds;        ArrayList txList = new ArrayList();        if (startDate == null)             throw new IllegalArgumentException("null startDate");        if (endDate == null)             throw new IllegalArgumentException("null endDate");        if (accountId == null)             throw new IllegalArgumentException("null accountId");        try {            txIds = txHome.findByAccountId(startDate, endDate, accountId);        } catch (Exception ex) {             return txList;        }        Iterator i = txIds.iterator();        while (i.hasNext()) {            Tx tx = (Tx)i.next();            TxDetails txDetail = tx.getDetails();            txList.add(txDetail);        }        return txList;    } // getTxsOfAccount    public TxDetails getDetails(String txId) throws TxNotFoundException {        Debug.print("TxControllerBean getDetails");        if (txId == null)           throw new IllegalArgumentException("null txId");        try {            Tx tx = txHome.findByPrimaryKey(txId);            return tx.getDetails();        } catch (Exception ex) {            throw new TxNotFoundException(txId);        }    } // getDetails    public void withdraw(double amount, String description,        String accountId) throws  IllegalArgumentException,         AccountNotFoundException, IllegalAccountTypeException,        InsufficientFundsException, RemoteException {        Debug.print("TxControllerBean withdraw");        Account account = checkAccountArgs(amount, description, accountId);        String type = account.getType();        if (DomainUtil.isCreditAccount(type))             throw new IllegalAccountTypeException(type);        double newBalance = account.getBalance() - amount;        if (newBalance < 0.0)            throw new InsufficientFundsException();        executeTx(-1 * amount, description, accountId, newBalance, account);    } // withdraw       public void deposit(double amount, String description,        String accountId) throws IllegalArgumentException,        AccountNotFoundException, IllegalAccountTypeException,        RemoteException {         Debug.print("TxControllerBean deposit");        Account account = checkAccountArgs(amount, description, accountId);        String type = account.getType();        if (DomainUtil.isCreditAccount(type))             throw new IllegalAccountTypeException(type);        double newBalance = account.getBalance() + amount;        executeTx(amount, description, accountId, newBalance, account);          } // deposit    public void makeCharge(double amount, String description,        String accountId) throws  IllegalArgumentException,         AccountNotFoundException, IllegalAccountTypeException,        InsufficientCreditException, RemoteException {        Debug.print("TxControllerBean charge");        Account account = checkAccountArgs(amount, description, accountId);        String type = account.getType();        if (DomainUtil.isCreditAccount(type) == false)             throw new IllegalAccountTypeException(type);        double newBalance = account.getBalance() + amount;        if (newBalance > account.getCreditLine())            throw new InsufficientCreditException();        executeTx(amount, description, accountId, newBalance, account);    } // charge    public void makePayment(double amount, String description,        String accountId) throws IllegalArgumentException,        AccountNotFoundException, IllegalAccountTypeException,        RemoteException {         Debug.print("TxControllerBean makePayment");        Account account = checkAccountArgs(amount, description, accountId);        String type = account.getType();        if (DomainUtil.isCreditAccount(type) == false)             throw new IllegalAccountTypeException(type);        double newBalance = account.getBalance() - amount;        executeTx(-1 * amount, description, accountId, newBalance, account);          } // makePayment    public void transferFunds(double amount, String description,        String fromAccountId, String toAccountId) throws          RemoteException, IllegalArgumentException,        AccountNotFoundException, InsufficientFundsException,        InsufficientCreditException {                Account fromAccount = checkAccountArgs(amount, description,             fromAccountId);        Account toAccount = checkAccountArgs(amount, description,             toAccountId);        String fromType = fromAccount.getType();        double fromBalance = fromAccount.getBalance();        if (DomainUtil.isCreditAccount(fromType)) {            double fromNewBalance =  fromBalance + amount;            if (fromNewBalance > fromAccount.getCreditLine())               throw new InsufficientCreditException();            executeTx(amount, description, fromAccountId,                 fromNewBalance, fromAccount);        } else {            double fromNewBalance =  fromBalance - amount;            if (fromNewBalance < 0.0)                throw new InsufficientFundsException();            executeTx(-1 * amount, description, fromAccountId,                 fromNewBalance, fromAccount);        }                   String toType = toAccount.getType();        double toBalance = toAccount.getBalance();           if (DomainUtil.isCreditAccount(toType)) {            double toNewBalance = toBalance - amount;            executeTx(-1 * amount, description, toAccountId,                 toNewBalance, toAccount);        } else {            double toNewBalance = toBalance + amount;            executeTx(amount, description, toAccountId,                 toNewBalance, toAccount);        }    } // transferFunds    // private methods    private void executeTx(double amount, String description,        String accountId, double newBalance, Account account) {        Debug.print("TxControllerBean executeTx");        try {            makeConnection();            String txId = DBHelper.getNextTxId(con);            account.setBalance(newBalance);            Tx tx = txHome.create(txId, accountId, new java.util.Date(),                amount, newBalance, description);        } catch (Exception ex) {             throw new EJBException("executeTx: " + ex.getMessage());        } finally {             releaseConnection();        }    } // executeTx    private Account checkAccountArgs(double amount, String description,        String accountId) throws IllegalArgumentException,        AccountNotFoundException {        Account account = null;        if (description == null)            throw new IllegalArgumentException("null description");        if (accountId == null)            throw new IllegalArgumentException("null accountId" );        if (amount <= 0)           throw new IllegalArgumentException("amount <= 0");        try {            account = accountHome.findByPrimaryKey(accountId);        } catch (Exception ex) {            throw new AccountNotFoundException(accountId);        }           return account;    } // checkAccountArgs    // ejb methods    public void ejbCreate() {        Debug.print("TxControllerBean ejbCreate");        try {            txHome = EJBGetter.getTxHome();            accountHome = EJBGetter.getAccountHome();        } catch (Exception ex) {             throw new EJBException("ejbCreate: " +                 ex.getMessage());        }    } // ejbCreate    public void setSessionContext(SessionContext context) {       this.context = context;    }    public TxControllerBean() {}    public void ejbRemove() {}    public void ejbActivate() {}    public void ejbPassivate() {}/*********************** Database Routines *************************/    private void makeConnection() {           Debug.print("TxControllerBean makeConnection");        try {            InitialContext ic = new InitialContext();            DataSource ds = (DataSource) ic.lookup(CodedNames.BANK_DATABASE);            con =  ds.getConnection();        } catch (Exception ex) {             throw new EJBException("Unable to connect to database. " +                 ex.getMessage());        }    } // makeConnection       private void releaseConnection() {           Debug.print("TxControllerBean releaseConnection");        try {            con.close();        } catch (SQLException ex) {             throw new EJBException("releaseConnection: " + ex.getMessage());        }    } // releaseConnection} // TxControllerBean

⌨️ 快捷键说明

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