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