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