]> git.mxchange.org Git - jcontacts-core.git/blobdiff - src/org/mxchange/jcontacts/contact/UserContact.java
Added named query SearchContactById which searches for a contact entity by it's id...
[jcontacts-core.git] / src / org / mxchange / jcontacts / contact / UserContact.java
index b98dfd33465374f277557a64fa4b0b2c90bffdc5..51b094709340787b80acb49fc9806c72151e0fda 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Roland Haeder
+ * Copyright (C) 2016 Roland Haeder
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,20 +19,20 @@ package org.mxchange.jcontacts.contact;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Objects;
-import javax.annotation.PostConstruct;
 import javax.persistence.Basic;
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
-import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
 import javax.persistence.Index;
 import javax.persistence.JoinColumn;
 import javax.persistence.Lob;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
 import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
@@ -48,7 +48,7 @@ import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
 
 /**
- * A general contact class which should only be extended.
+ * A general contact class which serves as an entity.
  * <p>
  * @author Roland Haeder<roland@mxchange.org>
  * @version 0.0
@@ -60,10 +60,21 @@ import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
                        @Index (
                                        name = "contact_gender",
                                        columnList = "contact_gender"
+                       ),
+                       @Index (
+                                       name = "contact_email_address",
+                                       unique = true,
+                                       columnList = "contact_email_address"
                        )
                }
 )
-public class UserContact implements Contact, Comparable<Contact> {
+@NamedQueries (
+               {
+                       @NamedQuery (name = "AllContactsByCellphone", query = "SELECT c FROM contacts AS c WHERE c.contactCellphoneNumber = :cellPhone ORDER BY c.contactId ASC"),
+                       @NamedQuery (name = "SearchContactById", query = "SELECT c FROM contacts AS c WHERE c.contactId = :contactId")
+               }
+)
+public class UserContact implements Contact {
 
        /**
         * Serial number
@@ -80,13 +91,14 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Cellphone number
         */
-       @JoinColumn (name = "contact_cellphone_number_id")
+       @JoinColumn (name = "contact_cellphone_number_id", referencedColumnName = "cellphone_id", unique = true)
        @OneToOne (targetEntity = CellphoneNumber.class, cascade = CascadeType.ALL)
        private DialableCellphoneNumber contactCellphoneNumber;
 
        /**
         * City
         */
+       @Basic (optional = false)
        @Column (name = "contact_city", nullable = false, length = 100)
        private String contactCity;
 
