2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jcontacts.contact;
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;
38 * A general contact class which should only be extended.
40 * @author Roland Haeder<roland@mxchange.org>
43 @Entity (name = "contacts")
44 @Table (name = "contacts")
45 public class UserContact implements Contact, Comparable<Contact> {
50 private static final long serialVersionUID = 58_744_284_981_863L;
55 @Column (name = "birthday")
56 @Temporal (TemporalType.DATE)
57 private Date birthday;
62 @Column (name = "cellphone_number", length = 100)
63 private String cellphoneNumber;
68 @Column (name = "city", nullable = false, length = 100)
75 @Column (name = "comment")
76 private String comment;
81 @Column (name = "company_name", nullable = false)
82 private String companyName;
88 @GeneratedValue (strategy = GenerationType.IDENTITY)
89 @Column (name = "contact_id", length = 20, updatable = false)
90 private Long contactId;
95 @Column (name = "country_code", length = 2, nullable = false)
96 private String countryCode;
99 * When the contact has been created
101 @Basic (optional = false)
102 @Temporal (TemporalType.TIMESTAMP)
103 @Column (name = "created", nullable = false)
104 private Calendar created;
109 @Column (name = "email_address", length = 100, nullable = false)
110 private String emailAddress;
115 @Basic (optional = false)
116 @Column (name = "family_name", length = 100, nullable = false)
117 private String familyName;
122 @Column (name = "fax_number", length = 100)
123 private String faxNumber;
128 @Basic (optional = false)
129 @Column (name = "first_name", length = 100, nullable = false)
130 private String firstName;
135 @Basic (optional = false)
136 @Column (name = "gender", nullable = false)
137 @Enumerated (EnumType.STRING)
138 private Gender gender;
143 @Column (name = "house_number", length = 5, nullable = false)
144 private Short houseNumber;
147 * Flag whether this contact is user's own data
149 @Column (name = "own_contact", nullable = false)
150 private Boolean ownContact;
155 @Column (name = "phone_number", length = 100)
156 private String phoneNumber;
161 @Column (name = "street", nullable = false)
162 private String street;
165 * When the contact has been updated
167 @Temporal (TemporalType.TIMESTAMP)
168 @Column (name = "updated")
169 private Calendar updated;
174 @Column (name = "zip_code", nullable = false, length = 6)
175 private Long zipCode;
178 * Constructor for gender and names
180 * @param gender Gender instance
181 * @param firstName First name
182 * @param familyName Family name
183 * @param companyName Company name
185 public UserContact (final Gender gender, final String firstName, final String familyName, final String companyName) {
187 this.gender = gender;
188 this.firstName = firstName;
189 this.familyName = familyName;
193 * Default constructor
195 public UserContact () {
199 * Compares two contacts with each other
201 * @param contact Contact comparator
202 * @return Comparison value
205 public int compareTo (final Contact contact) {
206 // contact should not be null
207 if (null == contact) {
208 throw new NullPointerException("contact is null"); //NOI18N
211 // Is the contactId the same?
212 if (Objects.equals(this.getContactId(), contact.getContactId())) {
213 // Same contactId, means same contact
215 } else if (this.getContactId() > contact.getContactId()) {
216 // This contactId is larger than compared to
220 // The other contactId is larger
225 public void copyAll (final Contact contact) {
228 this.setFirstName(contact.getFirstName());
229 this.setFamilyName(contact.getFamilyName());
230 this.setCompanyName(contact.getCompanyName());
231 this.setStreet(contact.getStreet());
232 this.setZipCode(contact.getZipCode());
233 this.setCity(contact.getCity());
234 this.setCountryCode(contact.getCountryCode());
236 // - phone, fax, email
237 this.setPhoneNumber(contact.getPhoneNumber());
238 this.setFaxNumber(contact.getFaxNumber());
239 this.setCellphoneNumber(contact.getCellphoneNumber());
242 this.setBirthday(contact.getBirthday());
243 this.setComment(contact.getComment());
244 this.setCreated(contact.getCreated());
245 this.setUpdated(contact.getUpdated());
249 * Check if contacts are same or throw an exception
251 * @param object Other possible contact class
252 * @return Whether both contacts are same TODO Needs a lot improvements
255 public boolean equals (final Object object) {
257 if (!(object instanceof UserContact)) {
260 } else if (!(object instanceof Contact)) {
261 // Not correct interface
266 Contact contact = (Contact) object;
268 // Now test some data TODO Definedly needs improvement
269 return ((this.getGender().equals(contact.getGender()))
270 && (this.getFirstName().toLowerCase().equals(contact.getFirstName().toLowerCase()))
271 && (this.getFamilyName().toLowerCase().equals(contact.getFamilyName().toLowerCase())));
275 public Date getBirthday () {
276 return this.birthday;
280 public void setBirthday (final Date birthday) {
281 this.birthday = birthday;
285 public String getCellphoneNumber () {
286 return this.cellphoneNumber;
290 public void setCellphoneNumber (final String cellphoneNumber) {
291 this.cellphoneNumber = cellphoneNumber;
295 public String getCity () {
300 public void setCity (final String city) {
305 public String getComment () {
310 public void setComment (final String comment) {
311 this.comment = comment;
315 public String getCompanyName () {
316 return this.companyName;
320 public void setCompanyName (final String companyName) {
321 this.companyName = companyName;
325 public Long getContactId () {
326 return this.contactId;
330 public void setContactId (final Long contactId) {
331 this.contactId = contactId;
335 public String getCountryCode () {
336 return this.countryCode;
340 public void setCountryCode (final String countryCode) {
341 this.countryCode = countryCode;
345 public Calendar getCreated () {
350 public void setCreated (final Calendar created) {
351 this.created = created;
355 public String getEmailAddress () {
356 return this.emailAddress;
360 public void setEmailAddress (final String emailAddress) {
361 this.emailAddress = emailAddress;
365 public String getFamilyName () {
366 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
367 return this.familyName;
371 public void setFamilyName (final String familyName) {
372 this.familyName = familyName;
376 public String getFaxNumber () {
377 return this.faxNumber;
381 public void setFaxNumber (final String faxNumber) {
382 this.faxNumber = faxNumber;
386 public String getFirstName () {
387 return this.firstName;
391 public void setFirstName (final String firstName) {
392 this.firstName = firstName;
396 public Gender getGender () {
401 public void setGender (final Gender gender) {
402 this.gender = gender;
406 public Short getHouseNumber () {
407 return this.houseNumber;
411 public void setHouseNumber (final Short houseNumber) {
412 this.houseNumber = houseNumber;
416 public void setOwnContact (final Boolean ownContact) {
417 this.ownContact = ownContact;
421 public String getPhoneNumber () {
422 return this.phoneNumber;
426 public void setPhoneNumber (final String phoneNumber) {
427 this.phoneNumber = phoneNumber;
431 public String getStreet () {
436 public void setStreet (final String street) {
437 this.street = street;
441 public Calendar getUpdated () {
446 public void setUpdated (final Calendar updated) {
447 this.updated = updated;
451 public Long getZipCode () {
456 public void setZipCode (final Long zipCode) {
457 this.zipCode = zipCode;
461 public int hashCode () {
462 // Validate gender instance
463 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
466 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
467 hash = 79 * hash + this.getGender().hashCode();
468 hash = 79 * hash + Objects.hashCode(this.getFirstName());
473 * Initialization with fake gender UNKNOWN
476 public void init () {
478 this.gender = Gender.UNKNOWN;
482 public Boolean isOwnContact () {
483 return this.ownContact;