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