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