2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jcontacts.model.contact;
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;
54 * A general contact class which serves as an entity.
56 * @author Roland Häder<roland@mxchange.org>
59 @Entity (name = "contacts")
64 name = "contact_personal_title",
65 columnList = "contact_personal_title"
71 @NamedQuery (name = "AllContacts", query = "SELECT c FROM contacts AS c ORDER BY c.contactId ASC")
74 @SuppressWarnings ("PersistenceUnitPresent")
75 public class UserContact implements Contact {
81 private static final long serialVersionUID = 58_744_284_981_863L;
86 @Column (name = "contact_birthday")
87 @Temporal (TemporalType.DATE)
88 private Date contactBirthday;
93 @Column (name = "contact_city", length = 100)
94 private String contactCity;
100 @Column (name = "contact_comment")
101 private String contactComment;
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;
113 @Column (name = "contact_email_address", length = 100, unique = true)
114 private String contactEmailAddress;
117 * When the contact has been created
119 @Basic (optional = false)
120 @Temporal (TemporalType.TIMESTAMP)
121 @Column (name = "contact_entry_created", nullable = false)
122 private Date contactEntryCreated;
125 * When the contact has been updated
127 @Temporal (TemporalType.TIMESTAMP)
128 @Column (name = "contact_entry_updated")
129 private Date contactEntryUpdated;
134 @Basic (optional = false)
135 @Column (name = "contact_family_name", length = 100, nullable = false)
136 private String contactFamilyName;
141 @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
142 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
143 private DialableFaxNumber contactFaxNumber;
148 @Basic (optional = false)
149 @Column (name = "contact_first_name", length = 100, nullable = false)
150 private String contactFirstName;
155 @Column (name = "contact_house_number")
156 private Short contactHouseNumber;
159 * House number extension
161 @Column (name = "contact_house_number_extension", length = 5)
162 private String contactHouseNumberExtension;
168 @GeneratedValue (strategy = GenerationType.IDENTITY)
169 @Column (name = "contact_id", nullable = false, updatable = false)
170 private Long contactId;
175 @JoinColumn (name = "contact_mobile_number_id", referencedColumnName = "mobile_id", unique = true)
176 @OneToOne (targetEntity = MobileNumber.class, cascade = CascadeType.ALL)
177 private DialableMobileNumber contactMobileNumber;
180 * Flag whether this contact is user's own data
182 @Basic (optional = false)
183 @Column (name = "contact_own_contact", nullable = false)
184 private Boolean contactOwnContact;
187 * Contact's personal title (Mr./Mrs.)
189 @Basic (optional = false)
190 @Column (name = "contact_personal_title", nullable = false)
191 @Enumerated (EnumType.STRING)
192 private PersonalTitle contactPersonalTitle;
197 @JoinColumn (name = "contact_landline_number_id", referencedColumnName = "landline_id", unique = true)
198 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
199 private DialableLandLineNumber contactPhoneNumber;
204 @Column (name = "contact_street")
205 private String contactStreet;
208 * Title (Doctor, etc)
210 @Column (name = "contact_title")
211 private String contactTitle;
216 @Column (name = "contact_zip_code")
217 private Integer contactZipCode;
220 * Default constructor
222 public UserContact () {
223 // Default is not user's own contact
224 this.contactOwnContact = Boolean.FALSE;
228 * Constructor for title and names
230 * @param contactPersonalTitle Personal title
231 * @param contactFirstName First name
232 * @param contactFamilyName Family name
234 public UserContact (final PersonalTitle contactPersonalTitle, final String contactFirstName, final String contactFamilyName) {
235 // Invoke default constructor
238 // Are all parameter set?
239 if (null == contactFamilyName) {
241 throw new NullPointerException("contactFamilyName is null"); //NOI18N
242 } else if (contactFamilyName.isEmpty()) {
244 throw new IllegalArgumentException("contactFamilyName is empty"); //NOI18N
245 } else if (null == contactFirstName) {
247 throw new NullPointerException("contactFirstName is null"); //NOI18N
248 } else if (contactFirstName.isEmpty()) {
250 throw new IllegalArgumentException("contactFirstName is empty"); //NOI18N
251 } else if (null == contactPersonalTitle) {
253 throw new NullPointerException("contactPersonalTitle is null"); //NOI18N
257 this.contactPersonalTitle = contactPersonalTitle;
258 this.contactFirstName = contactFirstName;
259 this.contactFamilyName = contactFamilyName;
263 public int compareTo (final Contact contact) {
264 // Checkparameter and return 0 if equal
265 if (null == contact) {
267 throw new NullPointerException("contact is null"); //NOI18N
268 } else if (contact.equals(this)) {
274 final int comparators[] = {
275 // First check country
276 this.getContactCountry().compareTo(contact.getContactCountry()),
278 SafeNumberUtils.compare(this.getContactZipCode(), contact.getContactZipCode()),
280 StringUtils.compare(this.getContactCity(), contact.getContactCity()),
282 StringUtils.compareIgnoreCase(this.getContactStreet(), contact.getContactStreet()),
284 SafeNumberUtils.compare(this.getContactHouseNumber(), contact.getContactHouseNumber()),
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()),};
299 final int comparison = Comparables.checkAll(comparators);
306 public boolean equals (final Object object) {
307 if (this == object) {
309 } else if (null == object) {
311 } else if (this.getClass() != object.getClass()) {
313 } else if (!(object instanceof Contact)) {
314 // Not correct interface
318 final Contact other = (Contact) object;
320 if (!Objects.equals(this.getContactCity(), other.getContactCity())) {
322 } else if (!Objects.equals(this.getContactEmailAddress(), other.getContactEmailAddress())) {
324 } else if (!Objects.equals(this.getContactFamilyName(), other.getContactFamilyName())) {
326 } else if (!Objects.equals(this.getContactFirstName(), other.getContactFirstName())) {
328 } else if (!Objects.equals(this.getContactStreet(), other.getContactStreet())) {
330 } else if (!Objects.equals(this.getContactTitle(), other.getContactTitle())) {
332 } else if (!Objects.equals(this.getContactBirthday(), other.getContactBirthday())) {
334 } else if (!Objects.equals(this.getContactCountry(), other.getContactCountry())) {
336 } else if (this.getContactPersonalTitle() != other.getContactPersonalTitle()) {
338 } else if (!Objects.equals(this.getContactHouseNumber(), other.getContactHouseNumber())) {
340 } else if (!Objects.equals(this.getContactHouseNumberExtension(), other.getContactHouseNumberExtension())) {
348 @SuppressWarnings ("ReturnOfDateField")
349 public Date getContactBirthday () {
350 return this.contactBirthday;
354 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
355 public void setContactBirthday (final Date contactBirthday) {
356 this.contactBirthday = contactBirthday;
360 public String getContactCity () {
361 return this.contactCity;
365 public void setContactCity (final String contactCity) {
366 this.contactCity = contactCity;
370 public String getContactComment () {
371 return this.contactComment;
375 public void setContactComment (final String contactComment) {
376 this.contactComment = contactComment;
380 public Country getContactCountry () {
381 return this.contactCountry;
385 public void setContactCountry (final Country contactCountry) {
386 this.contactCountry = contactCountry;
390 public String getContactEmailAddress () {
391 return this.contactEmailAddress;
395 public void setContactEmailAddress (final String contactEmailAddress) {
396 this.contactEmailAddress = contactEmailAddress;
400 @SuppressWarnings ("ReturnOfDateField")
401 public Date getContactEntryCreated () {
402 return this.contactEntryCreated;
406 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
407 public void setContactEntryCreated (final Date contactEntryCreated) {
408 this.contactEntryCreated = contactEntryCreated;
412 @SuppressWarnings ("ReturnOfDateField")
413 public Date getContactEntryUpdated () {
414 return this.contactEntryUpdated;
418 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
419 public void setContactEntryUpdated (final Date contactEntryUpdated) {
420 this.contactEntryUpdated = contactEntryUpdated;
424 public String getContactFamilyName () {
425 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
426 return this.contactFamilyName;
430 public void setContactFamilyName (final String contactFamilyName) {
431 this.contactFamilyName = contactFamilyName;
435 public DialableFaxNumber getContactFaxNumber () {
436 return this.contactFaxNumber;
440 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
441 this.contactFaxNumber = contactFaxNumber;
445 public String getContactFirstName () {
446 return this.contactFirstName;
450 public void setContactFirstName (final String contactFirstName) {
451 this.contactFirstName = contactFirstName;
455 public Short getContactHouseNumber () {
456 return this.contactHouseNumber;
460 public void setContactHouseNumber (final Short contactHouseNumber) {
461 this.contactHouseNumber = contactHouseNumber;
465 public String getContactHouseNumberExtension () {
466 return this.contactHouseNumberExtension;
470 public void setContactHouseNumberExtension (final String contactHouseNumberExtension) {
471 this.contactHouseNumberExtension = contactHouseNumberExtension;
475 public Long getContactId () {
476 return this.contactId;
480 public void setContactId (final Long contactId) {
481 this.contactId = contactId;
485 public DialableLandLineNumber getContactLandLineNumber () {
486 return this.contactPhoneNumber;
490 public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
491 this.contactPhoneNumber = contactPhoneNumber;
495 public DialableMobileNumber getContactMobileNumber () {
496 return this.contactMobileNumber;
500 public void setContactMobileNumber (final DialableMobileNumber contactMobileNumber) {
501 this.contactMobileNumber = contactMobileNumber;
505 public void setContactOwnContact (final Boolean contactOwnContact) {
506 this.contactOwnContact = contactOwnContact;
510 public PersonalTitle getContactPersonalTitle () {
511 return this.contactPersonalTitle;
515 public void setContactPersonalTitle (final PersonalTitle contactPersonalTitle) {
516 this.contactPersonalTitle = contactPersonalTitle;
520 public String getContactStreet () {
521 return this.contactStreet;
525 public void setContactStreet (final String contactStreet) {
526 this.contactStreet = contactStreet;
530 public String getContactTitle () {
531 return this.contactTitle;
535 public void setContactTitle (final String contactTitle) {
536 this.contactTitle = contactTitle;
540 public Integer getContactZipCode () {
541 return this.contactZipCode;
545 public void setContactZipCode (final Integer contactZipCode) {
546 this.contactZipCode = contactZipCode;
550 public int hashCode () {
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());
569 public Boolean isOwnContact () {
570 return this.contactOwnContact;