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", unique = true)
89 @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
90 private DialableCellphoneNumber contactCellphoneNumber;
95 @Basic(optional = false)
96 @Column (name = "contact_city", nullable = false, length = 100)
97 private String contactCity;
103 @Column (name = "contact_comment")
104 private String contactComment;
109 @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
110 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.MERGE, optional = false, fetch = FetchType.EAGER)
111 private Country contactCountry;
114 * When the contact has been created
116 @Basic (optional = false)
117 @Temporal (TemporalType.TIMESTAMP)
118 @Column (name = "contact_created", nullable = false)
119 private Calendar contactCreated;
124 @Basic(optional = false)
125 @Column (name = "contact_email_address", length = 100, nullable = false)
126 private String contactEmailAddress;
131 @Basic (optional = false)
132 @Column (name = "contact_family_name", length = 100, nullable = false)
133 private String contactFamilyName;
138 @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
139 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
140 private DialableFaxNumber contactFaxNumber;
145 @Basic (optional = false)
146 @Column (name = "contact_first_name", length = 100, nullable = false)
147 private String contactFirstName;
152 @Basic (optional = false)
153 @Column (name = "contact_gender", nullable = false)
154 @Enumerated (EnumType.STRING)
155 private Gender contactGender;
160 @Basic(optional = false)
161 @Column (name = "contact_house_number", length = 5, nullable = false)
162 private Short contactHouseNumber;
168 @GeneratedValue (strategy = GenerationType.IDENTITY)
169 @Column (name = "contact_id", length = 20, nullable = false, updatable = false)
170 private Long contactId;
173 * Flag whether this contact is user's own data
175 @Basic(optional = false)
176 @Column (name = "contact_own_contact", nullable = false)
177 private Boolean contactOwnContact;
182 @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
183 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
184 private DialableLandLineNumber contactPhoneNumber;
189 @Basic(optional = false)
190 @Column (name = "contact_street", nullable = false)
191 private String contactStreet;
194 * Title (Doctor, etc)
196 @Column (name = "contact_title")
197 private String contactTitle;
200 * When the contact has been updated
202 @Temporal (TemporalType.TIMESTAMP)
203 @Column (name = "contact_updated")
204 private Calendar contactUpdated;
209 @Basic(optional = false)
210 @Column (name = "contact_zip_code", nullable = false, length = 6)
211 private Integer contactZipCode;
214 * Constructor for contactGender and names
216 * @param contactGender Gender instance
217 * @param contactFirstName First name
218 * @param contactFamilyName Family name
220 public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
221 // Call other constructor
225 this.contactGender = contactGender;
226 this.contactFirstName = contactFirstName;
227 this.contactFamilyName = contactFamilyName;
231 * Default constructor
233 public UserContact () {
234 // Default is not user's own contact
235 this.contactOwnContact = Boolean.FALSE;
239 public int compareTo (final Contact contact) {
240 // contact should not be null
241 if (null == contact) {
242 throw new NullPointerException("contact is null"); //NOI18N
245 // Is the contactId the same?
246 if (Objects.equals(this.getContactId(), contact.getContactId())) {
247 // Same contactId, means same contact
249 } else if (this.getContactId() > contact.getContactId()) {
250 // This contactId is larger than compared to
254 // The other contactId is larger
259 public void copyAll (final Contact contact) {
262 this.setContactFirstName(contact.getContactFirstName());
263 this.setContactFamilyName(contact.getContactFamilyName());
264 this.setContactStreet(contact.getContactStreet());
265 this.setContactZipCode(contact.getContactZipCode());
266 this.setContactCity(contact.getContactCity());
267 this.setContactCountry(contact.getContactCountry());
269 // - phone, fax, email
270 this.setContactLandLineNumber(contact.getContactLandLineNumber());
271 this.setContactFaxNumber(contact.getContactFaxNumber());
272 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
275 this.setContactBirthday(contact.getContactBirthday());
276 this.setContactComment(contact.getContactComment());
277 this.setContactCreated(contact.getContactCreated());
278 this.setContactUpdated(contact.getContactUpdated());
282 public boolean equals (final Object object) {
284 if (!(object instanceof UserContact)) {
287 } else if (!(object instanceof Contact)) {
288 // Not correct interface
293 Contact contact = (Contact) object;
295 // Now test some data TODO Definedly needs improvement
296 return ((this.getContactGender().equals(contact.getContactGender())) &&
297 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
298 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
302 public int hashCode () {
303 // Validate contactGender instance
304 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
307 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
308 hash = 79 * hash + this.getContactGender().hashCode();
309 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
314 public Date getContactBirthday () {
315 return this.contactBirthday;
319 public void setContactBirthday (final Date contactBirthday) {
320 this.contactBirthday = contactBirthday;
324 public DialableCellphoneNumber getContactCellphoneNumber () {
325 return this.contactCellphoneNumber;
329 public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
330 this.contactCellphoneNumber = contactCellphoneNumber;
334 public String getContactCity () {
335 return this.contactCity;
339 public void setContactCity (final String contactCity) {
340 this.contactCity = contactCity;
344 public String getContactComment () {
345 return this.contactComment;
349 public void setContactComment (final String contactComment) {
350 this.contactComment = contactComment;
354 public Country getContactCountry () {
355 return this.contactCountry;
359 public void setContactCountry (final Country contactCountry) {
360 this.contactCountry = contactCountry;
364 public Calendar getContactCreated () {
365 return this.contactCreated;
369 public void setContactCreated (final Calendar contactCreated) {
370 this.contactCreated = contactCreated;
374 public String getContactEmailAddress () {
375 return this.contactEmailAddress;
379 public void setContactEmailAddress (final String contactEmailAddress) {
380 this.contactEmailAddress = contactEmailAddress;
384 public String getContactFamilyName () {
385 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
386 return this.contactFamilyName;
390 public void setContactFamilyName (final String contactFamilyName) {
391 this.contactFamilyName = contactFamilyName;
395 public DialableFaxNumber getContactFaxNumber () {
396 return this.contactFaxNumber;
400 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
401 this.contactFaxNumber = contactFaxNumber;
405 public String getContactFirstName () {
406 return this.contactFirstName;
410 public void setContactFirstName (final String contactFirstName) {
411 this.contactFirstName = contactFirstName;
415 public Gender getContactGender () {
416 return this.contactGender;
420 public void setContactGender (final Gender contactGender) {
421 this.contactGender = contactGender;
425 public Short getContactHouseNumber () {
426 return this.contactHouseNumber;
430 public void setContactHouseNumber (final Short contactHouseNumber) {
431 this.contactHouseNumber = contactHouseNumber;
435 public Long getContactId () {
436 return this.contactId;
440 public void setContactId (final Long contactId) {
441 this.contactId = contactId;
445 public void setContactOwnContact (final Boolean contactOwnContact) {
446 this.contactOwnContact = contactOwnContact;
450 public DialableLandLineNumber getContactLandLineNumber () {
451 return this.contactPhoneNumber;
455 public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
456 this.contactPhoneNumber = contactPhoneNumber;
460 public String getContactStreet () {
461 return this.contactStreet;
465 public void setContactStreet (final String contactStreet) {
466 this.contactStreet = contactStreet;
470 public String getContactTitle () {
471 return this.contactTitle;
475 public void setContactTitle (final String contactTitle) {
476 this.contactTitle = contactTitle;
480 public Calendar getContactUpdated () {
481 return this.contactUpdated;
485 public void setContactUpdated (final Calendar contactUpdated) {
486 this.contactUpdated = contactUpdated;
490 public Integer getContactZipCode () {
491 return this.contactZipCode;
495 public void setContactZipCode (final Integer contactZipCode) {
496 this.contactZipCode = contactZipCode;
500 * Initialization with fake contactGender UNKNOWN
503 public void init () {
504 // Fake contactGender
505 this.contactGender = Gender.UNKNOWN;
509 public Boolean isOwnContact () {
510 return this.contactOwnContact;