1 27 28 29 package com.sun.ebank.appclient; 30 31 import java.util.ResourceBundle ; 32 import java.util.Date ; 33 import java.util.ArrayList ; 34 import java.math.BigDecimal ; 35 import com.sun.ebank.util.EJBGetter; 36 import javax.naming.InitialContext ; 37 import javax.swing.JTextField ; 38 import javax.swing.JOptionPane ; 39 import java.rmi.RemoteException ; 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 static CustomerController customer; 56 private static AccountController account; 57 58 private BankAdmin frame; 60 private ResourceBundle messages; 61 private int currentFunction; 62 private String returned; 63 private Date timestamp; 64 65 protected String first; 67 68 protected String last; 70 71 protected String mid; 73 74 protected String str; 76 77 protected String cty; 79 80 protected String st; 82 83 protected String zp; 85 86 protected String tel; 88 89 protected String mail; 91 92 protected String descrip; 94 95 protected String credit; 97 98 protected String type; 100 101 protected String bal; 103 104 protected String begbal; 106 107 protected String custID; 109 110 protected String actID; 112 protected BigDecimal balance; 113 protected BigDecimal creditline; 114 protected BigDecimal beginbalance; 115 protected BigDecimal bigzero = new BigDecimal ("0.00"); 116 protected boolean checkbal; 117 protected boolean checkbegbal; 118 119 public DataModel(BankAdmin frame, ResourceBundle messages) { 121 this.frame = frame; 122 this.messages = messages; 123 124 try { 126 CustomerControllerHome customerControllerHome = 127 EJBGetter.getCustomerControllerHome(); 128 customer = customerControllerHome.create(); 129 } catch (Exception NamingException) { 130 NamingException.printStackTrace(); 131 } 132 133 try { 135 AccountControllerHome accountControllerHome = 136 EJBGetter.getAccountControllerHome(); 137 account = accountControllerHome.create(); 138 } catch (Exception NamingException) { 139 NamingException.printStackTrace(); 140 } 141 } 142 143 private String getData(JTextField component) { 144 String text; 145 String 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 returned, int currentFunction) { 161 this.currentFunction = currentFunction; 162 this.returned = returned; 163 164 if (currentFunction == 6) { 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 { 181 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 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 if (this.bal != null) { 210 checkbal = bal.equals("0"); 211 } 212 213 if (checkbal == true) { 214 String begbalstring = frame.begbal.getText(); 215 216 this.bal = begbalstring; 218 } 219 220 balance = new BigDecimal (bal); 224 creditline = new BigDecimal (credit); 225 beginbalance = new BigDecimal (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 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) { 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 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) { 329 try { 330 custID = 331 customer.createCustomer(last, first, mid, str, cty, st, zp, 332 tel, mail); 333 334 return 0; 335 } catch (RemoteException 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) { 349 try { 350 timestamp = new Date (); 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 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) { 383 try { 384 account.addCustomerToAccount(custID, actID); 385 386 return 0; 387 } catch (RemoteException 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 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 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 returned) { 439 try { 440 ArrayList list = customer.getCustomersOfLastName(returned); 441 442 if (!list.isEmpty()) { 443 String 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 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 returned) { 458 AccountDetails details = null; 459 460 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 alist = new ArrayList (); 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 ex) { 480 frame.messlab.setText("Remote Exception" + ex.getMessage()); 481 } catch (InvalidParameterException ex) { 482 frame.messlab.setText("InvalidParameterException"); 483 } 484 } 485 486 if (currentFunction == 5) { 488 timestamp = new Date (); 489 frame.setDescription(null); 490 491 boolean readonly = false; 492 ArrayList alist = new ArrayList (); 493 frame.createActFields(readonly, null, bigzero, bigzero, bigzero, 494 alist, timestamp); 495 } 496 } 497 498 protected void createCustInf(int currentFunction, String returned) { 499 CustomerDetails details = null; 500 501 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 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 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 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 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 |