]> git.mxchange.org Git - addressbook-swing.git/blob - Addressbook/src/org/mxchange/addressbook/contact/BaseContact.java
40326c3e27de08b6ee110462929149c809c5c555
[addressbook-swing.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                 super();
118         }
119
120         /**
121          * Check if contacts are same or throw an exception
122          *
123          * @param object Other possible contact class
124          * @return Whether both contacts are same
125          * @todo Needs a lot improvements
126          */
127         @Override
128         public boolean equals (final Object object) {
129                 // Is it same type?
130                 if (!(object instanceof BaseContact)) {
131                         // Not equal types
132                         return false;
133                 } else if (!(object instanceof Contact)) {
134                         // Not correct interface
135                         return false;
136                 }
137
138                 // Try to cast
139                 Contact contact = (Contact) object;
140
141                 // Now test some data @todo Definedly needs improvement
142                 return ((this.getGender().equals(contact.getGender()))
143                                 && (this.getSurname().toLowerCase().equals(contact.getSurname().toLowerCase()))
144                                 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
145         }
146
147         /**
148          * Birth day
149          *
150          * @return the birthday
151          */
152         public String getBirthday () {
153                 return this.birthday;
154         }
155
156         /**
157          * Cellphone number
158          *
159          * @return the cellphoneNumber
160          */
161         public String getCellphoneNumber () {
162                 return this.cellphoneNumber;
163         }
164
165         /**
166          * City
167          *
168          * @return the city
169          */
170         public String getCity () {
171                 return this.city;
172         }
173
174         /**
175          * City
176          *
177          * @param city the city to set
178          */
179         private void setCity (final String city) {
180                 this.city = city;
181         }
182
183         /**
184          * Comments
185          *
186          * @return the comment
187          */
188         public String getComment () {
189                 return this.comment;
190         }
191
192         /**
193          * Comments
194          *
195          * @param comment the comment to set
196          */
197         private void setComment (final String comment) {
198                 this.comment = comment;
199         }
200
201         /**
202          * Companyname
203          *
204          * @return the companyName
205          */
206         public String getCompanyName () {
207                 return this.companyName;
208         }
209
210         /**
211          * Companyname
212          *
213          * @param companyName the companyName to set
214          */
215         private void setCompanyName (final String companyName) {
216                 this.companyName = companyName;
217         }
218
219         /**
220          * Country code
221          *
222          * @return the countryCode
223          */
224         public String getCountryCode () {
225                 return this.countryCode;
226         }
227
228         /**
229          * Country code
230          *
231          * @param countryCode the countryCode to set
232          */
233         private void setCountryCode (final String countryCode) {
234                 this.countryCode = countryCode;
235         }
236
237         /**
238          * "Serializes" this object into a CSV string (this time with semicolons)
239          *
240          * @return "CSV-serialized" version of the stored data
241          */
242         public String getCsvStringFromStoreableObject () {
243                 // Trace message
244                 this.getLogger().trace("CALLED!"); //NOI18N
245
246                 // Get all together
247                 String csvString = String.format(
248                                 "\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\";\"%s\"",
249                                 this.isOwnContact(),
250                                 this.getGender().getDatabaseValue(),
251                                 this.getSurname(),
252                                 this.getFamilyName(),
253                                 this.getCompanyName(),
254                                 this.getStreet(),
255                                 this.getZipCode(),
256                                 this.getCity(),
257                                 this.getCountryCode(),
258                                 this.getPhoneNumber(),
259                                 this.getFaxNumber(),
260                                 this.getCellphoneNumber(),
261                                 this.getEmailAddress(),
262                                 this.getBirthday(),
263                                 this.getComment()
264                 );
265
266                 // Then return it
267                 return csvString;
268         }
269
270         /**
271          * Email address
272          *
273          * @return the emailAddress
274          */
275         public String getEmailAddress () {
276                 return this.emailAddress;
277         }
278
279         /**
280          * Email address
281          *
282          * @param emailAddress the emailAddress to set
283          */
284         private void setEmailAddress (final String emailAddress) {
285                 this.emailAddress = emailAddress;
286         }
287
288         /**
289          * Family name
290          *
291          * @return the familyName
292          */
293         public String getFamilyName () {
294                 return this.familyName;
295         }
296
297         /**
298          * Family name
299          *
300          * @param familyName the familyName to set
301          */
302         private void setFamilyName (final String familyName) {
303                 this.familyName = familyName;
304         }
305
306         /**
307          * Fax number
308          *
309          * @return the faxNumber
310          */
311         public String getFaxNumber () {
312                 return this.faxNumber;
313         }
314
315         /**
316          * Fax number
317          *
318          * @param faxNumber the faxNumber to set
319          */
320         private void setFaxNumber (final String faxNumber) {
321                 this.faxNumber = faxNumber;
322         }
323
324         /**
325          * Gender of the contact
326          *
327          * @return the gender
328          */
329         public Gender getGender () {
330                 return this.gender;
331         }
332
333         /**
334          * Gender of the contact
335          *
336          * @param gender the gender to set
337          */
338         private void setGender (final Gender gender) {
339                 this.gender = gender;
340         }
341
342         /**
343          * House number
344          *
345          * @return the houseNumber
346          */
347         public int getHouseNumber () {
348                 return this.houseNumber;
349         }
350
351         /**
352          * Phone number
353          *
354          * @return the phoneNumber
355          */
356         public String getPhoneNumber () {
357                 return this.phoneNumber;
358         }
359
360         /**
361          * Street
362          *
363          * @return the street
364          */
365         public String getStreet () {
366                 return this.street;
367         }
368
369         /**
370          * Street
371          *
372          * @param street the street to set
373          */
374         protected final void setStreet (final String street) {
375                 this.street = street;
376         }
377
378         /**
379          * Surname
380          *
381          * @return the surname
382          */
383         public final String getSurname () {
384                 return this.surname;
385         }
386
387         /**
388          * Some "getter" for a translated/human-readable gender
389          *
390          * @return gender Human-readable gender
391          */
392         public String getTranslatedGender () {
393                 // Default init
394                 String translated = null;
395
396                 // "Translate" it
397                 switch (this.getGender()) {
398                         case MALE: // Mr.
399                                 translated = "Herr";
400                                 break;
401
402                         case FEMALE: // Mrs.
403                                 translated = "Frau";
404                                 break;
405
406                         case COMPANY: // "Company"
407                                 translated = "Firma";
408                                 break;
409
410                         default: // Unsupported
411                                 this.getLogger().error("Gender " + this.getGender() + " not supported.");
412                                 break;
413                 }
414
415                 // Return it
416                 return translated;
417         }
418
419         /**
420          * ZIP code
421          *
422          * @return the zipCode
423          */
424         public final long getZipCode () {
425                 return this.zipCode;
426         }
427
428         /**
429          * ZIP code
430          *
431          * @param zipCode the zipCode to set
432          */
433         protected final void setZipCode (final long zipCode) {
434                 this.zipCode = zipCode;
435         }
436
437         @Override
438         public int hashCode () {
439                 // Validate gender instance
440                 assert (this.getGender() instanceof Gender) : "gender is not set.";
441
442                 int hash = 7;
443                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
444                 hash = 79 * hash + this.getGender().hashCode();
445                 hash = 79 * hash + Objects.hashCode(this.getSurname());
446                 return hash;
447         }
448
449         /**
450          * Checks whether the contact is user's own data
451          *
452          * @return Own data?
453          */
454         public final boolean isOwnContact () {
455                 return this.ownContact;
456         }
457
458         /**
459          * Shows this contact to the user
460          *
461          * @param client Client instance to use
462          */
463         public void show (final Client client) {
464                 // Trace message
465                 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
466
467                 // The client must be set
468                 if (client == null) {
469                         // Not set
470                         throw new NullPointerException("client is null");
471                 }
472
473                 // Display name "box"
474                 client.displayNameBox((Contact) this);
475
476                 // Display address "box"
477                 client.displayAddressBox((Contact) this);
478
479                 // Display other data "box"
480                 client.displayOtherDataBox((Contact) this);
481         }
482
483         /**
484          * Updates address data in this Contact instance
485          *
486          * @param street Street
487          * @param zipCode ZIP code
488          * @param city City
489          * @param countryCode Country code
490          */
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         public void updateNameData (final Gender gender, final String surname, final String familyName, final String companyName) {
522                 // Trace message
523                 this.getLogger().trace(MessageFormat.format("gender={0},surname={1},familyName={2},companyName={3} - CALLED!", gender, surname, familyName, companyName)); //NOI18N
524
525                 // Set all
526                 this.setGender(gender);
527
528                 if (surname != null) {
529                         this.setSurname(surname);
530                 }
531                 if (familyName != null) {
532                         this.setFamilyName(familyName);
533                 }
534                 if (companyName != null) {
535                         this.setCompanyName(companyName);
536                 }
537
538                 // Trace message
539                 this.getLogger().trace("EXIT!"); //NOI18N
540         }
541
542         /**
543          * Updates other data in this Contact instance
544          *
545          * @param phoneNumber Phone number
546          * @param cellphoneNumber Cellphone number
547          * @param faxNumber Fax number
548          * @param emailAddress Email address
549          * @param birthday Birth day
550          * @param comment Comments
551          */
552         public void updateOtherData (final String phoneNumber, final String cellphoneNumber, final String faxNumber, final String emailAddress, final String birthday, final String comment) {
553                 // Trace message
554                 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
555
556                 // Set all
557                 if (phoneNumber != null) {
558                         this.setPhoneNumber(phoneNumber);
559                 }
560                 if (cellphoneNumber != null) {
561                         this.setCellphoneNumber(cellphoneNumber);
562                 }
563                 if (faxNumber != null) {
564                         this.setFaxNumber(faxNumber);
565                 }
566                 if (emailAddress != null) {
567                         this.setEmailAddress(emailAddress);
568                 }
569                 if (birthday != null) {
570                         this.setBirthday(birthday);
571                 }
572                 if (comment != null) {
573                         this.setComment(comment);
574                 }
575
576                 // Trace message
577                 this.getLogger().trace("EXIT!"); //NOI18N
578         }
579
580         /**
581          * Enables the flag "own data" which signals that this contact is the user's
582          * own data.
583          */
584         protected final void enableFlagOwnContact () {
585                 this.ownContact = true;
586         }
587
588         /**
589          * Surname
590          *
591          * @param surname the surname to set
592          */
593         protected final void setSurname (final String surname) {
594                 this.surname = surname;
595         }
596
597         /**
598          * Phone number
599          *
600          * @param phoneNumber the phoneNumber to set
601          */
602         protected final void setPhoneNumber (final String phoneNumber) {
603                 this.phoneNumber = phoneNumber;
604         }
605
606         /**
607          * House number
608          *
609          * @param houseNumber the houseNumber to set
610          */
611         protected final void setHouseNumber (final int houseNumber) {
612                 this.houseNumber = houseNumber;
613         }
614
615         /**
616          * Cellphone number
617          *
618          * @param cellphoneNumber the cellphoneNumber to set
619          */
620         protected final void setCellphoneNumber (final String cellphoneNumber) {
621                 this.cellphoneNumber = cellphoneNumber;
622         }
623
624         /**
625          * Birth day
626          *
627          * @param birthday the birthday to set
628          */
629         protected final void setBirthday (final String birthday) {
630                 this.birthday = birthday;
631         }
632 }