]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/contact/BaseContact.java
Some comments improved to make the purpose of BaseContact more clear
[jcore.git] / src / org / mxchange / jcore / contact / BaseContact.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcore.contact;
18
19 import java.lang.reflect.Field;
20 import java.lang.reflect.InvocationTargetException;
21 import java.text.MessageFormat;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Objects;
25 import org.mxchange.jcore.BaseFrameworkSystem;
26 import org.mxchange.jcore.client.Client;
27
28 /**
29  * A general contact class which should only be extended.
30  *
31  * @author Roland Haeder
32  * @version 0.0
33  */
34 public class BaseContact extends BaseFrameworkSystem implements Contact {
35
36         /**
37          * Birth day
38          */
39         private String birthday;
40
41         /**
42          * Cellphone number
43          */
44         private String cellphoneNumber;
45
46         /**
47          * City
48          */
49         private String city;
50
51         /**
52          * Optional comments
53          */
54         private String comment;
55
56         /**
57          * Companyname
58          */
59         private String companyName;
60
61         /**
62          * Country code
63          */
64         private String countryCode;
65
66         /**
67          * Email address
68          */
69         private String emailAddress;
70
71         /**
72          * Family name
73          */
74         private String familyName;
75
76         /**
77          * Fax number
78          */
79         private String faxNumber;
80
81         /**
82          * Gender instance
83          */
84         private Gender gender;
85
86         /**
87          * House number
88          */
89         private Long houseNumber;
90
91         /**
92          * Flag whether this contact is user's own data
93          */
94         private boolean ownContact;
95
96         /**
97          * Phone number
98          */
99         private String phoneNumber;
100
101         /**
102          * Street
103          */
104         private String street;
105
106         /**
107          * Surname
108          */
109         private String surname;
110
111         /**
112          * ZIP code
113          */
114         private Long zipCode;
115
116         /**
117          * No instances should be created of this class. Better extend this class
118          * and provide proper constructors.
119          */
120         protected BaseContact () {
121         }
122
123         /**
124          * Check if contacts are same or throw an exception
125          *
126          * @param object Other possible contact class
127          * @return Whether both contacts are same
128          * @todo Needs a lot improvements
129          */
130         @Override
131         public boolean equals (final Object object) {
132                 // Is it same type?
133                 if (!(object instanceof BaseContact)) {
134                         // Not equal types
135                         return false;
136                 } else if (!(object instanceof Contact)) {
137                         // Not correct interface
138                         return false;
139                 }
140
141                 // Try to cast
142                 Contact contact = (Contact) object;
143
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())));
148         }
149
150         /**
151          * Birth day
152          *
153          * @return the birthday
154          */
155         @Override
156         public String getBirthday () {
157                 return this.birthday;
158         }
159
160         /**
161          * Birth day
162          *
163          * @param birthday the birthday to set
164          */
165         @Override
166         public final void setBirthday (final String birthday) {
167                 this.birthday = birthday;
168         }
169
170         /**
171          * Cellphone number
172          *
173          * @return the cellphoneNumber
174          */
175         @Override
176         public final String getCellphoneNumber () {
177                 return this.cellphoneNumber;
178         }
179
180         /**
181          * Cellphone number
182          *
183          * @param cellphoneNumber the cellphoneNumber to set
184          */
185         @Override
186         public final void setCellphoneNumber (final String cellphoneNumber) {
187                 this.cellphoneNumber = cellphoneNumber;
188         }
189
190         /**
191          * City
192          *
193          * @return the city
194          */
195         @Override
196         public String getCity () {
197                 return this.city;
198         }
199
200         /**
201          * City
202          *
203          * @param city the city to set
204          */
205         @Override
206         public final void setCity (final String city) {
207                 this.city = city;
208         }
209
210         /**
211          * Comments
212          *
213          * @return the comment
214          */
215         @Override
216         public String getComment () {
217                 return this.comment;
218         }
219
220         /**
221          * Comments
222          *
223          * @param comment the comment to set
224          */
225         @Override
226         public final void setComment (final String comment) {
227                 this.comment = comment;
228         }
229
230         /**
231          * Companyname
232          *
233          * @return the companyName
234          */
235         @Override
236         public String getCompanyName () {
237                 return this.companyName;
238         }
239
240         /**
241          * Companyname
242          *
243          * @param companyName the companyName to set
244          */
245         @Override
246         public final void setCompanyName (final String companyName) {
247                 this.companyName = companyName;
248         }
249
250         /**
251          * Country code
252          *
253          * @return the countryCode
254          */
255         @Override
256         public String getCountryCode () {
257                 return this.countryCode;
258         }
259
260         /**
261          * Country code
262          *
263          * @param countryCode the countryCode to set
264          */
265         @Override
266         public final void setCountryCode (final String countryCode) {
267                 this.countryCode = countryCode;
268         }
269
270         /**
271          * "Serializes" this object into a CSV string (this time with semicolons)
272          *
273          * @return "CSV-serialized" version of the stored data
274          * @deprecated Don't use this anymore
275          */
276         @Deprecated
277         public String getCsvStringFromStoreableObject () {
278                 // Trace message
279                 this.getLogger().trace("CALLED!"); //NOI18N
280
281                 // Get all together
282                 String csvString = String.format(
283                                 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
284                                 this.isOwnContact(),
285                                 this.getGender().name(),
286                                 this.getSurname(),
287                                 this.getFamilyName(),
288                                 this.getCompanyName(),
289                                 this.getStreet(),
290                                 this.getZipCode(),
291                                 this.getCity(),
292                                 this.getCountryCode(),
293                                 this.getPhoneNumber(),
294                                 this.getFaxNumber(),
295                                 this.getCellphoneNumber(),
296                                 this.getEmailAddress(),
297                                 this.getBirthday(),
298                                 this.getComment()
299                 );
300
301                 // Then return it
302                 return csvString;
303         }
304
305         /**
306          * Email address
307          *
308          * @return the emailAddress
309          */
310         @Override
311         public String getEmailAddress () {
312                 return this.emailAddress;
313         }
314
315         /**
316          * Email address
317          *
318          * @param emailAddress the emailAddress to set
319          */
320         @Override
321         public final void setEmailAddress (final String emailAddress) {
322                 this.emailAddress = emailAddress;
323         }
324
325         /**
326          * Family name
327          *
328          * @return the familyName
329          */
330         @Override
331         public String getFamilyName () {
332                 //* NOISY-DEBUG: */ this.getLogger().trace("CALLED!");
333                 return this.familyName;
334         }
335
336         /**
337          * Family name
338          *
339          * @param familyName the familyName to set
340          */
341         @Override
342         public final void setFamilyName (final String familyName) {
343                 /* NOISY-DEBUG: */ this.getLogger().trace(MessageFormat.format("familyName={0} - CALLED!", familyName));
344                 this.familyName = familyName;
345         }
346
347         /**
348          * Fax number
349          *
350          * @return the faxNumber
351          */
352         @Override
353         public String getFaxNumber () {
354                 return this.faxNumber;
355         }
356
357         /**
358          * Fax number
359          *
360          * @param faxNumber the faxNumber to set
361          */
362         @Override
363         public final void setFaxNumber (final String faxNumber) {
364                 this.faxNumber = faxNumber;
365         }
366
367         /**
368          * Gender of the contact
369          *
370          * @return the gender
371          */
372         @Override
373         public Gender getGender () {
374                 return this.gender;
375         }
376
377         /**
378          * Gender of the contact
379          *
380          * @param gender the gender to set
381          */
382         @Override
383         public final void setGender (final Gender gender) {
384                 this.gender = gender;
385         }
386
387         /**
388          * House number
389          *
390          * @return the houseNumber
391          */
392         @Override
393         public Long getHouseNumber () {
394                 return this.houseNumber;
395         }
396
397         /**
398          * House number
399          *
400          * @param houseNumber the houseNumber to set
401          */
402         @Override
403         public final void setHouseNumber (final Long houseNumber) {
404                 this.houseNumber = houseNumber;
405         }
406
407         /**
408          * Phone number
409          *
410          * @return the phoneNumber
411          */
412         @Override
413         public String getPhoneNumber () {
414                 return this.phoneNumber;
415         }
416
417         /**
418          * Phone number
419          *
420          * @param phoneNumber the phoneNumber to set
421          */
422         @Override
423         public final void setPhoneNumber (final String phoneNumber) {
424                 this.phoneNumber = phoneNumber;
425         }
426
427         /**
428          * Street
429          *
430          * @return the street
431          */
432         @Override
433         public String getStreet () {
434                 return this.street;
435         }
436
437         /**
438          * Street
439          *
440          * @param street the street to set
441          */
442         @Override
443         public final void setStreet (final String street) {
444                 this.street = street;
445         }
446
447         /**
448          * Surname
449          *
450          * @return the surname
451          */
452         @Override
453         public final String getSurname () {
454                 return this.surname;
455         }
456
457         /**
458          * Surname
459          *
460          * @param surname the surname to set
461          */
462         @Override
463         public final void setSurname (final String surname) {
464                 this.surname = surname;
465         }
466
467         /**
468          * Some "getter" for a translated/human-readable gender
469          *
470          * @return gender Human-readable gender
471          */
472         @Override
473         public String getTranslatedGender () {
474                 // "Translate" it
475                 String translated = this.getBundle().getString(this.getGender().getMessageKey());
476
477                 // Return it
478                 return translated;
479         }
480
481         /**
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.
484          *
485          * @param columnName Column name
486          * @return Value from field
487          */
488         @Override
489         public Object getValueFromColumn (final String columnName) throws IllegalArgumentException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
490                 // Trace message
491                 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
492
493                 // A '$' means not our field
494                 if (columnName.startsWith("$")) {
495                         // Don't handle these
496                         throw new IllegalArgumentException("columnsName contains $");
497                 }
498
499                 // Determine if the given column is boolean
500                 if (this.isBooleanField(this, "BaseContact", columnName)) {
501                         // Debug message
502                         this.getLogger().debug("Column " + columnName + " represents a boolean field.");
503
504                         // Yes, then call other method
505                         return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true));
506                 }
507
508                 // Convert column name to field name
509                 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
510
511                 // Debug message
512                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
513
514                 // Get field
515                 Object value = this.getField(this, "BaseContact", methodName);
516
517                 // Trace message
518                 this.getLogger().trace("value=" + value + " - EXIT!");
519
520                 // Return it
521                 return value;
522         }
523
524         /**
525          * ZIP code
526          *
527          * @return the zipCode
528          */
529         @Override
530         public final Long getZipCode () {
531                 return this.zipCode;
532         }
533
534         /**
535          * ZIP code
536          *
537          * @param zipCode the zipCode to set
538          */
539         @Override
540         public final void setZipCode (final Long zipCode) {
541                 this.zipCode = zipCode;
542         }
543
544         @Override
545         public int hashCode () {
546                 // Validate gender instance
547                 assert (this.getGender() instanceof Gender) : "gender is not set.";
548
549                 int hash = 7;
550                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
551                 hash = 79 * hash + this.getGender().hashCode();
552                 hash = 79 * hash + Objects.hashCode(this.getSurname());
553                 return hash;
554         }
555
556         /**
557          * Checks whether the contact is user's own data
558          *
559          * @return Own data?
560          */
561         @Override
562         public final boolean isOwnContact () {
563                 return this.ownContact;
564         }
565
566         /**
567          * Checks if given boolean field is available and set to same value
568          *
569          * @param columnName Column name to check
570          * @param bool Boolean value
571          * @return Whether all conditions are met
572          */
573         @Override
574         public boolean isValueEqual (final String columnName, final boolean bool) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
575                 // Trace message
576                 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
577
578                 // Convert column name to field name
579                 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
580
581                 // Debug message
582                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
583
584                 // Init class instance
585                 boolean value = this.getBooleanField(this, "BaseContact", methodName);
586
587                 // Debug message
588                 this.getLogger().debug(MessageFormat.format("value={0}", value));
589
590                 // Compare it
591                 boolean isFound = (bool == value);
592
593                 // Trace message
594                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
595
596                 // Return result
597                 return isFound;
598         }
599
600         /**
601          * Returns an iterator of all values from this object
602          * @return An iterator
603          */
604         @Override
605         public Iterator<Map.Entry<Field, Object>> iterator () throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
606                 return this.fieldIterator(this, "BaseContact");
607         }
608
609         /**
610          * Shows this contact to the user
611          *
612          * @param client Client instance to use
613          */
614         @Override
615         public void show (final Client client) {
616                 // Trace message
617                 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
618
619                 // The client must be set
620                 if (client == null) {
621                         // Not set
622                         throw new NullPointerException("client is null");
623                 }
624
625                 // Display name "box"
626                 client.displayNameBox(this);
627
628                 // Display address "box"
629                 client.displayAddressBox(this);
630
631                 // Display other data "box"
632                 client.displayOtherDataBox(this);
633         }
634
635         /**
636          * Enables the flag "own data" which signals that this contact is the user's
637          * own data.
638          */
639         protected final void enableFlagOwnContact () {
640                 this.ownContact = true;
641         }
642 }