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