@@ -97,19 +109,11 @@ public class UserContact implements Contact, Comparable<Contact> {
        @Column (name = "contact_comment")
        private String contactComment;
 
-       /**
-        * Id number
-        */
-       @Id
-       @GeneratedValue (strategy = GenerationType.IDENTITY)
-       @Column (name = "contact_id", length = 20, updatable = false)
-       private Long contactId;
-
        /**
         * Country code
         */
-       @JoinColumn (name = "contact_country_id", nullable = false)
-       @OneToOne(targetEntity = CountryData.class, optional = false, fetch = FetchType.EAGER)
+       @JoinColumn (name = "contact_country_id", nullable = false, referencedColumnName = "country_id")
+       @OneToOne (targetEntity = CountryData.class, cascade = CascadeType.REFRESH, optional = false)
        private Country contactCountry;
 
        /**
@@ -123,6 +127,7 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Email address
         */
+       @Basic (optional = false)
        @Column (name = "contact_email_address", length = 100, nullable = false)
        private String contactEmailAddress;
 
@@ -136,8 +141,8 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * Fax number
         */
-       @JoinColumn (name = "contact_fax_number_id")
-       @OneToOne(targetEntity = FaxNumber.class,cascade = CascadeType.ALL)
+       @JoinColumn (name = "contact_fax_number_id", referencedColumnName = "fax_id", unique = true)
+       @OneToOne (targetEntity = FaxNumber.class, cascade = CascadeType.ALL)
        private DialableFaxNumber contactFaxNumber;
 
        /**
@@ -158,28 +163,45 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * House number
         */
+       @Basic (optional = false)
        @Column (name = "contact_house_number", length = 5, nullable = false)
        private Short contactHouseNumber;
 
+       /**
+        * Id number
+        */
+       @Id
+       @GeneratedValue (strategy = GenerationType.IDENTITY)
+       @Column (name = "contact_id", nullable = false, updatable = false)
+       private Long contactId;
+
        /**
         * Flag whether this contact is user's own data
         */
+       @Basic (optional = false)
        @Column (name = "contact_own_contact", nullable = false)
        private Boolean contactOwnContact;
 
        /**
         * Phone number
         */
-       @JoinColumn (name = "contact_phone_number_id")
+       @JoinColumn (name = "contact_phone_number_id", referencedColumnName = "phone_id", unique = true)
        @OneToOne (targetEntity = LandLineNumber.class, cascade = CascadeType.ALL)
        private DialableLandLineNumber contactPhoneNumber;
 
        /**
         * Street
         */
+       @Basic (optional = false)
        @Column (name = "contact_street", nullable = false)
        private String contactStreet;
 
+       /**
+        * Title (Doctor, etc)
+        */
+       @Column (name = "contact_title")
+       private String contactTitle;
+
        /**
         * When the contact has been updated
         */
@@ -190,54 +212,36 @@ public class UserContact implements Contact, Comparable<Contact> {
        /**
         * ZIP code
         */
+       @Basic (optional = false)
        @Column (name = "contact_zip_code", nullable = false, length = 6)
        private Integer contactZipCode;
 
-       /**
-        * Constructor for contactGender and names
-        * <p>
-        * @param contactGender Gender instance
-        * @param contactFirstName First name
-        * @param contactFamilyName Family name
-        */
-       public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
-               // Set all
-               this.contactGender = contactGender;
-               this.contactFirstName = contactFirstName;
-               this.contactFamilyName = contactFamilyName;
-       }
-
        /**
         * Default constructor
         */
        public UserContact () {
+               // Default is not user's own contact
+               this.contactOwnContact = Boolean.FALSE;
+
+               // Unknown gender
+               this.contactGender = Gender.UNKNOWN;
        }
 
        /**
-        * Compares two contacts with each other
-        * <p>
-        * @param contact Contact comparator
+        * Constructor for contactGender and names
         * <p>
-        * @return Comparison value
+        * @param contactGender     Gender instance
+        * @param contactFirstName  First name
+        * @param contactFamilyName Family name
         */
-       @Override
-       public int compareTo (final Contact contact) {
-               // contact should not be null
-               if (null == contact) {
-                       throw new NullPointerException("contact is null"); //NOI18N
-               }
-
-               // Is the contactId the same?
-               if (Objects.equals(this.getContactId(), contact.getContactId())) {
-                       // Same contactId, means same contact
-                       return 0;
-               } else if (this.getContactId() > contact.getContactId()) {
-                       // This contactId is larger than compared to
-                       return -1;
-               }
+       public UserContact (final Gender contactGender, final String contactFirstName, final String contactFamilyName) {
+               // Call default constructor
+               this();
 
-               // The other contactId is larger
-               return 1;
+               // Set all
+               this.contactGender = contactGender;
+               this.contactFirstName = contactFirstName;
+               this.contactFamilyName = contactFamilyName;
        }
 
        @Override
@@ -252,7 +256,7 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.setContactCountry(contact.getContactCountry());
 
                // - phone, fax, email
-               this.setContactPhoneNumber(contact.getContactPhoneNumber());
+               this.setContactLandLineNumber(contact.getContactLandLineNumber());
                this.setContactFaxNumber(contact.getContactFaxNumber());
                this.setContactCellphoneNumber(contact.getContactCellphoneNumber());
 
@@ -263,13 +267,6 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.setContactUpdated(contact.getContactUpdated());
        }
 
-       /**
-        * Check if contacts are same or throw an exception
-        * <p>
-        * @param object Other possible contact class
-        * <p>
-        * @return Whether both contacts are same TODO Needs a lot improvements
-        */
        @Override
        public boolean equals (final Object object) {
                // Is it same type?
@@ -290,6 +287,18 @@ public class UserContact implements Contact, Comparable<Contact> {
                                (this.getContactFamilyName().toLowerCase().equals(contact.getContactFamilyName().toLowerCase())));
        }
 
+       @Override
+       public int hashCode () {
+               // Validate contactGender instance
+               assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
+
+               int hash = 7;
+               hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
+               hash = 79 * hash + this.getContactGender().hashCode();
+               hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
+               return hash;
+       }
+
        @Override
        public Date getContactBirthday () {
                return this.contactBirthday;
@@ -330,16 +339,6 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.contactComment = contactComment;
        }
 
-       @Override
-       public Long getContactId () {
-               return this.contactId;
-       }
-
-       @Override
-       public void setContactId (final Long contactId) {
-               this.contactId = contactId;
-       }
-
        @Override
        public Country getContactCountry () {
                return this.contactCountry;
@@ -421,18 +420,28 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.contactHouseNumber = contactHouseNumber;
        }
 
+       @Override
+       public Long getContactId () {
+               return this.contactId;
+       }
+
+       @Override
+       public void setContactId (final Long contactId) {
+               this.contactId = contactId;
+       }
+
        @Override
        public void setContactOwnContact (final Boolean contactOwnContact) {
                this.contactOwnContact = contactOwnContact;
        }
 
        @Override
-       public DialableLandLineNumber getContactPhoneNumber () {
+       public DialableLandLineNumber getContactLandLineNumber () {
                return this.contactPhoneNumber;
        }
 
        @Override
-       public void setContactPhoneNumber (final DialableLandLineNumber contactPhoneNumber) {
+       public void setContactLandLineNumber (final DialableLandLineNumber contactPhoneNumber) {
                this.contactPhoneNumber = contactPhoneNumber;
        }
 
@@ -446,6 +455,16 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.contactStreet = contactStreet;
        }
 
+       @Override
+       public String getContactTitle () {
+               return this.contactTitle;
+       }
+
+       @Override
+       public void setContactTitle (final String contactTitle) {
+               this.contactTitle = contactTitle;
+       }
+
        @Override
        public Calendar getContactUpdated () {
                return this.contactUpdated;
@@ -466,27 +485,6 @@ public class UserContact implements Contact, Comparable<Contact> {
                this.contactZipCode = contactZipCode;
        }
 
-       @Override
-       public int hashCode () {
-               // Validate contactGender instance
-               assert (this.getContactGender() instanceof Gender) : "gender is not set."; //NOI18N
-
-               int hash = 7;
-               hash = 79 * hash + Objects.hashCode(this.getContactFamilyName());
-               hash = 79 * hash + this.getContactGender().hashCode();
-               hash = 79 * hash + Objects.hashCode(this.getContactFirstName());
-               return hash;
-       }
-
-       /**
-        * Initialization with fake contactGender UNKNOWN
-        */
-       @PostConstruct
-       public void init () {
-               // Fake contactGender
-               this.contactGender = Gender.UNKNOWN;
-       }
-
        @Override
        public Boolean isOwnContact () {
                return this.contactOwnContact;