]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/UserContact.java
Unique constrains are required as 2 users should NOT use 1 cellphone number
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / UserContact.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.contact;
18
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;
49
50 /**
51  * A general contact class which should only be extended.
52  * <p>
53  * @author Roland Haeder<roland@mxchange.org>
54  * @version 0.0
55  */
56 @Entity (name = "contacts")
57 @Table (
58                 name = "contacts",
59                 indexes = {
60                         @Index (
61                                         name = "contact_gender",
62                                         columnList = "contact_gender"
63                         ),
64                         @Index (
65                                         name = "contact_email_address",
66                                         unique = true,
67                                         columnList = "contact_email_address"
68                         )
69                 }
70 )
71 public class UserContact implements Contact, Comparable<Contact> {
72
73         /**
74          * Serial number
75          */
76         private static final long serialVersionUID = 58_744_284_981_863L;
77
78         /**
79          * Birth day
80          */
81         @Column (name = "contact_birthday")
82         @Temporal (TemporalType.DATE)
83         private Date contactBirthday;
84
85         /**
86          * Cellphone number
87          */
88         @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id", unique = true)
89         @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
90         private DialableCellphoneNumber contactCellphoneNumber;
91
92         /**
93          * City
94          */
95         @Basic(optional = false)
96         @Column (name = "contact_city", nullable = false, length = 100)
97         private String contactCity;
98
99         /**
100          * Optional comments
101          */
102         @Lob
103         @Column (name = "contact_comment")
104         private String contactComment;
105
106         /**
107          * Country code
108          */
109         @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
110         @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.MERGE, optional = false, fetch = FetchType.EAGER)
111         private Country contactCountry;
112
113         /**
114          * When the contact has been created
115          */
116         @Basic (optional = false)
117         @Temporal (TemporalType.TIMESTAMP)
118         @Column (name = "contact_created", nullable = false)
119         private Calendar contactCreated;
120
121         /**
122          * Email address
123          */
124         @Basic(optional = false)
125         @Column (name = "contact_email_address", length = 100, nullable = false)
126         private String contactEmailAddress;
127
128         /**
129          * Family name
130          */
131         @Basic (optional = false)
132         @Column (name = "contact_family_name", length = 100, nullable = false)
133         private String contactFamilyName;
134
135         /**
136          * Fax number
137          */
138         @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
139         @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
140         private DialableFaxNumber contactFaxNumber;
141
142         /**
143          * First name
144          */
145         @Basic (optional = false)
146         @Column (name = "contact_first_name", length = 100, nullable = false)
147         private String contactFirstName;
148
149         /**
150          * Gender instance
151          */
152         @Basic (optional = false)
153         @Column (name = "contact_gender", nullable = false)
154         @Enumerated (EnumType.STRING)
155         private Gender contactGender;
156
157         /**
158          * House number
159          */
160         @Basic(optional = false)
161         @Column (name = "contact_house_number", length = 5, nullable = false)
162         private Short contactHouseNumber;
163
164         /**
165          * Id number
166          */
167         @Id
168         @GeneratedValue (strategy = GenerationType.IDENTITY)
169         @Column (name = "contact_id", length = 20, nullable = false, updatable = false)
170         private Long contactId;
171
172         /**
173          * Flag whether this contact is user's own data
174          */
175         @Basic(optional = false)
176         @Column (name = "contact_own_contact", nullable = false)
177         private Boolean contactOwnContact;
178
179         /**
180          * Phone number
181          */
182         @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
183         @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
184         private DialableLandLineNumber contactPhoneNumber;
185
186         /**
187          * Street
188          */
189         @Basic(optional = false)
190         @Column (name = "contact_street", nullable = false)
191         private String contactStreet;
192
193         /**
194          * Title (Doctor, etc)
195          */
196         @Column (name = "contact_title")
197         private String contactTitle;
198
199         /**
200          * When the contact has been updated
201          */
202         @Temporal (TemporalType.TIMESTAMP)
203         @Column (name = "contact_updated")
204         private Calendar contactUpdated;
205
206         /**
207          * ZIP code
208          */
209         @Basic(optional = false)
210         @Column (name = "contact_zip_code", nullable = false, length = 6)
211         private Integer contactZipCode;
212
213         /**
214          * Constructor for contactGender and names
215          * <p>
216          * @param contactGender     Gender instance
217          * @param contactFirstName  First name
218          * @param contactFamilyName Family name
219          */
220         public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
221                 // Call other constructor
222                 this();
223
224                 // Set all
225                 this.contactGender = contactGender;
226                 this.contactFirstName = contactFirstName;
227                 this.contactFamilyName = contactFamilyName;
228         }
229
230         /**
231          * Default constructor
232          */
233         public UserContact () {
234                 // Default is not user's own contact
235                 this.contactOwnContact = Boolean.FALSE;
236         }
237
238         @Override
239         public int compareTo (final Contact contact) {
240                 // contact should not be null
241                 if (null == contact) {
242                         throw new NullPointerException("contact is null"); //NOI18N
243                 }
244
245                 // Is the contactId the same?
246                 if (Objects.equals(this.getContactId(), contact.getContactId())) {
247                         // Same contactId, means same contact
248                         return 0;
249                 } else if (this.getContactId() > contact.getContactId()) {
250                         // This contactId is larger than compared to
251                         return -1;
252                 }
253
254                 // The other contactId is larger
255                 return 1;
256         }
257
258         @Override
259         public void copyAll (final Contact contact) {
260                 // Copy all:
261                 // - base data
262                 this.setContactFirstName(contact.getContactFirstName());
263                 this.setContactFamilyName(contact.getContactFamilyName());
264                 this.setContactStreet(contact.getContactStreet());
265                 this.setContactZipCode(contact.getContactZipCode());
266                 this.setContactCity(contact.getContactCity());
267                 this.setContactCountry(contact.getContactCountry());
268
269                 // - phone, fax, email
270                 this.setContactPhoneNumber(contact.getContactPhoneNumber());
271                 this.setContactFaxNumber(contact.getContactFaxNumber());
272                 this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
273
274                 // - other data
275                 this.setContactBirthday(contact.getContactBirthday());
276                 this.setContactComment(contact.getContactComment());
277                 this.setContactCreated(contact.getContactCreated());
278                 this.setContactUpdated(contact.getContactUpdated());
279         }
280
281         @Override
282         public boolean equals (final Object object) {
283                 // Is it same type?
284                 if (!(object instanceof UserContact)) {
285                         // Not equal types
286                         return false;
287                 } else if (!(object instanceof Contact)) {
288                         // Not correct interface
289                         return false;
290                 }
291
292                 // Try to cast
293                 Contact contact = (Contact) object;
294
295                 // Now test some data TODO Definedly needs improvement
296                 return ((this.getContactGender().equals(contact.getContactGender())) &&
297                                 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
298                                 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
299         }
300
301         @Override
302         public int hashCode () {
303                 // Validate contactGender instance
304                 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
305
306                 int hash = 7;
307                 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
308                 hash = 79 * hash + this.getContactGender().hashCode();
309                 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
310                 return hash;
311         }
312
313         @Override
314         public Date getContactBirthday () {
315                 return this.contactBirthday;
316         }
317
318         @Override
319         public void setContactBirthday (final Date contactBirthday) {
320                 this.contactBirthday = contactBirthday;
321         }
322
323         @Override
324         public DialableCellphoneNumber getContactCellphoneNumber () {
325                 return this.contactCellphoneNumber;
326         }
327
328         @Override
329         public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
330                 this.contactCellphoneNumber = contactCellphoneNumber;
331         }
332
333         @Override
334         public String getContactCity () {
335                 return this.contactCity;
336         }
337
338         @Override
339         public void setContactCity (final String contactCity) {
340                 this.contactCity = contactCity;
341         }
342
343         @Override
344         public String getContactComment () {
345                 return this.contactComment;
346         }
347
348         @Override
349         public void setContactComment (final String contactComment) {
350                 this.contactComment = contactComment;
351         }
352
353         @Override
354         public Country getContactCountry () {
355                 return this.contactCountry;
356         }
357
358         @Override
359         public void setContactCountry (final Country contactCountry) {
360                 this.contactCountry = contactCountry;
361         }
362
363         @Override
364         public Calendar getContactCreated () {
365                 return this.contactCreated;
366         }
367
368         @Override
369         public void setContactCreated (final Calendar contactCreated) {
370                 this.contactCreated = contactCreated;
371         }
372
373         @Override
374         public String getContactEmailAddress () {
375                 return this.contactEmailAddress;
376         }
377
378         @Override
379         public void setContactEmailAddress (final String contactEmailAddress) {
380                 this.contactEmailAddress = contactEmailAddress;
381         }
382
383         @Override
384         public String getContactFamilyName () {
385                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
386                 return this.contactFamilyName;
387         }
388
389         @Override
390         public void setContactFamilyName (final String contactFamilyName) {
391                 this.contactFamilyName = contactFamilyName;
392         }
393
394         @Override
395         public DialableFaxNumber getContactFaxNumber () {
396                 return this.contactFaxNumber;
397         }
398
399         @Override
400         public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
401                 this.contactFaxNumber = contactFaxNumber;
402         }
403
404         @Override
405         public String getContactFirstName () {
406                 return this.contactFirstName;
407         }
408
409         @Override
410         public void setContactFirstName (final String contactFirstName) {
411                 this.contactFirstName = contactFirstName;
412         }
413
414         @Override
415         public Gender getContactGender () {
416                 return this.contactGender;
417         }
418
419         @Override
420         public void setContactGender (final Gender contactGender) {
421                 this.contactGender = contactGender;
422         }
423
424         @Override
425         public Short getContactHouseNumber () {
426                 return this.contactHouseNumber;
427         }
428
429         @Override
430         public void setContactHouseNumber (final Short contactHouseNumber) {
431                 this.contactHouseNumber = contactHouseNumber;
432         }
433
434         @Override
435         public Long getContactId () {
436                 return this.contactId;
437         }
438
439         @Override
440         public void setContactId (final Long contactId) {
441                 this.contactId = contactId;
442         }
443
444         @Override
445         public void setContactOwnContact (final Boolean contactOwnContact) {
446                 this.contactOwnContact = contactOwnContact;
447         }
448
449         @Override
450         public DialableLandLineNumber getContactPhoneNumber () {
451                 return this.contactPhoneNumber;
452         }
453
454         @Override
455         public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
456                 this.contactPhoneNumber = contactPhoneNumber;
457         }
458
459         @Override
460         public String getContactStreet () {
461                 return this.contactStreet;
462         }
463
464         @Override
465         public void setContactStreet (final String contactStreet) {
466                 this.contactStreet = contactStreet;
467         }
468
469         @Override
470         public String getContactTitle () {
471                 return this.contactTitle;
472         }
473
474         @Override
475         public void setContactTitle (final String contactTitle) {
476                 this.contactTitle = contactTitle;
477         }
478
479         @Override
480         public Calendar getContactUpdated () {
481                 return this.contactUpdated;
482         }
483
484         @Override
485         public void setContactUpdated (final Calendar contactUpdated) {
486                 this.contactUpdated = contactUpdated;
487         }
488
489         @Override
490         public Integer getContactZipCode () {
491                 return this.contactZipCode;
492         }
493
494         @Override
495         public void setContactZipCode (final Integer contactZipCode) {
496                 this.contactZipCode = contactZipCode;
497         }
498
499         /**
500          * Initialization with fake contactGender UNKNOWN
501          */
502         @PostConstruct
503         public void init () {
504                 // Fake contactGender
505                 this.contactGender = Gender.UNKNOWN;
506         }
507
508         @Override
509         public Boolean isOwnContact () {
510                 return this.contactOwnContact;
511         }
512 }