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 contact = (Contact) object;
320 if (!Objects.equals(this.getContactBirthday(), contact.getContactBirthday())) {
322 } else if (!Objects.equals(this.getContactCity(), contact.getContactCity())) {
324 } else if (!Objects.equals(this.getContactComment(), contact.getContactComment())) {
326 } else if (!Objects.equals(this.getContactCountry(), contact.getContactCountry())) {
328 } else if (!Objects.equals(this.getContactEmailAddress(), contact.getContactEmailAddress())) {
330 } else if (!Objects.equals(this.getContactFamilyName(), contact.getContactFamilyName())) {
332 } else if (!Objects.equals(this.getContactFaxNumber(), contact.getContactFaxNumber())) {
334 } else if (!Objects.equals(this.getContactFirstName(), contact.getContactFirstName())) {
336 } else if (!Objects.equals(this.getContactHouseNumber(), contact.getContactHouseNumber())) {
338 } else if (!Objects.equals(this.getContactHouseNumberExtension(), contact.getContactHouseNumberExtension())) {
340 } else if (!Objects.equals(this.getContactId(), contact.getContactId())) {
342 } else if (!Objects.equals(this.getContactLandLineNumber(), contact.getContactLandLineNumber())) {
344 } else if (!Objects.equals(this.getContactMobileNumber(), contact.getContactMobileNumber())) {
346 } else if (this.getContactPersonalTitle() != contact.getContactPersonalTitle()) {
348 } else if (!Objects.equals(this.getContactStreet(), contact.getContactStreet())) {
350 } else if (!Objects.equals(this.getContactTitle(), contact.getContactTitle())) {
352 } else if (!Objects.equals(this.getContactZipCode(), contact.getContactZipCode())) {
354 } else if (!Objects.equals(this.isOwnContact(), contact.isOwnContact())) {
362 @SuppressWarnings ("ReturnOfDateField")
363 public Date getContactBirthday () {
364 return this.contactBirthday;
368 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
369 public void setContactBirthday (final Date contactBirthday) {
370 this.contactBirthday = contactBirthday;
374 public String getContactCity () {
375 return this.contactCity;
379 public void setContactCity (final String contactCity) {
380 this.contactCity = contactCity;
384 public String getContactComment () {
385 return this.contactComment;
389 public void setContactComment (final String contactComment) {
390 this.contactComment = contactComment;
394 public Country getContactCountry () {
395 return this.contactCountry;
399 public void setContactCountry (final Country contactCountry) {
400 this.contactCountry = contactCountry;
404 public String getContactEmailAddress () {
405 return this.contactEmailAddress;
409 public void setContactEmailAddress (final String contactEmailAddress) {
410 this.contactEmailAddress = contactEmailAddress;
414 @SuppressWarnings ("ReturnOfDateField")
415 public Date getContactEntryCreated () {
416 return this.contactEntryCreated;
420 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
421 public void setContactEntryCreated (final Date contactEntryCreated) {
422 this.contactEntryCreated = contactEntryCreated;
426 @SuppressWarnings ("ReturnOfDateField")
427 public Date getContactEntryUpdated () {
428 return this.contactEntryUpdated;
432 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
433 public void setContactEntryUpdated (final Date contactEntryUpdated) {
434 this.contactEntryUpdated = contactEntryUpdated;
438 public String getContactFamilyName () {
439 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
440 return this.contactFamilyName;
444 public void setContactFamilyName (final String contactFamilyName) {
445 this.contactFamilyName = contactFamilyName;
449 public DialableFaxNumber getContactFaxNumber () {
450 return this.contactFaxNumber;
454 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
455 this.contactFaxNumber = contactFaxNumber;
459 public String getContactFirstName () {
460 return this.contactFirstName;
464 public void setContactFirstName (final String contactFirstName) {
465 this.contactFirstName = contactFirstName;
469 public Short getContactHouseNumber () {
470 return this.contactHouseNumber;
474 public void setContactHouseNumber (final Short contactHouseNumber) {
475 this.contactHouseNumber = contactHouseNumber;
479 public String getContactHouseNumberExtension () {
480 return this.contactHouseNumberExtension;
484 public void setContactHouseNumberExtension (final String contactHouseNumberExtension) {
485 this.contactHouseNumberExtension = contactHouseNumberExtension;
489 public Long getContactId () {
490 return this.contactId;
494 public void setContactId (final Long contactId) {
495 this.contactId = contactId;
499 public DialableLandLineNumber getContactLandLineNumber () {
500 return this.contactPhoneNumber;
504 public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
505 this.contactPhoneNumber = contactPhoneNumber;
509 public DialableMobileNumber getContactMobileNumber () {
510 return this.contactMobileNumber;
514 public void setContactMobileNumber (final DialableMobileNumber contactMobileNumber) {
515 this.contactMobileNumber = contactMobileNumber;
519 public void setContactOwnContact (final Boolean contactOwnContact) {
520 this.contactOwnContact = contactOwnContact;
524 public PersonalTitle getContactPersonalTitle () {
525 return this.contactPersonalTitle;
529 public void setContactPersonalTitle (final PersonalTitle contactPersonalTitle) {
530 this.contactPersonalTitle = contactPersonalTitle;
534 public String getContactStreet () {
535 return this.contactStreet;
539 public void setContactStreet (final String contactStreet) {
540 this.contactStreet = contactStreet;
544 public String getContactTitle () {
545 return this.contactTitle;
549 public void setContactTitle (final String contactTitle) {
550 this.contactTitle = contactTitle;
554 public Integer getContactZipCode () {
555 return this.contactZipCode;
559 public void setContactZipCode (final Integer contactZipCode) {
560 this.contactZipCode = contactZipCode;
564 public int hashCode () {
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());
590 public Boolean isOwnContact () {
591 return this.contactOwnContact;