PageRenderTime 28ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/db/objects/Contact.java

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