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.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 javax.persistence.Transient;
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")
66 public class UserContact implements Contact, Comparable<Contact> {
71 private static final long serialVersionUID = 58_744_284_981_863L;
76 @Column (name = "birthday")
77 @Temporal (TemporalType.DATE)
78 private Date birthday;
83 @JoinColumn (name = "cellphone_number_id")
84 @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
85 private DialableCellphoneNumber cellphoneNumber;
90 @Column (name = "city", nullable = false, length = 100)
97 @Column (name = "comment")
98 private String comment;
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, cascade = CascadeType.ALL, optional = false)
114 private Country country;
117 * When the contact has been created
119 @Basic (optional = false)
120 @Temporal (TemporalType.TIMESTAMP)
121 @Column (name = "created", nullable = false)
122 private Calendar created;
127 @Column (name = "email_address", length = 100, nullable = false)
128 private String emailAddress;
133 @Basic (optional = false)
134 @Column (name = "family_name", length = 100, nullable = false)
135 private String familyName;
140 @JoinColumn (name = "fax_number_id")
141 @OneToOne(targetEntity = FaxNumber.class,cascade = CascadeType.ALL)
142 private DialableFaxNumber faxNumber;
147 @Basic (optional = false)
148 @Column (name = "first_name", length = 100, nullable = false)
149 private String firstName;
154 @Basic (optional = false)
155 @Column (name = "gender", nullable = false)
156 @Enumerated (EnumType.STRING)
157 private Gender gender;
162 @Column (name = "house_number", length = 5, nullable = false)
163 private Short houseNumber;
166 * Flag whether this contact is user's own data
168 @Column (name = "own_contact", nullable = false)
169 private Boolean ownContact;
174 @JoinColumn (name = "phone_number_id")
175 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
176 private DialableLandLineNumber phoneNumber;
181 @Column (name = "street", nullable = false)
182 private String street;
185 * When the contact has been updated
187 @Temporal (TemporalType.TIMESTAMP)
188 @Column (name = "updated")
189 private Calendar updated;
194 @Column (name = "zip_code", nullable = false, length = 6)
195 private Integer zipCode;
198 * Constructor for gender and names
200 * @param gender Gender instance
201 * @param firstName First name
202 * @param familyName Family name
204 public UserContact (final Gender gender, final String firstName, final String familyName) {
206 this.gender = gender;
207 this.firstName = firstName;
208 this.familyName = familyName;
212 * Default constructor
214 public UserContact () {
218 * Compares two contacts with each other
220 * @param contact Contact comparator
222 * @return Comparison value
225 public int compareTo (final Contact contact) {
226 // contact should not be null
227 if (null == contact) {
228 throw new NullPointerException("contact is null"); //NOI18N
231 // Is the contactId the same?
232 if (Objects.equals(this.getContactId(), contact.getContactId())) {
233 // Same contactId, means same contact
235 } else if (this.getContactId() > contact.getContactId()) {
236 // This contactId is larger than compared to
240 // The other contactId is larger
245 public void copyAll (final Contact contact) {
248 this.setFirstName(contact.getFirstName());
249 this.setFamilyName(contact.getFamilyName());
250 this.setStreet(contact.getStreet());
251 this.setZipCode(contact.getZipCode());
252 this.setCity(contact.getCity());
253 this.setCountry(contact.getCountry());
255 // - phone, fax, email
256 this.setPhoneNumber(contact.getPhoneNumber());
257 this.setFaxNumber(contact.getFaxNumber());
258 this.setCellphoneNumber(contact.getCellphoneNumber());
261 this.setBirthday(contact.getBirthday());
262 this.setComment(contact.getComment());
263 this.setCreated(contact.getCreated());
264 this.setUpdated(contact.getUpdated());
268 * Check if contacts are same or throw an exception
270 * @param object Other possible contact class
272 * @return Whether both contacts are same TODO Needs a lot improvements
275 public boolean equals (final Object object) {
277 if (!(object instanceof UserContact)) {
280 } else if (!(object instanceof Contact)) {
281 // Not correct interface
286 Contact contact = (Contact) object;
288 // Now test some data TODO Definedly needs improvement
289 return ((this.getGender().equals(contact.getGender())) &&
290 (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase())) &&
291 (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
295 public Date getBirthday () {
296 return this.birthday;
300 public void setBirthday (final Date birthday) {
301 this.birthday = birthday;
305 public DialableCellphoneNumber getCellphoneNumber () {
306 return this.cellphoneNumber;
310 public void setCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
311 this.cellphoneNumber = cellphoneNumber;
315 public String getCity () {
320 public void setCity (final String city) {
325 public String getComment () {
330 public void setComment (final String comment) {
331 this.comment = comment;
335 public Long getContactId () {
336 return this.contactId;
340 public void setContactId (final Long contactId) {
341 this.contactId = contactId;
345 public Country getCountry () {
350 public void setCountry (final Country country) {
351 this.country = country;
355 public Calendar getCreated () {
360 public void setCreated (final Calendar created) {
361 this.created = created;
365 public String getEmailAddress () {
366 return this.emailAddress;
370 public void setEmailAddress (final String emailAddress) {
371 this.emailAddress = emailAddress;
375 public String getFamilyName () {
376 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
377 return this.familyName;
381 public void setFamilyName (final String familyName) {
382 this.familyName = familyName;
386 public DialableFaxNumber getFaxNumber () {
387 return this.faxNumber;
391 public void setFaxNumber (final DialableFaxNumber faxNumber) {
392 this.faxNumber = faxNumber;
396 public String getFirstName () {
397 return this.firstName;
401 public void setFirstName (final String firstName) {
402 this.firstName = firstName;
406 public Gender getGender () {
411 public void setGender (final Gender gender) {
412 this.gender = gender;
416 public Short getHouseNumber () {
417 return this.houseNumber;
421 public void setHouseNumber (final Short houseNumber) {
422 this.houseNumber = houseNumber;
426 public void setOwnContact (final Boolean ownContact) {
427 this.ownContact = ownContact;
431 public DialableLandLineNumber getPhoneNumber () {
432 return this.phoneNumber;
436 public void setPhoneNumber (final DialableLandLineNumber phoneNumber) {
437 this.phoneNumber = phoneNumber;
441 public String getStreet () {
446 public void setStreet (final String street) {
447 this.street = street;
451 public Calendar getUpdated () {
456 public void setUpdated (final Calendar updated) {
457 this.updated = updated;
461 public Integer getZipCode () {
466 public void setZipCode (final Integer zipCode) {
467 this.zipCode = zipCode;
471 public int hashCode () {
472 // Validate gender instance
473 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
476 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
477 hash = 79 * hash + this.getGender().hashCode();
478 hash = 79 * hash + Objects.hashCode(this.getFirstName());
483 * Initialization with fake gender UNKNOWN
486 public void init () {
488 this.gender = Gender.UNKNOWN;
492 public Boolean isOwnContact () {
493 return this.ownContact;