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