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;
29 * A general contact class which should only be extended.
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 should be created of this class. Better extend this class
118 * and provide proper constructors.
120 protected BaseContact () {
124 * Check if contacts are same or throw an exception
126 * @param object Other possible contact class
127 * @return Whether both contacts are same
128 * @todo Needs a lot improvements
131 public boolean equals (final Object object) {
133 if (!(object instanceof BaseContact)) {
136 } else if (!(object instanceof Contact)) {
137 // Not correct interface
142 Contact contact = (Contact) object;
144 // Now test some data @todo Definedly needs improvement
145 return ((this.getGender().equals(contact.getGender()))
146 && (this.getSurname().toLowerCase().equals(contact.getSurname().toLowerCase()))
147 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
153 * @return the birthday
156 public String getBirthday () {
157 return this.birthday;
163 * @param birthday the birthday to set
166 public final void setBirthday (final String birthday) {
167 this.birthday = birthday;
173 * @return the cellphoneNumber
176 public final String getCellphoneNumber () {
177 return this.cellphoneNumber;
183 * @param cellphoneNumber the cellphoneNumber to set
186 public final void setCellphoneNumber (final String cellphoneNumber) {
187 this.cellphoneNumber = cellphoneNumber;
196 public String getCity () {
203 * @param city the city to set
206 public final void setCity (final String city) {
213 * @return the comment
216 public String getComment () {
223 * @param comment the comment to set
226 public final void setComment (final String comment) {
227 this.comment = comment;
233 * @return the companyName
236 public String getCompanyName () {
237 return this.companyName;
243 * @param companyName the companyName to set
246 public final void setCompanyName (final String companyName) {
247 this.companyName = companyName;
253 * @return the countryCode
256 public String getCountryCode () {
257 return this.countryCode;
263 * @param countryCode the countryCode to set
266 public final void setCountryCode (final String countryCode) {
267 this.countryCode = countryCode;
271 * "Serializes" this object into a CSV string (this time with semicolons)
273 * @return "CSV-serialized" version of the stored data
274 * @deprecated Don't use this anymore
277 public String getCsvStringFromStoreableObject () {
279 this.getLogger().trace("CALLED!"); //NOI18N
282 String csvString = String.format(
283 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
285 this.getGender().name(),
287 this.getFamilyName(),
288 this.getCompanyName(),
292 this.getCountryCode(),
293 this.getPhoneNumber(),
295 this.getCellphoneNumber(),
296 this.getEmailAddress(),
308 * @return the emailAddress
311 public String getEmailAddress () {
312 return this.emailAddress;
318 * @param emailAddress the emailAddress to set
321 public final void setEmailAddress (final String emailAddress) {
322 this.emailAddress = emailAddress;
328 * @return the familyName
331 public String getFamilyName () {
332 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
333 return this.familyName;
339 * @param familyName the familyName to set
342 public final void setFamilyName (final String familyName) {
343 /* NOISY-DEBUG: */ this.getLogger().trace(MessageFormat.format("familyName={0} - CALLED!", familyName));
344 this.familyName = familyName;
350 * @return the faxNumber
353 public String getFaxNumber () {
354 return this.faxNumber;
360 * @param faxNumber the faxNumber to set
363 public final void setFaxNumber (final String faxNumber) {
364 this.faxNumber = faxNumber;
368 * Gender of the contact
373 public Gender getGender () {
378 * Gender of the contact
380 * @param gender the gender to set
383 public final void setGender (final Gender gender) {
384 this.gender = gender;
390 * @return the houseNumber
393 public Long getHouseNumber () {
394 return this.houseNumber;
400 * @param houseNumber the houseNumber to set
403 public final void setHouseNumber (final Long houseNumber) {
404 this.houseNumber = houseNumber;
410 * @return the phoneNumber
413 public String getPhoneNumber () {
414 return this.phoneNumber;
420 * @param phoneNumber the phoneNumber to set
423 public final void setPhoneNumber (final String phoneNumber) {
424 this.phoneNumber = phoneNumber;
433 public String getStreet () {
440 * @param street the street to set
443 public final void setStreet (final String street) {
444 this.street = street;
450 * @return the surname
453 public final String getSurname () {
460 * @param surname the surname to set
463 public final void setSurname (final String surname) {
464 this.surname = surname;
468 * Some "getter" for a translated/human-readable gender
470 * @return gender Human-readable gender
473 public String getTranslatedGender () {
475 String translated = this.getBundle().getString(this.getGender().getMessageKey());
482 * Some "getter for a value from given column name. This name will be
483 * translated into a method name and then this method is called.
485 * @param columnName Column name
486 * @return Value from field
489 public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
491 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
493 // A '$' means not our field
494 if (columnName.startsWith("$")) {
495 // Don't handle these
496 throw new IllegalArgumentException("columnsName contains $");
499 // Determine if the given column is boolean
500 if (this.isBooleanField(this, "BaseContact", columnName)) {
502 this.getLogger().debug("Column " + columnName + " represents a boolean field.");
504 // Yes, then call other method
505 return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true));
508 // Convert column name to field name
509 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
512 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
515 Object value = this.getField(this, "BaseContact", methodName);
518 this.getLogger().trace("value=" + value + " - EXIT!");
527 * @return the zipCode
530 public final Long getZipCode () {
537 * @param zipCode the zipCode to set
540 public final void setZipCode (final Long zipCode) {
541 this.zipCode = zipCode;
545 public int hashCode () {
546 // Validate gender instance
547 assert (this.getGender() instanceof Gender) : "gender is not set.";
550 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
551 hash = 79 * hash + this.getGender().hashCode();
552 hash = 79 * hash + Objects.hashCode(this.getSurname());
557 * Checks whether the contact is user's own data
562 public final boolean isOwnContact () {
563 return this.ownContact;
567 * Checks if given boolean field is available and set to same value
569 * @param columnName Column name to check
570 * @param bool Boolean value
571 * @return Whether all conditions are met
574 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
576 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
578 // Convert column name to field name
579 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
582 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
584 // Init class instance
585 boolean value = this.getBooleanField(this, "BaseContact", methodName);
588 this.getLogger().debug(MessageFormat.format("value={0}", value));
591 boolean isFound = (bool == value);
594 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
601 * Returns an iterator of all values from this object
602 * @return An iterator
605 public Iterator<Map.Entry<Field, Object>> iterator () throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
606 return this.fieldIterator(this, "BaseContact");
610 * Shows this contact to the user
612 * @param client Client instance to use
615 public void show (final Client client) {
617 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
619 // The client must be set
620 if (client == null) {
622 throw new NullPointerException("client is null");
625 // Display name "box"
626 client.displayNameBox(this);
628 // Display address "box"
629 client.displayAddressBox(this);
631 // Display other data "box"
632 client.displayOtherDataBox(this);
636 * Enables the flag "own data" which signals that this contact is the user's
639 protected final void enableFlagOwnContact () {
640 this.ownContact = true;