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