]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/contact/BaseContact.java
Some fixes
[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) throws IllegalArgumentException {
484                 // Trace message
485                 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
486
487                 // A '$' means not our field
488                 if (columnName.startsWith("$")) {
489                         // Don't handle these
490                         throw new IllegalArgumentException("columnsName contains $");
491                 }
492
493                 // Determine if the given column is boolean
494                 if (this.isBooleanField(this, "BaseContact", columnName)) {
495                         // Debug message
496                         this.getLogger().debug("Column " + columnName + " represents a boolean field.");
497
498                         // Yes, then call other method
499                         return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true));
500                 }
501
502                 // Convert column name to field name
503                 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
504
505                 // Debug message
506                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
507
508                 // Get field
509                 Object value = this.getField(this, "BaseContact", methodName);
510
511                 // Trace message
512                 this.getLogger().trace("value=" + value + " - EXIT!");
513
514                 // Return it
515                 return value;
516         }
517
518         /**
519          * ZIP code
520          *
521          * @return the zipCode
522          */
523         @Override
524         public final long getZipCode () {
525                 return this.zipCode;
526         }
527
528         /**
529          * ZIP code
530          *
531          * @param zipCode the zipCode to set
532          */
533         @Override
534         public final void setZipCode (final long zipCode) {
535                 this.zipCode = zipCode;
536         }
537
538         @Override
539         public int hashCode () {
540                 // Validate gender instance
541                 assert (this.getGender() instanceof Gender) : "gender is not set.";
542
543                 int hash = 7;
544                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
545                 hash = 79 * hash + this.getGender().hashCode();
546                 hash = 79 * hash + Objects.hashCode(this.getSurname());
547                 return hash;
548         }
549
550         /**
551          * Checks whether the contact is user's own data
552          *
553          * @return Own data?
554          */
555         @Override
556         public final boolean isOwnContact () {
557                 return this.ownContact;
558         }
559
560         /**
561          * Checks if given boolean field is available and set to same value
562          *
563          * @param columnName Column name to check
564          * @param bool Boolean value
565          * @return Whether all conditions are met
566          */
567         @Override
568         public boolean isValueEqual (final String columnName, final boolean bool) {
569                 // Trace message
570                 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
571
572                 // Convert column name to field name
573                 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
574
575                 // Debug message
576                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
577
578                 // Init class instance
579                 boolean value = this.getBooleanField(this, "BaseContact", methodName);
580
581                 // Debug message
582                 this.getLogger().debug(MessageFormat.format("value={0}", value));
583
584                 // Compare it
585                 boolean isFound = (bool == value);
586
587                 // Trace message
588                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
589
590                 // Return result
591                 return isFound;
592         }
593
594         /**
595          * Returns an iterator of all values from this object
596          * @return An iterator
597          */
598         @Override
599         public Iterator<Object> iterator () {
600                 return this.fieldIterator(this, "BaseContact");
601         }
602
603         /**
604          * Shows this contact to the user
605          *
606          * @param client Client instance to use
607          */
608         @Override
609         public void show (final Client client) {
610                 // Trace message
611                 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
612
613                 // The client must be set
614                 if (client == null) {
615                         // Not set
616                         throw new NullPointerException("client is null");
617                 }
618
619                 // Display name "box"
620                 client.displayNameBox(this);
621
622                 // Display address "box"
623                 client.displayAddressBox(this);
624
625                 // Display other data "box"
626                 client.displayOtherDataBox(this);
627         }
628
629         /**
630          * Enables the flag "own data" which signals that this contact is the user's
631          * own data.
632          */
633         protected final void enableFlagOwnContact () {
634                 this.ownContact = true;
635         }
636 }