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