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