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