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