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