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;
109 @GeneratedValue (strategy = GenerationType.IDENTITY)
110 @Column (name = "contact_id", length = 20, updatable = false)
111 private Long contactId;
116 @JoinColumn (name = "contact_country_id", nullable = false)
117 @OneToOne (targetEntity = CountryData.class, optional = false, fetch = FetchType.EAGER)
118 private Country contactCountry;
121 * When the contact has been created
123 @Basic (optional = false)
124 @Temporal (TemporalType.TIMESTAMP)
125 @Column (name = "contact_created", nullable = false)
126 private Calendar contactCreated;
131 @Column (name = "contact_email_address", length = 100, nullable = false)
132 private String contactEmailAddress;
137 @Basic (optional = false)
138 @Column (name = "contact_family_name", length = 100, nullable = false)
139 private String contactFamilyName;
144 @JoinColumn (name = "contact_fax_number_id")
145 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
146 private DialableFaxNumber contactFaxNumber;
151 @Basic (optional = false)
152 @Column (name = "contact_first_name", length = 100, nullable = false)
153 private String contactFirstName;
158 @Basic (optional = false)
159 @Column (name = "contact_gender", nullable = false)
160 @Enumerated (EnumType.STRING)
161 private Gender contactGender;
166 @Column (name = "contact_house_number", length = 5, nullable = false)
167 private Short contactHouseNumber;
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 * When the contact has been updated
191 @Temporal (TemporalType.TIMESTAMP)
192 @Column (name = "contact_updated")
193 private Calendar contactUpdated;
198 @Column (name = "contact_zip_code", nullable = false, length = 6)
199 private Integer contactZipCode;
202 * Constructor for contactGender and names
204 * @param contactGender Gender instance
205 * @param contactFirstName First name
206 * @param contactFamilyName Family name
208 public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
210 this.contactGender = contactGender;
211 this.contactFirstName = contactFirstName;
212 this.contactFamilyName = contactFamilyName;
216 * Default constructor
218 public UserContact () {
222 * Compares two contacts with each other
224 * @param contact Contact comparator
226 * @return Comparison value
229 public int compareTo (final Contact contact) {
230 // contact should not be null
231 if (null == contact) {
232 throw new NullPointerException("contact is null"); //NOI18N
235 // Is the contactId the same?
236 if (Objects.equals(this.getContactId(), contact.getContactId())) {
237 // Same contactId, means same contact
239 } else if (this.getContactId() > contact.getContactId()) {
240 // This contactId is larger than compared to
244 // The other contactId is larger
249 public void copyAll (final Contact contact) {
252 this.setContactFirstName(contact.getContactFirstName());
253 this.setContactFamilyName(contact.getContactFamilyName());
254 this.setContactStreet(contact.getContactStreet());
255 this.setContactZipCode(contact.getContactZipCode());
256 this.setContactCity(contact.getContactCity());
257 this.setContactCountry(contact.getContactCountry());
259 // - phone, fax, email
260 this.setContactPhoneNumber(contact.getContactPhoneNumber());
261 this.setContactFaxNumber(contact.getContactFaxNumber());
262 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
265 this.setContactBirthday(contact.getContactBirthday());
266 this.setContactComment(contact.getContactComment());
267 this.setContactCreated(contact.getContactCreated());
268 this.setContactUpdated(contact.getContactUpdated());
272 * Check if contacts are same or throw an exception
274 * @param object Other possible contact class
276 * @return Whether both contacts are same TODO Needs a lot improvements
279 public boolean equals (final Object object) {
281 if (!(object instanceof UserContact)) {
284 } else if (!(object instanceof Contact)) {
285 // Not correct interface
290 Contact contact = (Contact) object;
292 // Now test some data TODO Definedly needs improvement
293 return ((this.getContactGender().equals(contact.getContactGender())) &&
294 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
295 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
299 public Date getContactBirthday () {
300 return this.contactBirthday;
304 public void setContactBirthday (final Date contactBirthday) {
305 this.contactBirthday = contactBirthday;
309 public DialableCellphoneNumber getContactCellphoneNumber () {
310 return this.contactCellphoneNumber;
314 public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
315 this.contactCellphoneNumber = contactCellphoneNumber;
319 public String getContactCity () {
320 return this.contactCity;
324 public void setContactCity (final String contactCity) {
325 this.contactCity = contactCity;
329 public String getContactComment () {
330 return this.contactComment;
334 public void setContactComment (final String contactComment) {
335 this.contactComment = contactComment;
339 public Long getContactId () {
340 return this.contactId;
344 public void setContactId (final Long contactId) {
345 this.contactId = contactId;
349 public Country getContactCountry () {
350 return this.contactCountry;
354 public void setContactCountry (final Country contactCountry) {
355 this.contactCountry = contactCountry;
359 public Calendar getContactCreated () {
360 return this.contactCreated;
364 public void setContactCreated (final Calendar contactCreated) {
365 this.contactCreated = contactCreated;
369 public String getContactEmailAddress () {
370 return this.contactEmailAddress;
374 public void setContactEmailAddress (final String contactEmailAddress) {
375 this.contactEmailAddress = contactEmailAddress;
379 public String getContactFamilyName () {
380 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
381 return this.contactFamilyName;
385 public void setContactFamilyName (final String contactFamilyName) {
386 this.contactFamilyName = contactFamilyName;
390 public DialableFaxNumber getContactFaxNumber () {
391 return this.contactFaxNumber;
395 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
396 this.contactFaxNumber = contactFaxNumber;
400 public String getContactFirstName () {
401 return this.contactFirstName;
405 public void setContactFirstName (final String contactFirstName) {
406 this.contactFirstName = contactFirstName;
410 public Gender getContactGender () {
411 return this.contactGender;
415 public void setContactGender (final Gender contactGender) {
416 this.contactGender = contactGender;
420 public Short getContactHouseNumber () {
421 return this.contactHouseNumber;
425 public void setContactHouseNumber (final Short contactHouseNumber) {
426 this.contactHouseNumber = contactHouseNumber;
430 public void setContactOwnContact (final Boolean contactOwnContact) {
431 this.contactOwnContact = contactOwnContact;
435 public DialableLandLineNumber getContactPhoneNumber () {
436 return this.contactPhoneNumber;
440 public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
441 this.contactPhoneNumber = contactPhoneNumber;
445 public String getContactStreet () {
446 return this.contactStreet;
450 public void setContactStreet (final String contactStreet) {
451 this.contactStreet = contactStreet;
455 public Calendar getContactUpdated () {
456 return this.contactUpdated;
460 public void setContactUpdated (final Calendar contactUpdated) {
461 this.contactUpdated = contactUpdated;
465 public Integer getContactZipCode () {
466 return this.contactZipCode;
470 public void setContactZipCode (final Integer contactZipCode) {
471 this.contactZipCode = contactZipCode;
475 public int hashCode () {
476 // Validate contactGender instance
477 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
480 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
481 hash = 79 * hash + this.getContactGender().hashCode();
482 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
487 * Initialization with fake contactGender UNKNOWN
490 public void init () {
491 // Fake contactGender
492 this.contactGender = Gender.UNKNOWN;
496 public Boolean isOwnContact () {
497 return this.contactOwnContact;