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