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.GeneratedValue;
30 import javax.persistence.GenerationType;
31 import javax.persistence.Id;
32 import javax.persistence.Index;
33 import javax.persistence.JoinColumn;
34 import javax.persistence.Lob;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import org.mxchange.jcontacts.contact.gender.Gender;
40 import org.mxchange.jcountry.data.Country;
41 import org.mxchange.jcountry.data.CountryData;
42 import org.mxchange.jphone.phonenumbers.cellphone.CellphoneNumber;
43 import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
44 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
46 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
47 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
50 * A general contact class which serves as an entity.
52 * @author Roland Haeder<roland@mxchange.org>
55 @Entity (name = "contacts")
60 name = "contact_gender",
61 columnList = "contact_gender"
64 name = "contact_email_address",
66 columnList = "contact_email_address"
70 public class UserContact implements Contact, Comparable<Contact> {
75 private static final long serialVersionUID = 58_744_284_981_863L;
80 @Column (name = "contact_birthday")
81 @Temporal (TemporalType.DATE)
82 private Date contactBirthday;
87 @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id", unique = true)
88 @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
89 private DialableCellphoneNumber contactCellphoneNumber;
94 @Basic (optional = false)
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.REFRESH, optional = false)
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 @Basic (optional = false)
124 @Column (name = "contact_email_address", length = 100, nullable = false)
125 private String contactEmailAddress;
130 @Basic (optional = false)
131 @Column (name = "contact_family_name", length = 100, nullable = false)
132 private String contactFamilyName;
137 @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
138 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
139 private DialableFaxNumber contactFaxNumber;
144 @Basic (optional = false)
145 @Column (name = "contact_first_name", length = 100, nullable = false)
146 private String contactFirstName;
151 @Basic (optional = false)
152 @Column (name = "contact_gender", nullable = false)
153 @Enumerated (EnumType.STRING)
154 private Gender contactGender;
159 @Basic (optional = false)
160 @Column (name = "contact_house_number", length = 5, nullable = false)
161 private Short contactHouseNumber;
167 @GeneratedValue (strategy = GenerationType.IDENTITY)
168 @Column (name = "contact_id", length = 20, nullable = false, updatable = false)
169 private Long contactId;
172 * Flag whether this contact is user's own data
174 @Basic (optional = false)
175 @Column (name = "contact_own_contact", nullable = false)
176 private Boolean contactOwnContact;
181 @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
182 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
183 private DialableLandLineNumber contactPhoneNumber;
188 @Basic (optional = false)
189 @Column (name = "contact_street", nullable = false)
190 private String contactStreet;
193 * Title (Doctor, etc)
195 @Column (name = "contact_title")
196 private String contactTitle;
199 * When the contact has been updated
201 @Temporal (TemporalType.TIMESTAMP)
202 @Column (name = "contact_updated")
203 private Calendar contactUpdated;
208 @Basic (optional = false)
209 @Column (name = "contact_zip_code", nullable = false, length = 6)
210 private Integer contactZipCode;
213 * Constructor for contactGender and names
215 * @param contactGender Gender instance
216 * @param contactFirstName First name
217 * @param contactFamilyName Family name
219 public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
220 // Call other constructor
224 this.contactGender = contactGender;
225 this.contactFirstName = contactFirstName;
226 this.contactFamilyName = contactFamilyName;
230 * Default constructor
232 public UserContact () {
233 // Default is not user's own contact
234 this.contactOwnContact = Boolean.FALSE;
238 public int compareTo (final Contact contact) {
239 // contact should not be null
240 if (null == contact) {
241 throw new NullPointerException("contact is null"); //NOI18N
244 // Is the contactId the same?
245 if (Objects.equals(this.getContactId(), contact.getContactId())) {
246 // Same contactId, means same contact
248 } else if (this.getContactId() > contact.getContactId()) {
249 // This contactId is larger than compared to
253 // The other contactId is larger
258 public void copyAll (final Contact contact) {
261 this.setContactFirstName(contact.getContactFirstName());
262 this.setContactFamilyName(contact.getContactFamilyName());
263 this.setContactStreet(contact.getContactStreet());
264 this.setContactZipCode(contact.getContactZipCode());
265 this.setContactCity(contact.getContactCity());
266 this.setContactCountry(contact.getContactCountry());
268 // - phone, fax, email
269 this.setContactLandLineNumber(contact.getContactLandLineNumber());
270 this.setContactFaxNumber(contact.getContactFaxNumber());
271 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
274 this.setContactBirthday(contact.getContactBirthday());
275 this.setContactComment(contact.getContactComment());
276 this.setContactCreated(contact.getContactCreated());
277 this.setContactUpdated(contact.getContactUpdated());
281 public boolean equals (final Object object) {
283 if (!(object instanceof UserContact)) {
286 } else if (!(object instanceof Contact)) {
287 // Not correct interface
292 Contact contact = (Contact) object;
294 // Now test some data TODO Definedly needs improvement
295 return ((this.getContactGender().equals(contact.getContactGender())) &&
296 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
297 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
301 public int hashCode () {
302 // Validate contactGender instance
303 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
306 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
307 hash = 79 * hash + this.getContactGender().hashCode();
308 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
313 public Date getContactBirthday () {
314 return this.contactBirthday;
318 public void setContactBirthday (final Date contactBirthday) {
319 this.contactBirthday = contactBirthday;
323 public DialableCellphoneNumber getContactCellphoneNumber () {
324 return this.contactCellphoneNumber;
328 public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
329 this.contactCellphoneNumber = contactCellphoneNumber;
333 public String getContactCity () {
334 return this.contactCity;
338 public void setContactCity (final String contactCity) {
339 this.contactCity = contactCity;
343 public String getContactComment () {
344 return this.contactComment;
348 public void setContactComment (final String contactComment) {
349 this.contactComment = contactComment;
353 public Country getContactCountry () {
354 return this.contactCountry;
358 public void setContactCountry (final Country contactCountry) {
359 this.contactCountry = contactCountry;
363 public Calendar getContactCreated () {
364 return this.contactCreated;
368 public void setContactCreated (final Calendar contactCreated) {
369 this.contactCreated = contactCreated;
373 public String getContactEmailAddress () {
374 return this.contactEmailAddress;
378 public void setContactEmailAddress (final String contactEmailAddress) {
379 this.contactEmailAddress = contactEmailAddress;
383 public String getContactFamilyName () {
384 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
385 return this.contactFamilyName;
389 public void setContactFamilyName (final String contactFamilyName) {
390 this.contactFamilyName = contactFamilyName;
394 public DialableFaxNumber getContactFaxNumber () {
395 return this.contactFaxNumber;
399 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
400 this.contactFaxNumber = contactFaxNumber;
404 public String getContactFirstName () {
405 return this.contactFirstName;
409 public void setContactFirstName (final String contactFirstName) {
410 this.contactFirstName = contactFirstName;
414 public Gender getContactGender () {
415 return this.contactGender;
419 public void setContactGender (final Gender contactGender) {
420 this.contactGender = contactGender;
424 public Short getContactHouseNumber () {
425 return this.contactHouseNumber;
429 public void setContactHouseNumber (final Short contactHouseNumber) {
430 this.contactHouseNumber = contactHouseNumber;
434 public Long getContactId () {
435 return this.contactId;
439 public void setContactId (final Long contactId) {
440 this.contactId = contactId;
444 public void setContactOwnContact (final Boolean contactOwnContact) {
445 this.contactOwnContact = contactOwnContact;
449 public DialableLandLineNumber getContactLandLineNumber () {
450 return this.contactPhoneNumber;
454 public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
455 this.contactPhoneNumber = contactPhoneNumber;
459 public String getContactStreet () {
460 return this.contactStreet;
464 public void setContactStreet (final String contactStreet) {
465 this.contactStreet = contactStreet;
469 public String getContactTitle () {
470 return this.contactTitle;
474 public void setContactTitle (final String contactTitle) {
475 this.contactTitle = contactTitle;
479 public Calendar getContactUpdated () {
480 return this.contactUpdated;
484 public void setContactUpdated (final Calendar contactUpdated) {
485 this.contactUpdated = contactUpdated;
489 public Integer getContactZipCode () {
490 return this.contactZipCode;
494 public void setContactZipCode (final Integer contactZipCode) {
495 this.contactZipCode = contactZipCode;
499 * Initialization with fake contactGender UNKNOWN
502 public void init () {
503 // Fake contactGender
504 this.contactGender = Gender.UNKNOWN;
508 public Boolean isOwnContact () {
509 return this.contactOwnContact;