]> git.mxchange.org Git - jcore.git/blob - src/org/mxchange/jcore/contact/BaseContact.java
Better return an iterator of Map.Entry<Field, Object>. This is really generic, it...
[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 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().getDatabaseValue(),
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                 return this.familyName;
331         }
332
333         /**
334          * Family name
335          *
336          * @param familyName the familyName to set
337          */
338         @Override
339         public final void setFamilyName (final String familyName) {
340                 this.familyName = familyName;
341         }
342
343         /**
344          * Fax number
345          *
346          * @return the faxNumber
347          */
348         @Override
349         public String getFaxNumber () {
350                 return this.faxNumber;
351         }
352
353         /**
354          * Fax number
355          *
356          * @param faxNumber the faxNumber to set
357          */
358         @Override
359         public final void setFaxNumber (final String faxNumber) {
360                 this.faxNumber = faxNumber;
361         }
362
363         /**
364          * Gender of the contact
365          *
366          * @return the gender
367          */
368         @Override
369         public Gender getGender () {
370                 return this.gender;
371         }
372
373         /**
374          * Gender of the contact
375          *
376          * @param gender the gender to set
377          */
378         @Override
379         public final void setGender (final Gender gender) {
380                 this.gender = gender;
381         }
382
383         /**
384          * House number
385          *
386          * @return the houseNumber
387          */
388         @Override
389         public long getHouseNumber () {
390                 return this.houseNumber;
391         }
392
393         /**
394          * House number
395          *
396          * @param houseNumber the houseNumber to set
397          */
398         @Override
399         public final void setHouseNumber (final long houseNumber) {
400                 this.houseNumber = houseNumber;
401         }
402
403         /**
404          * Phone number
405          *
406          * @return the phoneNumber
407          */
408         @Override
409         public String getPhoneNumber () {
410                 return this.phoneNumber;
411         }
412
413         /**
414          * Phone number
415          *
416          * @param phoneNumber the phoneNumber to set
417          */
418         @Override
419         public final void setPhoneNumber (final String phoneNumber) {
420                 this.phoneNumber = phoneNumber;
421         }
422
423         /**
424          * Street
425          *
426          * @return the street
427          */
428         @Override
429         public String getStreet () {
430                 return this.street;
431         }
432
433         /**
434          * Street
435          *
436          * @param street the street to set
437          */
438         @Override
439         public final void setStreet (final String street) {
440                 this.street = street;
441         }
442
443         /**
444          * Surname
445          *
446          * @return the surname
447          */
448         @Override
449         public final String getSurname () {
450                 return this.surname;
451         }
452
453         /**
454          * Surname
455          *
456          * @param surname the surname to set
457          */
458         @Override
459         public final void setSurname (final String surname) {
460                 this.surname = surname;
461         }
462
463         /**
464          * Some "getter" for a translated/human-readable gender
465          *
466          * @return gender Human-readable gender
467          */
468         @Override
469         public String getTranslatedGender () {
470                 // "Translate" it
471                 String translated = this.getBundle().getString(this.getGender().getMessageKey());
472
473                 // Return it
474                 return translated;
475         }
476
477         /**
478          * Some "getter for a value from given column name. This name will be
479          * translated into a method name and then this method is called.
480          *
481          * @param columnName Column name
482          * @return Value from field
483          */
484         @Override
485         public Object getValueFromColumn (final String columnName) throws IllegalArgumentException {
486                 // Trace message
487                 this.getLogger().trace(MessageFormat.format("columnName={0} - CALLED!", columnName));
488
489                 // A '$' means not our field
490                 if (columnName.startsWith("$")) {
491                         // Don't handle these
492                         throw new IllegalArgumentException("columnsName contains $");
493                 }
494
495                 // Determine if the given column is boolean
496                 if (this.isBooleanField(this, "BaseContact", columnName)) {
497                         // Debug message
498                         this.getLogger().debug("Column " + columnName + " represents a boolean field.");
499
500                         // Yes, then call other method
501                         return this.getBooleanField(this, "BaseContact", this.convertColumnNameToGetterMethod(columnName, true));
502                 }
503
504                 // Convert column name to field name
505                 String methodName = this.convertColumnNameToGetterMethod(columnName, false);
506
507                 // Debug message
508                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
509
510                 // Get field
511                 Object value = this.getField(this, "BaseContact", methodName);
512
513                 // Trace message
514                 this.getLogger().trace("value=" + value + " - EXIT!");
515
516                 // Return it
517                 return value;
518         }
519
520         /**
521          * ZIP code
522          *
523          * @return the zipCode
524          */
525         @Override
526         public final long getZipCode () {
527                 return this.zipCode;
528         }
529
530         /**
531          * ZIP code
532          *
533          * @param zipCode the zipCode to set
534          */
535         @Override
536         public final void setZipCode (final long zipCode) {
537                 this.zipCode = zipCode;
538         }
539
540         @Override
541         public int hashCode () {
542                 // Validate gender instance
543                 assert (this.getGender() instanceof Gender) : "gender is not set.";
544
545                 int hash = 7;
546                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
547                 hash = 79 * hash + this.getGender().hashCode();
548                 hash = 79 * hash + Objects.hashCode(this.getSurname());
549                 return hash;
550         }
551
552         /**
553          * Checks whether the contact is user's own data
554          *
555          * @return Own data?
556          */
557         @Override
558         public final boolean isOwnContact () {
559                 return this.ownContact;
560         }
561
562         /**
563          * Checks if given boolean field is available and set to same value
564          *
565          * @param columnName Column name to check
566          * @param bool Boolean value
567          * @return Whether all conditions are met
568          */
569         @Override
570         public boolean isValueEqual (final String columnName, final boolean bool) {
571                 // Trace message
572                 this.getLogger().trace(MessageFormat.format("columnName={0},bool={1} - CALLED!", columnName, bool));
573
574                 // Convert column name to field name
575                 String methodName = this.convertColumnNameToGetterMethod(columnName, true);
576
577                 // Debug message
578                 this.getLogger().debug(MessageFormat.format("field={0}", methodName));
579
580                 // Init class instance
581                 boolean value = this.getBooleanField(this, "BaseContact", methodName);
582
583                 // Debug message
584                 this.getLogger().debug(MessageFormat.format("value={0}", value));
585
586                 // Compare it
587                 boolean isFound = (bool == value);
588
589                 // Trace message
590                 this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound));
591
592                 // Return result
593                 return isFound;
594         }
595
596         /**
597          * Returns an iterator of all values from this object
598          * @return An iterator
599          */
600         @Override
601         public Iterator<Map.Entry<Field, Object>> iterator () {
602                 return this.fieldIterator(this, "BaseContact");
603         }
604
605         /**
606          * Shows this contact to the user
607          *
608          * @param client Client instance to use
609          */
610         @Override
611         public void show (final Client client) {
612                 // Trace message
613                 this.getLogger().trace(MessageFormat.format("client={0} - CALLED!", client)); //NOI18N
614
615                 // The client must be set
616                 if (client == null) {
617                         // Not set
618                         throw new NullPointerException("client is null");
619                 }
620
621                 // Display name "box"
622                 client.displayNameBox(this);
623
624                 // Display address "box"
625                 client.displayAddressBox(this);
626
627                 // Display other data "box"
628                 client.displayOtherDataBox(this);
629         }
630
631         /**
632          * Enables the flag "own data" which signals that this contact is the user's
633          * own data.
634          */
635         protected final void enableFlagOwnContact () {
636                 this.ownContact = true;
637         }
638 }