]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/model/contact/UserContact.java
df12788753654fb1c0d77ce10c661b4e56f9ef09
[jcontacts-core.git] / src / org / mxchange / jcontacts / model / contact / UserContact.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
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.jcontacts.model.contact;
18
19 import java.util.Date;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.EnumType;
26 import javax.persistence.Enumerated;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.Index;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.Lob;
33 import javax.persistence.NamedQueries;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import javax.persistence.Transient;
40 import org.apache.commons.lang3.StringUtils;
41 import org.mxchange.jcontacts.model.contact.title.PersonalTitle;
42 import org.mxchange.jcoreutils.Comparables;
43 import org.mxchange.jcoreutils.SafeNumberUtils;
44 import org.mxchange.jcountry.model.data.Country;
45 import org.mxchange.jcountry.model.data.CountryData;
46 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
47 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
48 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
49 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
50 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
51 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
52
53 /**
54  * A general contact class which serves as an entity.
55  * <p>
56  * @author Roland Häder<roland@mxchange.org>
57  * @version 0.0
58  */
59 @Entity (name = "contacts")
60 @Table (
61                 name = "contacts",
62                 indexes = {
63                         @Index (
64                                         name = "contact_personal_title",
65                                         columnList = "contact_personal_title"
66                         )
67                 }
68 )
69 @NamedQueries (
70                 {
71                         @NamedQuery (name = "AllContacts", query = "SELECT c FROM contacts AS c ORDER BY c.contactId ASC")
72                 }
73 )
74 @SuppressWarnings ("PersistenceUnitPresent")
75 public class UserContact implements Contact {
76
77         /**
78          * Serial number
79          */
80         @Transient
81         private static final long serialVersionUID = 58_744_284_981_863L;
82
83         /**
84          * Birth day
85          */
86         @Column (name = "contact_birthday")
87         @Temporal (TemporalType.DATE)
88         private Date contactBirthday;
89
90         /**
91          * City
92          */
93         @Column (name = "contact_city", length = 100)
94         private String contactCity;
95
96         /**
97          * Optional comments
98          */
99         @Lob
100         @Column (name = "contact_comment")
101         private String contactComment;
102
103         /**
104          * Country code
105          */
106         @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
107         @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
108         private Country contactCountry;
109
110         /**
111          * Email address
112          */
113         @Column (name = "contact_email_address", length = 100, unique = true)
114         private String contactEmailAddress;
115
116         /**
117          * When the contact has been created
118          */
119         @Basic (optional = false)
120         @Temporal (TemporalType.TIMESTAMP)
121         @Column (name = "contact_entry_created", nullable = false)
122         private Date contactEntryCreated;
123
124         /**
125          * When the contact has been updated
126          */
127         @Temporal (TemporalType.TIMESTAMP)
128         @Column (name = "contact_entry_updated")
129         private Date contactEntryUpdated;
130
131         /**
132          * Family name
133          */
134         @Basic (optional = false)
135         @Column (name = "contact_family_name", length = 100, nullable = false)
136         private String contactFamilyName;
137
138         /**
139          * Fax number
140          */
141         @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
142         @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
143         private DialableFaxNumber contactFaxNumber;
144
145         /**
146          * First name
147          */
148         @Basic (optional = false)
149         @Column (name = "contact_first_name", length = 100, nullable = false)
150         private String contactFirstName;
151
152         /**
153          * House number
154          */
155         @Column (name = "contact_house_number")
156         private Short contactHouseNumber;
157
158         /**
159          * House number extension
160          */
161         @Column (name = "contact_house_number_extension", length = 5)
162         private String contactHouseNumberExtension;
163
164         /**
165          * Id number
166          */
167         @Id
168         @GeneratedValue (strategy = GenerationType.IDENTITY)
169         @Column (name = "contact_id", nullable = false, updatable = false)
170         private Long contactId;
171
172         /**
173          * Cellphone number
174          */
175         @JoinColumn (name = "contact_mobile_number_id", referencedColumnName = "mobile_id", unique = true)
176         @OneToOne (targetEntity = MobileNumber.class, cascade = CascadeType.ALL)
177         private DialableMobileNumber contactMobileNumber;
178
179         /**
180          * Flag whether this contact is user's own data
181          */
182         @Basic (optional = false)
183         @Column (name = "contact_own_contact", nullable = false)
184         private Boolean contactOwnContact;
185
186         /**
187          * Contact's personal title (Mr./Mrs.)
188          */
189         @Basic (optional = false)
190         @Column (name = "contact_personal_title", nullable = false)
191         @Enumerated (EnumType.STRING)
192         private PersonalTitle contactPersonalTitle;
193
194         /**
195          * Phone number
196          */
197         @JoinColumn (name = "contact_landline_number_id", referencedColumnName = "landline_id", unique = true)
198         @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
199         private DialableLandLineNumber contactPhoneNumber;
200
201         /**
202          * Street
203          */
204         @Column (name = "contact_street")
205         private String contactStreet;
206
207         /**
208          * Title (Doctor, etc)
209          */
210         @Column (name = "contact_title")
211         private String contactTitle;
212
213         /**
214          * ZIP code
215          */
216         @Column (name = "contact_zip_code")
217         private Integer contactZipCode;
218
219         /**
220          * Default constructor
221          */
222         public UserContact () {
223                 // Default is not user's own contact
224                 this.contactOwnContact = Boolean.FALSE;
225         }
226
227         /**
228          * Constructor for title and names
229          * <p>
230          * @param contactPersonalTitle Personal title
231          * @param contactFirstName     First name
232          * @param contactFamilyName    Family name
233          */
234         public UserContact (final PersonalTitle contactPersonalTitle, final String contactFirstName, final String contactFamilyName) {
235                 // Invoke default constructor
236                 this();
237
238                 // Are all parameter set?
239                 if (null == contactFamilyName) {
240                         // Throw NPE
241                         throw new NullPointerException("contactFamilyName is null"); //NOI18N
242                 } else if (contactFamilyName.isEmpty()) {
243                         // Throw IAE
244                         throw new IllegalArgumentException("contactFamilyName is empty"); //NOI18N
245                 } else if (null == contactFirstName) {
246                         // Throw NPE
247                         throw new NullPointerException("contactFirstName is null"); //NOI18N
248                 } else if (contactFirstName.isEmpty()) {
249                         // Throw IAE
250                         throw new IllegalArgumentException("contactFirstName is empty"); //NOI18N
251                 } else if (null == contactPersonalTitle) {
252                         // Throw NPE
253                         throw new NullPointerException("contactPersonalTitle is null"); //NOI18N
254                 }
255
256                 // Set all
257                 this.contactPersonalTitle = contactPersonalTitle;
258                 this.contactFirstName = contactFirstName;
259                 this.contactFamilyName = contactFamilyName;
260         }
261
262         @Override
263         public int compareTo (final Contact contact) {
264                 // Checkparameter and return 0 if equal
265                 if (null == contact) {
266                         // Should not happen
267                         throw new NullPointerException("contact is null"); //NOI18N
268                 } else if (contact.equals(this)) {
269                         // Same object
270                         return 0;
271                 }
272
273                 // Init comparators
274                 final int comparators[] = {
275                         // First check country
276                         this.getContactCountry().compareTo(contact.getContactCountry()),
277                         // ... then ZIP code
278                         SafeNumberUtils.compare(this.getContactZipCode(), contact.getContactZipCode()),
279                         // ... and city
280                         StringUtils.compare(this.getContactCity(), contact.getContactCity()),
281                         // ... street name
282                         StringUtils.compareIgnoreCase(this.getContactStreet(), contact.getContactStreet()),
283                         // ... house number
284                         SafeNumberUtils.compare(this.getContactHouseNumber(), contact.getContactHouseNumber()),
285                         // ... extension
286                         StringUtils.compareIgnoreCase(this.getContactHouseNumberExtension(), contact.getContactHouseNumberExtension()),
287                         // ... now it is sure that address is different/same, continue with personal title
288                         this.getContactPersonalTitle().compareTo(contact.getContactPersonalTitle()),
289                         // ... academical title
290                         StringUtils.compareIgnoreCase(this.getContactTitle(), contact.getContactTitle()),
291                         // .. family name is next ...
292                         this.getContactFamilyName().compareToIgnoreCase(contact.getContactFamilyName()),
293                         // .. first name is second ...
294                         this.getContactFirstName().compareToIgnoreCase(contact.getContactFirstName()),
295                         // ... next is email address
296                         StringUtils.compareIgnoreCase(this.getContactEmailAddress(), contact.getContactEmailAddress()),};
297
298                 // Check all values
299                 final int comparison = Comparables.checkAll(comparators);
300
301                 // Return value
302                 return comparison;
303         }
304
305         @Override
306         public boolean equals (final Object object) {
307                 if (this == object) {
308                         return true;
309                 } else if (null == object) {
310                         return false;
311                 } else if (this.getClass() != object.getClass()) {
312                         return false;
313                 } else if (!(object instanceof Contact)) {
314                         // Not correct interface
315                         return false;
316                 }
317
318                 final Contact contact = (Contact) object;
319
320                 if (!Objects.equals(this.getContactBirthday(), contact.getContactBirthday())) {
321                         return false;
322                 } else if (!Objects.equals(this.getContactCity(), contact.getContactCity())) {
323                         return false;
324                 } else if (!Objects.equals(this.getContactComment(), contact.getContactComment())) {
325                         return false;
326                 } else if (!Objects.equals(this.getContactCountry(), contact.getContactCountry())) {
327                         return false;
328                 } else if (!Objects.equals(this.getContactEmailAddress(), contact.getContactEmailAddress())) {
329                         return false;
330                 } else if (!Objects.equals(this.getContactFamilyName(), contact.getContactFamilyName())) {
331                         return false;
332                 } else if (!Objects.equals(this.getContactFaxNumber(), contact.getContactFaxNumber())) {
333                         return false;
334                 } else if (!Objects.equals(this.getContactFirstName(), contact.getContactFirstName())) {
335                         return false;
336                 } else if (!Objects.equals(this.getContactHouseNumber(), contact.getContactHouseNumber())) {
337                         return false;
338                 } else if (!Objects.equals(this.getContactHouseNumberExtension(), contact.getContactHouseNumberExtension())) {
339                         return false;
340                 } else if (!Objects.equals(this.getContactId(), contact.getContactId())) {
341                         return false;
342                 } else if (!Objects.equals(this.getContactLandLineNumber(), contact.getContactLandLineNumber())) {
343                         return false;
344                 } else if (!Objects.equals(this.getContactMobileNumber(), contact.getContactMobileNumber())) {
345                         return false;
346                 } else if (this.getContactPersonalTitle() != contact.getContactPersonalTitle()) {
347                         return false;
348                 } else if (!Objects.equals(this.getContactStreet(), contact.getContactStreet())) {
349                         return false;
350                 } else if (!Objects.equals(this.getContactTitle(), contact.getContactTitle())) {
351                         return false;
352                 } else if (!Objects.equals(this.getContactZipCode(), contact.getContactZipCode())) {
353                         return false;
354                 } else if (!Objects.equals(this.isOwnContact(), contact.isOwnContact())) {
355                         return false;
356                 }
357
358                 return true;
359         }
360
361         @Override
362         @SuppressWarnings ("ReturnOfDateField")
363         public Date getContactBirthday () {
364                 return this.contactBirthday;
365         }
366
367         @Override
368         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
369         public void setContactBirthday (final Date contactBirthday) {
370                 this.contactBirthday = contactBirthday;
371         }
372
373         @Override
374         public String getContactCity () {
375                 return this.contactCity;
376         }
377
378         @Override
379         public void setContactCity (final String contactCity) {
380                 this.contactCity = contactCity;
381         }
382
383         @Override
384         public String getContactComment () {
385                 return this.contactComment;
386         }
387
388         @Override
389         public void setContactComment (final String contactComment) {
390                 this.contactComment = contactComment;
391         }
392
393         @Override
394         public Country getContactCountry () {
395                 return this.contactCountry;
396         }
397
398         @Override
399         public void setContactCountry (final Country contactCountry) {
400                 this.contactCountry = contactCountry;
401         }
402
403         @Override
404         public String getContactEmailAddress () {
405                 return this.contactEmailAddress;
406         }
407
408         @Override
409         public void setContactEmailAddress (final String contactEmailAddress) {
410                 this.contactEmailAddress = contactEmailAddress;
411         }
412
413         @Override
414         @SuppressWarnings ("ReturnOfDateField")
415         public Date getContactEntryCreated () {
416                 return this.contactEntryCreated;
417         }
418
419         @Override
420         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
421         public void setContactEntryCreated (final Date contactEntryCreated) {
422                 this.contactEntryCreated = contactEntryCreated;
423         }
424
425         @Override
426         @SuppressWarnings ("ReturnOfDateField")
427         public Date getContactEntryUpdated () {
428                 return this.contactEntryUpdated;
429         }
430
431         @Override
432         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
433         public void setContactEntryUpdated (final Date contactEntryUpdated) {
434                 this.contactEntryUpdated = contactEntryUpdated;
435         }
436
437         @Override
438         public String getContactFamilyName () {
439                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
440                 return this.contactFamilyName;
441         }
442
443         @Override
444         public void setContactFamilyName (final String contactFamilyName) {
445                 this.contactFamilyName = contactFamilyName;
446         }
447
448         @Override
449         public DialableFaxNumber getContactFaxNumber () {
450                 return this.contactFaxNumber;
451         }
452
453         @Override
454         public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
455                 this.contactFaxNumber = contactFaxNumber;
456         }
457
458         @Override
459         public String getContactFirstName () {
460                 return this.contactFirstName;
461         }
462
463         @Override
464         public void setContactFirstName (final String contactFirstName) {
465                 this.contactFirstName = contactFirstName;
466         }
467
468         @Override
469         public Short getContactHouseNumber () {
470                 return this.contactHouseNumber;
471         }
472
473         @Override
474         public void setContactHouseNumber (final Short contactHouseNumber) {
475                 this.contactHouseNumber = contactHouseNumber;
476         }
477
478         @Override
479         public String getContactHouseNumberExtension () {
480                 return this.contactHouseNumberExtension;
481         }
482
483         @Override
484         public void setContactHouseNumberExtension (final String contactHouseNumberExtension) {
485                 this.contactHouseNumberExtension = contactHouseNumberExtension;
486         }
487
488         @Override
489         public Long getContactId () {
490                 return this.contactId;
491         }
492
493         @Override
494         public void setContactId (final Long contactId) {
495                 this.contactId = contactId;
496         }
497
498         @Override
499         public DialableLandLineNumber getContactLandLineNumber () {
500                 return this.contactPhoneNumber;
501         }
502
503         @Override
504         public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
505                 this.contactPhoneNumber = contactPhoneNumber;
506         }
507
508         @Override
509         public DialableMobileNumber getContactMobileNumber () {
510                 return this.contactMobileNumber;
511         }
512
513         @Override
514         public void setContactMobileNumber (final DialableMobileNumber contactMobileNumber) {
515                 this.contactMobileNumber = contactMobileNumber;
516         }
517
518         @Override
519         public void setContactOwnContact (final Boolean contactOwnContact) {
520                 this.contactOwnContact = contactOwnContact;
521         }
522
523         @Override
524         public PersonalTitle getContactPersonalTitle () {
525                 return this.contactPersonalTitle;
526         }
527
528         @Override
529         public void setContactPersonalTitle (final PersonalTitle contactPersonalTitle) {
530                 this.contactPersonalTitle = contactPersonalTitle;
531         }
532
533         @Override
534         public String getContactStreet () {
535                 return this.contactStreet;
536         }
537
538         @Override
539         public void setContactStreet (final String contactStreet) {
540                 this.contactStreet = contactStreet;
541         }
542
543         @Override
544         public String getContactTitle () {
545                 return this.contactTitle;
546         }
547
548         @Override
549         public void setContactTitle (final String contactTitle) {
550                 this.contactTitle = contactTitle;
551         }
552
553         @Override
554         public Integer getContactZipCode () {
555                 return this.contactZipCode;
556         }
557
558         @Override
559         public void setContactZipCode (final Integer contactZipCode) {
560                 this.contactZipCode = contactZipCode;
561         }
562
563         @Override
564         public int hashCode () {
565                 int hash = 5;
566
567                 hash = 29 * hash + Objects.hashCode(this.getContactBirthday());
568                 hash = 29 * hash + Objects.hashCode(this.getContactCity());
569                 hash = 29 * hash + Objects.hashCode(this.getContactComment());
570                 hash = 29 * hash + Objects.hashCode(this.getContactCountry());
571                 hash = 29 * hash + Objects.hashCode(this.getContactEmailAddress());
572                 hash = 29 * hash + Objects.hashCode(this.getContactFamilyName());
573                 hash = 29 * hash + Objects.hashCode(this.getContactFaxNumber());
574                 hash = 29 * hash + Objects.hashCode(this.getContactFirstName());
575                 hash = 29 * hash + Objects.hashCode(this.getContactHouseNumber());
576                 hash = 29 * hash + Objects.hashCode(this.getContactHouseNumberExtension());
577                 hash = 29 * hash + Objects.hashCode(this.getContactId());
578                 hash = 29 * hash + Objects.hashCode(this.getContactLandLineNumber());
579                 hash = 29 * hash + Objects.hashCode(this.getContactMobileNumber());
580                 hash = 29 * hash + Objects.hashCode(this.getContactPersonalTitle());
581                 hash = 29 * hash + Objects.hashCode(this.getContactStreet());
582                 hash = 29 * hash + Objects.hashCode(this.getContactTitle());
583                 hash = 29 * hash + Objects.hashCode(this.getContactZipCode());
584                 hash = 29 * hash + Objects.hashCode(this.isOwnContact());
585
586                 return hash;
587         }
588
589         @Override
590         public Boolean isOwnContact () {
591                 return this.contactOwnContact;
592         }
593
594 }