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