]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/UserContact.java
Added equals() hashCode() for easy comparison + updated jar(s)
[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         @Override
272         public boolean equals (final Object object) {
273                 // Is it same type?
274                 if (!(object instanceof UserContact)) {
275                         // Not equal types
276                         return false;
277                 } else if (!(object instanceof Contact)) {
278                         // Not correct interface
279                         return false;
280                 }
281
282                 // Try to cast
283                 Contact contact = (Contact) object;
284
285                 // Now test some data TODO Definedly needs improvement
286                 return ((this.getContactGender().equals(contact.getContactGender())) &&
287                                 (this.getContactFirstName().toLowerCase().equals(contact.getContactFirstName().toLowerCase())) &&
288                                 (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
289         }
290
291         @Override
292         public Date getContactBirthday () {
293                 return this.contactBirthday;
294         }
295
296         @Override
297         public void setContactBirthday (final Date contactBirthday) {
298                 this.contactBirthday = contactBirthday;
299         }
300
301         @Override
302         public DialableCellphoneNumber getContactCellphoneNumber () {
303                 return this.contactCellphoneNumber;
304         }
305
306         @Override
307         public void setContactCellphoneNumber (final DialableCellphoneNumber contactCellphoneNumber) {
308                 this.contactCellphoneNumber = contactCellphoneNumber;
309         }
310
311         @Override
312         public String getContactCity () {
313                 return this.contactCity;
314         }
315
316         @Override
317         public void setContactCity (final String contactCity) {
318                 this.contactCity = contactCity;
319         }
320
321         @Override
322         public String getContactComment () {
323                 return this.contactComment;
324         }
325
326         @Override
327         public void setContactComment (final String contactComment) {
328                 this.contactComment = contactComment;
329         }
330
331         @Override
332         public Long getContactId () {
333                 return this.contactId;
334         }
335
336         @Override
337         public void setContactId (final Long contactId) {
338                 this.contactId = contactId;
339         }
340
341         @Override
342         public Country getContactCountry () {
343                 return this.contactCountry;
344         }
345
346         @Override
347         public void setContactCountry (final Country contactCountry) {
348                 this.contactCountry = contactCountry;
349         }
350
351         @Override
352         public Calendar getContactCreated () {
353                 return this.contactCreated;
354         }
355
356         @Override
357         public void setContactCreated (final Calendar contactCreated) {
358                 this.contactCreated = contactCreated;
359         }
360
361         @Override
362         public String getContactEmailAddress () {
363                 return this.contactEmailAddress;
364         }
365
366         @Override
367         public void setContactEmailAddress (final String contactEmailAddress) {
368                 this.contactEmailAddress = contactEmailAddress;
369         }
370
371         @Override
372         public String getContactFamilyName () {
373                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
374                 return this.contactFamilyName;
375         }
376
377         @Override
378         public void setContactFamilyName (final String contactFamilyName) {
379                 this.contactFamilyName = contactFamilyName;
380         }
381
382         @Override
383         public DialableFaxNumber getContactFaxNumber () {
384                 return this.contactFaxNumber;
385         }
386
387         @Override
388         public void setContactFaxNumber (final DialableFaxNumber contactFaxNumber) {
389                 this.contactFaxNumber = contactFaxNumber;
390         }
391
392         @Override
393         public String getContactFirstName () {
394                 return this.contactFirstName;
395         }
396
397         @Override
398         public void setContactFirstName (final String contactFirstName) {
399                 this.contactFirstName = contactFirstName;
400         }
401
402         @Override
403         public Gender getContactGender () {
404                 return this.contactGender;
405         }
406
407         @Override
408         public void setContactGender (final Gender contactGender) {
409                 this.contactGender = contactGender;
410         }
411
412         @Override
413         public Short getContactHouseNumber () {
414                 return this.contactHouseNumber;
415         }
416
417         @Override
418         public void setContactHouseNumber (final Short contactHouseNumber) {
419                 this.contactHouseNumber = contactHouseNumber;
420         }
421
422         @Override
423         public void setContactOwnContact (final Boolean contactOwnContact) {
424                 this.contactOwnContact = contactOwnContact;
425         }
426
427         @Override
428         public DialableLandLineNumber getContactPhoneNumber () {
429                 return this.contactPhoneNumber;
430         }
431
432         @Override
433         public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
434                 this.contactPhoneNumber = contactPhoneNumber;
435         }
436
437         @Override
438         public String getContactStreet () {
439                 return this.contactStreet;
440         }
441
442         @Override
443         public void setContactStreet (final String contactStreet) {
444                 this.contactStreet = contactStreet;
445         }
446
447         @Override
448         public Calendar getContactUpdated () {
449                 return this.contactUpdated;
450         }
451
452         @Override
453         public void setContactUpdated (final Calendar contactUpdated) {
454                 this.contactUpdated = contactUpdated;
455         }
456
457         @Override
458         public Integer getContactZipCode () {
459                 return this.contactZipCode;
460         }
461
462         @Override
463         public void setContactZipCode (final Integer contactZipCode) {
464                 this.contactZipCode = contactZipCode;
465         }
466
467         @Override
468         public int hashCode () {
469                 // Validate contactGender instance
470                 assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
471
472                 int hash = 7;
473                 hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
474                 hash = 79 * hash + this.getContactGender().hashCode();
475                 hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
476                 return hash;
477         }
478
479         /**
480          * Initialization with fake contactGender UNKNOWN
481          */
482         @PostConstruct
483         public void init () {
484                 // Fake contactGender
485                 this.contactGender = Gender.UNKNOWN;
486         }
487
488         @Override
489         public Boolean isOwnContact () {
490                 return this.contactOwnContact;
491         }
492 }