2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.jaddressbook.model.addressbook;
19 import java.util.Date;
20 import java.util.Objects;
21 import javax.persistence.Basic;
22 import javax.persistence.CascadeType;
23 import javax.persistence.Column;
24 import javax.persistence.Entity;
25 import javax.persistence.EnumType;
26 import javax.persistence.Enumerated;
27 import javax.persistence.GeneratedValue;
28 import javax.persistence.GenerationType;
29 import javax.persistence.Id;
30 import javax.persistence.JoinColumn;
31 import javax.persistence.Lob;
32 import javax.persistence.NamedQueries;
33 import javax.persistence.NamedQuery;
34 import javax.persistence.OneToOne;
35 import javax.persistence.Table;
36 import javax.persistence.Temporal;
37 import javax.persistence.TemporalType;
38 import javax.persistence.Transient;
39 import org.mxchange.jaddressbook.model.addressbook.status.AddressbokStatus;
40 import org.mxchange.jcoreutils.Comparables;
41 import org.mxchange.jusercore.model.user.LoginUser;
42 import org.mxchange.jusercore.model.user.User;
45 * A POJO for user address books
47 * @author Roland Häder<roland@mxchange.org>
49 @Entity (name = "addressbooks")
50 @Table (name = "addressbooks")
53 @NamedQuery (name = "AllAddressbooks", query = "SELECT a FROM addressbooks AS a ORDER BY a.addressbookId ASC"),
54 @NamedQuery (name = "AllUsersAddressbooks", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :param ORDER BY a.addressbookId ASC"),
55 @NamedQuery (name = "SearchUserAddressbookName", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :user AND LOWER(a.addressbookName) LIKE LOWER(:name)")
58 @SuppressWarnings ("PersistenceUnitPresent")
59 public class UserAddressbook implements Addressbook {
65 private static final long serialVersionUID = 176_573_148_678_169L;
68 * When this address book has been created
70 @Basic (optional = false)
71 @Temporal (value = TemporalType.TIMESTAMP)
72 @Column (name = "addressbook_created", updatable = false, nullable = false)
73 private Date addressbookCreated;
79 @GeneratedValue (strategy = GenerationType.IDENTITY)
80 @Column (name = "addressbook_id", nullable = false, updatable = false)
81 private Long addressbookId;
84 * Last locked timestamp
86 @Temporal (TemporalType.TIMESTAMP)
87 @Column (name = "addressbook_last_locked")
88 private Date addressbookLastLocked;
91 * Last locked timestamp
94 @Column (name = "addressbook_last_locked_reason")
95 private String addressbookLastLockedReason;
98 * Name for this address book
100 @Basic (optional = false)
101 @Column (name = "addressbook_name", length = 50, nullable = false)
102 private String addressbookName;
105 * Address book status
107 @Basic (optional = false)
108 @Enumerated (EnumType.STRING)
109 @Column (name = "addressbook_status", nullable = false, length = 10)
110 private AddressbokStatus addressbookStatus;
113 * When this address book has been created
115 @Temporal (value = TemporalType.TIMESTAMP)
116 @Column (name = "addressbook_updated", insertable = false, nullable = false)
117 private Date addressbookUpdated;
120 * Connection to "users" table
122 @JoinColumn (name = "addressbook_user_id", nullable = false)
123 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
124 private User addressbookUser;
127 * Public constructor with address book name
129 * @param addressbookName Address book name
130 * @param addressbookUser User instance
132 public UserAddressbook (final String addressbookName, final User addressbookUser) {
133 // Invoke default constructor
136 // Set name and user instance
137 this.addressbookName = addressbookName;
138 this.addressbookUser = addressbookUser;
142 * Default constructor
144 public UserAddressbook () {
148 public int compareTo (final Addressbook addressbook) {
149 // Check parameter and return 0 if equal
150 if (null == addressbook) {
152 throw new NullPointerException("addressbook is null"); //NOI18N
153 } else if (addressbook.equals(this)) {
154 // Same object, below "expensive" code can be safely skipped
159 final int comparators[] = {
161 this.getAddressbookName().compareToIgnoreCase(addressbook.getAddressbookName()),
163 this.getAddressbookStatus().compareTo(addressbook.getAddressbookStatus()),
165 this.getAddressbookUser().compareTo(addressbook.getAddressbookUser())
169 final int comparison = Comparables.checkAll(comparators);
176 public boolean equals (final Object object) {
177 if (null == object) {
179 } else if (this.getClass() != object.getClass()) {
183 final Addressbook other = (Addressbook) object;
185 if (!Objects.equals(this.getAddressbookId(), other.getAddressbookId())) {
187 } else if (!Objects.equals(this.getAddressbookLastLocked(), other.getAddressbookLastLocked())) {
189 } else if (!Objects.equals(this.getAddressbookLastLockedReason(), other.getAddressbookLastLockedReason())) {
191 } else if (!Objects.equals(this.getAddressbookName(), other.getAddressbookName())) {
193 } else if (!Objects.equals(this.getAddressbookStatus(), other.getAddressbookStatus())) {
195 } else if (!Objects.equals(this.getAddressbookUser(), other.getAddressbookUser())) {
203 @SuppressWarnings ("ReturnOfDateField")
204 public Date getAddressbookCreated () {
205 return this.addressbookCreated;
209 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
210 public void setAddressbookCreated (final Date addressbookCreated) {
211 this.addressbookCreated = addressbookCreated;
215 public Long getAddressbookId () {
216 return this.addressbookId;
220 public void setAddressbookId (final Long addressbookId) {
221 this.addressbookId = addressbookId;
225 @SuppressWarnings ("ReturnOfDateField")
226 public Date getAddressbookLastLocked () {
227 return this.addressbookLastLocked;
231 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
232 public void setAddressbookLastLocked (final Date addressbookLastLocked) {
233 this.addressbookLastLocked = addressbookLastLocked;
237 public String getAddressbookLastLockedReason () {
238 return this.addressbookLastLockedReason;
242 public void setAddressbookLastLockedReason (final String addressbookLastLockedReason) {
243 this.addressbookLastLockedReason = addressbookLastLockedReason;
247 public String getAddressbookName () {
248 return this.addressbookName;
252 public void setAddressbookName (final String addressbookName) {
253 this.addressbookName = addressbookName;
257 public AddressbokStatus getAddressbookStatus () {
258 return this.addressbookStatus;
262 public void setAddressbookStatus (final AddressbokStatus addressbookStatus) {
263 this.addressbookStatus = addressbookStatus;
267 @SuppressWarnings ("ReturnOfDateField")
268 public Date getAddressbookUpdated () {
269 return this.addressbookUpdated;
273 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
274 public void setAddressbookUpdated (final Date addressbookUpdated) {
275 this.addressbookUpdated = addressbookUpdated;
279 public User getAddressbookUser () {
280 return this.addressbookUser;
284 public void setAddressbookUser (final User addressbookUser) {
285 this.addressbookUser = addressbookUser;
289 public int hashCode () {
292 hash = 59 * hash + Objects.hashCode(this.getAddressbookId());
293 hash = 59 * hash + Objects.hashCode(this.getAddressbookLastLocked());
294 hash = 59 * hash + Objects.hashCode(this.getAddressbookLastLockedReason());
295 hash = 59 * hash + Objects.hashCode(this.getAddressbookName());
296 hash = 59 * hash + Objects.hashCode(this.getAddressbookStatus());
297 hash = 59 * hash + Objects.hashCode(this.getAddressbookUser());