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 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().getDatabaseValue(),
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 return this.familyName;
336 * @param familyName the familyName to set
339 public final void setFamilyName (final String familyName) {
340 this.familyName = familyName;
346 * @return the faxNumber
349 public String getFaxNumber () {
350 return this.faxNumber;
356 * @param faxNumber the faxNumber to set
359 public final void setFaxNumber (final String faxNumber) {
360 this.faxNumber = faxNumber;
364 * Gender of the contact
369 public Gender getGender () {
374 * Gender of the contact
376 * @param gender the gender to set
379 public final void setGender (final Gender gender) {
380 this.gender = gender;
386 * @return the houseNumber
389 public Long getHouseNumber () {
390 return this.houseNumber;
396 * @param houseNumber the houseNumber to set
399 public final void setHouseNumber (final Long houseNumber) {
400 this.houseNumber = houseNumber;
406 * @return the phoneNumber
409 public String getPhoneNumber () {
410 return this.phoneNumber;
416 * @param phoneNumber the phoneNumber to set
419 public final void setPhoneNumber (final String phoneNumber) {
420 this.phoneNumber = phoneNumber;
429 public String getStreet () {
436 * @param street the street to set
439 public final void setStreet (final String street) {
440 this.street = street;
446 * @return the surname
449 public final String getSurname () {
456 * @param surname the surname to set
459 public final void setSurname (final String surname) {
460 this.surname = surname;
464 * Some "getter" for a translated/human-readable gender
466 * @return gender Human-readable gender
469 public String getTranslatedGender () {
471 String translated = this.getBundle().getString(this.getGender().getMessageKey());
478 * Some "getter for a value from given column name. This name will be
479 * translated into a method name and then this method is called.
481 * @param columnName Column name
482 * @return Value from field
485 public Object getValueFromColumn (final String columnName) throws IllegalArgumentException {
487 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
489 // A '$' means not our field
490 if (columnName.startsWith("$")) {
491 // Don't handle these
492 throw new IllegalArgumentException("columnsName contains $");
495 // Determine if the given column is boolean
496 if (this.isBooleanField(this, "BaseContact", columnName)) {
498 this.getLogger().debug("Column " + columnName + " represents a boolean field.");
500 // Yes, then call other method
501 return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true));
504 // Convert column name to field name
505 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
508 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
511 Object value = this.getField(this, "BaseContact", methodName);
514 this.getLogger().trace("value=" + value + " - EXIT!");
523 * @return the zipCode
526 public final Long getZipCode () {
533 * @param zipCode the zipCode to set
536 public final void setZipCode (final Long zipCode) {
537 this.zipCode = zipCode;
541 public int hashCode () {
542 // Validate gender instance
543 assert (this.getGender() instanceof Gender) : "gender is not set.";
546 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
547 hash = 79 * hash + this.getGender().hashCode();
548 hash = 79 * hash + Objects.hashCode(this.getSurname());
553 * Checks whether the contact is user's own data
558 public final boolean isOwnContact () {
559 return this.ownContact;
563 * Checks if given boolean field is available and set to same value
565 * @param columnName Column name to check
566 * @param bool Boolean value
567 * @return Whether all conditions are met
570 public boolean isValueEqual (final String columnName, final boolean bool) {
572 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
574 // Convert column name to field name
575 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
578 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
580 // Init class instance
581 boolean value = this.getBooleanField(this, "BaseContact", methodName);
584 this.getLogger().debug(MessageFormat.format("value={0}", value));
587 boolean isFound = (bool == value);
590 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
597 * Returns an iterator of all values from this object
598 * @return An iterator
601 public Iterator<Map.Entry<Field, Object>> iterator () {
602 return this.fieldIterator(this, "BaseContact");
606 * Shows this contact to the user
608 * @param client Client instance to use
611 public void show (final Client client) {
613 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
615 // The client must be set
616 if (client == null) {
618 throw new NullPointerException("client is null");
621 // Display name "box"
622 client.displayNameBox(this);
624 // Display address "box"
625 client.displayAddressBox(this);
627 // Display other data "box"
628 client.displayOtherDataBox(this);
632 * Enables the flag "own data" which signals that this contact is the user's
635 protected final void enableFlagOwnContact () {
636 this.ownContact = true;