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.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.EnumType;
27 import javax.persistence.Enumerated;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.Index;
32 import javax.persistence.JoinColumn;
33 import javax.persistence.Lob;
34 import javax.persistence.NamedQueries;
35 import javax.persistence.NamedQuery;
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 serves as an entity.
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"
73 @NamedQuery( name = "AllContacts", query = "SELECT c FROM contacts AS c ORDER BY c.contactId ASC"),
74 @NamedQuery( name = "AllContactEmailAddresses", query = "SELECT c.contactEmailAddress FROM contacts AS c ORDER BY c.contactId ASC"),
75 @NamedQuery (name = "AllContactsByCellphone", query = "SELECT c FROM contacts AS c WHERE c.contactCellphoneNumber = :cellPhone ORDER BY c.contactId ASC"),
76 @NamedQuery (name = "SearchContactById", query = "SELECT c FROM contacts AS c WHERE c.contactId = :contactId")
79 @SuppressWarnings ("PersistenceUnitPresent")
80 public class UserContact implements Contact {
85 private static final long serialVersionUID = 58_744_284_981_863L;
90 @Column (name = "contact_birthday")
91 @Temporal (TemporalType.DATE)
92 private Date contactBirthday;
97 @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id", unique = true)
98 @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
99 private DialableCellphoneNumber contactCellphoneNumber;
104 @Basic (optional = false)
105 @Column (name = "contact_city", nullable = false, length = 100)
106 private String contactCity;
112 @Column (name = "contact_comment")
113 private String contactComment;
118 @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
119 @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
120 private Country contactCountry;
123 * When the contact has been created
125 @Basic (optional = false)
126 @Temporal (TemporalType.TIMESTAMP)
127 @Column (name = "contact_created", nullable = false)
128 private Calendar contactCreated;
133 @Column (name = "contact_email_address", length = 100)
134 private String contactEmailAddress;
139 @Basic (optional = false)
140 @Column (name = "contact_family_name", length = 100, nullable = false)
141 private String contactFamilyName;
146 @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
147 @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
148 private DialableFaxNumber contactFaxNumber;
153 @Basic (optional = false)
154 @Column (name = "contact_first_name", length = 100, nullable = false)
155 private String contactFirstName;
160 @Basic (optional = false)
161 @Column (name = "contact_gender", nullable = false)
162 @Enumerated (EnumType.STRING)
163 private Gender contactGender;
168 @Basic (optional = false)
169 @Column (name = "contact_house_number", length = 5, nullable = false)
170 private Short contactHouseNumber;
176 @GeneratedValue (strategy = GenerationType.IDENTITY)
177 @Column (name = "contact_id", nullable = false, updatable = false)
178 private Long contactId;
181 * Flag whether this contact is user's own data
183 @Basic (optional = false)
184 @Column (name = "contact_own_contact", nullable = false)
185 private Boolean contactOwnContact;
190 @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
191 @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
192 private DialableLandLineNumber contactPhoneNumber;
197 @Basic (optional = false)
198 @Column (name = "contact_street", nullable = false)
199 private String contactStreet;
202 * Title (Doctor, etc)
204 @Column (name = "contact_title")
205 private String contactTitle;
208 * When the contact has been updated
210 @Temporal (TemporalType.TIMESTAMP)
211 @Column (name = "contact_updated")
212 private Calendar contactUpdated;
217 @Basic (optional = false)
218 @Column (name = "contact_zip_code", nullable = false, length = 6)
219 private Integer contactZipCode;
222 * Default constructor
224 public UserContact () {
225 // Default is not user's own contact
226 this.contactOwnContact = Boolean.FALSE;
229 this.contactGender = Gender.UNKNOWN;
233 * Constructor for contactGender and names
235 * @param contactGender Gender instance
236 * @param contactFirstName First name
237 * @param contactFamilyName Family name
239 public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
240 // Call default constructor
244 this.contactGender = contactGender;
245 this.contactFirstName = contactFirstName;
246 this.contactFamilyName = contactFamilyName;
250 public void copyAll (final Contact contact) {
251 // Contact should be valid
252 if (null == contact) {
254 throw new NullPointerException("contact is null"); //NOI18N
259 this.setContactFirstName(contact.getContactFirstName());
260 this.setContactFamilyName(contact.getContactFamilyName());
261 this.setContactStreet(contact.getContactStreet());
262 this.setContactZipCode(contact.getContactZipCode());
263 this.setContactCity(contact.getContactCity());
264 this.setContactCountry(contact.getContactCountry());
266 // - phone, fax, email
267 this.setContactLandLineNumber(contact.getContactLandLineNumber());
268 this.setContactFaxNumber(contact.getContactFaxNumber());
269 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
272 this.setContactBirthday(contact.getContactBirthday());
273 this.setContactComment(contact.getContactComment());
274 this.setContactCreated(contact.getContactCreated());
275 this.setContactUpdated(contact.getContactUpdated());
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 int hashCode () {
300 // Validate contactGender instance
301 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
304 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
305 hash = 79 * hash + this.getContactGender().hashCode();
306 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
311 public Date getContactBirthday () {
312 return this.contactBirthday;
316 public void setContactBirthday (final Date contactBirthday) {
317 this.contactBirthday = contactBirthday;
321 public DialableCellphoneNumber getContactCellphoneNumber () {
322 return this.contactCellphoneNumber;
326 public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
327 this.contactCellphoneNumber = contactCellphoneNumber;
331 public String getContactCity () {
332 return this.contactCity;
336 public void setContactCity (final String contactCity) {
337 this.contactCity = contactCity;
341 public String getContactComment () {
342 return this.contactComment;
346 public void setContactComment (final String contactComment) {
347 this.contactComment = contactComment;
351 public Country getContactCountry () {
352 return this.contactCountry;
356 public void setContactCountry (final Country contactCountry) {
357 this.contactCountry = contactCountry;
361 public Calendar getContactCreated () {
362 return this.contactCreated;
366 public void setContactCreated (final Calendar contactCreated) {
367 this.contactCreated = contactCreated;
371 public String getContactEmailAddress () {
372 return this.contactEmailAddress;
376 public void setContactEmailAddress (final String contactEmailAddress) {
377 this.contactEmailAddress = contactEmailAddress;
381 public String getContactFamilyName () {
382 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
383 return this.contactFamilyName;
387 public void setContactFamilyName (final String contactFamilyName) {
388 this.contactFamilyName = contactFamilyName;
392 public DialableFaxNumber getContactFaxNumber () {
393 return this.contactFaxNumber;
397 public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
398 this.contactFaxNumber = contactFaxNumber;
402 public String getContactFirstName () {
403 return this.contactFirstName;
407 public void setContactFirstName (final String contactFirstName) {
408 this.contactFirstName = contactFirstName;
412 public Gender getContactGender () {
413 return this.contactGender;
417 public void setContactGender (final Gender contactGender) {
418 this.contactGender = contactGender;
422 public Short getContactHouseNumber () {
423 return this.contactHouseNumber;
427 public void setContactHouseNumber (final Short contactHouseNumber) {
428 this.contactHouseNumber = contactHouseNumber;
432 public Long getContactId () {
433 return this.contactId;
437 public void setContactId (final Long contactId) {
438 this.contactId = contactId;
442 public void setContactOwnContact (final Boolean contactOwnContact) {
443 this.contactOwnContact = contactOwnContact;
447 public DialableLandLineNumber getContactLandLineNumber () {
448 return this.contactPhoneNumber;
452 public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
453 this.contactPhoneNumber = contactPhoneNumber;
457 public String getContactStreet () {
458 return this.contactStreet;
462 public void setContactStreet (final String contactStreet) {
463 this.contactStreet = contactStreet;
467 public String getContactTitle () {
468 return this.contactTitle;
472 public void setContactTitle (final String contactTitle) {
473 this.contactTitle = contactTitle;
477 public Calendar getContactUpdated () {
478 return this.contactUpdated;
482 public void setContactUpdated (final Calendar contactUpdated) {
483 this.contactUpdated = contactUpdated;
487 public Integer getContactZipCode () {
488 return this.contactZipCode;
492 public void setContactZipCode (final Integer contactZipCode) {
493 this.contactZipCode = contactZipCode;
497 public Boolean isOwnContact () {
498 return this.contactOwnContact;