]> git.mxchange.org Git - jfinancials-lib.git/blob - Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java
Moved a lot classes and interfaces (generalized) to new jcore project + added a few...
[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.BaseAddressbookSystem;
22 import org.mxchange.addressbook.client.AddressbookClient;
23 import org.mxchange.jcore.client.Client;
24
25 /**
26  * A general contact
27  *
28  * @author Roland Haeder
29  * @version 0.0
30  */
31 public class BaseContact extends BaseAddressbookSystem {
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          * @deprecated Don't use this anymore
241          */
242         @Deprecated
243         public String getCsvStringFromStoreableObject () {
244                 // Trace message
245                 this.getLogger().trace("CALLED!"); //NOI18N
246
247                 // Get all together
248                 String csvString = String.format(
249                                 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
250                                 this.isOwnContact(),
251                                 this.getGender().getDatabaseValue(),
252                                 this.getSurname(),
253                                 this.getFamilyName(),
254                                 this.getCompanyName(),
255                                 this.getStreet(),
256                                 this.getZipCode(),
257                                 this.getCity(),
258                                 this.getCountryCode(),
259                                 this.getPhoneNumber(),
260                                 this.getFaxNumber(),
261                                 this.getCellphoneNumber(),
262                                 this.getEmailAddress(),
263                                 this.getBirthday(),
264                                 this.getComment()
265                 );
266
267                 // Then return it
268                 return csvString;
269         }
270
271         /**
272          * Email address
273          *
274          * @return the emailAddress
275          */
276         public String getEmailAddress () {
277                 return this.emailAddress;
278         }
279
280         /**
281          * Email address
282          *
283          * @param emailAddress the emailAddress to set
284          */
285         private void setEmailAddress (final String emailAddress) {
286                 this.emailAddress = emailAddress;
287         }
288
289         /**
290          * Family name
291          *
292          * @return the familyName
293          */
294         public String getFamilyName () {
295                 return this.familyName;
296         }
297
298         /**
299          * Family name
300          *
301          * @param familyName the familyName to set
302          */
303         private void setFamilyName (final String familyName) {
304                 this.familyName = familyName;
305         }
306
307         /**
308          * Fax number
309          *
310          * @return the faxNumber
311          */
312         public String getFaxNumber () {
313                 return this.faxNumber;
314         }
315
316         /**
317          * Fax number
318          *
319          * @param faxNumber the faxNumber to set
320          */
321         private void setFaxNumber (final String faxNumber) {
322                 this.faxNumber = faxNumber;
323         }
324
325         /**
326          * Gender of the contact
327          *
328          * @return the gender
329          */
330         public Gender getGender () {
331                 return this.gender;
332         }
333
334         /**
335          * Gender of the contact
336          *
337          * @param gender the gender to set
338          */
339         private void setGender (final Gender gender) {
340                 this.gender = gender;
341         }
342
343         /**
344          * House number
345          *
346          * @return the houseNumber
347          */
348         public int getHouseNumber () {
349                 return this.houseNumber;
350         }
351
352         /**
353          * Phone number
354          *
355          * @return the phoneNumber
356          */
357         public String getPhoneNumber () {
358                 return this.phoneNumber;
359         }
360
361         /**
362          * Street
363          *
364          * @return the street
365          */
366         public String getStreet () {
367                 return this.street;
368         }
369
370         /**
371          * Street
372          *
373          * @param street the street to set
374          */
375         protected final void setStreet (final String street) {
376                 this.street = street;
377         }
378
379         /**
380          * Surname
381          *
382          * @return the surname
383          */
384         public final String getSurname () {
385                 return this.surname;
386         }
387
388         /**
389          * Some "getter" for a translated/human-readable gender
390          *
391          * @return gender Human-readable gender
392          */
393         public String getTranslatedGender () {
394                 // "Translate" it
395                 String translated = this.getBundle().getString(this.getGender().getMessageKey());
396
397                 // Return it
398                 return translated;
399         }
400
401         /**
402          * ZIP code
403          *
404          * @return the zipCode
405          */
406         public final long getZipCode () {
407                 return this.zipCode;
408         }
409
410         /**
411          * ZIP code
412          *
413          * @param zipCode the zipCode to set
414          */
415         protected final void setZipCode (final long zipCode) {
416                 this.zipCode = zipCode;
417         }
418         
419         @Override
420         public int hashCode () {
421                 // Validate gender instance
422                 assert (this.getGender() instanceof Gender) : "gender is not set.";
423
424                 int hash = 7;
425                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
426                 hash = 79 * hash + this.getGender().hashCode();
427                 hash = 79 * hash + Objects.hashCode(this.getSurname());
428                 return hash;
429         }
430
431         /**
432          * Checks whether the contact is user's own data
433          *
434          * @return Own data?
435          */
436         public final boolean isOwnContact () {
437                 return this.ownContact;
438         }
439
440         /**
441          * Shows this contact to the user
442          *
443          * @param client Client instance to use
444          */
445         public void show (final Client client) {
446                 // Trace message
447                 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
448
449                 // The client must be set
450                 if (client == null) {
451                         // Not set
452                         throw new NullPointerException("client is null");
453                 }
454
455                 // Cast client
456                 AddressbookClient c = (AddressbookClient) client;
457
458                 // Display name "box"
459                 c.displayNameBox((Contact) this);
460
461                 // Display address "box"
462                 c.displayAddressBox((Contact) this);
463
464                 // Display other data "box"
465                 c.displayOtherDataBox((Contact) this);
466         }
467
468         /**
469          * Updates address data in this Contact instance
470          *
471          * @param street Street
472          * @param zipCode ZIP code
473          * @param city City
474          * @param countryCode Country code
475          */
476         public void updateAddressData (final String street, final long zipCode, final String city, final String countryCode) {
477                 // Trace message
478                 this.getLogger().trace(MessageFormat.format("street={0},zipCode={1},city={2},countryCode={3} - CALLED!", street, zipCode, city, countryCode)); //NOI18N
479
480                 // Set all
481                 if (street != null) {
482                         this.setStreet(street);
483                 }
484                 if (zipCode > 0) {
485                         this.setZipCode(zipCode);
486                 }
487                 if (city != null) {
488                         this.setCity(city);
489                 }
490                 if (countryCode != null) {
491                         this.setCountryCode(countryCode);
492                 }
493
494                 // Trace message
495                 this.getLogger().trace("EXIT!"); //NOI18N
496         }
497
498         /**
499          * Updates name data in this Contact instance
500          *
501          * @param gender Gender (M, F, C)
502          * @param surname Surname
503          * @param familyName Family name
504          * @param companyName Company name
505          */
506         public void updateNameData (final Gender gender, final String surname, final String familyName, final String companyName) {
507                 // Trace message
508                 this.getLogger().trace(MessageFormat.format("gender={0},surname={1},familyName={2},companyName={3} - CALLED!", gender, surname, familyName, companyName)); //NOI18N
509
510                 // Set all
511                 this.setGender(gender);
512
513                 if (surname != null) {
514                         this.setSurname(surname);
515                 }
516                 if (familyName != null) {
517                         this.setFamilyName(familyName);
518                 }
519                 if (companyName != null) {
520                         this.setCompanyName(companyName);
521                 }
522
523                 // Trace message
524                 this.getLogger().trace("EXIT!"); //NOI18N
525         }
526
527         /**
528          * Updates other data in this Contact instance
529          *
530          * @param phoneNumber Phone number
531          * @param cellphoneNumber Cellphone number
532          * @param faxNumber Fax number
533          * @param emailAddress Email address
534          * @param birthday Birth day
535          * @param comment Comments
536          */
537         public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String birthday, final String comment) {
538                 // Trace message
539                 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
540
541                 // Set all
542                 if (phoneNumber != null) {
543                         this.setPhoneNumber(phoneNumber);
544                 }
545                 if (cellphoneNumber != null) {
546                         this.setCellphoneNumber(cellphoneNumber);
547                 }
548                 if (faxNumber != null) {
549                         this.setFaxNumber(faxNumber);
550                 }
551                 if (emailAddress != null) {
552                         this.setEmailAddress(emailAddress);
553                 }
554                 if (birthday != null) {
555                         this.setBirthday(birthday);
556                 }
557                 if (comment != null) {
558                         this.setComment(comment);
559                 }
560
561                 // Trace message
562                 this.getLogger().trace("EXIT!"); //NOI18N
563         }
564
565         /**
566          * Enables the flag "own data" which signals that this contact is the user's
567          * own data.
568          */
569         protected final void enableFlagOwnContact () {
570                 this.ownContact = true;
571         }
572
573         /**
574          * Surname
575          *
576          * @param surname the surname to set
577          */
578         protected final void setSurname (final String surname) {
579                 this.surname = surname;
580         }
581
582         /**
583          * Phone number
584          *
585          * @param phoneNumber the phoneNumber to set
586          */
587         protected final void setPhoneNumber (final String phoneNumber) {
588                 this.phoneNumber = phoneNumber;
589         }
590
591         /**
592          * House number
593          *
594          * @param houseNumber the houseNumber to set
595          */
596         protected final void setHouseNumber (final int houseNumber) {
597                 this.houseNumber = houseNumber;
598         }
599
600         /**
601          * Cellphone number
602          *
603          * @param cellphoneNumber the cellphoneNumber to set
604          */
605         protected final void setCellphoneNumber (final String cellphoneNumber) {
606                 this.cellphoneNumber = cellphoneNumber;
607         }
608
609         /**
610          * Birth day
611          *
612          * @param birthday the birthday to set
613          */
614         protected final void setBirthday (final String birthday) {
615                 this.birthday = birthday;
616         }
617
618         /**
619          * Some "getter for a value from given column name. This name will be
620          * translated into a method name and then this method is called.
621          *
622          * @param columnName Column name
623          * @return Value from field
624          */
625         @Override
626         public Object getValueFromColumn (final String columnName) {
627                 // Trace message
628                 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
629                 
630                 // Determine if the given column is boolean
631                 if (this.isBooleanField(this, "BaseContact", columnName)) {
632                         // Yes, then call other method
633                         return this.getBooleanField(this, "BaseContact", columnName);
634                 }
635                 
636                 // Convert column name to field name
637                 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
638                 
639                 // Debug message
640                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
641                 
642                 // Get field
643                 Object value = this.getField(this, "BaseContact", methodName);
644                 
645                 // Trace message
646                 this.getLogger().trace("value=" + value + " - EXIT!");
647                 
648                 // Return it
649                 return value;
650         }
651
652         /**
653          * Checks if given boolean field is available and set to same value
654          *
655          * @param columnName Column name to check
656          * @param bool Boolean value
657          * @return Whether all conditions are met
658          */
659         @Override
660         public boolean isValueEqual (final String columnName, final boolean bool) {
661                 // Trace message
662                 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
663                 
664                 // Convert column name to field name
665                 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
666                 
667                 // Debug message
668                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
669                 
670                 // Init class instance
671                 boolean value = this.getBooleanField(this, "BaseContact", methodName);
672                 
673                 // Debug message
674                 this.getLogger().debug(MessageFormat.format("value={0}", value));
675                 
676                 // Compare it
677                 boolean isFound = (bool == value);
678                 
679                 // Trace message
680                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
681                 
682                 // Return result
683                 return isFound;
684         }
685 }