001: package bank;
002:
003: import javax.ejb.SessionBean;
004: import javax.ejb.SessionContext;
005: import javax.ejb.EJBException;
006: import javax.jdo.PersistenceManagerFactory;
007: import javax.jdo.PersistenceManager;
008: import javax.jdo.Query;
009: import javax.naming.InitialContext;
010: import javax.naming.NamingException;
011: import java.rmi.RemoteException;
012: import java.util.List;
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.util.ArrayList;
016:
017: /**
018: * @author S.Chassande-Barrioz
019: */
020: public class BankApplicationImpl implements SessionBean {
021:
022: protected SessionContext sessionContext = null;
023:
024: public static PersistenceManagerFactory pmf = null;
025:
026: private static Bank bank = null;
027:
028: private static final String SOCIETE_GENERALE = "SG";
029:
030: private static final String PMF_CTX_NAME = "java:comp/env/jdo/pmf";
031:
032: public static Bank getBank(PersistenceManager pm) {
033: if (bank != null) {
034: return bank;
035: }
036: synchronized (BankApplicationImpl.class) {
037: if (bank != null) {
038: return bank;
039: }
040: Query q = pm.newQuery(Bank.class);
041: q.declareParameters("String bn");
042: q.setFilter("(name==bn)");
043: Collection c = (Collection) q.execute(SOCIETE_GENERALE);
044: if (c.isEmpty()) {
045: //First execution: create the bank
046: bank = new Bank(SOCIETE_GENERALE);
047: pm.makePersistent(bank);
048: } else {
049: //Load from the persistence support
050: bank = (Bank) c.iterator().next();
051: }
052: q.closeAll();
053: }
054: return bank;
055: }
056:
057: // IMPLEMENTATION OF THE BankApplication INTERFACE //
058: //-------------------------------------------------//
059:
060: public void createAgency(String name) throws RemoteException {
061: PersistenceManager pm = pmf.getPersistenceManager();
062: getBank(pm).createAgency(name);
063: pm.close();
064: }
065:
066: public List getAgencies() throws RemoteException {
067: PersistenceManager pm = pmf.getPersistenceManager();
068: Iterator it = getBank(pm).agencies.iterator();
069: ArrayList res = new ArrayList(bank.agencies.size());
070: while (it.hasNext()) {
071: res.add(((Agency) it.next()).name);
072: }
073: pm.close();
074: return res;
075: }
076:
077: public boolean removeAgency(String name) throws RemoteException {
078: PersistenceManager pm = pmf.getPersistenceManager();
079: Iterator it = getBank(pm).agencies.iterator();
080: while (it.hasNext()) {
081: Agency a = (Agency) it.next();
082: if (a.name.equals(name) && a.clients.isEmpty()) {
083: bank.agencies.remove(a);
084: pm.deletePersistent(a);
085: pm.close();
086: return true;
087: }
088: }
089: pm.close();
090: return false;
091: }
092:
093: public void createClient(ClientId cid, String agencyName)
094: throws RemoteException {
095: PersistenceManager pm = pmf.getPersistenceManager();
096: Agency a = getBank(pm).getAgency(agencyName);
097: if (a == null) {
098: pm.close();
099: throw new RemoteException("No agency '" + agencyName
100: + "' found.");
101: }
102: a.createClient(cid.firstName, cid.lastName, cid.address);
103: pm.close();
104: }
105:
106: public List getClients(String agencyName) throws RemoteException {
107: PersistenceManager pm = pmf.getPersistenceManager();
108: Agency a = getBank(pm).getAgency(agencyName);
109: if (a == null) {
110: pm.close();
111: throw new RemoteException("No agency '" + agencyName
112: + "' found.");
113: }
114: Iterator it = a.clients.iterator();
115: ArrayList res = new ArrayList(a.clients.size());
116: while (it.hasNext()) {
117: res.add(new ClientId((Client) it.next()));
118: }
119: pm.close();
120: return res;
121: }
122:
123: public boolean removeClient(String agencyName, ClientId cid)
124: throws RemoteException {
125: PersistenceManager pm = pmf.getPersistenceManager();
126: Agency a = getBank(pm).getAgency(agencyName);
127: if (a == null) {
128: pm.close();
129: throw new RemoteException("No agency '" + agencyName
130: + "' found.");
131: }
132: Client c = a
133: .getClient(cid.firstName, cid.lastName, cid.address);
134: boolean remove = c != null && c.accounts.isEmpty();
135: if (remove) {
136: a.removeClient(c);
137: pm.deletePersistent(c);
138: }
139: pm.close();
140: return remove;
141: }
142:
143: public String createAccount(ClientId cid, String agencyName)
144: throws RemoteException {
145: PersistenceManager pm = pmf.getPersistenceManager();
146: Agency a = getBank(pm).getAgency(agencyName);
147: if (a == null) {
148: pm.close();
149: throw new RemoteException("No agency '" + agencyName
150: + "' found.");
151: }
152: Client c = a
153: .getClient(cid.firstName, cid.lastName, cid.address);
154: if (c == null) {
155: pm.close();
156: throw new RemoteException("No client '" + cid + "' found.");
157: }
158: String number = c.createAccount(a.getNextAccountNumber()).number;
159: pm.close();
160: return number;
161: }
162:
163: public AccountInfo getAccountInfo(String number, ClientId cid,
164: String agencyName) throws RemoteException {
165: PersistenceManager pm = pmf.getPersistenceManager();
166: Agency a = getBank(pm).getAgency(agencyName);
167: if (a == null) {
168: pm.close();
169: throw new RemoteException("No agency '" + agencyName
170: + "' found.");
171: }
172: Client c = a
173: .getClient(cid.firstName, cid.lastName, cid.address);
174: if (c == null) {
175: pm.close();
176: throw new RemoteException("No client '" + cid + "' found.");
177: }
178: Iterator it = c.accounts.iterator();
179: while (it.hasNext()) {
180: Account ac = (Account) it.next();
181: if (ac.number.equals(number)) {
182: return new AccountInfo(ac.number, ac.solde);
183: }
184: }
185: pm.close();
186: return null;
187: }
188:
189: public List getAccounts(String agencyName, ClientId cid)
190: throws RemoteException {
191: PersistenceManager pm = pmf.getPersistenceManager();
192: Agency a = getBank(pm).getAgency(agencyName);
193: if (a == null) {
194: pm.close();
195: throw new RemoteException("No agency '" + agencyName
196: + "' found.");
197: }
198: Client c = a
199: .getClient(cid.firstName, cid.lastName, cid.address);
200: if (c == null) {
201: pm.close();
202: throw new RemoteException("No client '" + cid + "' found.");
203: }
204: Iterator it = c.accounts.iterator();
205: ArrayList res = new ArrayList(c.accounts.size());
206: while (it.hasNext()) {
207: Account ac = (Account) it.next();
208: res.add(new AccountInfo(ac.number, ac.solde));
209: }
210: pm.close();
211: return res;
212: }
213:
214: public AccountInfo closeAccount(String agencyName, ClientId cid,
215: String accountNumber) throws RemoteException {
216: PersistenceManager pm = pmf.getPersistenceManager();
217: Agency a = getBank(pm).getAgency(agencyName);
218: if (a == null) {
219: pm.close();
220: throw new RemoteException("No agency '" + agencyName
221: + "' found.");
222: }
223: Client c = a
224: .getClient(cid.firstName, cid.lastName, cid.address);
225: if (c == null) {
226: pm.close();
227: throw new RemoteException("No client '" + cid + "' found.");
228: }
229: Iterator it = c.accounts.iterator();
230: while (it.hasNext()) {
231: Account ac = (Account) it.next();
232: if (ac.number.equals(accountNumber)) {
233: c.removeAccount(ac);
234: pm.deletePersistent(ac);
235: AccountInfo ai = new AccountInfo(ac.number, ac.solde);
236: pm.close();
237: return ai;
238: }
239: }
240: pm.close();
241: return null;
242: }
243:
244: // IMPLEMENTATION OF THE SessionBean INTERFACE //
245: //---------------------------------------------//
246:
247: public void setSessionContext(SessionContext sessionContext)
248: throws EJBException, RemoteException {
249: if (pmf == null) {
250: synchronized (BankApplicationImpl.class) {
251: if (pmf == null) {
252: try {
253: pmf = (PersistenceManagerFactory) new InitialContext()
254: .lookup(PMF_CTX_NAME);
255: } catch (NamingException e) {
256: throw new EJBException(
257: "No PersistenceMangerFactory registered"
258: + " in JNDI with the name '"
259: + PMF_CTX_NAME + "'");
260: }
261: }
262: }
263: }
264: }
265:
266: public void ejbCreate() throws RemoteException {
267: }
268:
269: public void ejbRemove() throws EJBException, RemoteException {
270: }
271:
272: public void ejbActivate() throws EJBException, RemoteException {
273: }
274:
275: public void ejbPassivate() throws EJBException, RemoteException {
276: }
277: }
|