1 package sellwin.server; 2 3 import sellwin.domain.*; 4 import java.util.*; 5 import java.io.*; 6 7 11 17 public class LocalDB { 18 private String BASE; 19 public final static String EXTENSION = ".opp"; 20 public final static String F_INDEX = "index.objs"; 21 public final static String F_ROLES = "role.objs"; 22 public final static String F_GROPS = "group.objs"; 23 public final static String F_SPERS = "salesPerson.objs"; 24 public final static String F_PRODS = "product.objs"; 25 public final static String F_CAMPS = "campaign.objs"; 26 public final static String F_CUSTS = "customer.objs"; 27 public final static String F_TAX = "tax.objs"; 28 public final static String F_DELETE = "deleted.objs"; 29 30 35 public LocalDB(String base) { 36 BASE = base; 37 } 38 39 43 public final void setBASE(String b) { 44 BASE = b; 45 } 46 47 51 public final String getBASE() { 52 return BASE; 53 } 54 55 60 public final ArrayList getOppIndex() 61 throws AngError { 62 63 FileInputStream istream = null; 64 ArrayList oppIndex = null; 65 66 try { 67 istream = new FileInputStream(BASE + F_INDEX); 68 ObjectInputStream p = new ObjectInputStream(istream); 69 oppIndex = (ArrayList)p.readObject(); 70 } catch (FileNotFoundException f) { 71 System.out.println("creating " + BASE + F_INDEX); 73 oppIndex = new ArrayList(); 74 saveOppIndex(oppIndex); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 throw new AngError(e.getMessage()); 78 } finally { 79 try { istream.close(); } catch (Exception x) { } 80 } 81 82 return oppIndex; 83 84 } 85 86 91 public final void saveOppIndex(ArrayList oppIndex) 92 throws AngError { 93 94 FileOutputStream ostream = null; 95 96 try { 97 System.out.println("saving " + BASE + F_INDEX); 98 ostream = new FileOutputStream(BASE + F_INDEX); 99 ObjectOutputStream p = new ObjectOutputStream(ostream); 100 p.writeObject(oppIndex); 101 p.flush(); 102 } catch (Exception e) { 103 e.printStackTrace(); 104 throw new AngError(e.getMessage()); 105 } finally { 106 try { ostream.close(); } catch (Exception x) { } 107 } 108 } 109 110 116 public final Opportunity getOpportunity(String oppName) 117 throws AngError { 118 119 FileInputStream istream = null; 120 121 try { 122 istream = new FileInputStream(generateOppFileName(oppName)); 123 ObjectInputStream p = new ObjectInputStream(istream); 124 Opportunity o = (Opportunity)p.readObject(); 125 return o; 126 } catch (Exception e) { 127 e.printStackTrace(); 128 throw new AngError(e.getMessage()); 129 } finally { 130 try { istream.close(); } catch (Exception x) { } 131 } 132 } 133 134 140 public final Opportunity getOpportunity(long pk) 141 throws AngError { 142 143 FileInputStream istream = null; 144 145 try { 146 istream = new FileInputStream(generateOppFileName(pk)); 147 ObjectInputStream p = new ObjectInputStream(istream); 148 Opportunity o = (Opportunity)p.readObject(); 149 return o; 150 } catch (Exception e) { 151 e.printStackTrace(); 152 throw new AngError(e.getMessage()); 153 } finally { 154 try { istream.close(); } catch (Exception x) { } 155 } 156 } 157 158 166 public int findOpp(ArrayList index, long pk) { 167 168 OppIndex oi = null; 169 for (int i=0;i<index.size();i++) { 170 oi = (OppIndex)index.get(i); 171 if (oi.getPK() == pk) 172 return i; 173 } 174 175 return -1; } 177 178 183 public final void saveOpportunity(Opportunity o) 184 throws AngError { 185 186 FileOutputStream ostream = null; 187 188 try { 189 System.out.println("saving "+ generateOppFileName(o.getPK())); 190 ostream = new FileOutputStream(generateOppFileName(o.getPK())); 191 ObjectOutputStream p = new ObjectOutputStream(ostream); 192 p.writeObject(o); 193 p.flush(); 194 195 ArrayList index = getOppIndex(); 197 int i = findOpp(index, o.getPK()); 198 OppIndex oi = null; 199 if (i >= 0) { oi = (OppIndex)index.get(i); 201 oi.setName(o.getName()); 202 } else { oi = new OppIndex(o.getName(), o.getStage(), o.getPK()); 204 index.add(oi); 205 } 206 saveOppIndex(index); 207 } catch (Exception e) { 208 e.printStackTrace(); 209 throw new AngError(e.getMessage()); 210 } finally { 211 try { ostream.close(); } catch (Exception x) { } 212 } 213 } 214 215 221 private OppIndex getOppIndex(String oppName) 222 throws AngError { 223 OppIndex oi = null; 224 ArrayList index = getOppIndex(); 225 for (int i=0;i<index.size();i++) { 226 oi = (OppIndex)index.get(i); 227 if (oi.getName().equals(oppName)) 228 return oi; 229 } 230 231 throw new AngError("not found not correct"); 232 } 233 234 240 public final String generateOppFileName(String oppName) 241 throws AngError { 242 243 ArrayList index = getOppIndex(); 244 OppIndex oi = getOppIndex(oppName); 245 return generateOppFileName(oi.getPK()); 246 } 247 248 255 public final String generateOppFileName(long pk) { 256 return new String (BASE + pk + EXTENSION); 257 } 258 259 264 public final ArrayList getRoleList() 265 throws AngError { 266 267 FileInputStream istream=null; 268 ArrayList roleList = null; 269 270 try { 271 istream = new FileInputStream(BASE + F_ROLES); 272 ObjectInputStream p = new ObjectInputStream(istream); 273 roleList = (ArrayList)p.readObject(); 274 } catch (FileNotFoundException f) { 275 System.out.println("creating " + BASE + F_ROLES); 276 roleList = new ArrayList(); 277 saveRoleList(roleList); 278 } catch (Exception e) { 279 e.printStackTrace(); 280 throw new AngError(e.getMessage()); 281 } finally { 282 try { istream.close(); } catch (Exception x) { } 283 } 284 return roleList; 285 } 286 287 292 public final void saveRoleList(ArrayList roles) 293 throws AngError { 294 295 FileOutputStream ostream = null; 296 297 try { 298 System.out.println("saving " + BASE + F_ROLES); 299 ostream = new FileOutputStream(BASE + F_ROLES); 300 ObjectOutputStream p = new ObjectOutputStream(ostream); 301 p.writeObject(roles); 302 p.flush(); 303 } catch (Exception e) { 304 throw new AngError(e.getMessage()); 305 } finally { 306 try { ostream.close(); } catch (Exception x) { } 307 } 308 } 309 310 315 public final ArrayList getGroupList() 316 throws AngError { 317 318 FileInputStream istream = null; 319 ArrayList groupList = null; 320 321 try { 322 istream = new FileInputStream(BASE + F_GROPS); 323 ObjectInputStream p = new ObjectInputStream(istream); 324 groupList = (ArrayList)p.readObject(); 325 } catch (FileNotFoundException f) { 326 System.out.println("creating " + BASE + F_GROPS); 327 groupList = new ArrayList(); 328 saveGroupList(groupList); 329 } catch (Exception e) { 330 e.printStackTrace(); 331 throw new AngError(e.getMessage()); 332 } finally { 333 try { istream.close(); } catch (Exception x) { } 334 } 335 return groupList; 336 } 337 338 343 public final void saveGroupList(ArrayList groups) 344 throws AngError { 345 346 FileOutputStream ostream = null; 347 348 try { 349 System.out.println("saving " + BASE + F_GROPS); 350 ostream = new FileOutputStream(BASE + F_GROPS); 351 ObjectOutputStream p = new ObjectOutputStream(ostream); 352 p.writeObject(groups); 353 p.flush(); 354 } catch (Exception e) { 355 throw new AngError(e.getMessage()); 356 } finally { 357 try { ostream.close(); } catch (Exception x) { } 358 } 359 } 360 361 366 public final ArrayList getProducts() 367 throws AngError { 368 369 FileInputStream istream = null; 370 ArrayList prodList = null; 371 372 try { 373 istream = new FileInputStream(BASE + F_PRODS); 374 ObjectInputStream p = new ObjectInputStream(istream); 375 prodList = (ArrayList)p.readObject(); 376 } catch (FileNotFoundException f) { 377 System.out.println("creating " + BASE + F_PRODS); 378 prodList = new ArrayList(); 379 saveProducts(prodList); 380 } catch (Exception e) { 381 e.printStackTrace(); 382 throw new AngError(e.getMessage()); 383 } finally { 384 try { istream.close(); } catch (Exception x) { } 385 } 386 return prodList; 387 } 388 389 394 public final void saveProducts(ArrayList prods) 395 throws AngError { 396 397 FileOutputStream ostream = null; 398 399 try { 400 ostream = new FileOutputStream(BASE + F_PRODS); 401 ObjectOutputStream p = new ObjectOutputStream(ostream); 402 p.writeObject(prods); 403 p.flush(); 404 } catch (Exception e) { 405 throw new AngError(e.getMessage()); 406 } finally { 407 try { ostream.close(); } catch (Exception x) { } 408 } 409 } 410 411 416 public final TreeMap getSalesPersons() 417 throws AngError { 418 419 FileInputStream istream = null; 420 TreeMap salesPersonList = null; 421 422 try { 423 istream = new FileInputStream(BASE + F_SPERS); 424 ObjectInputStream p = new ObjectInputStream(istream); 425 salesPersonList = (TreeMap)p.readObject(); 426 } catch (FileNotFoundException f) { 427 System.out.println("creating " + BASE + F_SPERS); 428 salesPersonList = new TreeMap(); 429 saveSalesPersons(salesPersonList); 430 } catch (Exception e) { 431 e.printStackTrace(); 432 throw new AngError(e.getMessage()); 433 } finally { 434 try { istream.close(); } catch (Exception x) { } 435 } 436 return salesPersonList; 437 } 438 439 444 public final void saveSalesPersons(TreeMap persons) 445 throws AngError { 446 447 FileOutputStream ostream = null; 448 449 try { 450 ostream = new FileOutputStream(BASE + F_SPERS); 451 ObjectOutputStream p = new ObjectOutputStream(ostream); 452 p.writeObject(persons); 453 p.flush(); 454 } catch (Exception e) { 455 throw new AngError(e.getMessage()); 456 } finally { 457 try { ostream.close(); } catch (Exception x) { } 458 } 459 } 460 461 466 public final ArrayList getCampaigns() 467 throws AngError { 468 469 FileInputStream istream = null; 470 ArrayList campaigns = null; 471 472 try { 473 istream = new FileInputStream(BASE + F_CAMPS); 474 ObjectInputStream p = new ObjectInputStream(istream); 475 campaigns = (ArrayList)p.readObject(); 476 } catch (FileNotFoundException f) { 477 campaigns = new ArrayList(); 478 saveCampaigns(campaigns); 479 } catch (Exception e) { 480 e.printStackTrace(); 481 throw new AngError(e.getMessage()); 482 } finally { 483 try { istream.close(); } catch (Exception x) { } 484 } 485 return campaigns; 486 } 487 488 493 public final void saveCampaigns(ArrayList campaigns) 494 throws AngError { 495 496 FileOutputStream ostream = null; 497 498 try { 499 ostream = new FileOutputStream(BASE + F_CAMPS); 500 ObjectOutputStream p = new ObjectOutputStream(ostream); 501 p.writeObject(campaigns); 502 p.flush(); 503 } catch (Exception e) { 504 throw new AngError(e.getMessage()); 505 } finally { 506 try { ostream.close(); } catch (Exception x) { } 507 } 508 } 509 510 515 public final ArrayList getCustomers() 516 throws AngError { 517 518 FileInputStream istream = null; 519 ArrayList custList = null; 520 521 try { 522 istream = new FileInputStream(BASE + F_CUSTS); 523 ObjectInputStream p = new ObjectInputStream(istream); 524 custList = (ArrayList)p.readObject(); 525 } catch (FileNotFoundException f) { 526 System.out.println("creating " + BASE + F_CUSTS); 527 custList = new ArrayList(); 528 saveCustomers(custList); 529 } catch (Exception e) { 530 e.printStackTrace(); 531 throw new AngError(e.getMessage()); 532 } finally { 533 try { istream.close(); } catch (Exception x) { } 534 } 535 return custList; 536 } 537 538 543 public final void saveCustomers(ArrayList custs) 544 throws AngError { 545 546 FileOutputStream ostream = null; 547 548 try { 549 ostream = new FileOutputStream(BASE + F_CUSTS); 550 ObjectOutputStream p = new ObjectOutputStream(ostream); 551 p.writeObject(custs); 552 p.flush(); 553 } catch (Exception e) { 554 throw new AngError(e.getMessage()); 555 } finally { 556 try { ostream.close(); } catch (Exception x) { } 557 } 558 } 559 560 567 public final void writeDelete(DeleteInfo deleteInfo) 568 throws AngError { 569 570 FileInputStream istream = null; 571 ArrayList deleteList = null; 572 573 try { 574 istream = new FileInputStream(BASE + F_DELETE); 576 ObjectInputStream p = new ObjectInputStream(istream); 577 deleteList = (ArrayList)p.readObject(); 578 579 deleteList.add(deleteInfo); 581 582 saveDeletes(deleteList); 584 585 } catch (FileNotFoundException f) { 586 System.out.println("creating " + BASE + F_DELETE); 587 deleteList = new ArrayList(); 588 deleteList.add(deleteInfo); 589 saveDeletes(deleteList); 590 } catch (Exception e) { 591 e.printStackTrace(); 592 throw new AngError(e.getMessage()); 593 } finally { 594 try { istream.close(); } catch (Exception x) { } 595 } 596 } 597 598 602 public void saveDeletes(ArrayList deletes) 603 throws AngError { 604 605 FileOutputStream ostream = null; 606 try { 607 ostream = new FileOutputStream(BASE + F_DELETE); 608 ObjectOutputStream p = new ObjectOutputStream(ostream); 609 p.writeObject(deletes); 610 p.flush(); 611 } catch (Exception e) { 612 throw new AngError(e.getMessage()); 613 } finally { 614 try { ostream.close(); } catch (Exception x) { } 615 } 616 } 617 618 public final ArrayList getDeletes() 619 throws AngError { 620 621 FileInputStream istream = null; 622 ArrayList deletes = null; 623 624 try { 625 istream = new FileInputStream(BASE + F_DELETE); 626 ObjectInputStream p = new ObjectInputStream(istream); 627 deletes = (ArrayList)p.readObject(); 628 } catch (FileNotFoundException f) { 629 deletes = new ArrayList(); 630 saveDeletes(deletes); 631 } catch (Exception e) { 632 e.printStackTrace(); 633 throw new AngError(e.getMessage()); 634 } finally { 635 try { istream.close(); } catch (Exception x) { } 636 } 637 return deletes; 638 } 639 640 645 public final ArrayList getStateTax() 646 throws AngError { 647 648 FileInputStream istream = null; 649 ArrayList taxList = null; 650 651 try { 652 istream = new FileInputStream(BASE + F_TAX); 653 ObjectInputStream p = new ObjectInputStream(istream); 654 taxList = (ArrayList)p.readObject(); 655 } catch (FileNotFoundException f) { 656 System.out.println("creating " + BASE + F_TAX); 657 taxList = new ArrayList(); 658 saveStateTax(taxList); 659 } catch (Exception e) { 660 e.printStackTrace(); 661 throw new AngError(e.getMessage()); 662 } finally { 663 try { istream.close(); } catch (Exception x) { } 664 } 665 return taxList; 666 } 667 668 673 public final void saveStateTax(ArrayList tax) 674 throws AngError { 675 676 FileOutputStream ostream = null; 677 678 try { 679 ostream = new FileOutputStream(BASE + F_TAX); 680 ObjectOutputStream p = new ObjectOutputStream(ostream); 681 p.writeObject(tax); 682 p.flush(); 683 } catch (Exception e) { 684 throw new AngError(e.getMessage()); 685 } finally { 686 try { ostream.close(); } catch (Exception x) { } 687 } 688 } 689 } 690 | Popular Tags |