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;
82 @GeneratedValue (strategy = GenerationType.IDENTITY)
83 @Column (name = "contact_id", length = 20, updatable = false)
84 private Long contactId;
89 @Column (name = "country_code", length = 2, nullable = false)
90 private String countryCode;
93 * When the contact has been created
95 @Basic (optional = false)
96 @Temporal (TemporalType.TIMESTAMP)
97 @Column (name = "created", nullable = false)
98 private Calendar created;
103 @Column (name = "email_address", length = 100, nullable = false)
104 private String emailAddress;
109 @Basic (optional = false)
110 @Column (name = "family_name", length = 100, nullable = false)
111 private String familyName;
116 @Column (name = "fax_number", length = 100)
117 private String faxNumber;
122 @Basic (optional = false)
123 @Column (name = "first_name", length = 100, nullable = false)
124 private String firstName;
129 @Basic (optional = false)
130 @Column (name = "gender", nullable = false)
131 @Enumerated (EnumType.STRING)
132 private Gender gender;
137 @Column (name = "house_number", length = 5, nullable = false)
138 private Short houseNumber;
141 * Flag whether this contact is user's own data
143 @Column (name = "own_contact", nullable = false)
144 private Boolean ownContact;
149 @Column (name = "phone_number", length = 100)
150 private String phoneNumber;
155 @Column (name = "street", nullable = false)
156 private String street;
159 * When the contact has been updated
161 @Temporal (TemporalType.TIMESTAMP)
162 @Column (name = "updated")
163 private Calendar updated;
168 @Column (name = "zip_code", nullable = false, length = 6)
169 private Integer zipCode;
172 * Constructor for gender and names
174 * @param gender Gender instance
175 * @param firstName First name
176 * @param familyName Family name
178 public UserContact (final Gender gender, final String firstName, final String familyName) {
180 this.gender = gender;
181 this.firstName = firstName;
182 this.familyName = familyName;
186 * Default constructor
188 public UserContact () {
192 * Compares two contacts with each other
194 * @param contact Contact comparator
195 * @return Comparison value
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
204 // Is the contactId the same?
205 if (Objects.equals(this.getContactId(), contact.getContactId())) {
206 // Same contactId, means same contact
208 } else if (this.getContactId() > contact.getContactId()) {
209 // This contactId is larger than compared to
213 // The other contactId is larger
218 public void copyAll (final Contact contact) {
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());
228 // - phone, fax, email
229 this.setPhoneNumber(contact.getPhoneNumber());
230 this.setFaxNumber(contact.getFaxNumber());
231 this.setCellphoneNumber(contact.getCellphoneNumber());
234 this.setBirthday(contact.getBirthday());
235 this.setComment(contact.getComment());
236 this.setCreated(contact.getCreated());
237 this.setUpdated(contact.getUpdated());
241 * Check if contacts are same or throw an exception
243 * @param object Other possible contact class
244 * @return Whether both contacts are same TODO Needs a lot improvements
247 public boolean equals (final Object object) {
249 if (!(object instanceof UserContact)) {
252 } else if (!(object instanceof Contact)) {
253 // Not correct interface
258 Contact contact = (Contact) object;
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())));
267 public Date getBirthday () {
268 return this.birthday;
272 public void setBirthday (final Date birthday) {
273 this.birthday = birthday;
277 public String getCellphoneNumber () {
278 return this.cellphoneNumber;
282 public void setCellphoneNumber (final String cellphoneNumber) {
283 this.cellphoneNumber = cellphoneNumber;
287 public String getCity () {
292 public void setCity (final String city) {
297 public String getComment () {
302 public void setComment (final String comment) {
303 this.comment = comment;
307 public Long getContactId () {
308 return this.contactId;
312 public void setContactId (final Long contactId) {
313 this.contactId = contactId;
317 public String getCountryCode () {
318 return this.countryCode;
322 public void setCountryCode (final String countryCode) {
323 this.countryCode = countryCode;
327 public Calendar getCreated () {
332 public void setCreated (final Calendar created) {
333 this.created = created;
337 public String getEmailAddress () {
338 return this.emailAddress;
342 public void setEmailAddress (final String emailAddress) {
343 this.emailAddress = emailAddress;
347 public String getFamilyName () {
348 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
349 return this.familyName;
353 public void setFamilyName (final String familyName) {
354 this.familyName = familyName;
358 public String getFaxNumber () {
359 return this.faxNumber;
363 public void setFaxNumber (final String faxNumber) {
364 this.faxNumber = faxNumber;
368 public String getFirstName () {
369 return this.firstName;
373 public void setFirstName (final String firstName) {
374 this.firstName = firstName;
378 public Gender getGender () {
383 public void setGender (final Gender gender) {
384 this.gender = gender;
388 public Short getHouseNumber () {
389 return this.houseNumber;
393 public void setHouseNumber (final Short houseNumber) {
394 this.houseNumber = houseNumber;
398 public void setOwnContact (final Boolean ownContact) {
399 this.ownContact = ownContact;
403 public String getPhoneNumber () {
404 return this.phoneNumber;
408 public void setPhoneNumber (final String phoneNumber) {
409 this.phoneNumber = phoneNumber;
413 public String getStreet () {
418 public void setStreet (final String street) {
419 this.street = street;
423 public Calendar getUpdated () {
428 public void setUpdated (final Calendar updated) {
429 this.updated = updated;
433 public Integer getZipCode () {
438 public void setZipCode (final Integer zipCode) {
439 this.zipCode = zipCode;
443 public int hashCode () {
444 // Validate gender instance
445 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
448 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
449 hash = 79 * hash + this.getGender().hashCode();
450 hash = 79 * hash + Objects.hashCode(this.getFirstName());
455 * Initialization with fake gender UNKNOWN
458 public void init () {
460 this.gender = Gender.UNKNOWN;
464 public Boolean isOwnContact () {
465 return this.ownContact;