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.lang.reflect.InvocationTargetException;
21 import java.text.MessageFormat;
22 import java.util.Iterator;
24 import java.util.Objects;
25 import org.mxchange.jcore.BaseFrameworkSystem;
26 import org.mxchange.jcore.client.Client;
31 * @author Roland Haeder
34 public class BaseContact extends BaseFrameworkSystem implements Contact {
39 private String birthday;
44 private String cellphoneNumber;
54 private String comment;
59 private String companyName;
64 private String countryCode;
69 private String emailAddress;
74 private String familyName;
79 private String faxNumber;
84 private Gender gender;
89 private Long houseNumber;
92 * Flag whether this contact is user's own data
94 private boolean ownContact;
99 private String phoneNumber;
104 private String street;
109 private String surname;
114 private Long zipCode;
117 * No instances can be created of this class
119 protected BaseContact () {
123 * Check if contacts are same or throw an exception
125 * @param object Other possible contact class
126 * @return Whether both contacts are same
127 * @todo Needs a lot improvements
130 public boolean equals (final Object object) {
132 if (!(object instanceof BaseContact)) {
135 } else if (!(object instanceof Contact)) {
136 // Not correct interface
141 Contact contact = (Contact) object;
143 // Now test some data @todo Definedly needs improvement
144 return ((this.getGender().equals(contact.getGender()))
145 && (this.getSurname().toLowerCase().equals(contact.getSurname().toLowerCase()))
146 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
152 * @return the birthday
155 public String getBirthday () {
156 return this.birthday;
162 * @param birthday the birthday to set
165 public final void setBirthday (final String birthday) {
166 this.birthday = birthday;
172 * @return the cellphoneNumber
175 public final String getCellphoneNumber () {
176 return this.cellphoneNumber;
182 * @param cellphoneNumber the cellphoneNumber to set
185 public final void setCellphoneNumber (final String cellphoneNumber) {
186 this.cellphoneNumber = cellphoneNumber;
195 public String getCity () {
202 * @param city the city to set
205 public final void setCity (final String city) {
212 * @return the comment
215 public String getComment () {
222 * @param comment the comment to set
225 public final void setComment (final String comment) {
226 this.comment = comment;
232 * @return the companyName
235 public String getCompanyName () {
236 return this.companyName;
242 * @param companyName the companyName to set
245 public final void setCompanyName (final String companyName) {
246 this.companyName = companyName;
252 * @return the countryCode
255 public String getCountryCode () {
256 return this.countryCode;
262 * @param countryCode the countryCode to set
265 public final void setCountryCode (final String countryCode) {
266 this.countryCode = countryCode;
270 * "Serializes" this object into a CSV string (this time with semicolons)
272 * @return "CSV-serialized" version of the stored data
273 * @deprecated Don't use this anymore
276 public String getCsvStringFromStoreableObject () {
278 this.getLogger().trace("CALLED!"); //NOI18N
281 String csvString = String.format(
282 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
284 this.getGender().name(),
286 this.getFamilyName(),
287 this.getCompanyName(),
291 this.getCountryCode(),
292 this.getPhoneNumber(),
294 this.getCellphoneNumber(),
295 this.getEmailAddress(),
307 * @return the emailAddress
310 public String getEmailAddress () {
311 return this.emailAddress;
317 * @param emailAddress the emailAddress to set
320 public final void setEmailAddress (final String emailAddress) {
321 this.emailAddress = emailAddress;
327 * @return the familyName
330 public String getFamilyName () {
331 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
332 return this.familyName;
338 * @param familyName the familyName to set
341 public final void setFamilyName (final String familyName) {
342 /* NOISY-DEBUG: */ this.getLogger().trace(MessageFormat.format("familyName={0} - CALLED!", familyName));
343 this.familyName = familyName;
349 * @return the faxNumber
352 public String getFaxNumber () {
353 return this.faxNumber;
359 * @param faxNumber the faxNumber to set
362 public final void setFaxNumber (final String faxNumber) {
363 this.faxNumber = faxNumber;
367 * Gender of the contact
372 public Gender getGender () {
377 * Gender of the contact
379 * @param gender the gender to set
382 public final void setGender (final Gender gender) {
383 this.gender = gender;
389 * @return the houseNumber
392 public Long getHouseNumber () {
393 return this.houseNumber;
399 * @param houseNumber the houseNumber to set
402 public final void setHouseNumber (final Long houseNumber) {
403 this.houseNumber = houseNumber;
409 * @return the phoneNumber
412 public String getPhoneNumber () {
413 return this.phoneNumber;
419 * @param phoneNumber the phoneNumber to set
422 public final void setPhoneNumber (final String phoneNumber) {
423 this.phoneNumber = phoneNumber;
432 public String getStreet () {
439 * @param street the street to set
442 public final void setStreet (final String street) {
443 this.street = street;
449 * @return the surname
452 public final String getSurname () {
459 * @param surname the surname to set
462 public final void setSurname (final String surname) {
463 this.surname = surname;
467 * Some "getter" for a translated/human-readable gender
469 * @return gender Human-readable gender
472 public String getTranslatedGender () {
474 String translated = this.getBundle().getString(this.getGender().getMessageKey());
481 * Some "getter for a value from given column name. This name will be
482 * translated into a method name and then this method is called.
484 * @param columnName Column name
485 * @return Value from field
488 public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
490 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
492 // A '$' means not our field
493 if (columnName.startsWith("$")) {
494 // Don't handle these
495 throw new IllegalArgumentException("columnsName contains $");
498 // Determine if the given column is boolean
499 if (this.isBooleanField(this, "BaseContact", columnName)) {
501 this.getLogger().debug("Column " + columnName + " represents a boolean field.");
503 // Yes, then call other method
504 return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true));
507 // Convert column name to field name
508 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
511 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
514 Object value = this.getField(this, "BaseContact", methodName);
517 this.getLogger().trace("value=" + value + " - EXIT!");
526 * @return the zipCode
529 public final Long getZipCode () {
536 * @param zipCode the zipCode to set
539 public final void setZipCode (final Long zipCode) {
540 this.zipCode = zipCode;
544 public int hashCode () {
545 // Validate gender instance
546 assert (this.getGender() instanceof Gender) : "gender is not set.";
549 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
550 hash = 79 * hash + this.getGender().hashCode();
551 hash = 79 * hash + Objects.hashCode(this.getSurname());
556 * Checks whether the contact is user's own data
561 public final boolean isOwnContact () {
562 return this.ownContact;
566 * Checks if given boolean field is available and set to same value
568 * @param columnName Column name to check
569 * @param bool Boolean value
570 * @return Whether all conditions are met
573 public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
575 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
577 // Convert column name to field name
578 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
581 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
583 // Init class instance
584 boolean value = this.getBooleanField(this, "BaseContact", methodName);
587 this.getLogger().debug(MessageFormat.format("value={0}", value));
590 boolean isFound = (bool == value);
593 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
600 * Returns an iterator of all values from this object
601 * @return An iterator
604 public Iterator<Map.Entry<Field, Object>> iterator () throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
605 return this.fieldIterator(this, "BaseContact");
609 * Shows this contact to the user
611 * @param client Client instance to use
614 public void show (final Client client) {
616 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
618 // The client must be set
619 if (client == null) {
621 throw new NullPointerException("client is null");
624 // Display name "box"
625 client.displayNameBox(this);
627 // Display address "box"
628 client.displayAddressBox(this);
630 // Display other data "box"
631 client.displayOtherDataBox(this);
635 * Enables the flag "own data" which signals that this contact is the user's
638 protected final void enableFlagOwnContact () {
639 this.ownContact = true;