KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ebank > appclient > DataModel


1 /*
2  * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2004 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27
28
29 package com.sun.ebank.appclient;
30
31 import java.util.ResourceBundle JavaDoc;
32 import java.util.Date JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.math.BigDecimal JavaDoc;
35 import com.sun.ebank.util.EJBGetter;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.swing.JTextField JavaDoc;
38 import javax.swing.JOptionPane JavaDoc;
39 import java.rmi.RemoteException JavaDoc;
40 import com.sun.ebank.ejb.customer.CustomerController;
41 import com.sun.ebank.ejb.customer.CustomerControllerHome;
42 import com.sun.ebank.ejb.exception.CustomerNotFoundException;
43 import com.sun.ebank.ejb.account.AccountController;
44 import com.sun.ebank.ejb.account.AccountControllerHome;
45 import com.sun.ebank.ejb.exception.AccountNotFoundException;
46 import com.sun.ebank.ejb.exception.IllegalAccountTypeException;
47 import com.sun.ebank.ejb.exception.CustomerInAccountException;
48 import com.sun.ebank.ejb.exception.InvalidParameterException;
49 import com.sun.ebank.util.CustomerDetails;
50 import com.sun.ebank.util.AccountDetails;
51
52
53 public class DataModel {
54     //Private EJB variables
55
private static CustomerController customer;
56     private static AccountController account;
57
58     //Private instance variables
59
private BankAdmin frame;
60     private ResourceBundle JavaDoc messages;
61     private int currentFunction;
62     private String JavaDoc returned;
63     private Date JavaDoc timestamp;
64
65     //Protected instance variables
66
protected String JavaDoc first;
67
68     //Protected instance variables
69
protected String JavaDoc last;
70
71     //Protected instance variables
72
protected String JavaDoc mid;
73
74     //Protected instance variables
75
protected String JavaDoc str;
76
77     //Protected instance variables
78
protected String JavaDoc cty;
79
80     //Protected instance variables
81
protected String JavaDoc st;
82
83     //Protected instance variables
84
protected String JavaDoc zp;
85
86     //Protected instance variables
87
protected String JavaDoc tel;
88
89     //Protected instance variables
90
protected String JavaDoc mail;
91
92     //Protected instance variables
93
protected String JavaDoc descrip;
94
95     //Protected instance variables
96
protected String JavaDoc credit;
97
98     //Protected instance variables
99
protected String JavaDoc type;
100
101     //Protected instance variables
102
protected String JavaDoc bal;
103
104     //Protected instance variables
105
protected String JavaDoc begbal;
106
107     //Protected instance variables
108
protected String JavaDoc custID;
109
110     //Protected instance variables
111
protected String JavaDoc actID;
112     protected BigDecimal JavaDoc balance;
113     protected BigDecimal JavaDoc creditline;
114     protected BigDecimal JavaDoc beginbalance;
115     protected BigDecimal JavaDoc bigzero = new BigDecimal JavaDoc("0.00");
116     protected boolean checkbal;
117     protected boolean checkbegbal;
118
119     //Constructor
120
public DataModel(BankAdmin frame, ResourceBundle JavaDoc messages) {
121         this.frame = frame;
122         this.messages = messages;
123
124         //Look up and create CustomerController bean
125
try {
126             CustomerControllerHome customerControllerHome =
127                 EJBGetter.getCustomerControllerHome();
128             customer = customerControllerHome.create();
129         } catch (Exception JavaDoc NamingException) {
130             NamingException.printStackTrace();
131         }
132
133         //Look up and create AccountController bean
134
try {
135             AccountControllerHome accountControllerHome =
136                 EJBGetter.getAccountControllerHome();
137             account = accountControllerHome.create();
138         } catch (Exception JavaDoc NamingException) {
139             NamingException.printStackTrace();
140         }
141     }
142
143     private String JavaDoc getData(JTextField JavaDoc component) {
144         String JavaDoc text;
145         String JavaDoc trimmed;
146
147         if (component.getText()
148                          .length() > 0) {
149             text = component.getText();
150             trimmed = text.trim();
151
152             return trimmed;
153         } else {
154             text = null;
155
156             return text;
157         }
158     }
159
160     protected int checkActData(String JavaDoc returned, int currentFunction) {
161         this.currentFunction = currentFunction;
162         this.returned = returned;
163
164         if (currentFunction == 6) { //remove account
165
this.actID = getData(frame.account);
166             this.custID = getData(frame.customer);
167             frame.clearMessages(1);
168
169             if ((this.custID != null) && (this.actID != null)) {
170                 int success = writeData();
171
172                 return success;
173             } else {
174                 frame.messlab5.setText(messages.getString(
175                         "MissingRequiredException"));
176
177                 return 1;
178             }
179         } else { // create account
180

181             //Retrieve data from UI
182
this.descrip = getData(frame.descrip);
183             this.bal = getData(frame.bal);
184             this.credit = getData(frame.credit);
185             this.begbal = getData(frame.begbal);
186             this.custID = getData(frame.cust);
187
188             //Get type
189
if (frame.savingsact.isSelected()) {
190                 this.type = "Savings";
191             } else if (frame.checkingact.isSelected()) {
192                 this.type = "Checking";
193             } else if (frame.creditact.isSelected()) {
194                 this.type = "Credit";
195             } else if (frame.mnymktact.isSelected()) {
196                 this.type = "Money Market";
197             } else {
198                 this.type = null;
199             }
200
201             frame.clearMessages(1);
202
203             if (this.begbal != null) {
204                 checkbegbal = begbal.equals("0");
205             }
206
207             //See if user pressed Return after entering
208
//beginning balance
209
if (this.bal != null) {
210                 checkbal = bal.equals("0");
211             }
212
213             if (checkbal == true) {
214                 String JavaDoc begbalstring = frame.begbal.getText();
215
216                 //Assign beginning balance to balance
217
this.bal = begbalstring;
218             }
219
220             //Convert balance, begin balance, and credit line
221
//String values to BigDecimal types for
222
//writing to the database
223
balance = new BigDecimal JavaDoc(bal);
224             creditline = new BigDecimal JavaDoc(credit);
225             beginbalance = new BigDecimal JavaDoc(begbal);
226
227             if ((this.custID != null) &&
228                     (this.begbal != null) &&
229                     (this.type != null) &&
230                     (checkbegbal == false)) {
231                 int success = writeData();
232
233                 return success;
234             } else {
235                 frame.messlab5.setText(messages.getString(
236                         "MissingRequiredException"));
237
238                 return 1;
239             }
240         }
241     }
242
243     protected int checkCustData(String JavaDoc returned, int currentFunction) {
244         this.currentFunction = currentFunction;
245         this.returned = returned;
246
247         int i;
248         int j;
249         int k;
250
251         this.last = getData(frame.lname);
252         this.first = getData(frame.fname);
253         this.mid = getData(frame.mi);
254         this.str = getData(frame.street);
255         this.cty = getData(frame.city);
256         this.st = getData(frame.state);
257         this.zp = getData(frame.zip);
258         this.tel = getData(frame.phone);
259         this.mail = getData(frame.e);
260
261         frame.clearMessages(1);
262
263         if ((last != null) &&
264                 (first != null) &&
265                 (str != null) &&
266                 (cty != null) &&
267                 (st != null)) {
268             i = 0;
269         } else {
270             frame.messlab5.setText(messages.getString(
271                     "MissingRequiredException"));
272             i = 1;
273         }
274
275         if (frame.mi.getText()
276                         .length() > 1) {
277             frame.messlab4.setText(messages.getString("MILimitException"));
278             j = 1;
279         } else {
280             j = 0;
281         }
282
283         if (frame.state.getText()
284                            .length() > 2) {
285             frame.messlab3.setText(messages.getString("StateLimitException"));
286             k = 1;
287         } else {
288             k = 0;
289         }
290
291         if ((i == 0) && (j == 0) && (k == 0)) {
292             int success = writeData();
293
294             return success;
295         } else {
296             return 1;
297         }
298     }
299
300     private int writeData() {
301         if (currentFunction == 2) { //Update customer information
302

303             try {
304                 customer.setName(last, first, mid, returned);
305                 customer.setAddress(str, cty, st, zp, tel, mail, returned);
306
307                 return 0;
308             } catch (RemoteException JavaDoc ex) {
309                 frame.messlab.setText(messages.getString("RemoteException") +
310                     ex.getMessage());
311
312                 return 1;
313             } catch (InvalidParameterException ex) {
314                 frame.messlab.setText(messages.getString(
315                         "InvalidParameterException"));
316
317                 return 1;
318             } catch (CustomerNotFoundException ex) {
319                 frame.messlab2.setText(messages.getString("CustomerException") +
320                     " " + returned + " " +
321                     messages.getString("NotFoundException"));
322
323                 return 1;
324             }
325         }
326
327         if (currentFunction == 1) { //Add new customer information
328

329             try {
330                 custID =
331                     customer.createCustomer(last, first, mid, str, cty, st, zp,
332                         tel, mail);
333
334                 return 0;
335             } catch (RemoteException JavaDoc ex) {
336                 frame.messlab.setText(messages.getString("RemoteException"));
337
338                 return 1;
339             } catch (InvalidParameterException ex) {
340                 frame.messlab.setText(messages.getString(
341                         "InvalidParameterException"));
342
343                 return 1;
344             }
345         }
346
347         if (currentFunction == 5) { //Create New Account
348

349             try {
350                 timestamp = new Date JavaDoc();
351                 actID =
352                     account.createAccount(custID, type, descrip, balance,
353                         creditline, beginbalance, timestamp);
354                 System.out.println(actID);
355
356                 return 0;
357             } catch (CustomerNotFoundException ex) {
358                 frame.messlab2.setText(messages.getString("CustomerException") +
359                     " " + this.custID + " " +
360                     messages.getString("NotFoundException"));
361
362                 return 1;
363             } catch (RemoteException JavaDoc ex) {
364                 frame.messlab.setText(messages.getString("RemoteException") +
365                     ex.getMessage());
366
367                 return 1;
368             } catch (InvalidParameterException ex) {
369                 frame.messlab.setText(messages.getString(
370                         "InvalidParameterException"));
371
372                 return 1;
373             } catch (IllegalAccountTypeException ex) {
374                 frame.messlab3.setText(messages.getString(
375                         "IllegalAccountTypeException"));
376
377                 return 1;
378             }
379         }
380
381         if (currentFunction == 6) { //Add Customer to Account
382

383             try {
384                 account.addCustomerToAccount(custID, actID);
385
386                 return 0;
387             } catch (RemoteException JavaDoc ex) {
388                 frame.messlab.setText(messages.getString("RemoteException"));
389
390                 return 1;
391             } catch (InvalidParameterException ex) {
392                 frame.messlab.setText(messages.getString(
393                         "InvalidParameterException"));
394
395                 return 1;
396             } catch (CustomerNotFoundException ex) {
397                 frame.messlab2.setText(messages.getString("CustomerException") +
398                     " " + this.custID + " " +
399                     messages.getString("NotFoundException"));
400
401                 return 1;
402             } catch (AccountNotFoundException ex) {
403                 frame.messlab2.setText(messages.getString("AccountException") +
404                     " " + this.actID + " " +
405                     messages.getString("NotFoundException"));
406
407                 return 1;
408             } catch (CustomerInAccountException ex) {
409                 frame.messlab4.setText(messages.getString("CustomerException") +
410                     " " + this.custID + " " +
411                     messages.getString("CustomerInAccountException") + " " +
412                     this.actID);
413
414                 return 1;
415             }
416         }
417
418         return 0;
419     }
420
421     protected void removeAccount(String JavaDoc returned) {
422         try {
423             account.removeAccount(returned);
424             frame.messlab2.setText(messages.getString("AccountException") +
425                 " " + returned + " " + messages.getString("Removed"));
426         } catch (AccountNotFoundException ex) {
427             frame.messlab2.setText(messages.getString("AccountException") +
428                 " " + returned + " " + messages.getString("NotFoundException"));
429         } catch (RemoteException JavaDoc ex) {
430             frame.messlab.setText(messages.getString("RemoteException") +
431                 ex.getMessage());
432         } catch (InvalidParameterException ex) {
433             frame.messlab.setText(messages.getString(
434                     "InvalidParameterException"));
435         }
436     }
437
438     protected void searchByLastName(String JavaDoc returned) {
439         try {
440             ArrayList JavaDoc list = customer.getCustomersOfLastName(returned);
441
442             if (!list.isEmpty()) {
443                 String JavaDoc custID = ((CustomerDetails) list.get(0)).getCustomerId();
444                 JOptionPane.showMessageDialog(frame, custID, "Customer ID is:",
445                     JOptionPane.PLAIN_MESSAGE);
446             } else {
447                 frame.messlab.setText(returned + " " +
448                     messages.getString("NotFoundException"));
449             }
450         } catch (RemoteException JavaDoc ex) {
451             frame.messlab.setText("RemoteException" + ex.getMessage());
452         } catch (InvalidParameterException ex) {
453             frame.messlab.setText("InvalidParameterException");
454         }
455     }
456
457     protected void createActInf(int currentFunction, String JavaDoc returned) {
458         AccountDetails details = null;
459
460         //View Account Information
461
if ((currentFunction == 4) && (returned.length() > 0)) {
462             try {
463                 details = account.getDetails(returned);
464
465                 boolean readonly = true;
466                 frame.setDescription(details.getDescription());
467
468                 ArrayList JavaDoc alist = new ArrayList JavaDoc();
469                 alist = details.getCustomerIds();
470                 frame.createActFields(readonly, details.getType(),
471                     details.getBalance(), details.getCreditLine(),
472                     details.getBeginBalance(), alist,
473                     details.getBeginBalanceTimeStamp());
474             } catch (AccountNotFoundException ex) {
475                 frame.resetPanelTwo();
476                 frame.messlab3.setText(messages.getString("AccountException") +
477                     " " + returned + " " +
478                     messages.getString("NotFoundException"));
479             } catch (RemoteException JavaDoc ex) {
480                 frame.messlab.setText("Remote Exception" + ex.getMessage());
481             } catch (InvalidParameterException ex) {
482                 frame.messlab.setText("InvalidParameterException");
483             }
484         }
485
486         //Create Account Information
487
if (currentFunction == 5) {
488             timestamp = new Date JavaDoc();
489             frame.setDescription(null);
490
491             boolean readonly = false;
492             ArrayList JavaDoc alist = new ArrayList JavaDoc();
493             frame.createActFields(readonly, null, bigzero, bigzero, bigzero,
494                 alist, timestamp);
495         }
496     }
497
498     protected void createCustInf(int currentFunction, String JavaDoc returned) {
499         CustomerDetails details = null;
500
501         //View Customer Information
502
if ((currentFunction == 3) && (returned.length() > 0)) {
503             try {
504                 details = customer.getDetails(returned);
505
506                 boolean readonly = true;
507                 frame.createCustFields(true, details.getFirstName(),
508                     details.getLastName(), details.getMiddleInitial(),
509                     details.getStreet(), details.getCity(), details.getState(),
510                     details.getZip(), details.getPhone(), details.getEmail());
511             } catch (RemoteException JavaDoc ex) {
512                 frame.messlab.setText("Remote Exception" + ex.getMessage());
513             } catch (InvalidParameterException ex) {
514                 frame.messlab.setText("InvalidParameterException");
515             } catch (CustomerNotFoundException ex) {
516                 frame.resetPanelTwo();
517                 frame.messlab2.setText(messages.getString("CustomerException") +
518                     " " + returned + " " +
519                     messages.getString("NotFoundException"));
520             }
521         }
522
523         //Update Customer Information
524
if ((currentFunction == 2) && (returned.length() > 0)) {
525             try {
526                 details = customer.getDetails(returned);
527
528                 boolean readonly = false;
529                 frame.createCustFields(false, details.getFirstName(),
530                     details.getLastName(), details.getMiddleInitial(),
531                     details.getStreet(), details.getCity(), details.getState(),
532                     details.getZip(), details.getPhone(), details.getEmail());
533             } catch (RemoteException JavaDoc ex) {
534                 frame.messlab.setText("Remote Exception" + ex.getMessage());
535             } catch (InvalidParameterException ex) {
536                 frame.messlab.setText("InvalidParameterException");
537             } catch (CustomerNotFoundException ex) {
538                 frame.resetPanelTwo();
539                 frame.messlab2.setText(messages.getString("CustomerException") +
540                     " " + returned + " " +
541                     messages.getString("NotFoundException"));
542             }
543         }
544
545         //Create Customer Information
546
if (currentFunction == 1) {
547             boolean readonly = false;
548             frame.createCustFields(false, null, null, null, null, null, null,
549                 null, null, null);
550         }
551     }
552 }
553
Popular Tags