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.jcore.contact;
19 import java.text.MessageFormat;
20 import java.util.Objects;
21 import org.mxchange.jcore.BaseFrameworkSystem;
22 import org.mxchange.jcore.client.Client;
27 * @author Roland Haeder
30 public class BaseContact extends BaseFrameworkSystem implements Contact {
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
151 public String getBirthday () {
152 return this.birthday;
158 * @return the cellphoneNumber
161 public String getCellphoneNumber () {
162 return this.cellphoneNumber;
171 public String getCity () {
178 * @param city the city to set
180 private void setCity (final String city) {
187 * @return the comment
190 public String getComment () {
197 * @param comment the comment to set
199 private void setComment (final String comment) {
200 this.comment = comment;
206 * @return the companyName
209 public String getCompanyName () {
210 return this.companyName;
216 * @param companyName the companyName to set
218 private void setCompanyName (final String companyName) {
219 this.companyName = companyName;
225 * @return the countryCode
228 public String getCountryCode () {
229 return this.countryCode;
235 * @param countryCode the countryCode to set
237 private void setCountryCode (final String countryCode) {
238 this.countryCode = countryCode;
242 * "Serializes" this object into a CSV string (this time with semicolons)
244 * @return "CSV-serialized" version of the stored data
245 * @deprecated Don't use this anymore
248 public String getCsvStringFromStoreableObject () {
250 this.getLogger().trace("CALLED!"); //NOI18N
253 String csvString = String.format(
254 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
256 this.getGender().getDatabaseValue(),
258 this.getFamilyName(),
259 this.getCompanyName(),
263 this.getCountryCode(),
264 this.getPhoneNumber(),
266 this.getCellphoneNumber(),
267 this.getEmailAddress(),
279 * @return the emailAddress
282 public String getEmailAddress () {
283 return this.emailAddress;
289 * @param emailAddress the emailAddress to set
291 private void setEmailAddress (final String emailAddress) {
292 this.emailAddress = emailAddress;
298 * @return the familyName
301 public String getFamilyName () {
302 return this.familyName;
308 * @param familyName the familyName to set
310 private void setFamilyName (final String familyName) {
311 this.familyName = familyName;
317 * @return the faxNumber
320 public String getFaxNumber () {
321 return this.faxNumber;
327 * @param faxNumber the faxNumber to set
329 private void setFaxNumber (final String faxNumber) {
330 this.faxNumber = faxNumber;
334 * Gender of the contact
339 public Gender getGender () {
344 * Gender of the contact
346 * @param gender the gender to set
348 private void setGender (final Gender gender) {
349 this.gender = gender;
355 * @return the houseNumber
358 public int getHouseNumber () {
359 return this.houseNumber;
365 * @return the phoneNumber
368 public String getPhoneNumber () {
369 return this.phoneNumber;
378 public String getStreet () {
385 * @param street the street to set
387 protected final void setStreet (final String street) {
388 this.street = street;
394 * @return the surname
397 public final String getSurname () {
402 * Some "getter" for a translated/human-readable gender
404 * @return gender Human-readable gender
407 public String getTranslatedGender () {
409 String translated = this.getBundle().getString(this.getGender().getMessageKey());
418 * @return the zipCode
421 public final long getZipCode () {
428 * @param zipCode the zipCode to set
430 protected final void setZipCode (final long zipCode) {
431 this.zipCode = zipCode;
435 public int hashCode () {
436 // Validate gender instance
437 assert (this.getGender() instanceof Gender) : "gender is not set.";
440 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
441 hash = 79 * hash + this.getGender().hashCode();
442 hash = 79 * hash + Objects.hashCode(this.getSurname());
447 * Checks whether the contact is user's own data
452 public final boolean isOwnContact () {
453 return this.ownContact;
457 * Shows this contact to the user
459 * @param client Client instance to use
462 public void show (final Client client) {
464 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
466 // The client must be set
467 if (client == null) {
469 throw new NullPointerException("client is null");
472 // Display name "box"
473 client.displayNameBox(this);
475 // Display address "box"
476 client.displayAddressBox(this);
478 // Display other data "box"
479 client.displayOtherDataBox(this);
483 * Updates address data in this Contact instance
485 * @param street Street
486 * @param zipCode ZIP code
488 * @param countryCode Country code
491 public void updateAddressData (final String street, final long zipCode, final String city, final String countryCode) {
493 this.getLogger().trace(MessageFormat.format("street={0},zipCode={1},city={2},countryCode={3} - CALLED!", street, zipCode, city, countryCode)); //NOI18N
496 if (street != null) {
497 this.setStreet(street);
500 this.setZipCode(zipCode);
505 if (countryCode != null) {
506 this.setCountryCode(countryCode);
510 this.getLogger().trace("EXIT!"); //NOI18N
514 * Updates name data in this Contact instance
516 * @param gender Gender (M, F, C)
517 * @param surname Surname
518 * @param familyName Family name
519 * @param companyName Company name
522 public void updateNameData (final Gender gender, final String surname, final String familyName, final String companyName) {
524 this.getLogger().trace(MessageFormat.format("gender={0},surname={1},familyName={2},companyName={3} - CALLED!", gender, surname, familyName, companyName)); //NOI18N
527 this.setGender(gender);
529 if (surname != null) {
530 this.setSurname(surname);
532 if (familyName != null) {
533 this.setFamilyName(familyName);
535 if (companyName != null) {
536 this.setCompanyName(companyName);
540 this.getLogger().trace("EXIT!"); //NOI18N
544 * Updates other data in this Contact instance
546 * @param phoneNumber Phone number
547 * @param cellphoneNumber Cellphone number
548 * @param faxNumber Fax number
549 * @param emailAddress Email address
550 * @param birthday Birth day
551 * @param comment Comments
554 public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String birthday, final String comment) {
556 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
559 if (phoneNumber != null) {
560 this.setPhoneNumber(phoneNumber);
562 if (cellphoneNumber != null) {
563 this.setCellphoneNumber(cellphoneNumber);
565 if (faxNumber != null) {
566 this.setFaxNumber(faxNumber);
568 if (emailAddress != null) {
569 this.setEmailAddress(emailAddress);
571 if (birthday != null) {
572 this.setBirthday(birthday);
574 if (comment != null) {
575 this.setComment(comment);
579 this.getLogger().trace("EXIT!"); //NOI18N
583 * Enables the flag "own data" which signals that this contact is the user's
586 protected final void enableFlagOwnContact () {
587 this.ownContact = true;
593 * @param surname the surname to set
595 protected final void setSurname (final String surname) {
596 this.surname = surname;
602 * @param phoneNumber the phoneNumber to set
604 protected final void setPhoneNumber (final String phoneNumber) {
605 this.phoneNumber = phoneNumber;
611 * @param houseNumber the houseNumber to set
613 protected final void setHouseNumber (final int houseNumber) {
614 this.houseNumber = houseNumber;
620 * @param cellphoneNumber the cellphoneNumber to set
622 protected final void setCellphoneNumber (final String cellphoneNumber) {
623 this.cellphoneNumber = cellphoneNumber;
629 * @param birthday the birthday to set
631 protected final void setBirthday (final String birthday) {
632 this.birthday = birthday;
636 * Some "getter for a value from given column name. This name will be
637 * translated into a method name and then this method is called.
639 * @param columnName Column name
640 * @return Value from field
643 public Object getValueFromColumn (final String columnName) {
645 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
647 // Determine if the given column is boolean
648 if (this.isBooleanField(this, "BaseContact", columnName)) {
649 // Yes, then call other method
650 return this.getBooleanField(this, "BaseContact", columnName);
653 // Convert column name to field name
654 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
657 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
660 Object value = this.getField(this, "BaseContact", methodName);
663 this.getLogger().trace("value=" + value + " - EXIT!");
670 * Checks if given boolean field is available and set to same value
672 * @param columnName Column name to check
673 * @param bool Boolean value
674 * @return Whether all conditions are met
677 public boolean isValueEqual (final String columnName, final boolean bool) {
679 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
681 // Convert column name to field name
682 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
685 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
687 // Init class instance
688 boolean value = this.getBooleanField(this, "BaseContact", methodName);
691 this.getLogger().debug(MessageFormat.format("value={0}", value));
694 boolean isFound = (bool == value);
697 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));