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