/src/mpv5/db/objects/Contact.java
Java | 652 lines | 381 code | 82 blank | 189 comment | 31 complexity | 700b9ab936ce3b1ba3eb4eecad6843b6 MD5 | raw file
1package mpv5.db.objects; 2 3import enoa.handler.TemplateHandler; 4import java.util.Collections; 5import java.util.Comparator; 6import java.util.HashMap; 7import java.util.List; 8import javax.swing.JComponent; 9import mpv5.db.common.Context; 10import mpv5.db.common.DatabaseObject; 11import mpv5.db.common.Formattable; 12import mpv5.db.common.Templateable; 13import mpv5.globals.Constants; 14import mpv5.globals.Messages; 15import mpv5.handler.FormatHandler; 16import mpv5.i18n.LanguageManager; 17import mpv5.logging.Log; 18import mpv5.ui.panels.ContactPanel; 19import mpv5.utils.images.MPIcon; 20 21/** 22 * 23 * 24 */ 25public class Contact extends DatabaseObject implements Formattable, Templateable { 26 27 private static final long serialVersionUID = 1L; 28 private String cnumber = ""; 29 private String taxnumber = ""; 30 private String title = ""; 31 private String prename = ""; 32 private String street = ""; 33 private String zip = ""; 34 private String city = ""; 35 private String mainphone = ""; 36 private String workphone = ""; 37 private String fax = ""; 38 private String mobilephone = ""; 39 private String mailaddress = ""; 40 private String website = ""; 41 private String notes = ""; 42 private String company = ""; 43 private String department = ""; 44 private String bankaccount = ""; 45 private String bankid = ""; 46 private String bankname = ""; 47 private String bankcurrency = ""; 48 private String bankcountry = ""; 49 private boolean ismale = true; 50 private boolean isenabled = true; 51 private boolean iscompany = false; 52 private boolean iscustomer = false; 53 private boolean ismanufacturer = false; 54 private boolean issupplier = false; 55 private String country = ""; 56 private FormatHandler formatHandler; 57 public final static int TYPE_CONTACT = 0; 58 public final static int TYPE_CUSTOMER = 1; 59 public final static int TYPE_SUPPLIER = 2; 60 public final static int TYPE_MANUFACTURER = 3; 61 62 public static String getTypeString(int typ) { 63 switch (typ) { 64 case TYPE_CONTACT: 65 return Messages.TYPE_CONTACT.getValue(); 66 case TYPE_CUSTOMER: 67 return Messages.TYPE_CUSTOMER.getValue(); 68 case TYPE_SUPPLIER: 69 return Messages.TYPE_SUPPLIER.getValue(); 70 case TYPE_MANUFACTURER: 71 return Messages.TYPE_MANUFACTURER.getValue(); 72 } 73 return Messages.TYPE_CONTACT.getValue(); 74 } 75 76 public Contact() { 77 setContext(Context.getContact()); 78 } 79 80 /** 81 * @return the number 82 */ 83 public String __getCNumber() { 84 return cnumber; 85 } 86 87 /** 88 * @param number the number to set 89 */ 90 public void setCNumber(String number) { 91 this.cnumber = number; 92 } 93 94 /** 95 * @return the taxnumber 96 */ 97 public String __getTaxnumber() { 98 return taxnumber; 99 } 100 101 /** 102 * @param taxnumber 103 */ 104 public void settaxnumber(String taxnumber) { 105 this.taxnumber = taxnumber; 106 } 107 108 /** 109 * @return the title 110 */ 111 public String __getTitle() { 112 return title; 113 } 114 115 /** 116 * @param title the title to set 117 */ 118 public void setTitle(String title) { 119 this.title = title; 120 } 121 122 /** 123 * @return the prename 124 */ 125 public String __getPrename() { 126 return prename; 127 } 128 129 /** 130 * @param prename the prename to set 131 */ 132 public void setPrename(String prename) { 133 this.prename = prename; 134 } 135 136 /** 137 * @return the street 138 */ 139 public String __getStreet() { 140 return street; 141 } 142 143 /** 144 * @param street the street to set 145 */ 146 public void setStreet(String street) { 147 this.street = street; 148 } 149 150 /** 151 * @return the zip 152 */ 153 public String __getZip() { 154 return zip; 155 } 156 157 /** 158 * @param zip the zip to set 159 */ 160 public void setZip(String zip) { 161 this.zip = zip; 162 } 163 164 /** 165 * @return the city 166 */ 167 public String __getCity() { 168 return city; 169 } 170 171 /** 172 * @param city the city to set 173 */ 174 public void setCity(String city) { 175 this.city = city; 176 } 177 178 /** 179 * @return the phone 180 */ 181 public String __getMainphone() { 182 return mainphone; 183 } 184 185 /** 186 * @param phone the phone to set 187 */ 188 public void setMainphone(String phone) { 189 this.mainphone = phone; 190 } 191 192 /** 193 * @return the workphone 194 */ 195 public String __getWorkphone() { 196 return workphone; 197 } 198 199 /** 200 * @param workphone the workphone to set 201 */ 202 public void setWorkphone(String workphone) { 203 this.workphone = workphone; 204 } 205 206 /** 207 * @return the fax 208 */ 209 public String __getFax() { 210 return fax; 211 } 212 213 /** 214 * @param fax the fax to set 215 */ 216 public void setFax(String fax) { 217 this.fax = fax; 218 } 219 220 /** 221 * @return the mobilephone 222 */ 223 public String __getMobilephone() { 224 return mobilephone; 225 } 226 227 /** 228 * @param mobilephone the mobilephone to set 229 */ 230 public void setMobilephone(String mobilephone) { 231 this.mobilephone = mobilephone; 232 } 233 234 /** 235 * @return the mailaddress 236 */ 237 public String __getMailaddress() { 238 return mailaddress; 239 } 240 241 /** 242 * @param mailaddress the mailaddress to set 243 */ 244 public void setMailaddress(String mailaddress) { 245 this.mailaddress = mailaddress; 246 } 247 248 /** 249 * @return the website 250 */ 251 public String __getWebsite() { 252 return website; 253 } 254 255 /** 256 * @param website the website to set 257 */ 258 public void setWebsite(String website) { 259 this.website = website; 260 } 261 262 /** 263 * @return the notes 264 */ 265 public String __getNotes() { 266 return notes; 267 } 268 269 /** 270 * @param notes the notes to set 271 */ 272 public void setNotes(String notes) { 273 this.notes = notes; 274 } 275 276 /** 277 * @return the male 278 */ 279 public boolean __getisMale() { 280 return ismale; 281 } 282 283 /** 284 * @param male the male to set 285 */ 286 public void setisMale(boolean male) { 287 this.ismale = male; 288 } 289 290 /** 291 * @return the enabled 292 */ 293 public boolean __getisEnabled() { 294 return isenabled; 295 } 296 297 /** 298 * @param enabled the enabled to set 299 */ 300 public void setisEnabled(boolean enabled) { 301 this.isenabled = enabled; 302 } 303 304 /** 305 * @return the company 306 */ 307 public boolean __getisCompany() { 308 return iscompany; 309 } 310 311 /** 312 * @param company the company to set 313 */ 314 public void setisCompany(boolean company) { 315 this.iscompany = company; 316 } 317 318 /** 319 * @return the iscustomer 320 */ 321 public boolean __getIscustomer() { 322 return iscustomer; 323 } 324 325 /** 326 * @param iscustomer the iscustomer to set 327 */ 328 public void setisCustomer(boolean iscustomer) { 329 this.iscustomer = iscustomer; 330 } 331 332 /** 333 * @return the ismanufacturer 334 */ 335 public boolean __getIsmanufacturer() { 336 return ismanufacturer; 337 } 338 339 /** 340 * @param ismanufacturer the ismanufacturer to set 341 */ 342 public void setisManufacturer(boolean ismanufacturer) { 343 this.ismanufacturer = ismanufacturer; 344 } 345 346 /** 347 * @return the issupplier 348 */ 349 public boolean __getIssupplier() { 350 return issupplier; 351 } 352 353 /** 354 * @param issupplier the issupplier to set 355 */ 356 public void setisSupplier(boolean issupplier) { 357 this.issupplier = issupplier; 358 } 359 360 /** 361 * @return the company 362 */ 363 public String __getCompany() { 364 return company; 365 } 366 367 /** 368 * @param company the company to set 369 */ 370 public void setCompany(String company) { 371 this.company = company; 372 } 373 374 /** 375 * @return the department 376 */ 377 public String __getDepartment() { 378 return department; 379 } 380 381 /** 382 * @param dep 383 */ 384 public void setDepartment(String dep) { 385 this.department = dep; 386 } 387 388 /** 389 * @return the country 390 */ 391 public String __getCountry() { 392 return country; 393 } 394 395 /** 396 * @param country the country to set 397 */ 398 public void setCountry(String country) { 399 this.country = country; 400 } 401 402 @Override 403 public JComponent getView() { 404 ContactPanel x = new ContactPanel(getContext()); 405 return (JComponent) x; 406 } 407 408 @Override 409 public MPIcon getIcon() { 410 return new MPIcon("/mpv5/resources/images/22/evolution-contacts.png"); 411 } 412 413 @Override 414 public String toString() { 415 return getCname() + " (" + cnumber + ")"; 416 } 417 418 /** 419 * @return the formatHandler 420 */ 421 @Override 422 public FormatHandler getFormatHandler() { 423 if (formatHandler == null) { 424 formatHandler = new FormatHandler(this); 425 } 426 return formatHandler; 427 } 428 429 @Override 430 public void ensureUniqueness() { 431// if (cnumber == null || cnumber.length() == 0) { 432 setCNumber(getFormatHandler().next()); 433// } 434 } 435 436 @Override 437 public HashMap<String, Object> resolveReferences(HashMap<String, Object> map) { 438 super.resolveReferences(map); 439 List<Address> data; 440 try { 441 data = DatabaseObject.getReferencedObjects(this, Context.getAddress(), new Address()); 442 Collections.sort(data, new Comparator<Address>() { 443//[0 = billing adress, 1 = delivery adress, 2 = both, 3 = undefined] 444 445 public int compare(Address o1, Address o2) { 446 return Integer.valueOf(o1.__getInttype()).compareTo(Integer.valueOf(o2.__getInttype())); 447 } 448 }); 449 boolean hasInvAdd = false; 450 boolean hasDelAdd = false; 451 for (int i = 0; i < data.size(); i++) { 452 map.put("address" + i, data.get(i)); 453 if (!hasInvAdd && (data.get(i).__getInttype() == 0 || data.get(i).__getInttype() == 2)) { 454 map.put("invoiceaddress", data.get(i)); 455 hasInvAdd = true; 456 } 457 if (!hasDelAdd && (data.get(i).__getInttype() == 1 || data.get(i).__getInttype() == 2)) { 458 map.put("deliveryaddress", data.get(i)); 459 hasDelAdd = true; 460 } 461 } 462 if (!hasDelAdd || !hasInvAdd) { 463 Address def = new Address(); 464 def.setCity(city); 465 def.setCountry(country); 466 def.setDepartment(department); 467 def.setCompany(company); 468 def.setCname(cnumber); 469 def.setIsmale(ismale); 470 def.setPrename(prename); 471 def.setStreet(street); 472 def.setTaxnumber(taxnumber); 473 def.setTitle(title); 474 def.setZip(zip); 475 if (!hasInvAdd) { 476 map.put("invoiceaddress", def); 477 hasInvAdd = true; 478 } 479 if (!hasDelAdd) { 480 map.put("deliveryaddress", def); 481 hasDelAdd = true; 482 } 483 } 484 } catch (Exception ex) { 485 Log.Debug(this, ex.getMessage()); 486 } 487 488 if (map.containsKey("country")) { 489 try { 490 map.put("country", LanguageManager.getCountryName(Integer.valueOf(map.get("country").toString()))); 491 } catch (Exception numberFormatException) { 492 //already resolved? 493 } 494 } 495 496 try { 497 if (map.containsKey("ismale")) { 498 if (Boolean.valueOf(map.get("ismale").toString())) { 499 map.put("gender", Messages.CONTACT_TYPE_MALE.toString()); 500 map.put("intro", Messages.CONTACT_INTRO_MALE.toString()); 501 } else { 502 map.put("gender", Messages.CONTACT_TYPE_FEMALE.toString()); 503 map.put("intro", Messages.CONTACT_INTRO_FEMALE.toString()); 504 } 505 506 } 507 } catch (Exception numberFormatException) { 508 //already resolved? 509 } 510 511 if (!map.containsKey("fullname")) { 512 try { 513 if (prename != null && prename.length() > 0) { 514 map.put("fullname", prename + " " + getCname()); 515 } else { 516 map.put("fullname", getCname()); 517 } 518 } catch (Exception numberFormatException) { 519 //already resolved? 520 } 521 } 522 523 return map; 524 } 525 526 public void defineFormatHandler(FormatHandler handler) { 527 formatHandler = handler; 528 } 529 530 /** 531 * @return the bankaccount 532 */ 533 public String __getBankaccount() { 534 return bankaccount; 535 } 536 537 /** 538 * @param bankaccount the bankaccount to set 539 */ 540 public void setBankaccount(String bankaccount) { 541 this.bankaccount = bankaccount; 542 } 543 544 /** 545 * @return the bankid 546 */ 547 public String __getBankid() { 548 return bankid; 549 } 550 551 /** 552 * @param bankid the bankid to set 553 */ 554 public void setBankid(String bankid) { 555 this.bankid = bankid; 556 } 557 558 /** 559 * @return the bankname 560 */ 561 public String __getBankname() { 562 return bankname; 563 } 564 565 /** 566 * @param bankname the bankname to set 567 */ 568 public void setBankname(String bankname) { 569 this.bankname = bankname; 570 } 571 572 /** 573 * @return the bankcurrency 574 */ 575 public String __getBankcurrency() { 576 return bankcurrency; 577 } 578 579 /** 580 * @param bankcurrency the bankcurrency to set 581 */ 582 public void setBankcurrency(String bankcurrency) { 583 this.bankcurrency = bankcurrency; 584 } 585 586 /** 587 * @return the bankcountry 588 */ 589 public String __getBankcountry() { 590 return bankcountry; 591 } 592 593 /** 594 * @param bankcountry the bankcountry to set 595 */ 596 public void setBankcountry(String bankcountry) { 597 this.bankcountry = bankcountry; 598 } 599 600 /** 601 * Fetches all properties for this contact from the db 602 * @return A (possibly empty) list of {@link ValueProperty}s 603 */ 604 public List<ValueProperty> getProperties() { 605 return ValueProperty.getProperties(this); 606 } 607 608 @Override 609 public boolean delete() { 610 try { 611 List<DatabaseObject> it = getReferencedObjects(this, Context.getItem(), getObject(Context.getItem())); 612 for (int i = 0; i < it.size(); i++) { 613 it.get(i).delete(); 614 } 615 } catch (Exception e) { 616 } 617 618 return super.delete(); 619 } 620 621 @Override 622 public boolean undelete() { 623 try { 624 List<DatabaseObject> it = getReferencedObjects(this, Context.getItem(), getObject(Context.getItem()), true); 625 for (int i = 0; i < it.size(); i++) { 626 it.get(i).undelete(); 627 } 628 } catch (Exception e) { 629 } 630 631 return super.undelete(); 632 } 633 634 @Override 635 public int templateType() { 636 return Constants.TYPE_CONTACT; 637 } 638 639 /** 640 * 641 * @return 642 */ 643 @Override 644 public int templateGroupIds() { 645 return __getGroupsids(); 646 } 647 648 @Persistable(false) 649 public String getInfoString() { 650 return (__getStreet() == null ? __getStreet() : "") + ", " + (__getCity() == null ? __getCity() : ""); 651 } 652}