]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/model/contact/UserContact.java
e885716de54a62ac98ce4609ed0da64f46f3e72d
[jcontacts-core.git] / src / org / mxchange / jcontacts / model / contact / UserContact.java
1 /*
2  * Copyright (C) 2016, 2017 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jcontacts.model.contact;
18
19 import java.util.Date;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.EnumType;
26 import javax.persistence.Enumerated;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.Index;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.Lob;
33 import javax.persistence.NamedQueries;
34 import javax.persistence.NamedQuery;
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.model.contact.title.PersonalTitle;
41 import org.mxchange.jcountry.model.data.Country;
42 import org.mxchange.jcountry.model.data.CountryData;
43 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
44 import org.mxchange.jphone.model.phonenumbers.fax.FaxNumber;
45 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
46 import org.mxchange.jphone.model.phonenumbers.landline.LandLineNumber;
47 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
48 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
49
50 /**
51  * A general contact class which serves as an entity.
52  * <p>
53  * @author Roland Häder<roland@mxchange.org>
54  * @version 0.0
55  */
56 @Entity (name = "contacts")
57 @Table (
58                 name = "contacts",
59                 indexes = {
60                         @Index (
61                                         name = "contact_personal_title",
62                                         columnList = "contact_personal_title"
63                         )
64                 }
65 )
66 @NamedQueries (
67                 {
68                         @NamedQuery (name = "AllContacts", query = "SELECT c FROM contacts AS c ORDER BY c.contactId ASC"),
69                         @NamedQuery (name = "AllContactEmailAddresses", query = "SELECT c.contactEmailAddress FROM contacts AS c ORDER BY c.contactId ASC"),
70                         @NamedQuery (name = "AllContactsByCellphone", query = "SELECT c FROM contacts AS c WHERE c.contactMobileNumber = :mobileNumber ORDER BY c.contactId ASC"),
71                         @NamedQuery (name = "SearchContact", query = "SELECT c FROM contacts AS c WHERE c = :contact"),
72                         @NamedQuery (name = "SearchContactByEmailAddress", query = "SELECT c FROM contacts AS c WHERE LOWER(c.contactEmailAddress) LIKE LOWER(:emailAddress)")
73                 }
74 )
75 @SuppressWarnings ("PersistenceUnitPresent")
76 public class UserContact implements Contact {
77
78         /**
79          * Serial number
80          */
81         @Transient
82         private static final long serialVersionUID = 58_744_284_981_863L;
83
84         /**
85          * Birth day
86          */
87         @Column (name = "contact_birthday")
88         @Temporal (TemporalType.DATE)
89         private Date contactBirthday;
90
91         /**
92          * City
93          */
94         @Column (name = "contact_city", length = 100)
95         private String contactCity;
96
97         /**
98          * Optional comments
99          */
100         @Lob
101         @Column (name = "contact_comment")
102         private String contactComment;
103
104         /**
105          * Country code
106          */
107         @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
108         @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
109         private Country contactCountry;
110
111         /**
112          * When the contact has been created
113          */
114         @Basic (optional = false)
115         @Temporal (TemporalType.TIMESTAMP)
116         @Column (name = "contact_created", nullable = false)
117         private Date contactCreated;
118
119         /**
120          * Email address
121          */
122         @Column (name = "contact_email_address", length = 100, unique = true)
123         private String contactEmailAddress;
124
125         /**
126          * Family name
127          */
128         @Basic (optional = false)
129         @Column (name = "contact_family_name", length = 100, nullable = false)
130         private String contactFamilyName;
131
132         /**
133          * Fax number
134          */
135         @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
136         @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
137         private DialableFaxNumber contactFaxNumber;
138
139         /**
140          * First name
141          */
142         @Basic (optional = false)
143         @Column (name = "contact_first_name", length = 100, nullable = false)
144         private String contactFirstName;
145
146         /**
147          * House number
148          */
149         @Column (name = "contact_house_number")
150         private Short contactHouseNumber;
151
152         /**
153          * House number extension
154          */
155         @Column (name = "contact_house_number_extension", length = 5)
156         private String contactHouseNumberExtension;
157
158         /**
159          * Id number
160          */
161         @Id
162         @GeneratedValue (strategy = GenerationType.IDENTITY)
163         @Column (name = "contact_id", nullable = false, updatable = false)
164         private Long contactId;
165
166         /**
167          * Cellphone number
168          */
169         @JoinColumn (name = "contact_mobile_number_id", referencedColumnName = "mobile_id", unique = true)
170         @OneToOne (targetEntity = MobileNumber.class, cascade = CascadeType.ALL)
171         private DialableMobileNumber contactMobileNumber;
172
173         /**
174          * Flag whether this contact is user's own data
175          */
176         @Basic (optional = false)
177         @Column (name = "contact_own_contact", nullable = false)
178         private Boolean contactOwnContact;
179
180         /**
181          * Contact's personal title (Mr./Mrs.)
182          */
183         @Basic (optional = false)
184         @Column (name = "contact_personal_title", nullable = false)
185         @Enumerated (EnumType.STRING)
186         private PersonalTitle contactPersonalTitle;
187
188         /**
189          * Phone number
190          */
191         @JoinColumn (name = "contact_landline_number_id", referencedColumnName = "landline_id", unique = true)
192         @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
193         private DialableLandLineNumber contactPhoneNumber;
194
195         /**
196          * Street
197          */
198         @Column (name = "contact_street")
199         private String contactStreet;
200
201         /**
202          * Title (Doctor, etc)
203          */
204         @Column (name = "contact_title")
205         private String contactTitle;
206
207         /**
208          * When the contact has been updated
209          */
210         @Temporal (TemporalType.TIMESTAMP)
211         @Column (name = "contact_updated")
212         private Date contactUpdated;
213
214         /**
215          * ZIP code
216          */
217         @Column (name = "contact_zip_code")
218         private Integer contactZipCode;
219
220         /**
221          * Default constructor
222          */
223         public UserContact () {
224                 // Default is not user's own contact
225                 this.contactOwnContact = Boolean.FALSE;
226         }
227
228         /**
229          * Constructor for title and names
230          * <p>
231          * @param contactTitle      Personal title
232          * @param contactFirstName  First name
233          * @param contactFamilyName Family name
234          */
235         public UserContact (final PersonalTitle contactTitle, final String contactFirstName, final String contactFamilyName) {
236                 // Call default constructor
237                 this();
238
239                 // Set all
240                 this.contactPersonalTitle = contactTitle;
241                 this.contactFirstName = contactFirstName;
242                 this.contactFamilyName = contactFamilyName;
243         }
244
245         @Override
246         public boolean equals (final Object object) {
247                 if (this == object) {
248                         return true;
249                 } else if (null == object) {
250                         return false;
251                 } else if (this.getClass() != object.getClass()) {
252                         return false;
253                 } else if (!(object instanceof Contact)) {
254                         // Not correct interface
255                         return false;
256                 }
257
258                 final Contact other = (Contact) object;
259
260                 if (!Objects.equals(this.getContactId(), other.getContactId())) {
261                         return false;
262                 } else if (!Objects.equals(this.getContactCity(), other.getContactCity())) {
263                         return false;
264                 } else if (!Objects.equals(this.getContactEmailAddress(), other.getContactEmailAddress())) {
265                         return false;
266                 } else if (!Objects.equals(this.getContactFamilyName(), other.getContactFamilyName())) {
267                         return false;
268                 } else if (!Objects.equals(this.getContactFirstName(), other.getContactFirstName())) {
269                         return false;
270                 } else if (!Objects.equals(this.getContactStreet(), other.getContactStreet())) {
271                         return false;
272                 } else if (!Objects.equals(this.getContactTitle(), other.getContactTitle())) {
273                         return false;
274                 } else if (!Objects.equals(this.getContactBirthday(), other.getContactBirthday())) {
275                         return false;
276                 } else if (!Objects.equals(this.getContactCountry(), other.getContactCountry())) {
277                         return false;
278                 } else if (this.getContactPersonalTitle() != other.getContactPersonalTitle()) {
279                         return false;
280                 } else if (!Objects.equals(this.getContactHouseNumber(), other.getContactHouseNumber())) {
281                         return false;
282                 } else if (!Objects.equals(this.getContactHouseNumberExtension(), other.getContactHouseNumberExtension())) {
283                         return false;
284                 }
285
286                 return true;
287         }
288
289         @Override
290         @SuppressWarnings ("ReturnOfDateField")
291         public Date getContactBirthday () {
292                 return this.contactBirthday;
293         }
294
295         @Override
296         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
297         public void setContactBirthday (final Date contactBirthday) {
298                 this.contactBirthday = contactBirthday;
299         }
300
301         @Override
302         public String getContactCity () {
303                 return this.contactCity;
304         }
305
306         @Override
307         public void setContactCity (final String contactCity) {
308                 this.contactCity = contactCity;
309         }
310
311         @Override
312         public String getContactComment () {
313                 return this.contactComment;
314         }
315
316         @Override
317         public void setContactComment (final String contactComment) {
318                 this.contactComment = contactComment;
319         }
320
321         @Override
322         public Country getContactCountry () {
323                 return this.contactCountry;
324         }
325
326         @Override
327         public void setContactCountry (final Country contactCountry) {
328                 this.contactCountry = contactCountry;
329         }
330
331         @Override
332         @SuppressWarnings ("ReturnOfDateField")
333         public Date getContactCreated () {
334                 return this.contactCreated;
335         }
336
337         @Override
338         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
339         public void setContactCreated (final Date contactCreated) {
340                 this.contactCreated = contactCreated;
341         }
342
343         @Override
344         public String getContactEmailAddress () {
345                 return this.contactEmailAddress;
346         }
347
348         @Override
349         public void setContactEmailAddress (final String contactEmailAddress) {
350                 this.contactEmailAddress = contactEmailAddress;
351         }
352
353         @Override
354         public String getContactFamilyName () {
355                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
356                 return this.contactFamilyName;
357         }
358
359         @Override
360         public void setContactFamilyName (final String contactFamilyName) {
361                 this.contactFamilyName = contactFamilyName;
362         }
363
364         @Override
365         public DialableFaxNumber getContactFaxNumber () {
366                 return this.contactFaxNumber;
367         }
368
369         @Override
370         public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
371                 this.contactFaxNumber = contactFaxNumber;
372         }
373
374         @Override
375         public String getContactFirstName () {
376                 return this.contactFirstName;
377         }
378
379         @Override
380         public void setContactFirstName (final String contactFirstName) {
381                 this.contactFirstName = contactFirstName;
382         }
383
384         @Override
385         public Short getContactHouseNumber () {
386                 return this.contactHouseNumber;
387         }
388
389         @Override
390         public void setContactHouseNumber (final Short contactHouseNumber) {
391                 this.contactHouseNumber = contactHouseNumber;
392         }
393
394         @Override
395         public String getContactHouseNumberExtension () {
396                 return this.contactHouseNumberExtension;
397         }
398
399         @Override
400         public void setContactHouseNumberExtension (final String contactHouseNumberExtension) {
401                 this.contactHouseNumberExtension = contactHouseNumberExtension;
402         }
403
404         @Override
405         public Long getContactId () {
406                 return this.contactId;
407         }
408
409         @Override
410         public void setContactId (final Long contactId) {
411                 this.contactId = contactId;
412         }
413
414         @Override
415         public DialableLandLineNumber getContactLandLineNumber () {
416                 return this.contactPhoneNumber;
417         }
418
419         @Override
420         public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
421                 this.contactPhoneNumber = contactPhoneNumber;
422         }
423
424         @Override
425         public DialableMobileNumber getContactMobileNumber () {
426                 return this.contactMobileNumber;
427         }
428
429         @Override
430         public void setContactMobileNumber (final DialableMobileNumber contactMobileNumber) {
431                 this.contactMobileNumber = contactMobileNumber;
432         }
433
434         @Override
435         public void setContactOwnContact (final Boolean contactOwnContact) {
436                 this.contactOwnContact = contactOwnContact;
437         }
438
439         @Override
440         public PersonalTitle getContactPersonalTitle () {
441                 return this.contactPersonalTitle;
442         }
443
444         @Override
445         public void setContactPersonalTitle (final PersonalTitle contactPersonalTitle) {
446                 this.contactPersonalTitle = contactPersonalTitle;
447         }
448
449         @Override
450         public String getContactStreet () {
451                 return this.contactStreet;
452         }
453
454         @Override
455         public void setContactStreet (final String contactStreet) {
456                 this.contactStreet = contactStreet;
457         }
458
459         @Override
460         public String getContactTitle () {
461                 return this.contactTitle;
462         }
463
464         @Override
465         public void setContactTitle (final String contactTitle) {
466                 this.contactTitle = contactTitle;
467         }
468
469         @Override
470         @SuppressWarnings ("ReturnOfDateField")
471         public Date getContactUpdated () {
472                 return this.contactUpdated;
473         }
474
475         @Override
476         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
477         public void setContactUpdated (final Date contactUpdated) {
478                 this.contactUpdated = contactUpdated;
479         }
480
481         @Override
482         public Integer getContactZipCode () {
483                 return this.contactZipCode;
484         }
485
486         @Override
487         public void setContactZipCode (final Integer contactZipCode) {
488                 this.contactZipCode = contactZipCode;
489         }
490
491         @Override
492         public int hashCode () {
493                 int hash = 5;
494
495                 hash = 29 * hash + Objects.hashCode(this.getContactBirthday());
496                 hash = 29 * hash + Objects.hashCode(this.getContactCity());
497                 hash = 29 * hash + Objects.hashCode(this.getContactCountry());
498                 hash = 29 * hash + Objects.hashCode(this.getContactEmailAddress());
499                 hash = 29 * hash + Objects.hashCode(this.getContactFamilyName());
500                 hash = 29 * hash + Objects.hashCode(this.getContactFirstName());
501                 hash = 29 * hash + Objects.hashCode(this.getContactPersonalTitle());
502                 hash = 29 * hash + Objects.hashCode(this.getContactHouseNumber());
503                 hash = 29 * hash + Objects.hashCode(this.getContactHouseNumberExtension());
504                 hash = 29 * hash + Objects.hashCode(this.getContactId());
505                 hash = 29 * hash + Objects.hashCode(this.getContactStreet());
506                 hash = 29 * hash + Objects.hashCode(this.getContactTitle());
507
508                 return hash;
509         }
510
511         @Override
512         public Boolean isOwnContact () {
513                 return this.contactOwnContact;
514         }
515
516 }