1 package bank; 2 3 import javax.ejb.SessionBean ; 4 import javax.ejb.SessionContext ; 5 import javax.ejb.EJBException ; 6 import javax.jdo.PersistenceManagerFactory; 7 import javax.jdo.PersistenceManager; 8 import javax.jdo.Query; 9 import javax.naming.InitialContext ; 10 import javax.naming.NamingException ; 11 import java.rmi.RemoteException ; 12 import java.util.List ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.util.ArrayList ; 16 17 20 public class BankApplicationImpl implements SessionBean { 21 22 protected SessionContext sessionContext = null; 23 24 public static PersistenceManagerFactory pmf = null; 25 26 private static Bank bank = null; 27 28 private static final String SOCIETE_GENERALE = "SG"; 29 30 private static final String PMF_CTX_NAME = "java:comp/env/jdo/pmf"; 31 32 public static Bank getBank(PersistenceManager pm) { 33 if (bank != null) { 34 return bank; 35 } 36 synchronized (BankApplicationImpl.class) { 37 if (bank != null) { 38 return bank; 39 } 40 Query q = pm.newQuery(Bank.class); 41 q.declareParameters("String bn"); 42 q.setFilter("(name==bn)"); 43 Collection c = (Collection ) q.execute(SOCIETE_GENERALE); 44 if (c.isEmpty()) { 45 bank = new Bank(SOCIETE_GENERALE); 47 pm.makePersistent(bank); 48 } else { 49 bank = (Bank) c.iterator().next(); 51 } 52 q.closeAll(); 53 } 54 return bank; 55 } 56 57 60 public void createAgency(String name) throws RemoteException { 61 PersistenceManager pm = pmf.getPersistenceManager(); 62 getBank(pm).createAgency(name); 63 pm.close(); 64 } 65 66 public List getAgencies() throws RemoteException { 67 PersistenceManager pm = pmf.getPersistenceManager(); 68 Iterator it = getBank(pm).agencies.iterator(); 69 ArrayList res = new ArrayList (bank.agencies.size()); 70 while (it.hasNext()) { 71 res.add(((Agency) it.next()).name); 72 } 73 pm.close(); 74 return res; 75 } 76 77 public boolean removeAgency(String name) throws RemoteException { 78 PersistenceManager pm = pmf.getPersistenceManager(); 79 Iterator it = getBank(pm).agencies.iterator(); 80 while (it.hasNext()) { 81 Agency a = (Agency) it.next(); 82 if (a.name.equals(name) && a.clients.isEmpty()) { 83 bank.agencies.remove(a); 84 pm.deletePersistent(a); 85 pm.close(); 86 return true; 87 } 88 } 89 pm.close(); 90 return false; 91 } 92 93 public void createClient(ClientId cid, String agencyName) 94 throws RemoteException { 95 PersistenceManager pm = pmf.getPersistenceManager(); 96 Agency a = getBank(pm).getAgency(agencyName); 97 if (a == null) { 98 pm.close(); 99 throw new RemoteException ("No agency '" + agencyName + "' found."); 100 } 101 a.createClient(cid.firstName, cid.lastName, cid.address); 102 pm.close(); 103 } 104 105 public List getClients(String agencyName) throws RemoteException { 106 PersistenceManager pm = pmf.getPersistenceManager(); 107 Agency a = getBank(pm).getAgency(agencyName); 108 if (a == null) { 109 pm.close(); 110 throw new RemoteException ("No agency '" + agencyName + "' found."); 111 } 112 Iterator it = a.clients.iterator(); 113 ArrayList res = new ArrayList (a.clients.size()); 114 while (it.hasNext()) { 115 res.add(new ClientId((Client) it.next())); 116 } 117 pm.close(); 118 return res; 119 } 120 121 public boolean removeClient(String agencyName, ClientId cid) 122 throws RemoteException { 123 PersistenceManager pm = pmf.getPersistenceManager(); 124 Agency a = getBank(pm).getAgency(agencyName); 125 if (a == null) { 126 pm.close(); 127 throw new RemoteException ("No agency '" + agencyName + "' found."); 128 } 129 Client c = a.getClient(cid.firstName, cid.lastName, cid.address); 130 boolean remove = c != null && c.accounts.isEmpty(); 131 if (remove) { 132 a.removeClient(c); 133 pm.deletePersistent(c); 134 } 135 pm.close(); 136 return remove; 137 } 138 139 public String createAccount(ClientId cid, String agencyName) 140 throws RemoteException { 141 PersistenceManager pm = pmf.getPersistenceManager(); 142 Agency a = getBank(pm).getAgency(agencyName); 143 if (a == null) { 144 pm.close(); 145 throw new RemoteException ("No agency '" + agencyName + "' found."); 146 } 147 Client c = a.getClient(cid.firstName, cid.lastName, cid.address); 148 if (c == null) { 149 pm.close(); 150 throw new RemoteException ("No client '" + cid + "' found."); 151 } 152 String number = c.createAccount(a.getNextAccountNumber()).number; 153 pm.close(); 154 return number; 155 } 156 157 public AccountInfo getAccountInfo(String number, ClientId cid, 158 String agencyName) throws RemoteException { 159 PersistenceManager pm = pmf.getPersistenceManager(); 160 Agency a = getBank(pm).getAgency(agencyName); 161 if (a == null) { 162 pm.close(); 163 throw new RemoteException ("No agency '" + agencyName + "' found."); 164 } 165 Client c = a.getClient(cid.firstName, cid.lastName, cid.address); 166 if (c == null) { 167 pm.close(); 168 throw new RemoteException ("No client '" + cid + "' found."); 169 } 170 Iterator it = c.accounts.iterator(); 171 while (it.hasNext()) { 172 Account ac = (Account) it.next(); 173 if (ac.number.equals(number)) { 174 return new AccountInfo(ac.number, ac.solde); 175 } 176 } 177 pm.close(); 178 return null; 179 } 180 181 public List getAccounts(String agencyName, ClientId cid) 182 throws RemoteException { 183 PersistenceManager pm = pmf.getPersistenceManager(); 184 Agency a = getBank(pm).getAgency(agencyName); 185 if (a == null) { 186 pm.close(); 187 throw new RemoteException ("No agency '" + agencyName + "' found."); 188 } 189 Client c = a.getClient(cid.firstName, cid.lastName, cid.address); 190 if (c == null) { 191 pm.close(); 192 throw new RemoteException ("No client '" + cid + "' found."); 193 } 194 Iterator it = c.accounts.iterator(); 195 ArrayList res = new ArrayList (c.accounts.size()); 196 while (it.hasNext()) { 197 Account ac = (Account) it.next(); 198 res.add(new AccountInfo(ac.number, ac.solde)); 199 } 200 pm.close(); 201 return res; 202 } 203 204 public AccountInfo closeAccount(String agencyName, ClientId cid, 205 String accountNumber) throws RemoteException { 206 PersistenceManager pm = pmf.getPersistenceManager(); 207 Agency a = getBank(pm).getAgency(agencyName); 208 if (a == null) { 209 pm.close(); 210 throw new RemoteException ("No agency '" + agencyName + "' found."); 211 } 212 Client c = a.getClient(cid.firstName, cid.lastName, cid.address); 213 if (c == null) { 214 pm.close(); 215 throw new RemoteException ("No client '" + cid + "' found."); 216 } 217 Iterator it = c.accounts.iterator(); 218 while (it.hasNext()) { 219 Account ac = (Account) it.next(); 220 if (ac.number.equals(accountNumber)) { 221 c.removeAccount(ac); 222 pm.deletePersistent(ac); 223 AccountInfo ai = new AccountInfo(ac.number, ac.solde); 224 pm.close(); 225 return ai; 226 } 227 } 228 pm.close(); 229 return null; 230 } 231 232 235 public void setSessionContext(SessionContext sessionContext) 236 throws EJBException , RemoteException { 237 if (pmf == null) { 238 synchronized (BankApplicationImpl.class) { 239 if (pmf == null) { 240 try { 241 pmf = (PersistenceManagerFactory) new InitialContext () 242 .lookup(PMF_CTX_NAME); 243 } catch (NamingException e) { 244 throw new EJBException ( 245 "No PersistenceMangerFactory registered" 246 + " in JNDI with the name '" 247 + PMF_CTX_NAME + "'"); 248 } 249 } 250 } 251 } 252 } 253 254 public void ejbCreate() throws RemoteException { 255 } 256 257 public void ejbRemove() throws EJBException , RemoteException { 258 } 259 260 public void ejbActivate() throws EJBException , RemoteException { 261 } 262 263 public void ejbPassivate() throws EJBException , RemoteException { 264 } 265 } | Popular Tags |