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 String firstName;
89 private Gender gender;
94 private Long houseNumber;
97 * Flag whether this contact is user's own data
99 private boolean ownContact;
104 private String phoneNumber;
109 private String street;
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.getFirstName().toLowerCase().equals(contact.getFirstName().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;
273 * @return the emailAddress
276 public String getEmailAddress () {
277 return this.emailAddress;
283 * @param emailAddress the emailAddress to set
286 public final void setEmailAddress (final String emailAddress) {
287 this.emailAddress = emailAddress;
293 * @return the familyName
296 public String getFamilyName () {
297 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
298 return this.familyName;
304 * @param familyName the familyName to set
307 public final void setFamilyName (final String familyName) {
308 /* NOISY-DEBUG: */ this.getLogger().trace(MessageFormat.format("familyName={0} - CALLED!", familyName)); //NOI18N
309 this.familyName = familyName;
315 * @return the faxNumber
318 public String getFaxNumber () {
319 return this.faxNumber;
325 * @param faxNumber the faxNumber to set
328 public final void setFaxNumber (final String faxNumber) {
329 this.faxNumber = faxNumber;
335 * @return the firstName
338 public final String getFirstName () {
339 return this.firstName;
345 * @param firstName the firstName to set
348 public final void setFirstName (final String firstName) {
349 this.firstName = firstName;
353 * Gender of the contact
358 public Gender getGender () {
363 * Gender of the contact
365 * @param gender the gender to set
368 public final void setGender (final Gender gender) {
369 this.gender = gender;
375 * @return the houseNumber
378 public Long getHouseNumber () {
379 return this.houseNumber;
385 * @param houseNumber the houseNumber to set
388 public final void setHouseNumber (final Long houseNumber) {
389 this.houseNumber = houseNumber;
395 * @return the phoneNumber
398 public String getPhoneNumber () {
399 return this.phoneNumber;
405 * @param phoneNumber the phoneNumber to set
408 public final void setPhoneNumber (final String phoneNumber) {
409 this.phoneNumber = phoneNumber;
418 public String getStreet () {
425 * @param street the street to set
428 public final void setStreet (final String street) {
429 this.street = street;
433 * Some "getter" for a translated/human-readable gender
435 * @return gender Human-readable gender
438 public String getTranslatedGender () {
440 String translated = this.getBundle().getString(this.getGender().getMessageKey());
447 * Some "getter for a value from given column name. This name will be
448 * translated into a method name and then this method is called.
450 * @param columnName Column name
451 * @return Value from field
454 public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
456 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName)); //NOI18N
458 // A '$' means not our field
459 if (columnName.startsWith("$")) { //NOI18N
460 // Don't handle these
461 throw new IllegalArgumentException("columnsName contains $"); //NOI18N
464 // Determine if the given column is boolean
465 if (this.isBooleanField(this, "BaseContact", columnName)) { //NOI18N
467 this.getLogger().debug(MessageFormat.format("Column {0} represents a boolean field.", columnName)); //NOI18N
469 // Yes, then call other method
470 return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true)); //NOI18N
473 // Convert column name to field name
474 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
477 this.getLogger().debug(MessageFormat.format("field={0}", methodName)); //NOI18N
480 Object value = this.getField(this, "BaseContact", methodName); //NOI18N
483 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
492 * @return the zipCode
495 public final Long getZipCode () {
502 * @param zipCode the zipCode to set
505 public final void setZipCode (final Long zipCode) {
506 this.zipCode = zipCode;
510 public int hashCode () {
511 // Validate gender instance
512 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
515 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
516 hash = 79 * hash + this.getGender().hashCode();
517 hash = 79 * hash + Objects.hashCode(this.getFirstName());
522 * Checks if given boolean field is available and set to same value
524 * @param columnName Column name to check
525 * @param bool Boolean value
526 * @return Whether all conditions are met
529 public boolean isFieldValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
531 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool)); //NOI18N
533 // Convert column name to field name
534 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
537 this.getLogger().debug(MessageFormat.format("field={0}", methodName)); //NOI18N
539 // Init class instance
540 boolean value = this.getBooleanField(this, "BaseContact", methodName); //NOI18N
543 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
546 boolean isFound = (bool == value);
549 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); //NOI18N
556 * Checks whether the contact is user's own data
561 public final boolean isOwnContact () {
562 return this.ownContact;
566 * Returns an iterator of all values from this object
567 * @return An iterator
570 public Iterator<Map.Entry<Field, Object>> iterator () throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
571 return this.fieldIterator(this, "BaseContact"); //NOI18N
575 public void setValueFromColumn (final String columnName, final Object value) throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
577 this.getLogger().trace(MessageFormat.format("columnName={0},value={1} - CALLED!", columnName, value)); //NOI18N
580 this.setValueInStoreableFromColumn(this, "BaseContact", columnName, value); //NOI18N
583 this.getLogger().trace("EXIT!"); //NOI18N
587 * Shows this contact to the user
589 * @param client Client instance to use
592 public void show (final Client client) {
594 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
596 // The client must be set
597 if (client == null) {
599 throw new NullPointerException("client is null"); //NOI18N
602 // Display name "box"
603 client.displayNameBox(this);
605 // Display address "box"
606 client.displayAddressBox(this);
608 // Display other data "box"
609 client.displayOtherDataBox(this);
613 * Enables the flag "own data" which signals that this contact is the user's
616 protected final void enableFlagOwnContact () {
617 this.ownContact = true;