]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/model/contact/UserContact.java
Continued:
[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 other = (Contact) object;
319
320                 if (!Objects.equals(this.getContactCity(), other.getContactCity())) {
321                         return false;
322                 } else if (!Objects.equals(this.getContactEmailAddress(), other.getContactEmailAddress())) {
323                         return false;
324                 } else if (!Objects.equals(this.getContactFamilyName(), other.getContactFamilyName())) {
325                         return false;
326                 } else if (!Objects.equals(this.getContactFirstName(), other.getContactFirstName())) {
327                         return false;
328                 } else if (!Objects.equals(this.getContactStreet(), other.getContactStreet())) {
329                         return false;
330                 } else if (!Objects.equals(this.getContactTitle(), other.getContactTitle())) {
331                         return false;
332                 } else if (!Objects.equals(this.getContactBirthday(), other.getContactBirthday())) {
333                         return false;
334                 } else if (!Objects.equals(this.getContactCountry(), other.getContactCountry())) {
335                         return false;
336                 } else if (this.getContactPersonalTitle() != other.getContactPersonalTitle()) {
337                         return false;
338                 } else if (!Objects.equals(this.getContactHouseNumber(), other.getContactHouseNumber())) {
339                         return false;
340                 } else if (!Objects.equals(this.getContactHouseNumberExtension(), other.getContactHouseNumberExtension())) {
341                         return false;
342                 }
343
344                 return true;
345         }
346
347         @Override
348         @SuppressWarnings ("ReturnOfDateField")
349         public Date getContactBirthday () {
350                 return this.contactBirthday;
351         }
352
353         @Override
354         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
355         public void setContactBirthday (final Date contactBirthday) {
356                 this.contactBirthday = contactBirthday;
357         }
358
359         @Override
360         public String getContactCity () {
361                 return this.contactCity;
362         }
363
364         @Override
365         public void setContactCity (final String contactCity) {
366                 this.contactCity = contactCity;
367         }
368
369         @Override
370         public String getContactComment () {
371                 return this.contactComment;
372         }
373
374         @Override
375         public void setContactComment (final String contactComment) {
376                 this.contactComment = contactComment;
377         }
378
379         @Override
380         public Country getContactCountry () {
381                 return this.contactCountry;
382         }
383
384         @Override
385         public void setContactCountry (final Country contactCountry) {
386                 this.contactCountry = contactCountry;
387         }
388
389         @Override
390         public String getContactEmailAddress () {
391                 return this.contactEmailAddress;
392         }
393
394         @Override
395         public void setContactEmailAddress (final String contactEmailAddress) {
396                 this.contactEmailAddress = contactEmailAddress;
397         }
398
399         @Override
400         @SuppressWarnings ("ReturnOfDateField")
401         public Date getContactEntryCreated () {
402                 return this.contactEntryCreated;
403         }
404
405         @Override
406         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
407         public void setContactEntryCreated (final Date contactEntryCreated) {
408                 this.contactEntryCreated = contactEntryCreated;
409         }
410
411         @Override
412         @SuppressWarnings ("ReturnOfDateField")
413         public Date getContactEntryUpdated () {
414                 return this.contactEntryUpdated;
415         }
416
417         @Override
418         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
419         public void setContactEntryUpdated (final Date contactEntryUpdated) {
420                 this.contactEntryUpdated = contactEntryUpdated;
421         }
422
423         @Override
424         public String getContactFamilyName () {
425                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
426                 return this.contactFamilyName;
427         }
428
429         @Override
430         public void setContactFamilyName (final String contactFamilyName) {
431                 this.contactFamilyName = contactFamilyName;
432         }
433
434         @Override
435         public DialableFaxNumber getContactFaxNumber () {
436                 return this.contactFaxNumber;
437         }
438
439         @Override
440         public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
441                 this.contactFaxNumber = contactFaxNumber;
442         }
443
444         @Override
445         public String getContactFirstName () {
446                 return this.contactFirstName;
447         }
448
449         @Override
450         public void setContactFirstName (final String contactFirstName) {
451                 this.contactFirstName = contactFirstName;
452         }
453
454         @Override
455         public Short getContactHouseNumber () {
456                 return this.contactHouseNumber;
457         }
458
459         @Override
460         public void setContactHouseNumber (final Short contactHouseNumber) {
461                 this.contactHouseNumber = contactHouseNumber;
462         }
463
464         @Override
465         public String getContactHouseNumberExtension () {
466                 return this.contactHouseNumberExtension;
467         }
468
469         @Override
470         public void setContactHouseNumberExtension (final String contactHouseNumberExtension) {
471                 this.contactHouseNumberExtension = contactHouseNumberExtension;
472         }
473
474         @Override
475         public Long getContactId () {
476                 return this.contactId;
477         }
478
479         @Override
480         public void setContactId (final Long contactId) {
481                 this.contactId = contactId;
482         }
483
484         @Override
485         public DialableLandLineNumber getContactLandLineNumber () {
486                 return this.contactPhoneNumber;
487         }
488
489         @Override
490         public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
491                 this.contactPhoneNumber = contactPhoneNumber;
492         }
493
494         @Override
495         public DialableMobileNumber getContactMobileNumber () {
496                 return this.contactMobileNumber;
497         }
498
499         @Override
500         public void setContactMobileNumber (final DialableMobileNumber contactMobileNumber) {
501                 this.contactMobileNumber = contactMobileNumber;
502         }
503
504         @Override
505         public void setContactOwnContact (final Boolean contactOwnContact) {
506                 this.contactOwnContact = contactOwnContact;
507         }
508
509         @Override
510         public PersonalTitle getContactPersonalTitle () {
511                 return this.contactPersonalTitle;
512         }
513
514         @Override
515         public void setContactPersonalTitle (final PersonalTitle contactPersonalTitle) {
516                 this.contactPersonalTitle = contactPersonalTitle;
517         }
518
519         @Override
520         public String getContactStreet () {
521                 return this.contactStreet;
522         }
523
524         @Override
525         public void setContactStreet (final String contactStreet) {
526                 this.contactStreet = contactStreet;
527         }
528
529         @Override
530         public String getContactTitle () {
531                 return this.contactTitle;
532         }
533
534         @Override
535         public void setContactTitle (final String contactTitle) {
536                 this.contactTitle = contactTitle;
537         }
538
539         @Override
540         public Integer getContactZipCode () {
541                 return this.contactZipCode;
542         }
543
544         @Override
545         public void setContactZipCode (final Integer contactZipCode) {
546                 this.contactZipCode = contactZipCode;
547         }
548
549         @Override
550         public int hashCode () {
551                 int hash = 5;
552
553                 hash = 29 * hash + Objects.hashCode(this.getContactBirthday());
554                 hash = 29 * hash + Objects.hashCode(this.getContactCity());
555                 hash = 29 * hash + Objects.hashCode(this.getContactCountry());
556                 hash = 29 * hash + Objects.hashCode(this.getContactEmailAddress());
557                 hash = 29 * hash + Objects.hashCode(this.getContactFamilyName());
558                 hash = 29 * hash + Objects.hashCode(this.getContactFirstName());
559                 hash = 29 * hash + Objects.hashCode(this.getContactPersonalTitle());
560                 hash = 29 * hash + Objects.hashCode(this.getContactHouseNumber());
561                 hash = 29 * hash + Objects.hashCode(this.getContactHouseNumberExtension());
562                 hash = 29 * hash + Objects.hashCode(this.getContactStreet());
563                 hash = 29 * hash + Objects.hashCode(this.getContactTitle());
564
565                 return hash;
566         }
567
568         @Override
569         public Boolean isOwnContact () {
570                 return this.contactOwnContact;
571         }
572
573 }