2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.contact;
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import org.mxchange.addressbook.BaseAddressbookSystem;
22 import org.mxchange.addressbook.client.AddressbookClient;
23 import org.mxchange.jcore.client.Client;
28 * @author Roland Haeder
31 public class BaseContact extends BaseAddressbookSystem {
36 private String birthday;
41 private String cellphoneNumber;
51 private String comment;
56 private String companyName;
61 private String countryCode;
66 private String emailAddress;
71 private String familyName;
76 private String faxNumber;
81 private Gender gender;
86 private int houseNumber;
89 * Marker whether this contact is user's own data
91 private boolean ownContact;
96 private String phoneNumber;
101 private String street;
106 private String surname;
111 private long zipCode;
114 * No instances can be created of this class
116 protected BaseContact () {
120 * Check if contacts are same or throw an exception
122 * @param object Other possible contact class
123 * @return Whether both contacts are same
124 * @todo Needs a lot improvements
127 public boolean equals (final Object object) {
129 if (!(object instanceof BaseContact)) {
132 } else if (!(object instanceof Contact)) {
133 // Not correct interface
138 Contact contact = (Contact) object;
140 // Now test some data @todo Definedly needs improvement
141 return ((this.getGender().equals(contact.getGender()))
142 && (this.getSurname().toLowerCase().equals(contact.getSurname().toLowerCase()))
143 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
149 * @return the birthday
151 public String getBirthday () {
152 return this.birthday;
158 * @return the cellphoneNumber
160 public String getCellphoneNumber () {
161 return this.cellphoneNumber;
169 public String getCity () {
176 * @param city the city to set
178 private void setCity (final String city) {
185 * @return the comment
187 public String getComment () {
194 * @param comment the comment to set
196 private void setComment (final String comment) {
197 this.comment = comment;
203 * @return the companyName
205 public String getCompanyName () {
206 return this.companyName;
212 * @param companyName the companyName to set
214 private void setCompanyName (final String companyName) {
215 this.companyName = companyName;
221 * @return the countryCode
223 public String getCountryCode () {
224 return this.countryCode;
230 * @param countryCode the countryCode to set
232 private void setCountryCode (final String countryCode) {
233 this.countryCode = countryCode;
237 * "Serializes" this object into a CSV string (this time with semicolons)
239 * @return "CSV-serialized" version of the stored data
240 * @deprecated Don't use this anymore
243 public String getCsvStringFromStoreableObject () {
245 this.getLogger().trace("CALLED!"); //NOI18N
248 String csvString = String.format(
249 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
251 this.getGender().getDatabaseValue(),
253 this.getFamilyName(),
254 this.getCompanyName(),
258 this.getCountryCode(),
259 this.getPhoneNumber(),
261 this.getCellphoneNumber(),
262 this.getEmailAddress(),
274 * @return the emailAddress
276 public String getEmailAddress () {
277 return this.emailAddress;
283 * @param emailAddress the emailAddress to set
285 private void setEmailAddress (final String emailAddress) {
286 this.emailAddress = emailAddress;
292 * @return the familyName
294 public String getFamilyName () {
295 return this.familyName;
301 * @param familyName the familyName to set
303 private void setFamilyName (final String familyName) {
304 this.familyName = familyName;
310 * @return the faxNumber
312 public String getFaxNumber () {
313 return this.faxNumber;
319 * @param faxNumber the faxNumber to set
321 private void setFaxNumber (final String faxNumber) {
322 this.faxNumber = faxNumber;
326 * Gender of the contact
330 public Gender getGender () {
335 * Gender of the contact
337 * @param gender the gender to set
339 private void setGender (final Gender gender) {
340 this.gender = gender;
346 * @return the houseNumber
348 public int getHouseNumber () {
349 return this.houseNumber;
355 * @return the phoneNumber
357 public String getPhoneNumber () {
358 return this.phoneNumber;
366 public String getStreet () {
373 * @param street the street to set
375 protected final void setStreet (final String street) {
376 this.street = street;
382 * @return the surname
384 public final String getSurname () {
389 * Some "getter" for a translated/human-readable gender
391 * @return gender Human-readable gender
393 public String getTranslatedGender () {
395 String translated = this.getBundle().getString(this.getGender().getMessageKey());
404 * @return the zipCode
406 public final long getZipCode () {
413 * @param zipCode the zipCode to set
415 protected final void setZipCode (final long zipCode) {
416 this.zipCode = zipCode;
420 public int hashCode () {
421 // Validate gender instance
422 assert (this.getGender() instanceof Gender) : "gender is not set.";
425 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
426 hash = 79 * hash + this.getGender().hashCode();
427 hash = 79 * hash + Objects.hashCode(this.getSurname());
432 * Checks whether the contact is user's own data
436 public final boolean isOwnContact () {
437 return this.ownContact;
441 * Shows this contact to the user
443 * @param client Client instance to use
445 public void show (final Client client) {
447 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
449 // The client must be set
450 if (client == null) {
452 throw new NullPointerException("client is null");
456 AddressbookClient c = (AddressbookClient) client;
458 // Display name "box"
459 c.displayNameBox((Contact) this);
461 // Display address "box"
462 c.displayAddressBox((Contact) this);
464 // Display other data "box"
465 c.displayOtherDataBox((Contact) this);
469 * Updates address data in this Contact instance
471 * @param street Street
472 * @param zipCode ZIP code
474 * @param countryCode Country code
476 public void updateAddressData (final String street, final long zipCode, final String city, final String countryCode) {
478 this.getLogger().trace(MessageFormat.format("street={0},zipCode={1},city={2},countryCode={3} - CALLED!", street, zipCode, city, countryCode)); //NOI18N
481 if (street != null) {
482 this.setStreet(street);
485 this.setZipCode(zipCode);
490 if (countryCode != null) {
491 this.setCountryCode(countryCode);
495 this.getLogger().trace("EXIT!"); //NOI18N
499 * Updates name data in this Contact instance
501 * @param gender Gender (M, F, C)
502 * @param surname Surname
503 * @param familyName Family name
504 * @param companyName Company name
506 public void updateNameData (final Gender gender, final String surname, final String familyName, final String companyName) {
508 this.getLogger().trace(MessageFormat.format("gender={0},surname={1},familyName={2},companyName={3} - CALLED!", gender, surname, familyName, companyName)); //NOI18N
511 this.setGender(gender);
513 if (surname != null) {
514 this.setSurname(surname);
516 if (familyName != null) {
517 this.setFamilyName(familyName);
519 if (companyName != null) {
520 this.setCompanyName(companyName);
524 this.getLogger().trace("EXIT!"); //NOI18N
528 * Updates other data in this Contact instance
530 * @param phoneNumber Phone number
531 * @param cellphoneNumber Cellphone number
532 * @param faxNumber Fax number
533 * @param emailAddress Email address
534 * @param birthday Birth day
535 * @param comment Comments
537 public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String birthday, final String comment) {
539 this.getLogger().trace(MessageFormat.format("phoneNumber={0},cellphoneNumber={1}faxNumber={2},emailAddress={3},birthday={4},comment={5} - CALLED!", phoneNumber, cellphoneNumber, faxNumber, emailAddress, birthday, comment)); //NOI18N
542 if (phoneNumber != null) {
543 this.setPhoneNumber(phoneNumber);
545 if (cellphoneNumber != null) {
546 this.setCellphoneNumber(cellphoneNumber);
548 if (faxNumber != null) {
549 this.setFaxNumber(faxNumber);
551 if (emailAddress != null) {
552 this.setEmailAddress(emailAddress);
554 if (birthday != null) {
555 this.setBirthday(birthday);
557 if (comment != null) {
558 this.setComment(comment);
562 this.getLogger().trace("EXIT!"); //NOI18N
566 * Enables the flag "own data" which signals that this contact is the user's
569 protected final void enableFlagOwnContact () {
570 this.ownContact = true;
576 * @param surname the surname to set
578 protected final void setSurname (final String surname) {
579 this.surname = surname;
585 * @param phoneNumber the phoneNumber to set
587 protected final void setPhoneNumber (final String phoneNumber) {
588 this.phoneNumber = phoneNumber;
594 * @param houseNumber the houseNumber to set
596 protected final void setHouseNumber (final int houseNumber) {
597 this.houseNumber = houseNumber;
603 * @param cellphoneNumber the cellphoneNumber to set
605 protected final void setCellphoneNumber (final String cellphoneNumber) {
606 this.cellphoneNumber = cellphoneNumber;
612 * @param birthday the birthday to set
614 protected final void setBirthday (final String birthday) {
615 this.birthday = birthday;
619 * Some "getter for a value from given column name. This name will be
620 * translated into a method name and then this method is called.
622 * @param columnName Column name
623 * @return Value from field
626 public Object getValueFromColumn (final String columnName) {
628 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
630 // Determine if the given column is boolean
631 if (this.isBooleanField(this, "BaseContact", columnName)) {
632 // Yes, then call other method
633 return this.getBooleanField(this, "BaseContact", columnName);
636 // Convert column name to field name
637 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
640 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
643 Object value = this.getField(this, "BaseContact", methodName);
646 this.getLogger().trace("value=" + value + " - EXIT!");
653 * Checks if given boolean field is available and set to same value
655 * @param columnName Column name to check
656 * @param bool Boolean value
657 * @return Whether all conditions are met
660 public boolean isValueEqual (final String columnName, final boolean bool) {
662 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
664 // Convert column name to field name
665 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
668 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
670 // Init class instance
671 boolean value = this.getBooleanField(this, "BaseContact", methodName);
674 this.getLogger().debug(MessageFormat.format("value={0}", value));
677 boolean isFound = (bool == value);
680 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));