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"
66 public class UserContact implements Contact, Comparable<Contact> {
71 private static final long serialVersionUID = 58_744_284_981_863L;
76 @Column (name = "contact_birthday")
77 @Temporal (TemporalType.DATE)
78 private Date contactBirthday;
83 @JoinColumn (name = "contact_cellphone_number_id")
84 @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
85 private DialableCellphoneNumber contactCellphoneNumber;
90 @Column (name = "contact_city", nullable = false, length = 100)
91 private String contactCity;
97 @Column (name = "contact_comment")
98 private String contactComment;
104 @GeneratedValue (strategy = GenerationType.IDENTITY)
105 @Column (name = "contact_id", length = 20, updatable = false)
106 private Long contactId;
111 @JoinColumn (name = "contact_country_id", nullable = false)
112 @OneToOne(targetEntity = CountryData.class, optional = false, fetch = FetchType.EAGER)
113 private Country contactCountry;
116 * When the contact has been created
118 @Basic (optional = false)
119 @Temporal (TemporalType.TIMESTAMP)
120 @Column (name = "contact_created", nullable = false)
121 private Calendar contactCreated;
126 @Column (name = "contact_email_address", length = 100, nullable = false)
127 private String contactEmailAddress;
132 @Basic (optional = false)
133 @Column (name = "contact_family_name", length = 100, nullable = false)
134 private String contactFamilyName;
139 @JoinColumn (name = "contact_fax_number_id")
140 @OneToOne(targetEntity = FaxNumber.class,cascade = CascadeType.ALL)
141 private DialableFaxNumber contactFaxNumber;
146 @Basic (optional = false)
147 @Column (name = "contact_first_name", length = 100, nullable = false)
148 private String contactFirstName;
153 @Basic (optional = false)
154 @Column (name = "contact_gender", nullable = false)
155 @Enumerated (EnumType.STRING)
156 private Gender contactGender;
161 @Column (name = "contact_house_number", length = 5, nullable = false)
162 private Short contactHouseNumber;
165 * Flag whether this contact is user's own data
167 @Column (name = "contact_own_contact", nullable = false)
168 private Boolean contactOwnContact;
173 @JoinColumn (name = "contact_phone_number_id")
174 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
175 private DialableLandLineNumber contactPhoneNumber;
180 @Column (name = "contact_street", nullable = false)
181 private String contactStreet;
184 * When the contact has been updated
186 @Temporal (TemporalType.TIMESTAMP)
187 @Column (name = "contact_updated")
188 private Calendar contactUpdated;
193 @Column (name = "contact_zip_code", nullable = false, length = 6)
194 private Integer contactZipCode;
197 * Constructor for contactGender and names
199 * @param contactGender Gender instance
200 * @param contactFirstName First name
201 * @param contactFamilyName Family name
203 public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
205 this.contactGender = contactGender;
206 this.contactFirstName = contactFirstName;
207 this.contactFamilyName = contactFamilyName;
211 * Default constructor
213 public UserContact () {
217 * Compares two contacts with each other
219 * @param contact Contact comparator
221 * @return Comparison value
224 public int compareTo (final Contact contact) {
225 // contact should not be null
226 if (null == contact) {
227 throw new NullPointerException("contact is null"); //NOI18N
230 // Is the contactId the same?
231 if (Objects.equals(this.getContactId(), contact.getContactId())) {
232 // Same contactId, means same contact
234 } else if (this.getContactId() > contact.getContactId()) {
235 // This contactId is larger than compared to
239 // The other contactId is larger
244 public void copyAll (final Contact contact) {
247 this.setContactFirstName(contact.getContactFirstName());
248 this.setContactFamilyName(contact.getContactFamilyName());
249 this.setContactStreet(contact.getContactStreet());
250 this.setContactZipCode(contact.getContactZipCode());
251 this.setContactCity(contact.getContactCity());
252 this.setContactCountry(contact.getContactCountry());
254 // - phone, fax, email
255 this.setContactPhoneNumber(contact.getContactPhoneNumber());
256 this.setContactFaxNumber(contact.getContactFaxNumber());
257 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
260 this.setContactBirthday(contact.getContactBirthday());
261 this.setContactComment(contact.getContactComment());
262 this.setContactCreated(contact.getContactCreated());
263 this.setContactUpdated(contact.getContactUpdated());
267 * Check if contacts are same or throw an exception
269 * @param object Other possible contact class
271 * @return Whether both contacts are same TODO Needs a lot improvements
274 public boolean equals (final Object object) {
276 if (!(object instanceof UserContact)) {
279 } else if (!(object instanceof Contact)) {
280 // Not correct interface
285 Contact contact = (Contact) object;
287 // Now test some data TODO Definedly needs improvement
288 return ((this.getContactGender().equals(contact.getContactGender())) &&
289 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
290 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
294 public Date getContactBirthday () {
295 return this.contactBirthday;
299 public void setContactBirthday (final Date contactBirthday) {
300 this.contactBirthday = contactBirthday;
304 public DialableCellphoneNumber getContactCellphoneNumber () {
305 return this.contactCellphoneNumber;
309 public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
310 this.contactCellphoneNumber = contactCellphoneNumber;
314 public String getContactCity () {
315 return this.contactCity;
319 public void setContactCity (final String contactCity) {
320 this.contactCity = contactCity;
324 public String getContactComment () {
325 return this.contactComment;
329 public void setContactComment (final String contactComment) {
330 this.contactComment = contactComment;
334 public Long getContactId () {
335 return this.contactId;
339 public void setContactId (final Long contactId) {
340 this.contactId = contactId;
344 public Country getContactCountry () {
345 return this.contactCountry;
349 public void setContactCountry (final Country contactCountry) {
350 this.contactCountry = contactCountry;
354 public Calendar getContactCreated () {
355 return this.contactCreated;
359 public void setContactCreated (final Calendar contactCreated) {
360 this.contactCreated = contactCreated;
364 public String getContactEmailAddress () {
365 return this.contactEmailAddress;
369 public void setContactEmailAddress (final String contactEmailAddress) {
370 this.contactEmailAddress = contactEmailAddress;
374 public String getContactFamilyName () {
375 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
376 return this.contactFamilyName;
380 public void setContactFamilyName (final String contactFamilyName) {
381 this.contactFamilyName = contactFamilyName;
385 public DialableFaxNumber getContactFaxNumber () {
386 return this.contactFaxNumber;
390 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
391 this.contactFaxNumber = contactFaxNumber;
395 public String getContactFirstName () {
396 return this.contactFirstName;
400 public void setContactFirstName (final String contactFirstName) {
401 this.contactFirstName = contactFirstName;
405 public Gender getContactGender () {
406 return this.contactGender;
410 public void setContactGender (final Gender contactGender) {
411 this.contactGender = contactGender;
415 public Short getContactHouseNumber () {
416 return this.contactHouseNumber;
420 public void setContactHouseNumber (final Short contactHouseNumber) {
421 this.contactHouseNumber = contactHouseNumber;
425 public void setContactOwnContact (final Boolean contactOwnContact) {
426 this.contactOwnContact = contactOwnContact;
430 public DialableLandLineNumber getContactPhoneNumber () {
431 return this.contactPhoneNumber;
435 public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
436 this.contactPhoneNumber = contactPhoneNumber;
440 public String getContactStreet () {
441 return this.contactStreet;
445 public void setContactStreet (final String contactStreet) {
446 this.contactStreet = contactStreet;
450 public Calendar getContactUpdated () {
451 return this.contactUpdated;
455 public void setContactUpdated (final Calendar contactUpdated) {
456 this.contactUpdated = contactUpdated;
460 public Integer getContactZipCode () {
461 return this.contactZipCode;
465 public void setContactZipCode (final Integer contactZipCode) {
466 this.contactZipCode = contactZipCode;
470 public int hashCode () {
471 // Validate contactGender instance
472 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
475 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
476 hash = 79 * hash + this.getContactGender().hashCode();
477 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
482 * Initialization with fake contactGender UNKNOWN
485 public void init () {
486 // Fake contactGender
487 this.contactGender = Gender.UNKNOWN;
491 public Boolean isOwnContact () {
492 return this.contactOwnContact;