2 * Copyright (C) 2015 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")
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)
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")
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")
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) {
216 this.contactGender = contactGender;
217 this.contactFirstName = contactFirstName;
218 this.contactFamilyName = contactFamilyName;
222 * Default constructor
224 public UserContact () {
228 * Compares two contacts with each other
230 * @param contact Contact comparator
232 * @return Comparison value
235 public int compareTo (final Contact contact) {
236 // contact should not be null
237 if (null == contact) {
238 throw new NullPointerException("contact is null"); //NOI18N
241 // Is the contactId the same?
242 if (Objects.equals(this.getContactId(), contact.getContactId())) {
243 // Same contactId, means same contact
245 } else if (this.getContactId() > contact.getContactId()) {
246 // This contactId is larger than compared to
250 // The other contactId is larger
255 public void copyAll (final Contact contact) {
258 this.setContactFirstName(contact.getContactFirstName());
259 this.setContactFamilyName(contact.getContactFamilyName());
260 this.setContactStreet(contact.getContactStreet());
261 this.setContactZipCode(contact.getContactZipCode());
262 this.setContactCity(contact.getContactCity());
263 this.setContactCountry(contact.getContactCountry());
265 // - phone, fax, email
266 this.setContactPhoneNumber(contact.getContactPhoneNumber());
267 this.setContactFaxNumber(contact.getContactFaxNumber());
268 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
271 this.setContactBirthday(contact.getContactBirthday());
272 this.setContactComment(contact.getContactComment());
273 this.setContactCreated(contact.getContactCreated());
274 this.setContactUpdated(contact.getContactUpdated());
278 public boolean equals (final Object object) {
280 if (!(object instanceof UserContact)) {
283 } else if (!(object instanceof Contact)) {
284 // Not correct interface
289 Contact contact = (Contact) object;
291 // Now test some data TODO Definedly needs improvement
292 return ((this.getContactGender().equals(contact.getContactGender()))
293 && (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase()))
294 && (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
298 public Date getContactBirthday () {
299 return this.contactBirthday;
303 public void setContactBirthday (final Date contactBirthday) {
304 this.contactBirthday = contactBirthday;
308 public DialableCellphoneNumber getContactCellphoneNumber () {
309 return this.contactCellphoneNumber;
313 public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
314 this.contactCellphoneNumber = contactCellphoneNumber;
318 public String getContactCity () {
319 return this.contactCity;
323 public void setContactCity (final String contactCity) {
324 this.contactCity = contactCity;
328 public String getContactComment () {
329 return this.contactComment;
333 public void setContactComment (final String contactComment) {
334 this.contactComment = contactComment;
338 public Country getContactCountry () {
339 return this.contactCountry;
343 public void setContactCountry (final Country contactCountry) {
344 this.contactCountry = contactCountry;
348 public Calendar getContactCreated () {
349 return this.contactCreated;
353 public void setContactCreated (final Calendar contactCreated) {
354 this.contactCreated = contactCreated;
358 public String getContactEmailAddress () {
359 return this.contactEmailAddress;
363 public void setContactEmailAddress (final String contactEmailAddress) {
364 this.contactEmailAddress = contactEmailAddress;
368 public String getContactFamilyName () {
369 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
370 return this.contactFamilyName;
374 public void setContactFamilyName (final String contactFamilyName) {
375 this.contactFamilyName = contactFamilyName;
379 public DialableFaxNumber getContactFaxNumber () {
380 return this.contactFaxNumber;
384 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
385 this.contactFaxNumber = contactFaxNumber;
389 public String getContactFirstName () {
390 return this.contactFirstName;
394 public void setContactFirstName (final String contactFirstName) {
395 this.contactFirstName = contactFirstName;
399 public Gender getContactGender () {
400 return this.contactGender;
404 public void setContactGender (final Gender contactGender) {
405 this.contactGender = contactGender;
409 public Short getContactHouseNumber () {
410 return this.contactHouseNumber;
414 public void setContactHouseNumber (final Short contactHouseNumber) {
415 this.contactHouseNumber = contactHouseNumber;
419 public Long getContactId () {
420 return this.contactId;
423 public void setContactId (final Long contactId) {
424 this.contactId = contactId;
428 public void setContactOwnContact (final Boolean contactOwnContact) {
429 this.contactOwnContact = contactOwnContact;
433 public DialableLandLineNumber getContactPhoneNumber () {
434 return this.contactPhoneNumber;
438 public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
439 this.contactPhoneNumber = contactPhoneNumber;
443 public String getContactStreet () {
444 return this.contactStreet;
448 public void setContactStreet (final String contactStreet) {
449 this.contactStreet = contactStreet;
453 public String getContactTitle () {
454 return this.contactTitle;
458 public void setContactTitle (final String contactTitle) {
459 this.contactTitle = contactTitle;
463 public Calendar getContactUpdated () {
464 return this.contactUpdated;
468 public void setContactUpdated (final Calendar contactUpdated) {
469 this.contactUpdated = contactUpdated;
473 public Integer getContactZipCode () {
474 return this.contactZipCode;
478 public void setContactZipCode (final Integer contactZipCode) {
479 this.contactZipCode = contactZipCode;
483 public int hashCode () {
484 // Validate contactGender instance
485 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
488 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
489 hash = 79 * hash + this.getContactGender().hashCode();
490 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
495 * Initialization with fake contactGender UNKNOWN
498 public void init () {
499 // Fake contactGender
500 this.contactGender = Gender.UNKNOWN;
504 public Boolean isOwnContact () {
505 return this.contactOwnContact;