]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/UserContact.java
do not persist this data as the user should not be able to enter such data (only...
[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.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 javax.persistence.Transient;
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 = "gender",
62                                         columnList = "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 = "birthday")
77         @Temporal (TemporalType.DATE)
78         private Date birthday;
79
80         /**
81          * Cellphone number
82          */
83         @JoinColumn (name = "cellphone_number_id")
84         @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
85         private DialableCellphoneNumber cellphoneNumber;
86
87         /**
88          * City
89          */
90         @Column (name = "city", nullable = false, length = 100)
91         private String city;
92
93         /**
94          * Optional comments
95          */
96         @Lob
97         @Column (name = "comment")
98         private String comment;
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, cascade = CascadeType.ALL, optional = false)
113         @Transient
114         private Country country;
115
116         /**
117          * When the contact has been created
118          */
119         @Basic (optional = false)
120         @Temporal (TemporalType.TIMESTAMP)
121         @Column (name = "created", nullable = false)
122         private Calendar created;
123
124         /**
125          * Email address
126          */
127         @Column (name = "email_address", length = 100, nullable = false)
128         private String emailAddress;
129
130         /**
131          * Family name
132          */
133         @Basic (optional = false)
134         @Column (name = "family_name", length = 100, nullable = false)
135         private String familyName;
136
137         /**
138          * Fax number
139          */
140         @JoinColumn (name = "fax_number_id")
141         @OneToOne(targetEntity = FaxNumber.class,cascade = CascadeType.ALL)
142         private DialableFaxNumber faxNumber;
143
144         /**
145          * First name
146          */
147         @Basic (optional = false)
148         @Column (name = "first_name", length = 100, nullable = false)
149         private String firstName;
150
151         /**
152          * Gender instance
153          */
154         @Basic (optional = false)
155         @Column (name = "gender", nullable = false)
156         @Enumerated (EnumType.STRING)
157         private Gender gender;
158
159         /**
160          * House number
161          */
162         @Column (name = "house_number", length = 5, nullable = false)
163         private Short houseNumber;
164
165         /**
166          * Flag whether this contact is user's own data
167          */
168         @Column (name = "own_contact", nullable = false)
169         private Boolean ownContact;
170
171         /**
172          * Phone number
173          */
174         @JoinColumn (name = "phone_number_id")
175         @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
176         private DialableLandLineNumber phoneNumber;
177
178         /**
179          * Street
180          */
181         @Column (name = "street", nullable = false)
182         private String street;
183
184         /**
185          * When the contact has been updated
186          */
187         @Temporal (TemporalType.TIMESTAMP)
188         @Column (name = "updated")
189         private Calendar updated;
190
191         /**
192          * ZIP code
193          */
194         @Column (name = "zip_code", nullable = false, length = 6)
195         private Integer zipCode;
196
197         /**
198          * Constructor for gender and names
199          * <p>
200          * @param gender Gender instance
201          * @param firstName First name
202          * @param familyName Family name
203          */
204         public UserContact (final Gender gender, final String firstName, final String familyName) {
205                 // Set all
206                 this.gender = gender;
207                 this.firstName = firstName;
208                 this.familyName = familyName;
209         }
210
211         /**
212          * Default constructor
213          */
214         public UserContact () {
215         }
216
217         /**
218          * Compares two contacts with each other
219          * <p>
220          * @param contact Contact comparator
221          * <p>
222          * @return Comparison value
223          */
224         @Override
225         public int compareTo (final Contact contact) {
226                 // contact should not be null
227                 if (null == contact) {
228                         throw new NullPointerException("contact is null"); //NOI18N
229                 }
230
231                 // Is the contactId the same?
232                 if (Objects.equals(this.getContactId(), contact.getContactId())) {
233                         // Same contactId, means same contact
234                         return 0;
235                 } else if (this.getContactId() > contact.getContactId()) {
236                         // This contactId is larger than compared to
237                         return -1;
238                 }
239
240                 // The other contactId is larger
241                 return 1;
242         }
243
244         @Override
245         public void copyAll (final Contact contact) {
246                 // Copy all:
247                 // - base data
248                 this.setFirstName(contact.getFirstName());
249                 this.setFamilyName(contact.getFamilyName());
250                 this.setStreet(contact.getStreet());
251                 this.setZipCode(contact.getZipCode());
252                 this.setCity(contact.getCity());
253                 this.setCountry(contact.getCountry());
254
255                 // - phone, fax, email
256                 this.setPhoneNumber(contact.getPhoneNumber());
257                 this.setFaxNumber(contact.getFaxNumber());
258                 this.setCellphoneNumber(contact.getCellphoneNumber());
259
260                 // - other data
261                 this.setBirthday(contact.getBirthday());
262                 this.setComment(contact.getComment());
263                 this.setCreated(contact.getCreated());
264                 this.setUpdated(contact.getUpdated());
265         }
266
267         /**
268          * Check if contacts are same or throw an exception
269          * <p>
270          * @param object Other possible contact class
271          * <p>
272          * @return Whether both contacts are same TODO Needs a lot improvements
273          */
274         @Override
275         public boolean equals (final Object object) {
276                 // Is it same type?
277                 if (!(object instanceof UserContact)) {
278                         // Not equal types
279                         return false;
280                 } else if (!(object instanceof Contact)) {
281                         // Not correct interface
282                         return false;
283                 }
284
285                 // Try to cast
286                 Contact contact = (Contact) object;
287
288                 // Now test some data TODO Definedly needs improvement
289                 return ((this.getGender().equals(contact.getGender())) &&
290                                 (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase())) &&
291                                 (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
292         }
293
294         @Override
295         public Date getBirthday () {
296                 return this.birthday;
297         }
298
299         @Override
300         public void setBirthday (final Date birthday) {
301                 this.birthday = birthday;
302         }
303
304         @Override
305         public DialableCellphoneNumber getCellphoneNumber () {
306                 return this.cellphoneNumber;
307         }
308
309         @Override
310         public void setCellphoneNumber (final DialableCellphoneNumber cellphoneNumber) {
311                 this.cellphoneNumber = cellphoneNumber;
312         }
313
314         @Override
315         public String getCity () {
316                 return this.city;
317         }
318
319         @Override
320         public void setCity (final String city) {
321                 this.city = city;
322         }
323
324         @Override
325         public String getComment () {
326                 return this.comment;
327         }
328
329         @Override
330         public void setComment (final String comment) {
331                 this.comment = comment;
332         }
333
334         @Override
335         public Long getContactId () {
336                 return this.contactId;
337         }
338
339         @Override
340         public void setContactId (final Long contactId) {
341                 this.contactId = contactId;
342         }
343
344         @Override
345         public Country getCountry () {
346                 return this.country;
347         }
348
349         @Override
350         public void setCountry (final Country country) {
351                 this.country = country;
352         }
353
354         @Override
355         public Calendar getCreated () {
356                 return this.created;
357         }
358
359         @Override
360         public void setCreated (final Calendar created) {
361                 this.created = created;
362         }
363
364         @Override
365         public String getEmailAddress () {
366                 return this.emailAddress;
367         }
368
369         @Override
370         public void setEmailAddress (final String emailAddress) {
371                 this.emailAddress = emailAddress;
372         }
373
374         @Override
375         public String getFamilyName () {
376                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
377                 return this.familyName;
378         }
379
380         @Override
381         public void setFamilyName (final String familyName) {
382                 this.familyName = familyName;
383         }
384
385         @Override
386         public DialableFaxNumber getFaxNumber () {
387                 return this.faxNumber;
388         }
389
390         @Override
391         public void setFaxNumber (final DialableFaxNumber faxNumber) {
392                 this.faxNumber = faxNumber;
393         }
394
395         @Override
396         public String getFirstName () {
397                 return this.firstName;
398         }
399
400         @Override
401         public void setFirstName (final String firstName) {
402                 this.firstName = firstName;
403         }
404
405         @Override
406         public Gender getGender () {
407                 return this.gender;
408         }
409
410         @Override
411         public void setGender (final Gender gender) {
412                 this.gender = gender;
413         }
414
415         @Override
416         public Short getHouseNumber () {
417                 return this.houseNumber;
418         }
419
420         @Override
421         public void setHouseNumber (final Short houseNumber) {
422                 this.houseNumber = houseNumber;
423         }
424
425         @Override
426         public void setOwnContact (final Boolean ownContact) {
427                 this.ownContact = ownContact;
428         }
429
430         @Override
431         public DialableLandLineNumber getPhoneNumber () {
432                 return this.phoneNumber;
433         }
434
435         @Override
436         public void setPhoneNumber (final DialableLandLineNumber phoneNumber) {
437                 this.phoneNumber = phoneNumber;
438         }
439
440         @Override
441         public String getStreet () {
442                 return this.street;
443         }
444
445         @Override
446         public void setStreet (final String street) {
447                 this.street = street;
448         }
449
450         @Override
451         public Calendar getUpdated () {
452                 return this.updated;
453         }
454
455         @Override
456         public void setUpdated (final Calendar updated) {
457                 this.updated = updated;
458         }
459
460         @Override
461         public Integer getZipCode () {
462                 return this.zipCode;
463         }
464
465         @Override
466         public void setZipCode (final Integer zipCode) {
467                 this.zipCode = zipCode;
468         }
469
470         @Override
471         public int hashCode () {
472                 // Validate gender instance
473                 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
474
475                 int hash = 7;
476                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
477                 hash = 79 * hash + this.getGender().hashCode();
478                 hash = 79 * hash + Objects.hashCode(this.getFirstName());
479                 return hash;
480         }
481
482         /**
483          * Initialization with fake gender UNKNOWN
484          */
485         @PostConstruct
486         public void init () {
487                 // Fake gender
488                 this.gender = Gender.UNKNOWN;
489         }
490
491         @Override
492         public Boolean isOwnContact () {
493                 return this.ownContact;
494         }
495 }