KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bank > BankApplicationImpl


1 package bank;
2
3 import javax.ejb.SessionBean JavaDoc;
4 import javax.ejb.SessionContext JavaDoc;
5 import javax.ejb.EJBException JavaDoc;
6 import javax.jdo.PersistenceManagerFactory;
7 import javax.jdo.PersistenceManager;
8 import javax.jdo.Query;
9 import javax.naming.InitialContext JavaDoc;
10 import javax.naming.NamingException JavaDoc;
11 import java.rmi.RemoteException JavaDoc;
12 import java.util.List JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.ArrayList JavaDoc;
16
17 /**
18  * @author S.Chassande-Barrioz
19  */

20 public class BankApplicationImpl implements SessionBean JavaDoc {
21
22     protected SessionContext JavaDoc sessionContext = null;
23
24     public static PersistenceManagerFactory pmf = null;
25
26     private static Bank bank = null;
27
28     private static final String JavaDoc SOCIETE_GENERALE = "SG";
29
30     private static final String JavaDoc 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 JavaDoc c = (Collection JavaDoc) q.execute(SOCIETE_GENERALE);
44             if (c.isEmpty()) {
45                 //First execution: create the bank
46
bank = new Bank(SOCIETE_GENERALE);
47                 pm.makePersistent(bank);
48             } else {
49                 //Load from the persistence support
50
bank = (Bank) c.iterator().next();
51             }
52             q.closeAll();
53         }
54         return bank;
55     }
56
57     // IMPLEMENTATION OF THE BankApplication INTERFACE //
58
//-------------------------------------------------//
59

60     public void createAgency(String JavaDoc name) throws RemoteException JavaDoc {
61         PersistenceManager pm = pmf.getPersistenceManager();
62         getBank(pm).createAgency(name);
63         pm.close();
64     }
65
66     public List JavaDoc getAgencies() throws RemoteException JavaDoc {
67         PersistenceManager pm = pmf.getPersistenceManager();
68         Iterator JavaDoc it = getBank(pm).agencies.iterator();
69         ArrayList JavaDoc res = new ArrayList JavaDoc(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 JavaDoc name) throws RemoteException JavaDoc {
78         PersistenceManager pm = pmf.getPersistenceManager();
79         Iterator JavaDoc 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 JavaDoc agencyName)
94             throws RemoteException JavaDoc {
95         PersistenceManager pm = pmf.getPersistenceManager();
96         Agency a = getBank(pm).getAgency(agencyName);
97         if (a == null) {
98             pm.close();
99             throw new RemoteException JavaDoc("No agency '" + agencyName + "' found.");
100         }
101         a.createClient(cid.firstName, cid.lastName, cid.address);
102         pm.close();
103     }
104
105     public List JavaDoc getClients(String JavaDoc agencyName) throws RemoteException JavaDoc {
106         PersistenceManager pm = pmf.getPersistenceManager();
107         Agency a = getBank(pm).getAgency(agencyName);
108         if (a == null) {
109             pm.close();
110             throw new RemoteException JavaDoc("No agency '" + agencyName + "' found.");
111         }
112         Iterator JavaDoc it = a.clients.iterator();
113         ArrayList JavaDoc res = new ArrayList JavaDoc(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 JavaDoc agencyName, ClientId cid)
122             throws RemoteException JavaDoc {
123         PersistenceManager pm = pmf.getPersistenceManager();
124         Agency a = getBank(pm).getAgency(agencyName);
125         if (a == null) {
126             pm.close();
127             throw new RemoteException JavaDoc("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 JavaDoc createAccount(ClientId cid, String JavaDoc agencyName)
140             throws RemoteException JavaDoc {
141         PersistenceManager pm = pmf.getPersistenceManager();
142         Agency a = getBank(pm).getAgency(agencyName);
143         if (a == null) {
144             pm.close();
145             throw new RemoteException JavaDoc("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 JavaDoc("No client '" + cid + "' found.");
151         }
152         String JavaDoc number = c.createAccount(a.getNextAccountNumber()).number;
153         pm.close();
154         return number;
155     }
156
157     public AccountInfo getAccountInfo(String JavaDoc number, ClientId cid,
158             String JavaDoc agencyName) throws RemoteException JavaDoc {
159         PersistenceManager pm = pmf.getPersistenceManager();
160         Agency a = getBank(pm).getAgency(agencyName);
161         if (a == null) {
162             pm.close();
163             throw new RemoteException JavaDoc("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 JavaDoc("No client '" + cid + "' found.");
169         }
170         Iterator JavaDoc 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 JavaDoc getAccounts(String JavaDoc agencyName, ClientId cid)
182             throws RemoteException JavaDoc {
183         PersistenceManager pm = pmf.getPersistenceManager();
184         Agency a = getBank(pm).getAgency(agencyName);
185         if (a == null) {
186             pm.close();
187             throw new RemoteException JavaDoc("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 JavaDoc("No client '" + cid + "' found.");
193         }
194         Iterator JavaDoc it = c.accounts.iterator();
195         ArrayList JavaDoc res = new ArrayList JavaDoc(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 JavaDoc agencyName, ClientId cid,
205             String JavaDoc accountNumber) throws RemoteException JavaDoc {
206         PersistenceManager pm = pmf.getPersistenceManager();
207         Agency a = getBank(pm).getAgency(agencyName);
208         if (a == null) {
209             pm.close();
210             throw new RemoteException JavaDoc("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 JavaDoc("No client '" + cid + "' found.");
216         }
217         Iterator JavaDoc 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     // IMPLEMENTATION OF THE SessionBean INTERFACE //
233
//---------------------------------------------//
234

235     public void setSessionContext(SessionContext JavaDoc sessionContext)
236             throws EJBException JavaDoc, RemoteException JavaDoc {
237         if (pmf == null) {
238             synchronized (BankApplicationImpl.class) {
239                 if (pmf == null) {
240                     try {
241                         pmf = (PersistenceManagerFactory) new InitialContext JavaDoc()
242                                 .lookup(PMF_CTX_NAME);
243                     } catch (NamingException JavaDoc e) {
244                         throw new EJBException JavaDoc(
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 JavaDoc {
255     }
256
257     public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc {
258     }
259
260     public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc {
261     }
262
263     public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc {
264     }
265 }
Popular Tags