]> git.mxchange.org Git - jcontacts-core.git/blob - src/org/mxchange/jcontacts/contact/UserContact.java
Auto-formatted whole project
[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          * <p>
196          * @return Comparison value
197          */
198         @Override
199         public int compareTo (final Contact contact) {
200                 // contact should not be null
201                 if (null == contact) {
202                         throw new NullPointerException("contact is null"); //NOI18N
203                 }
204
205                 // Is the contactId the same?
206                 if (Objects.equals(this.getContactId(), contact.getContactId())) {
207                         // Same contactId, means same contact
208                         return 0;
209                 } else if (this.getContactId() > contact.getContactId()) {
210                         // This contactId is larger than compared to
211                         return -1;
212                 }
213
214                 // The other contactId is larger
215                 return 1;
216         }
217
218         @Override
219         public void copyAll (final Contact contact) {
220                 // Copy all:
221                 // - base data
222                 this.setFirstName(contact.getFirstName());
223                 this.setFamilyName(contact.getFamilyName());
224                 this.setStreet(contact.getStreet());
225                 this.setZipCode(contact.getZipCode());
226                 this.setCity(contact.getCity());
227                 this.setCountryCode(contact.getCountryCode());
228
229                 // - phone, fax, email
230                 this.setPhoneNumber(contact.getPhoneNumber());
231                 this.setFaxNumber(contact.getFaxNumber());
232                 this.setCellphoneNumber(contact.getCellphoneNumber());
233
234                 // - other data
235                 this.setBirthday(contact.getBirthday());
236                 this.setComment(contact.getComment());
237                 this.setCreated(contact.getCreated());
238                 this.setUpdated(contact.getUpdated());
239         }
240
241         /**
242          * Check if contacts are same or throw an exception
243          * <p>
244          * @param object Other possible contact class
245          * <p>
246          * @return Whether both contacts are same TODO Needs a lot improvements
247          */
248         @Override
249         public boolean equals (final Object object) {
250                 // Is it same type?
251                 if (!(object instanceof UserContact)) {
252                         // Not equal types
253                         return false;
254                 } else if (!(object instanceof Contact)) {
255                         // Not correct interface
256                         return false;
257                 }
258
259                 // Try to cast
260                 Contact contact = (Contact) object;
261
262                 // Now test some data TODO Definedly needs improvement
263                 return ((this.getGender().equals(contact.getGender())) &&
264                                 (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase())) &&
265                                 (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
266         }
267
268         @Override
269         public Date getBirthday () {
270                 return this.birthday;
271         }
272
273         @Override
274         public void setBirthday (final Date birthday) {
275                 this.birthday = birthday;
276         }
277
278         @Override
279         public String getCellphoneNumber () {
280                 return this.cellphoneNumber;
281         }
282
283         @Override
284         public void setCellphoneNumber (final String cellphoneNumber) {
285                 this.cellphoneNumber = cellphoneNumber;
286         }
287
288         @Override
289         public String getCity () {
290                 return this.city;
291         }
292
293         @Override
294         public void setCity (final String city) {
295                 this.city = city;
296         }
297
298         @Override
299         public String getComment () {
300                 return this.comment;
301         }
302
303         @Override
304         public void setComment (final String comment) {
305                 this.comment = comment;
306         }
307
308         @Override
309         public Long getContactId () {
310                 return this.contactId;
311         }
312
313         @Override
314         public void setContactId (final Long contactId) {
315                 this.contactId = contactId;
316         }
317
318         @Override
319         public String getCountryCode () {
320                 return this.countryCode;
321         }
322
323         @Override
324         public void setCountryCode (final String countryCode) {
325                 this.countryCode = countryCode;
326         }
327
328         @Override
329         public Calendar getCreated () {
330                 return this.created;
331         }
332
333         @Override
334         public void setCreated (final Calendar created) {
335                 this.created = created;
336         }
337
338         @Override
339         public String getEmailAddress () {
340                 return this.emailAddress;
341         }
342
343         @Override
344         public void setEmailAddress (final String emailAddress) {
345                 this.emailAddress = emailAddress;
346         }
347
348         @Override
349         public String getFamilyName () {
350                 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
351                 return this.familyName;
352         }
353
354         @Override
355         public void setFamilyName (final String familyName) {
356                 this.familyName = familyName;
357         }
358
359         @Override
360         public String getFaxNumber () {
361                 return this.faxNumber;
362         }
363
364         @Override
365         public void setFaxNumber (final String faxNumber) {
366                 this.faxNumber = faxNumber;
367         }
368
369         @Override
370         public String getFirstName () {
371                 return this.firstName;
372         }
373
374         @Override
375         public void setFirstName (final String firstName) {
376                 this.firstName = firstName;
377         }
378
379         @Override
380         public Gender getGender () {
381                 return this.gender;
382         }
383
384         @Override
385         public void setGender (final Gender gender) {
386                 this.gender = gender;
387         }
388
389         @Override
390         public Short getHouseNumber () {
391                 return this.houseNumber;
392         }
393
394         @Override
395         public void setHouseNumber (final Short houseNumber) {
396                 this.houseNumber = houseNumber;
397         }
398
399         @Override
400         public void setOwnContact (final Boolean ownContact) {
401                 this.ownContact = ownContact;
402         }
403
404         @Override
405         public String getPhoneNumber () {
406                 return this.phoneNumber;
407         }
408
409         @Override
410         public void setPhoneNumber (final String phoneNumber) {
411                 this.phoneNumber = phoneNumber;
412         }
413
414         @Override
415         public String getStreet () {
416                 return this.street;
417         }
418
419         @Override
420         public void setStreet (final String street) {
421                 this.street = street;
422         }
423
424         @Override
425         public Calendar getUpdated () {
426                 return this.updated;
427         }
428
429         @Override
430         public void setUpdated (final Calendar updated) {
431                 this.updated = updated;
432         }
433
434         @Override
435         public Integer getZipCode () {
436                 return this.zipCode;
437         }
438
439         @Override
440         public void setZipCode (final Integer zipCode) {
441                 this.zipCode = zipCode;
442         }
443
444         @Override
445         public int hashCode () {
446                 // Validate gender instance
447                 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
448
449                 int hash = 7;
450                 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
451                 hash = 79 * hash + this.getGender().hashCode();
452                 hash = 79 * hash + Objects.hashCode(this.getFirstName());
453                 return hash;
454         }
455
456         /**
457          * Initialization with fake gender UNKNOWN
458          */
459         @PostConstruct
460         public void init () {
461                 // Fake gender
462                 this.gender = Gender.UNKNOWN;
463         }
464
465         @Override
466         public Boolean isOwnContact () {
467                 return this.ownContact;
468         }
469 }