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, Comparable<Contact> {
39 private Long contactId;
44 private String birthday;
49 private String cellphoneNumber;
59 private String comment;
64 private String companyName;
69 private String countryCode;
74 private String emailAddress;
79 private String familyName;
84 private String faxNumber;
89 private String firstName;
94 private Gender gender;
99 private Long houseNumber;
102 * Flag whether this contact is user's own data
104 private boolean ownContact;
109 private String phoneNumber;
114 private String street;
119 private Long zipCode;
122 * No instances should be created of this class. Better extend this class
123 * and provide proper constructors.
125 protected BaseContact () {
129 * Check if contacts are same or throw an exception
131 * @param object Other possible contact class
132 * @return Whether both contacts are same
133 * TODO Needs a lot improvements
136 public boolean equals (final Object object) {
138 if (!(object instanceof BaseContact)) {
141 } else if (!(object instanceof Contact)) {
142 // Not correct interface
147 Contact contact = (Contact) object;
149 // Now test some data TODO Definedly needs improvement
150 return ((this.getGender().equals(contact.getGender()))
151 && (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase()))
152 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
158 * @return the birthday
161 public String getBirthday () {
162 return this.birthday;
168 * @param birthday the birthday to set
171 public final void setBirthday (final String birthday) {
172 this.birthday = birthday;
178 * @return the cellphoneNumber
181 public final String getCellphoneNumber () {
182 return this.cellphoneNumber;
188 * @param cellphoneNumber the cellphoneNumber to set
191 public final void setCellphoneNumber (final String cellphoneNumber) {
192 this.cellphoneNumber = cellphoneNumber;
201 public String getCity () {
208 * @param city the city to set
211 public final void setCity (final String city) {
218 * @return the comment
221 public String getComment () {
228 * @param comment the comment to set
231 public final void setComment (final String comment) {
232 this.comment = comment;
238 * @return the companyName
241 public String getCompanyName () {
242 return this.companyName;
248 * @param companyName the companyName to set
251 public final void setCompanyName (final String companyName) {
252 this.companyName = companyName;
257 * @return the contactId
260 public final Long getContactId () {
261 return this.contactId;
266 * @param contactId the contactId to set
269 public final void setContactId (final Long contactId) {
270 this.contactId = contactId;
276 * @return the countryCode
279 public String getCountryCode () {
280 return this.countryCode;
286 * @param countryCode the countryCode to set
289 public final void setCountryCode (final String countryCode) {
290 this.countryCode = countryCode;
296 * @return the emailAddress
299 public String getEmailAddress () {
300 return this.emailAddress;
306 * @param emailAddress the emailAddress to set
309 public final void setEmailAddress (final String emailAddress) {
310 this.emailAddress = emailAddress;
316 * @return the familyName
319 public String getFamilyName () {
320 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
321 return this.familyName;
327 * @param familyName the familyName to set
330 public final void setFamilyName (final String familyName) {
331 /* NOISY-DEBUG: */ this.getLogger().trace(MessageFormat.format("familyName={0} - CALLED!", familyName)); //NOI18N
332 this.familyName = familyName;
338 * @return the faxNumber
341 public String getFaxNumber () {
342 return this.faxNumber;
348 * @param faxNumber the faxNumber to set
351 public final void setFaxNumber (final String faxNumber) {
352 this.faxNumber = faxNumber;
358 * @return the firstName
361 public final String getFirstName () {
362 return this.firstName;
368 * @param firstName the firstName to set
371 public final void setFirstName (final String firstName) {
372 this.firstName = firstName;
376 * Gender of the contact
381 public Gender getGender () {
386 * Gender of the contact
388 * @param gender the gender to set
391 public final void setGender (final Gender gender) {
392 this.gender = gender;
398 * @return the houseNumber
401 public Long getHouseNumber () {
402 return this.houseNumber;
408 * @param houseNumber the houseNumber to set
411 public final void setHouseNumber (final Long houseNumber) {
412 this.houseNumber = houseNumber;
418 * @return the phoneNumber
421 public String getPhoneNumber () {
422 return this.phoneNumber;
428 * @param phoneNumber the phoneNumber to set
431 public final void setPhoneNumber (final String phoneNumber) {
432 this.phoneNumber = phoneNumber;
441 public String getStreet () {
448 * @param street the street to set
451 public final void setStreet (final String street) {
452 this.street = street;
456 * Some "getter" for a translated/human-readable gender
458 * @return gender Human-readable gender
461 public String getTranslatedGender () {
463 String translated = this.getMessageStringFromKey(this.getGender().getMessageKey());
470 * Some "getter for a value from given column name. This name will be
471 * translated into a method name and then this method is called.
473 * @param columnName Column name
474 * @return Value from field
477 public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
479 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
481 // A '$' means not our field
482 if (columnName.startsWith("$")) { //NOI18N
483 // Don't handle these
484 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
487 // Determine if the given column is boolean
488 if (this.isBooleanField(this, "BaseContact", columnName)) { //NOI18N
490 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
492 // Yes, then call other method
493 return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true)); //NOI18N
496 // Convert column name to field name
497 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
500 this.getLogger().debug(MessageFormat.format("field={0}", methodName)); //NOI18N
503 Object value = this.getField(this, "BaseContact", methodName); //NOI18N
506 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
515 * @return the zipCode
518 public final Long getZipCode () {
525 * @param zipCode the zipCode to set
528 public final void setZipCode (final Long zipCode) {
529 this.zipCode = zipCode;
533 public int hashCode () {
534 // Validate gender instance
535 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
538 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
539 hash = 79 * hash + this.getGender().hashCode();
540 hash = 79 * hash + Objects.hashCode(this.getFirstName());
545 * Checks if given boolean field is available and set to same value
547 * @param columnName Column name to check
548 * @param bool Boolean value
549 * @return Whether all conditions are met
552 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
554 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool)); //NOI18N
556 // Convert column name to field name
557 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
560 this.getLogger().debug(MessageFormat.format("field={0}", methodName)); //NOI18N
562 // Init class instance
563 boolean value = this.getBooleanField(this, "BaseContact", methodName); //NOI18N
566 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
569 boolean isFound = (bool == value);
572 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
579 * Checks whether the contact is user's own data
584 public final boolean isOwnContact () {
585 return this.ownContact;
589 * Returns an iterator of all values from this object
590 * @return An iterator
593 public Iterator<Map.Entry<Field, Object>> iterator () throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
594 return this.fieldIterator(this, "BaseContact"); //NOI18N
598 public void setValueFromColumn (final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
600 this.getLogger().trace(MessageFormat.format("columnName={0},value={1} - CALLED!", columnName, value)); //NOI18N
603 this.setValueInStorableFromColumn(this, "BaseContact", columnName, value); //NOI18N
606 this.getLogger().trace("EXIT!"); //NOI18N
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 (null == client) {
622 throw new NullPointerException("client is null"); //NOI18N
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;
644 * Compares two contacts with each other
646 * @param contact Contact comparator
647 * @return Comparison value
650 public int compareTo (final Contact contact) {
652 this.getLogger().trace(MessageFormat.format("contact={0} - CALLED!", contact)); //NOI18N
654 // contact should not be null
655 if (null == contact) {
656 throw new NullPointerException("contact is null"); //NOI18N
660 this.getLogger().debug(MessageFormat.format("this.id={0},contact.id={1}", this.getContactId(), contact.getContactId())); //NOI18N
662 // Is the contactId the same?
663 if (Objects.equals(this.getContactId(), contact.getContactId())) {
664 // Same contactId, means same contact
666 } else if (this.getContactId() > contact.getContactId()) {
667 // This contactId is larger than compared to
671 // The other contactId is larger