]> git.mxchange.org Git - jfinancials-lib.git/blob - Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java
Some more cleanups + added initial SQL dump
[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\"",
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                 // Default init
392                 String translated = null;
393
394                 // "Translate" it
395                 switch (this.getGender()) {
396                         case MALE: // Mr.
397                                 translated = "Herr";
398                                 break;
399
400                         case FEMALE: // Mrs.
401                                 translated = "Frau";
402                                 break;
403
404                         case COMPANY: // "Company"
405                                 translated = "Firma";
406                                 break;
407
408                         default: // Unsupported
409                                 this.getLogger().error("Gender " + this.getGender() + " not supported.");
410                                 break;
411                 }
412
413                 // Return it
414                 return translated;
415         }
416
417         /**
418          * ZIP code
419          *
420          * @return the zipCode
421          */
422         public final long getZipCode () {
423                 return this.zipCode;
424         }
425
426         /**
427          * ZIP code
428          *
429          * @param zipCode the zipCode to set
430          */
431         protected final void setZipCode (final long zipCode) {
432                 this.zipCode = zipCode;
433         }
434
435         @Override
436         public int hashCode () {
437                 // Validate gender instance
438                 assert (this.getGender() instanceof Gender) : "gender is not set.";
439
440                 int hash = 7;
441                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
442                 hash = 79 * hash + this.getGender().hashCode();
443                 hash = 79 * hash + Objects.hashCode(this.getSurname());
444                 return hash;
445         }
446
447         /**
448          * Checks whether the contact is user's own data
449          *
450          * @return Own data?
451          */
452         public final boolean isOwnContact () {
453                 return this.ownContact;
454         }
455
456         /**
457          * Shows this contact to the user
458          *
459          * @param client Client instance to use
460          */
461         public void show (final Client client) {
462                 // Trace message
463                 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
464
465                 // The client must be set
466                 if (client == null) {
467                         // Not set
468                         throw new NullPointerException("client is null");
469                 }
470
471                 // Display name "box"
472                 client.displayNameBox((Contact) this);
473
474                 // Display address "box"
475                 client.displayAddressBox((Contact) this);
476
477                 // Display other data "box"
478                 client.displayOtherDataBox((Contact) this);
479         }
480
481         /**
482          * Updates address data in this Contact instance
483          *
484          * @param street Street
485          * @param zipCode ZIP code
486          * @param city City
487          * @param countryCode Country code
488          */
489         public void updateAddressData (final String street, final long zipCode, final String city, final String countryCode) {
490                 // Trace message
491                 this.getLogger().trace(MessageFormat.format("street={0},zipCode={1},city={2},countryCode={3} - CALLED!", street, zipCode, city, countryCode)); //NOI18N
492
493                 // Set all
494                 if (street != null) {
495                         this.setStreet(street);
496                 }
497                 if (zipCode > 0) {
498                         this.setZipCode(zipCode);
499                 }
500                 if (city != null) {
501                         this.setCity(city);
502                 }
503                 if (countryCode != null) {
504                         this.setCountryCode(countryCode);
505                 }
506
507                 // Trace message
508                 this.getLogger().trace("EXIT!"); //NOI18N
509         }
510
511         /**
512          * Updates name data in this Contact instance
513          *
514          * @param gender Gender (M, F, C)
515          * @param surname Surname
516          * @param familyName Family name
517          * @param companyName Company name
518          */
519         public void updateNameData (final Gender gender, final String surname, final String familyName, final String companyName) {
520                 // Trace message
521                 this.getLogger().trace(MessageFormat.format("gender={0},surname={1},familyName={2},companyName={3} - CALLED!", gender, surname, familyName, companyName)); //NOI18N
522
523                 // Set all
524                 this.setGender(gender);
525
526                 if (surname != null) {
527                         this.setSurname(surname);
528                 }
529                 if (familyName != null) {
530                         this.setFamilyName(familyName);
531                 }
532                 if (companyName != null) {
533                         this.setCompanyName(companyName);
534                 }
535
536                 // Trace message
537                 this.getLogger().trace("EXIT!"); //NOI18N
538         }
539
540         /**
541          * Updates other data in this Contact instance
542          *
543          * @param phoneNumber Phone number
544          * @param cellphoneNumber Cellphone number
545          * @param faxNumber Fax number
546          * @param emailAddress Email address
547          * @param birthday Birth day
548          * @param comment Comments
549          */
550         public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String birthday, final String comment) {
551                 // Trace message
552                 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
553
554                 // Set all
555                 if (phoneNumber != null) {
556                         this.setPhoneNumber(phoneNumber);
557                 }
558                 if (cellphoneNumber != null) {
559                         this.setCellphoneNumber(cellphoneNumber);
560                 }
561                 if (faxNumber != null) {
562                         this.setFaxNumber(faxNumber);
563                 }
564                 if (emailAddress != null) {
565                         this.setEmailAddress(emailAddress);
566                 }
567                 if (birthday != null) {
568                         this.setBirthday(birthday);
569                 }
570                 if (comment != null) {
571                         this.setComment(comment);
572                 }
573
574                 // Trace message
575                 this.getLogger().trace("EXIT!"); //NOI18N
576         }
577
578         /**
579          * Enables the flag "own data" which signals that this contact is the user's
580          * own data.
581          */
582         protected final void enableFlagOwnContact () {
583                 this.ownContact = true;
584         }
585
586         /**
587          * Surname
588          *
589          * @param surname the surname to set
590          */
591         protected final void setSurname (final String surname) {
592                 this.surname = surname;
593         }
594
595         /**
596          * Phone number
597          *
598          * @param phoneNumber the phoneNumber to set
599          */
600         protected final void setPhoneNumber (final String phoneNumber) {
601                 this.phoneNumber = phoneNumber;
602         }
603
604         /**
605          * House number
606          *
607          * @param houseNumber the houseNumber to set
608          */
609         protected final void setHouseNumber (final int houseNumber) {
610                 this.houseNumber = houseNumber;
611         }
612
613         /**
614          * Cellphone number
615          *
616          * @param cellphoneNumber the cellphoneNumber to set
617          */
618         protected final void setCellphoneNumber (final String cellphoneNumber) {
619                 this.cellphoneNumber = cellphoneNumber;
620         }
621
622         /**
623          * Birth day
624          *
625          * @param birthday the birthday to set
626          */
627         protected final void setBirthday (final String birthday) {
628                 this.birthday = birthday;
629         }
630
631         /**
632          * Checks if given boolean field is available and set to same value
633          *
634          * @param columnName Column name to check
635          * @param bool Boolean value
636          * @return Whether all conditions are met
637          */
638         public boolean isValueEqual (final String columnName, final boolean bool) {
639                 // Trace message
640                 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
641
642                 // Convert column name to field name
643                 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
644
645                 // Debug message
646                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
647
648                 // Init class instance
649                 boolean value = this.getBooleanField(this, "BaseContact", methodName);
650
651                 // Debug message
652                 this.getLogger().debug(MessageFormat.format("value={0}", value));
653
654                 // Compare it
655                 boolean isFound = (bool == value);
656
657                 // Trace message
658                 this.getLogger().trace("isFound=" + isFound + " - EXIT!");
659
660                 // Return result
661                 return isFound;
662         }
663 }