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
196 * @return Comparison value
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
205 // Is the contactId the same?
206 if (Objects.equals(this.getContactId(), contact.getContactId())) {
207 // Same contactId, means same contact
209 } else if (this.getContactId() > contact.getContactId()) {
210 // This contactId is larger than compared to
214 // The other contactId is larger
219 public void copyAll (final Contact contact) {
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());
229 // - phone, fax, email
230 this.setPhoneNumber(contact.getPhoneNumber());
231 this.setFaxNumber(contact.getFaxNumber());
232 this.setCellphoneNumber(contact.getCellphoneNumber());
235 this.setBirthday(contact.getBirthday());
236 this.setComment(contact.getComment());
237 this.setCreated(contact.getCreated());
238 this.setUpdated(contact.getUpdated());
242 * Check if contacts are same or throw an exception
244 * @param object Other possible contact class
246 * @return Whether both contacts are same TODO Needs a lot improvements
249 public boolean equals (final Object object) {
251 if (!(object instanceof UserContact)) {
254 } else if (!(object instanceof Contact)) {
255 // Not correct interface
260 Contact contact = (Contact) object;
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())));
269 public Date getBirthday () {
270 return this.birthday;
274 public void setBirthday (final Date birthday) {
275 this.birthday = birthday;
279 public String getCellphoneNumber () {
280 return this.cellphoneNumber;
284 public void setCellphoneNumber (final String cellphoneNumber) {
285 this.cellphoneNumber = cellphoneNumber;
289 public String getCity () {
294 public void setCity (final String city) {
299 public String getComment () {
304 public void setComment (final String comment) {
305 this.comment = comment;
309 public Long getContactId () {
310 return this.contactId;
314 public void setContactId (final Long contactId) {
315 this.contactId = contactId;
319 public String getCountryCode () {
320 return this.countryCode;
324 public void setCountryCode (final String countryCode) {
325 this.countryCode = countryCode;
329 public Calendar getCreated () {
334 public void setCreated (final Calendar created) {
335 this.created = created;
339 public String getEmailAddress () {
340 return this.emailAddress;
344 public void setEmailAddress (final String emailAddress) {
345 this.emailAddress = emailAddress;
349 public String getFamilyName () {
350 //* NOISY-DEBUG: */ this.getLogger().logTrace("CALLED!");
351 return this.familyName;
355 public void setFamilyName (final String familyName) {
356 this.familyName = familyName;
360 public String getFaxNumber () {
361 return this.faxNumber;
365 public void setFaxNumber (final String faxNumber) {
366 this.faxNumber = faxNumber;
370 public String getFirstName () {
371 return this.firstName;
375 public void setFirstName (final String firstName) {
376 this.firstName = firstName;
380 public Gender getGender () {
385 public void setGender (final Gender gender) {
386 this.gender = gender;
390 public Short getHouseNumber () {
391 return this.houseNumber;
395 public void setHouseNumber (final Short houseNumber) {
396 this.houseNumber = houseNumber;
400 public void setOwnContact (final Boolean ownContact) {
401 this.ownContact = ownContact;
405 public String getPhoneNumber () {
406 return this.phoneNumber;
410 public void setPhoneNumber (final String phoneNumber) {
411 this.phoneNumber = phoneNumber;
415 public String getStreet () {
420 public void setStreet (final String street) {
421 this.street = street;
425 public Calendar getUpdated () {
430 public void setUpdated (final Calendar updated) {
431 this.updated = updated;
435 public Integer getZipCode () {
440 public void setZipCode (final Integer zipCode) {
441 this.zipCode = zipCode;
445 public int hashCode () {
446 // Validate gender instance
447 assert (this.getGender() instanceof Gender) : "gender is not set."; //NOI18N
450 hash = 79 * hash + Objects.hashCode(this.getFamilyName());
451 hash = 79 * hash + this.getGender().hashCode();
452 hash = 79 * hash + Objects.hashCode(this.getFirstName());
457 * Initialization with fake gender UNKNOWN
460 public void init () {
462 this.gender = Gender.UNKNOWN;
466 public Boolean isOwnContact () {
467 return this.ownContact;