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.BaseFrameworkSystem;
22 import org.mxchange.addressbook.client.Client;
27 * @author Roland Haeder
30 public class BaseContact extends BaseFrameworkSystem {
35 private String birthday;
40 private String cellphoneNumber;
50 private String comment;
55 private String companyName;
60 private String countryCode;
65 private String emailAddress;
70 private String familyName;
75 private String faxNumber;
80 private Gender gender;
85 private int houseNumber;
88 * Marker whether this contact is user's own data
90 private boolean ownContact;
95 private String phoneNumber;
100 private String street;
105 private String surname;
110 private long zipCode;
113 * No instances can be created of this class
115 protected BaseContact () {
119 * Check if contacts are same or throw an exception
121 * @param object Other possible contact class
122 * @return Whether both contacts are same
123 * @todo Needs a lot improvements
126 public boolean equals (final Object object) {
128 if (!(object instanceof BaseContact)) {
131 } else if (!(object instanceof Contact)) {
132 // Not correct interface
137 Contact contact = (Contact) object;
139 // Now test some data @todo Definedly needs improvement
140 return ((this.getGender().equals(contact.getGender()))
141 && (this.getSurname().toLowerCase().equals(contact.getSurname().toLowerCase()))
142 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
148 * @return the birthday
150 public String getBirthday () {
151 return this.birthday;
157 * @return the cellphoneNumber
159 public String getCellphoneNumber () {
160 return this.cellphoneNumber;
168 public String getCity () {
175 * @param city the city to set
177 private void setCity (final String city) {
184 * @return the comment
186 public String getComment () {
193 * @param comment the comment to set
195 private void setComment (final String comment) {
196 this.comment = comment;
202 * @return the companyName
204 public String getCompanyName () {
205 return this.companyName;
211 * @param companyName the companyName to set
213 private void setCompanyName (final String companyName) {
214 this.companyName = companyName;
220 * @return the countryCode
222 public String getCountryCode () {
223 return this.countryCode;
229 * @param countryCode the countryCode to set
231 private void setCountryCode (final String countryCode) {
232 this.countryCode = countryCode;
236 * "Serializes" this object into a CSV string (this time with semicolons)
238 * @return "CSV-serialized" version of the stored data
240 public String getCsvStringFromStoreableObject () {
242 this.getLogger().trace("CALLED!"); //NOI18N
245 String csvString = String.format(
246 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
248 this.getGender().getDatabaseValue(),
250 this.getFamilyName(),
251 this.getCompanyName(),
255 this.getCountryCode(),
256 this.getPhoneNumber(),
258 this.getCellphoneNumber(),
259 this.getEmailAddress(),
271 * @return the emailAddress
273 public String getEmailAddress () {
274 return this.emailAddress;
280 * @param emailAddress the emailAddress to set
282 private void setEmailAddress (final String emailAddress) {
283 this.emailAddress = emailAddress;
289 * @return the familyName
291 public String getFamilyName () {
292 return this.familyName;
298 * @param familyName the familyName to set
300 private void setFamilyName (final String familyName) {
301 this.familyName = familyName;
307 * @return the faxNumber
309 public String getFaxNumber () {
310 return this.faxNumber;
316 * @param faxNumber the faxNumber to set
318 private void setFaxNumber (final String faxNumber) {
319 this.faxNumber = faxNumber;
323 * Gender of the contact
327 public Gender getGender () {
332 * Gender of the contact
334 * @param gender the gender to set
336 private void setGender (final Gender gender) {
337 this.gender = gender;
343 * @return the houseNumber
345 public int getHouseNumber () {
346 return this.houseNumber;
352 * @return the phoneNumber
354 public String getPhoneNumber () {
355 return this.phoneNumber;
363 public String getStreet () {
370 * @param street the street to set
372 protected final void setStreet (final String street) {
373 this.street = street;
379 * @return the surname
381 public final String getSurname () {
386 * Some "getter" for a translated/human-readable gender
388 * @return gender Human-readable gender
390 public String getTranslatedGender () {
392 String translated = this.getBundle().getString(this.getGender().getMessageKey());
401 * @return the zipCode
403 public final long getZipCode () {
410 * @param zipCode the zipCode to set
412 protected final void setZipCode (final long zipCode) {
413 this.zipCode = zipCode;
417 public int hashCode () {
418 // Validate gender instance
419 assert (this.getGender() instanceof Gender) : "gender is not set.";
422 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
423 hash = 79 * hash + this.getGender().hashCode();
424 hash = 79 * hash + Objects.hashCode(this.getSurname());
429 * Checks whether the contact is user's own data
433 public final boolean isOwnContact () {
434 return this.ownContact;
438 * Shows this contact to the user
440 * @param client Client instance to use
442 public void show (final Client client) {
444 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
446 // The client must be set
447 if (client == null) {
449 throw new NullPointerException("client is null");
452 // Display name "box"
453 client.displayNameBox((Contact) this);
455 // Display address "box"
456 client.displayAddressBox((Contact) this);
458 // Display other data "box"
459 client.displayOtherDataBox((Contact) this);
463 * Updates address data in this Contact instance
465 * @param street Street
466 * @param zipCode ZIP code
468 * @param countryCode Country code
470 public void updateAddressData (final String street, final long zipCode, final String city, final String countryCode) {
472 this.getLogger().trace(MessageFormat.format("street={0},zipCode={1},city={2},countryCode={3} - CALLED!", street, zipCode, city, countryCode)); //NOI18N
475 if (street != null) {
476 this.setStreet(street);
479 this.setZipCode(zipCode);
484 if (countryCode != null) {
485 this.setCountryCode(countryCode);
489 this.getLogger().trace("EXIT!"); //NOI18N
493 * Updates name data in this Contact instance
495 * @param gender Gender (M, F, C)
496 * @param surname Surname
497 * @param familyName Family name
498 * @param companyName Company name
500 public void updateNameData (final Gender gender, final String surname, final String familyName, final String companyName) {
502 this.getLogger().trace(MessageFormat.format("gender={0},surname={1},familyName={2},companyName={3} - CALLED!", gender, surname, familyName, companyName)); //NOI18N
505 this.setGender(gender);
507 if (surname != null) {
508 this.setSurname(surname);
510 if (familyName != null) {
511 this.setFamilyName(familyName);
513 if (companyName != null) {
514 this.setCompanyName(companyName);
518 this.getLogger().trace("EXIT!"); //NOI18N
522 * Updates other data in this Contact instance
524 * @param phoneNumber Phone number
525 * @param cellphoneNumber Cellphone number
526 * @param faxNumber Fax number
527 * @param emailAddress Email address
528 * @param birthday Birth day
529 * @param comment Comments
531 public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String birthday, final String comment) {
533 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
536 if (phoneNumber != null) {
537 this.setPhoneNumber(phoneNumber);
539 if (cellphoneNumber != null) {
540 this.setCellphoneNumber(cellphoneNumber);
542 if (faxNumber != null) {
543 this.setFaxNumber(faxNumber);
545 if (emailAddress != null) {
546 this.setEmailAddress(emailAddress);
548 if (birthday != null) {
549 this.setBirthday(birthday);
551 if (comment != null) {
552 this.setComment(comment);
556 this.getLogger().trace("EXIT!"); //NOI18N
560 * Enables the flag "own data" which signals that this contact is the user's
563 protected final void enableFlagOwnContact () {
564 this.ownContact = true;
570 * @param surname the surname to set
572 protected final void setSurname (final String surname) {
573 this.surname = surname;
579 * @param phoneNumber the phoneNumber to set
581 protected final void setPhoneNumber (final String phoneNumber) {
582 this.phoneNumber = phoneNumber;
588 * @param houseNumber the houseNumber to set
590 protected final void setHouseNumber (final int houseNumber) {
591 this.houseNumber = houseNumber;
597 * @param cellphoneNumber the cellphoneNumber to set
599 protected final void setCellphoneNumber (final String cellphoneNumber) {
600 this.cellphoneNumber = cellphoneNumber;
606 * @param birthday the birthday to set
608 protected final void setBirthday (final String birthday) {
609 this.birthday = birthday;
613 * Checks if given boolean field is available and set to same value
615 * @param columnName Column name to check
616 * @param bool Boolean value
617 * @return Whether all conditions are met
620 public boolean isValueEqual (final String columnName, final boolean bool) {
622 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
624 // Convert column name to field name
625 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
628 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
630 // Init class instance
631 boolean value = this.getBooleanField(this, "BaseContact", methodName);
634 this.getLogger().debug(MessageFormat.format("value={0}", value));
637 boolean isFound = (bool == value);
640 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
647 * Some "getter for a value from given column name. This name will be
648 * translated into a method name and then this method is called.
650 * @param columnName Column name
651 * @return Value from field
654 public Object getValueFromColumn (final String columnName) {
656 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
658 // Determine if the given column is boolean
659 if (this.isBooleanField(this, "BaseContact", columnName)) {
660 // Yes, then call other method
661 return this.getBooleanField(this, "BaseContact", columnName);
664 // Convert column name to field name
665 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
668 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
671 Object value = this.getField(this, "BaseContact", methodName);
674 this.getLogger().trace("value=" + value + " - EXIT!");