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