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.lang.reflect.Field;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
23 import java.util.Objects;
24 import org.mxchange.jcore.BaseFrameworkSystem;
25 import org.mxchange.jcore.client.Client;
30 * @author Roland Haeder
33 public class BaseContact extends BaseFrameworkSystem implements Contact {
38 private String birthday;
43 private String cellphoneNumber;
53 private String comment;
58 private String companyName;
63 private String countryCode;
68 private String emailAddress;
73 private String familyName;
78 private String faxNumber;
83 private Gender gender;
88 private Long houseNumber;
91 * Flag whether this contact is user's own data
93 private boolean ownContact;
98 private String phoneNumber;
103 private String street;
108 private String surname;
113 private Long zipCode;
116 * No instances can be created of this class
118 protected BaseContact () {
122 * Check if contacts are same or throw an exception
124 * @param object Other possible contact class
125 * @return Whether both contacts are same
126 * @todo Needs a lot improvements
129 public boolean equals (final Object object) {
131 if (!(object instanceof BaseContact)) {
134 } else if (!(object instanceof Contact)) {
135 // Not correct interface
140 Contact contact = (Contact) object;
142 // Now test some data @todo Definedly needs improvement
143 return ((this.getGender().equals(contact.getGender()))
144 && (this.getSurname().toLowerCase().equals(contact.getSurname().toLowerCase()))
145 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
151 * @return the birthday
154 public String getBirthday () {
155 return this.birthday;
161 * @param birthday the birthday to set
164 public final void setBirthday (final String birthday) {
165 this.birthday = birthday;
171 * @return the cellphoneNumber
174 public final String getCellphoneNumber () {
175 return this.cellphoneNumber;
181 * @param cellphoneNumber the cellphoneNumber to set
184 public final void setCellphoneNumber (final String cellphoneNumber) {
185 this.cellphoneNumber = cellphoneNumber;
194 public String getCity () {
201 * @param city the city to set
204 public final void setCity (final String city) {
211 * @return the comment
214 public String getComment () {
221 * @param comment the comment to set
224 public final void setComment (final String comment) {
225 this.comment = comment;
231 * @return the companyName
234 public String getCompanyName () {
235 return this.companyName;
241 * @param companyName the companyName to set
244 public final void setCompanyName (final String companyName) {
245 this.companyName = companyName;
251 * @return the countryCode
254 public String getCountryCode () {
255 return this.countryCode;
261 * @param countryCode the countryCode to set
264 public final void setCountryCode (final String countryCode) {
265 this.countryCode = countryCode;
269 * "Serializes" this object into a CSV string (this time with semicolons)
271 * @return "CSV-serialized" version of the stored data
272 * @deprecated Don't use this anymore
275 public String getCsvStringFromStoreableObject () {
277 this.getLogger().trace("CALLED!"); //NOI18N
280 String csvString = String.format(
281 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
283 this.getGender().name(),
285 this.getFamilyName(),
286 this.getCompanyName(),
290 this.getCountryCode(),
291 this.getPhoneNumber(),
293 this.getCellphoneNumber(),
294 this.getEmailAddress(),
306 * @return the emailAddress
309 public String getEmailAddress () {
310 return this.emailAddress;
316 * @param emailAddress the emailAddress to set
319 public final void setEmailAddress (final String emailAddress) {
320 this.emailAddress = emailAddress;
326 * @return the familyName
329 public String getFamilyName () {
330 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
331 return this.familyName;
337 * @param familyName the familyName to set
340 public final void setFamilyName (final String familyName) {
341 /* NOISY-DEBUG: */ this.getLogger().trace(MessageFormat.format("familyName={0} - CALLED!", familyName));
342 this.familyName = familyName;
348 * @return the faxNumber
351 public String getFaxNumber () {
352 return this.faxNumber;
358 * @param faxNumber the faxNumber to set
361 public final void setFaxNumber (final String faxNumber) {
362 this.faxNumber = faxNumber;
366 * Gender of the contact
371 public Gender getGender () {
376 * Gender of the contact
378 * @param gender the gender to set
381 public final void setGender (final Gender gender) {
382 this.gender = gender;
388 * @return the houseNumber
391 public Long getHouseNumber () {
392 return this.houseNumber;
398 * @param houseNumber the houseNumber to set
401 public final void setHouseNumber (final Long houseNumber) {
402 this.houseNumber = houseNumber;
408 * @return the phoneNumber
411 public String getPhoneNumber () {
412 return this.phoneNumber;
418 * @param phoneNumber the phoneNumber to set
421 public final void setPhoneNumber (final String phoneNumber) {
422 this.phoneNumber = phoneNumber;
431 public String getStreet () {
438 * @param street the street to set
441 public final void setStreet (final String street) {
442 this.street = street;
448 * @return the surname
451 public final String getSurname () {
458 * @param surname the surname to set
461 public final void setSurname (final String surname) {
462 this.surname = surname;
466 * Some "getter" for a translated/human-readable gender
468 * @return gender Human-readable gender
471 public String getTranslatedGender () {
473 String translated = this.getBundle().getString(this.getGender().getMessageKey());
480 * Some "getter for a value from given column name. This name will be
481 * translated into a method name and then this method is called.
483 * @param columnName Column name
484 * @return Value from field
487 public Object getValueFromColumn (final String columnName) throws IllegalArgumentException {
489 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
491 // A '$' means not our field
492 if (columnName.startsWith("$")) {
493 // Don't handle these
494 throw new IllegalArgumentException("columnsName contains $");
497 // Determine if the given column is boolean
498 if (this.isBooleanField(this, "BaseContact", columnName)) {
500 this.getLogger().debug("Column " + columnName + " represents a boolean field.");
502 // Yes, then call other method
503 return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true));
506 // Convert column name to field name
507 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
510 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
513 Object value = this.getField(this, "BaseContact", methodName);
516 this.getLogger().trace("value=" + value + " - EXIT!");
525 * @return the zipCode
528 public final Long getZipCode () {
535 * @param zipCode the zipCode to set
538 public final void setZipCode (final Long zipCode) {
539 this.zipCode = zipCode;
543 public int hashCode () {
544 // Validate gender instance
545 assert (this.getGender() instanceof Gender) : "gender is not set.";
548 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
549 hash = 79 * hash + this.getGender().hashCode();
550 hash = 79 * hash + Objects.hashCode(this.getSurname());
555 * Checks whether the contact is user's own data
560 public final boolean isOwnContact () {
561 return this.ownContact;
565 * Checks if given boolean field is available and set to same value
567 * @param columnName Column name to check
568 * @param bool Boolean value
569 * @return Whether all conditions are met
572 public boolean isValueEqual (final String columnName, final boolean bool) {
574 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
576 // Convert column name to field name
577 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
580 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
582 // Init class instance
583 boolean value = this.getBooleanField(this, "BaseContact", methodName);
586 this.getLogger().debug(MessageFormat.format("value={0}", value));
589 boolean isFound = (bool == value);
592 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
599 * Returns an iterator of all values from this object
600 * @return An iterator
603 public Iterator<Map.Entry<Field, Object>> iterator () {
604 return this.fieldIterator(this, "BaseContact");
608 * Shows this contact to the user
610 * @param client Client instance to use
613 public void show (final Client client) {
615 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
617 // The client must be set
618 if (client == null) {
620 throw new NullPointerException("client is null");
623 // Display name "box"
624 client.displayNameBox(this);
626 // Display address "box"
627 client.displayAddressBox(this);
629 // Display other data "box"
630 client.displayOtherDataBox(this);
634 * Enables the flag "own data" which signals that this contact is the user's
637 protected final void enableFlagOwnContact () {
638 this.ownContact = true;