2 * Copyright (C) 2016 Roland Haeder
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.contact;
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
23 import javax.persistence.Basic;
24 import javax.persistence.CascadeType;
25 import javax.persistence.Column;
26 import javax.persistence.Entity;
27 import javax.persistence.EnumType;
28 import javax.persistence.Enumerated;
29 import javax.persistence.FetchType;
30 import javax.persistence.GeneratedValue;
31 import javax.persistence.GenerationType;
32 import javax.persistence.Id;
33 import javax.persistence.Index;
34 import javax.persistence.JoinColumn;
35 import javax.persistence.Lob;
36 import javax.persistence.OneToOne;
37 import javax.persistence.Table;
38 import javax.persistence.Temporal;
39 import javax.persistence.TemporalType;
40 import org.mxchange.jcontacts.contact.gender.Gender;
41 import org.mxchange.jcountry.data.Country;
42 import org.mxchange.jcountry.data.CountryData;
43 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
44 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
45 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
46 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
47 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
48 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
51 * A general contact class which should only be extended.
53 * @author Roland Haeder<roland@mxchange.org>
56 @Entity (name = "contacts")
61 name = "contact_gender",
62 columnList = "contact_gender"
65 name = "contact_email_address",
67 columnList = "contact_email_address"
71 public class UserContact implements Contact, Comparable<Contact> {
76 private static final long serialVersionUID = 58_744_284_981_863L;
81 @Column (name = "contact_birthday")
82 @Temporal (TemporalType.DATE)
83 private Date contactBirthday;
88 @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id")
89 @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
90 private DialableCellphoneNumber contactCellphoneNumber;
95 @Column (name = "contact_city", nullable = false, length = 100)
96 private String contactCity;
102 @Column (name = "contact_comment")
103 private String contactComment;
108 @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
109 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.MERGE, optional = false, fetch = FetchType.EAGER)
110 private Country contactCountry;
113 * When the contact has been created
115 @Basic (optional = false)
116 @Temporal (TemporalType.TIMESTAMP)
117 @Column (name = "contact_created", nullable = false)
118 private Calendar contactCreated;
123 @Column (name = "contact_email_address", length = 100, nullable = false)
124 private String contactEmailAddress;
129 @Basic (optional = false)
130 @Column (name = "contact_family_name", length = 100, nullable = false)
131 private String contactFamilyName;
136 @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id")
137 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
138 private DialableFaxNumber contactFaxNumber;
143 @Basic (optional = false)
144 @Column (name = "contact_first_name", length = 100, nullable = false)
145 private String contactFirstName;
150 @Basic (optional = false)
151 @Column (name = "contact_gender", nullable = false)
152 @Enumerated (EnumType.STRING)
153 private Gender contactGender;
158 @Column (name = "contact_house_number", length = 5, nullable = false)
159 private Short contactHouseNumber;
165 @GeneratedValue (strategy = GenerationType.IDENTITY)
166 @Column (name = "contact_id", length = 20, nullable = false, updatable = false)
167 private Long contactId;
170 * Flag whether this contact is user's own data
172 @Column (name = "contact_own_contact", nullable = false)
173 private Boolean contactOwnContact;
178 @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id")
179 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
180 private DialableLandLineNumber contactPhoneNumber;
185 @Column (name = "contact_street", nullable = false)
186 private String contactStreet;
189 * Title (Doctor, etc)
191 @Column (name = "contact_title")
192 private String contactTitle;
195 * When the contact has been updated
197 @Temporal (TemporalType.TIMESTAMP)
198 @Column (name = "contact_updated")
199 private Calendar contactUpdated;
204 @Column (name = "contact_zip_code", nullable = false, length = 6)
205 private Integer contactZipCode;
208 * Constructor for contactGender and names
210 * @param contactGender Gender instance
211 * @param contactFirstName First name
212 * @param contactFamilyName Family name
214 public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
215 // Call other constructor
219 this.contactGender = contactGender;
220 this.contactFirstName = contactFirstName;
221 this.contactFamilyName = contactFamilyName;
225 * Default constructor
227 public UserContact () {
228 // Default is not user's own contact
229 this.contactOwnContact = Boolean.FALSE;
233 public int compareTo (final Contact contact) {
234 // contact should not be null
235 if (null == contact) {
236 throw new NullPointerException("contact is null"); //NOI18N
239 // Is the contactId the same?
240 if (Objects.equals(this.getContactId(), contact.getContactId())) {
241 // Same contactId, means same contact
243 } else if (this.getContactId() > contact.getContactId()) {
244 // This contactId is larger than compared to
248 // The other contactId is larger
253 public void copyAll (final Contact contact) {
256 this.setContactFirstName(contact.getContactFirstName());
257 this.setContactFamilyName(contact.getContactFamilyName());
258 this.setContactStreet(contact.getContactStreet());
259 this.setContactZipCode(contact.getContactZipCode());
260 this.setContactCity(contact.getContactCity());
261 this.setContactCountry(contact.getContactCountry());
263 // - phone, fax, email
264 this.setContactPhoneNumber(contact.getContactPhoneNumber());
265 this.setContactFaxNumber(contact.getContactFaxNumber());
266 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
269 this.setContactBirthday(contact.getContactBirthday());
270 this.setContactComment(contact.getContactComment());
271 this.setContactCreated(contact.getContactCreated());
272 this.setContactUpdated(contact.getContactUpdated());
276 public boolean equals (final Object object) {
278 if (!(object instanceof UserContact)) {
281 } else if (!(object instanceof Contact)) {
282 // Not correct interface
287 Contact contact = (Contact) object;
289 // Now test some data TODO Definedly needs improvement
290 return ((this.getContactGender().equals(contact.getContactGender())) &&
291 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
292 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
296 public int hashCode () {
297 // Validate contactGender instance
298 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
301 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
302 hash = 79 * hash + this.getContactGender().hashCode();
303 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
308 public Date getContactBirthday () {
309 return this.contactBirthday;
313 public void setContactBirthday (final Date contactBirthday) {
314 this.contactBirthday = contactBirthday;
318 public DialableCellphoneNumber getContactCellphoneNumber () {
319 return this.contactCellphoneNumber;
323 public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
324 this.contactCellphoneNumber = contactCellphoneNumber;
328 public String getContactCity () {
329 return this.contactCity;
333 public void setContactCity (final String contactCity) {
334 this.contactCity = contactCity;
338 public String getContactComment () {
339 return this.contactComment;
343 public void setContactComment (final String contactComment) {
344 this.contactComment = contactComment;
348 public Country getContactCountry () {
349 return this.contactCountry;
353 public void setContactCountry (final Country contactCountry) {
354 this.contactCountry = contactCountry;
358 public Calendar getContactCreated () {
359 return this.contactCreated;
363 public void setContactCreated (final Calendar contactCreated) {
364 this.contactCreated = contactCreated;
368 public String getContactEmailAddress () {
369 return this.contactEmailAddress;
373 public void setContactEmailAddress (final String contactEmailAddress) {
374 this.contactEmailAddress = contactEmailAddress;
378 public String getContactFamilyName () {
379 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
380 return this.contactFamilyName;
384 public void setContactFamilyName (final String contactFamilyName) {
385 this.contactFamilyName = contactFamilyName;
389 public DialableFaxNumber getContactFaxNumber () {
390 return this.contactFaxNumber;
394 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
395 this.contactFaxNumber = contactFaxNumber;
399 public String getContactFirstName () {
400 return this.contactFirstName;
404 public void setContactFirstName (final String contactFirstName) {
405 this.contactFirstName = contactFirstName;
409 public Gender getContactGender () {
410 return this.contactGender;
414 public void setContactGender (final Gender contactGender) {
415 this.contactGender = contactGender;
419 public Short getContactHouseNumber () {
420 return this.contactHouseNumber;
424 public void setContactHouseNumber (final Short contactHouseNumber) {
425 this.contactHouseNumber = contactHouseNumber;
429 public Long getContactId () {
430 return this.contactId;
434 public void setContactId (final Long contactId) {
435 this.contactId = contactId;
439 public void setContactOwnContact (final Boolean contactOwnContact) {
440 this.contactOwnContact = contactOwnContact;
444 public DialableLandLineNumber getContactPhoneNumber () {
445 return this.contactPhoneNumber;
449 public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
450 this.contactPhoneNumber = contactPhoneNumber;
454 public String getContactStreet () {
455 return this.contactStreet;
459 public void setContactStreet (final String contactStreet) {
460 this.contactStreet = contactStreet;
464 public String getContactTitle () {
465 return this.contactTitle;
469 public void setContactTitle (final String contactTitle) {
470 this.contactTitle = contactTitle;
474 public Calendar getContactUpdated () {
475 return this.contactUpdated;
479 public void setContactUpdated (final Calendar contactUpdated) {
480 this.contactUpdated = contactUpdated;
484 public Integer getContactZipCode () {
485 return this.contactZipCode;
489 public void setContactZipCode (final Integer contactZipCode) {
490 this.contactZipCode = contactZipCode;
494 * Initialization with fake contactGender UNKNOWN
497 public void init () {
498 // Fake contactGender
499 this.contactGender = Gender.UNKNOWN;
503 public Boolean isOwnContact () {
504 return this.contactOwnContact;