View Javadoc
1   package ejava.examples.ejbsessionbank.blimpl;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  import ejava.examples.ejbsessionbank.bl.BankException;
11  import ejava.examples.ejbsessionbank.bl.Teller;
12  import ejava.examples.ejbsessionbank.bo.Account;
13  import ejava.examples.ejbsessionbank.bo.Ledger;
14  import ejava.examples.ejbsessionbank.bo.Owner;
15  import ejava.examples.ejbsessionbank.dao.AccountDAO;
16  import ejava.examples.ejbsessionbank.dao.OwnerDAO;
17  
18  /**
19   * This class implements the business logic of the Teller. Its logically
20   * implemented in 2 halves; Account and Owner + Account. The Account-only
21   * half is used to demonstrate some of the basics. The Owner+Account half 
22   * is used to add a small amount of complexity to the data model to demo
23   * some lazy load issues within the remote facade/EJB layer.
24   *
25   */
26  public class TellerImpl implements Teller {
27      Logger log = LoggerFactory.getLogger(TellerImpl.class);
28      public static final String OVERDRAWN_ACCOUNTS = "getOverdrawnAccounts";
29      public static final String ALL_ACCOUNTS = "getAccounts";
30      private AccountDAO acctDAO;
31      private OwnerDAO ownerDAO;
32      
33      public void setAcctDAO(AccountDAO acctDAO) {
34          this.acctDAO = acctDAO;
35      }
36      public void setOwnerDAO(OwnerDAO ownerDAO) {
37          this.ownerDAO = ownerDAO;
38      }
39      
40      public Account createAccount(String accountNum) throws BankException {
41          log.debug("createAccount(num=" + accountNum + ")");
42          
43          Account account = new Account();
44          account.setAccountNumber(accountNum);
45          account = acctDAO.createAccount(account);
46          
47          log.debug("account created:" + account);
48          return account;        
49      }
50  
51      public Account getAccount(String accountNum) throws BankException { 
52          log.debug("getAccount(num=" + accountNum + ")");
53          
54          Account account = acctDAO.getAccountByNum(accountNum);
55          if (account==null) {
56              throw new BankException("unable to locate account:" + accountNum);
57          }
58          log.debug("found account:" + account);
59          return account;
60      }
61  
62      public List<Account> getOverdrawnAccounts(int index, int count) throws BankException {
63          log.debug("getOverdrawnAccounts(" +
64                  "index=" + index + ", count=" + count + ")");
65          
66          List<Account> accounts = acctDAO.findAccounts(
67                  OVERDRAWN_ACCOUNTS, null, index, count);
68          
69          log.debug("found " + accounts.size() + " accounts");
70          return accounts;
71      }
72  
73      public List<Account> getAccounts(int index, int count) throws BankException {
74          log.debug("getAccounts(" +
75                  "index=" + index + ", count=" + count + ")");
76          
77          List<Account> accounts = acctDAO.findAccounts(
78                  ALL_ACCOUNTS, null, index, count);
79          
80          log.debug("found " + accounts.size() + " accounts");
81          return accounts;
82      }
83  
84      public Account closeAccount(String accountNum) throws BankException {
85          log.debug("removeAccount(num" + accountNum + ")");
86  
87          Account account = acctDAO.getAccountByNum(accountNum);
88          if (account == null) {
89              log.debug("account num found:" + accountNum);
90              throw new BankException("unable to locate account:" + accountNum);
91          }
92          else if (account.getBalance() != 0) {
93              log.debug("account balance not 0; bal=" + account.getBalance());
94              throw new BankException("unable to close account, " +
95                      "invalid balance:" + account.getBalance());
96          }
97          else {
98          	//unlink the account from the owner first
99          	for (Owner owner : ownerDAO.getAccountOwners(account)) {
100 	        	List<Account> accounts = new ArrayList<Account>(owner.getAccounts());
101 	        	for (Account a : accounts) {
102 	        		if (a.getId() == account.getId()) {
103 	        			owner.getAccounts().remove(a);
104 	        		}
105 	        	}
106         	}
107         	//now we can remove account
108             acctDAO.removeAccount(account);
109         }
110         
111         log.debug("removed account:" + account);
112         return account;
113     }
114 
115     public void updateAccount(Account account) throws BankException {
116         log.debug("updateAccount(update=" + account + ")");
117 
118         Account updated = acctDAO.updateAccount(account);
119         
120         log.debug("updated account:" + updated);
121     }
122     public Ledger getLedger() throws BankException {
123         return acctDAO.getLedger();
124     }
125     
126     public double getLedgerAveBalance() throws BankException {
127         return acctDAO.getLedgerAveBalance();
128     }
129     public long getLedgerCount() throws BankException {
130         return acctDAO.getLedgerCount();
131     }
132     public double getLedgerSum() throws BankException {
133         return acctDAO.getLedgerSum();
134     }
135     
136     
137     public Owner addOwner(long ownerId, String accountNumber)
138             throws BankException {
139         Owner owner = ownerDAO.getOwnerById(ownerId);
140         if (owner == null) {
141             throw new BankException("unable to locate owner:" + ownerId);
142         }
143         Account account = acctDAO.getAccountByNum(accountNumber);
144         if (account == null) {
145             throw new BankException("unable to locate account:"+accountNumber);
146         }
147         owner.getAccounts().add(account);
148         return owner;
149     }    
150     
151     public Owner createOwner(String firstName, String lastName, String ssn)
152             throws BankException {
153         Owner owner = new Owner();
154         owner.setFirstName(firstName);
155         owner.setLastName(lastName);
156         owner.setSsn(ssn);
157         return ownerDAO.createOwner(owner);
158     }
159     
160     public void removeOwner(long ownerId) {
161         Owner owner = ownerDAO.getOwnerById(ownerId);
162         if (owner != null) {
163             ownerDAO.removeOwner(owner);
164         }
165     }
166     
167     public List<Owner> getOwners(int index, int count) throws BankException {
168         return ownerDAO.findOwners(
169                 OwnerDAO.GET_OWNERS_QUERY, null, index, count);
170     }
171     
172     public Owner openAccount(long ownerId, String accountNumber)
173             throws BankException {
174         Owner owner = ownerDAO.getOwnerById(ownerId);
175         if (owner == null) {
176             throw new BankException("owner not found, id=" + ownerId);
177         }
178         
179         Account account = new Account();
180         account.setAccountNumber(accountNumber);
181         owner.getAccounts().add(account);
182         
183         return owner;
184     }    
185 }