]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/contact/BaseContact.java
Moved more stuff from addressbook to here
[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.text.MessageFormat;
20 import java.util.Objects;
21 import org.mxchange.jcore.BaseFrameworkSystem;
22 import org.mxchange.jcore.client.Client;
23
24 /**
25  * A general contact
26  *
27  * @author Roland Haeder
28  * @version 0.0
29  */
30 public class BaseContact extends BaseFrameworkSystem implements Contact {
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         @Override
151         public String getBirthday () {
152                 return this.birthday;
153         }
154
155         /**
156          * Cellphone number
157          *
158          * @return the cellphoneNumber
159          */
160         @Override
161         public String getCellphoneNumber () {
162                 return this.cellphoneNumber;
163         }
164
165         /**
166          * City
167          *
168          * @return the city
169          */
170         @Override
171         public String getCity () {
172                 return this.city;
173         }
174
175         /**
176          * City
177          *
178          * @param city the city to set
179          */
180         private void setCity (final String city) {
181                 this.city = city;
182         }
183
184         /**
185          * Comments
186          *
187          * @return the comment
188          */
189         @Override
190         public String getComment () {
191                 return this.comment;
192         }
193
194         /**
195          * Comments
196          *
197          * @param comment the comment to set
198          */
199         private void setComment (final String comment) {
200                 this.comment = comment;
201         }
202
203         /**
204          * Companyname
205          *
206          * @return the companyName
207          */
208         @Override
209         public String getCompanyName () {
210                 return this.companyName;
211         }
212
213         /**
214          * Companyname
215          *
216          * @param companyName the companyName to set
217          */
218         private void setCompanyName (final String companyName) {
219                 this.companyName = companyName;
220         }
221
222         /**
223          * Country code
224          *
225          * @return the countryCode
226          */
227         @Override
228         public String getCountryCode () {
229                 return this.countryCode;
230         }
231
232         /**
233          * Country code
234          *
235          * @param countryCode the countryCode to set
236          */
237         private void setCountryCode (final String countryCode) {
238                 this.countryCode = countryCode;
239         }
240
241         /**
242          * "Serializes" this object into a CSV string (this time with semicolons)
243          *
244          * @return "CSV-serialized" version of the stored data
245          * @deprecated Don't use this anymore
246          */
247         @Deprecated
248         public String getCsvStringFromStoreableObject () {
249                 // Trace message
250                 this.getLogger().trace("CALLED!"); //NOI18N
251
252                 // Get all together
253                 String csvString = String.format(
254                                 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"", //NOI18N
255                                 this.isOwnContact(),
256                                 this.getGender().getDatabaseValue(),
257                                 this.getSurname(),
258                                 this.getFamilyName(),
259                                 this.getCompanyName(),
260                                 this.getStreet(),
261                                 this.getZipCode(),
262                                 this.getCity(),
263                                 this.getCountryCode(),
264                                 this.getPhoneNumber(),
265                                 this.getFaxNumber(),
266                                 this.getCellphoneNumber(),
267                                 this.getEmailAddress(),
268                                 this.getBirthday(),
269                                 this.getComment()
270                 );
271
272                 // Then return it
273                 return csvString;
274         }
275
276         /**
277          * Email address
278          *
279          * @return the emailAddress
280          */
281         @Override
282         public String getEmailAddress () {
283                 return this.emailAddress;
284         }
285
286         /**
287          * Email address
288          *
289          * @param emailAddress the emailAddress to set
290          */
291         private void setEmailAddress (final String emailAddress) {
292                 this.emailAddress = emailAddress;
293         }
294
295         /**
296          * Family name
297          *
298          * @return the familyName
299          */
300         @Override
301         public String getFamilyName () {
302                 return this.familyName;
303         }
304
305         /**
306          * Family name
307          *
308          * @param familyName the familyName to set
309          */
310         private void setFamilyName (final String familyName) {
311                 this.familyName = familyName;
312         }
313
314         /**
315          * Fax number
316          *
317          * @return the faxNumber
318          */
319         @Override
320         public String getFaxNumber () {
321                 return this.faxNumber;
322         }
323
324         /**
325          * Fax number
326          *
327          * @param faxNumber the faxNumber to set
328          */
329         private void setFaxNumber (final String faxNumber) {
330                 this.faxNumber = faxNumber;
331         }
332
333         /**
334          * Gender of the contact
335          *
336          * @return the gender
337          */
338         @Override
339         public Gender getGender () {
340                 return this.gender;
341         }
342
343         /**
344          * Gender of the contact
345          *
346          * @param gender the gender to set
347          */
348         private void setGender (final Gender gender) {
349                 this.gender = gender;
350         }
351
352         /**
353          * House number
354          *
355          * @return the houseNumber
356          */
357         @Override
358         public int getHouseNumber () {
359                 return this.houseNumber;
360         }
361
362         /**
363          * Phone number
364          *
365          * @return the phoneNumber
366          */
367         @Override
368         public String getPhoneNumber () {
369                 return this.phoneNumber;
370         }
371
372         /**
373          * Street
374          *
375          * @return the street
376          */
377         @Override
378         public String getStreet () {
379                 return this.street;
380         }
381
382         /**
383          * Street
384          *
385          * @param street the street to set
386          */
387         protected final void setStreet (final String street) {
388                 this.street = street;
389         }
390
391         /**
392          * Surname
393          *
394          * @return the surname
395          */
396         @Override
397         public final String getSurname () {
398                 return this.surname;
399         }
400
401         /**
402          * Some "getter" for a translated/human-readable gender
403          *
404          * @return gender Human-readable gender
405          */
406         @Override
407         public String getTranslatedGender () {
408                 // "Translate" it
409                 String translated = this.getBundle().getString(this.getGender().getMessageKey());
410
411                 // Return it
412                 return translated;
413         }
414
415         /**
416          * ZIP code
417          *
418          * @return the zipCode
419          */
420         @Override
421         public final long getZipCode () {
422                 return this.zipCode;
423         }
424
425         /**
426          * ZIP code
427          *
428          * @param zipCode the zipCode to set
429          */
430         protected final void setZipCode (final long zipCode) {
431                 this.zipCode = zipCode;
432         }
433         
434         @Override
435         public int hashCode () {
436                 // Validate gender instance
437                 assert (this.getGender() instanceof Gender) : "gender is not set.";
438
439                 int hash = 7;
440                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
441                 hash = 79 * hash + this.getGender().hashCode();
442                 hash = 79 * hash + Objects.hashCode(this.getSurname());
443                 return hash;
444         }
445
446         /**
447          * Checks whether the contact is user's own data
448          *
449          * @return Own data?
450          */
451         @Override
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         @Override
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(this);
474
475                 // Display address "box"
476                 client.displayAddressBox(this);
477
478                 // Display other data "box"
479                 client.displayOtherDataBox(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         @Override
491         public void updateAddressData (final String street, final long zipCode, final String city, final String countryCode) {
492                 // Trace message
493                 this.getLogger().trace(MessageFormat.format("street={0},zipCode={1},city={2},countryCode={3} - CALLED!", street, zipCode, city, countryCode)); //NOI18N
494
495                 // Set all
496                 if (street != null) {
497                         this.setStreet(street);
498                 }
499                 if (zipCode > 0) {
500                         this.setZipCode(zipCode);
501                 }
502                 if (city != null) {
503                         this.setCity(city);
504                 }
505                 if (countryCode != null) {
506                         this.setCountryCode(countryCode);
507                 }
508
509                 // Trace message
510                 this.getLogger().trace("EXIT!"); //NOI18N
511         }
512
513         /**
514          * Updates name data in this Contact instance
515          *
516          * @param gender Gender (M, F, C)
517          * @param surname Surname
518          * @param familyName Family name
519          * @param companyName Company name
520          */
521         @Override
522         public void updateNameData (final Gender gender, final String surname, final String familyName, final String companyName) {
523                 // Trace message
524                 this.getLogger().trace(MessageFormat.format("gender={0},surname={1},familyName={2},companyName={3} - CALLED!", gender, surname, familyName, companyName)); //NOI18N
525
526                 // Set all
527                 this.setGender(gender);
528
529                 if (surname != null) {
530                         this.setSurname(surname);
531                 }
532                 if (familyName != null) {
533                         this.setFamilyName(familyName);
534                 }
535                 if (companyName != null) {
536                         this.setCompanyName(companyName);
537                 }
538
539                 // Trace message
540                 this.getLogger().trace("EXIT!"); //NOI18N
541         }
542
543         /**
544          * Updates other data in this Contact instance
545          *
546          * @param phoneNumber Phone number
547          * @param cellphoneNumber Cellphone number
548          * @param faxNumber Fax number
549          * @param emailAddress Email address
550          * @param birthday Birth day
551          * @param comment Comments
552          */
553         @Override
554         public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String birthday, final String comment) {
555                 // Trace message
556                 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
557
558                 // Set all
559                 if (phoneNumber != null) {
560                         this.setPhoneNumber(phoneNumber);
561                 }
562                 if (cellphoneNumber != null) {
563                         this.setCellphoneNumber(cellphoneNumber);
564                 }
565                 if (faxNumber != null) {
566                         this.setFaxNumber(faxNumber);
567                 }
568                 if (emailAddress != null) {
569                         this.setEmailAddress(emailAddress);
570                 }
571                 if (birthday != null) {
572                         this.setBirthday(birthday);
573                 }
574                 if (comment != null) {
575                         this.setComment(comment);
576                 }
577
578                 // Trace message
579                 this.getLogger().trace("EXIT!"); //NOI18N
580         }
581
582         /**
583          * Enables the flag "own data" which signals that this contact is the user's
584          * own data.
585          */
586         protected final void enableFlagOwnContact () {
587                 this.ownContact = true;
588         }
589
590         /**
591          * Surname
592          *
593          * @param surname the surname to set
594          */
595         protected final void setSurname (final String surname) {
596                 this.surname = surname;
597         }
598
599         /**
600          * Phone number
601          *
602          * @param phoneNumber the phoneNumber to set
603          */
604         protected final void setPhoneNumber (final String phoneNumber) {
605                 this.phoneNumber = phoneNumber;
606         }
607
608         /**
609          * House number
610          *
611          * @param houseNumber the houseNumber to set
612          */
613         protected final void setHouseNumber (final int houseNumber) {
614                 this.houseNumber = houseNumber;
615         }
616
617         /**
618          * Cellphone number
619          *
620          * @param cellphoneNumber the cellphoneNumber to set
621          */
622         protected final void setCellphoneNumber (final String cellphoneNumber) {
623                 this.cellphoneNumber = cellphoneNumber;
624         }
625
626         /**
627          * Birth day
628          *
629          * @param birthday the birthday to set
630          */
631         protected final void setBirthday (final String birthday) {
632                 this.birthday = birthday;
633         }
634
635         /**
636          * Some "getter for a value from given column name. This name will be
637          * translated into a method name and then this method is called.
638          *
639          * @param columnName Column name
640          * @return Value from field
641          */
642         @Override
643         public Object getValueFromColumn (final String columnName) {
644                 // Trace message
645                 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
646                 
647                 // Determine if the given column is boolean
648                 if (this.isBooleanField(this, "BaseContact", columnName)) {
649                         // Yes, then call other method
650                         return this.getBooleanField(this, "BaseContact", columnName);
651                 }
652                 
653                 // Convert column name to field name
654                 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
655                 
656                 // Debug message
657                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
658                 
659                 // Get field
660                 Object value = this.getField(this, "BaseContact", methodName);
661                 
662                 // Trace message
663                 this.getLogger().trace("value=" + value + " - EXIT!");
664                 
665                 // Return it
666                 return value;
667         }
668
669         /**
670          * Checks if given boolean field is available and set to same value
671          *
672          * @param columnName Column name to check
673          * @param bool Boolean value
674          * @return Whether all conditions are met
675          */
676         @Override
677         public boolean isValueEqual (final String columnName, final boolean bool) {
678                 // Trace message
679                 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
680                 
681                 // Convert column name to field name
682                 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
683                 
684                 // Debug message
685                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
686                 
687                 // Init class instance
688                 boolean value = this.getBooleanField(this, "BaseContact", methodName);
689                 
690                 // Debug message
691                 this.getLogger().debug(MessageFormat.format("value={0}", value));
692                 
693                 // Compare it
694                 boolean isFound = (bool == value);
695                 
696                 // Trace message
697                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
698                 
699                 // Return result
700                 return isFound;
701         }
702 }