2 * Copyright (C) 2016 - 2022 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.comparable.Comparables;
41 import org.mxchange.jusercore.model.user.LoginUser;
42 import org.mxchange.jusercore.model.user.User;
43 import org.mxchange.jusercore.model.utils.UserUtils;
46 * A POJO for user address books
48 * @author Roland Häder<roland@mxchange.org>
50 @Entity (name = "addressbooks")
51 @Table (name = "addressbooks")
54 @NamedQuery (name = "AllAddressbooks", query = "SELECT a FROM addressbooks AS a ORDER BY a.addressbookId ASC"),
55 @NamedQuery (name = "AllUsersAddressbooks", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :param ORDER BY a.addressbookId ASC"),
56 @NamedQuery (name = "SearchUserAddressbookName", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :user AND LOWER(a.addressbookName) LIKE LOWER(:name)")
59 @SuppressWarnings ("PersistenceUnitPresent")
60 public class UserAddressbook implements Addressbook {
66 private static final long serialVersionUID = 176_573_148_678_169L;
69 * When this address book has been created
71 @Basic (optional = false)
72 @Temporal (value = TemporalType.TIMESTAMP)
73 @Column (name = "addressbook_created", updatable = false, nullable = false)
74 private Date addressbookCreated;
80 @GeneratedValue (strategy = GenerationType.IDENTITY)
81 @Column (name = "addressbook_id", nullable = false, updatable = false)
82 private Long addressbookId;
85 * Last locked timestamp
87 @Temporal (TemporalType.TIMESTAMP)
88 @Column (name = "addressbook_last_locked")
89 private Date addressbookLastLocked;
92 * Last locked timestamp
95 @Column (name = "addressbook_last_locked_reason")
96 private String addressbookLastLockedReason;
99 * Name for this address book
101 @Basic (optional = false)
102 @Column (name = "addressbook_name", length = 50, nullable = false)
103 private String addressbookName;
106 * Address book status
108 @Basic (optional = false)
109 @Enumerated (EnumType.STRING)
110 @Column (name = "addressbook_status", nullable = false, length = 10)
111 private AddressbokStatus addressbookStatus;
114 * When this address book has been created
116 @Temporal (value = TemporalType.TIMESTAMP)
117 @Column (name = "addressbook_updated", insertable = false, nullable = false)
118 private Date addressbookUpdated;
121 * Connection to "users" table
123 @JoinColumn (name = "addressbook_user_id", nullable = false)
124 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
125 private User addressbookUser;
128 * Public constructor with address book name
130 * @param addressbookName Address book name
131 * @param addressbookUser User instance
133 public UserAddressbook (final String addressbookName, final User addressbookUser) {
134 // Invoke default constructor
137 // Set name and user instance
138 this.addressbookName = addressbookName;
139 this.addressbookUser = addressbookUser;
143 * Default constructor
145 public UserAddressbook () {
149 public int compareTo (final Addressbook addressbook) {
150 // Check parameter and return 0 if equal
151 if (null == addressbook) {
153 throw new NullPointerException("addressbook is null"); //NOI18N
154 } else if (addressbook.equals(this)) {
155 // Same object, below "expensive" code can be safely skipped
160 final int comparators[] = {
162 this.getAddressbookName().compareToIgnoreCase(addressbook.getAddressbookName()),
164 this.getAddressbookStatus().compareTo(addressbook.getAddressbookStatus()),
166 UserUtils.compare(this.getAddressbookUser(), addressbook.getAddressbookUser())
170 final int comparison = Comparables.checkAll(comparators);
177 public boolean equals (final Object object) {
178 if (null == object) {
180 } else if (this.getClass() != object.getClass()) {
184 final Addressbook addressbook = (Addressbook) object;
186 if (!Objects.equals(this.getAddressbookId(), addressbook.getAddressbookId())) {
188 } else if (!Objects.equals(this.getAddressbookLastLocked(), addressbook.getAddressbookLastLocked())) {
190 } else if (!Objects.equals(this.getAddressbookLastLockedReason(), addressbook.getAddressbookLastLockedReason())) {
192 } else if (!Objects.equals(this.getAddressbookName(), addressbook.getAddressbookName())) {
194 } else if (!Objects.equals(this.getAddressbookStatus(), addressbook.getAddressbookStatus())) {
196 } else if (!Objects.equals(this.getAddressbookUser(), addressbook.getAddressbookUser())) {
204 @SuppressWarnings ("ReturnOfDateField")
205 public Date getAddressbookCreated () {
206 return this.addressbookCreated;
210 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
211 public void setAddressbookCreated (final Date addressbookCreated) {
212 this.addressbookCreated = addressbookCreated;
216 public Long getAddressbookId () {
217 return this.addressbookId;
221 public void setAddressbookId (final Long addressbookId) {
222 this.addressbookId = addressbookId;
226 @SuppressWarnings ("ReturnOfDateField")
227 public Date getAddressbookLastLocked () {
228 return this.addressbookLastLocked;
232 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
233 public void setAddressbookLastLocked (final Date addressbookLastLocked) {
234 this.addressbookLastLocked = addressbookLastLocked;
238 public String getAddressbookLastLockedReason () {
239 return this.addressbookLastLockedReason;
243 public void setAddressbookLastLockedReason (final String addressbookLastLockedReason) {
244 this.addressbookLastLockedReason = addressbookLastLockedReason;
248 public String getAddressbookName () {
249 return this.addressbookName;
253 public void setAddressbookName (final String addressbookName) {
254 this.addressbookName = addressbookName;
258 public AddressbokStatus getAddressbookStatus () {
259 return this.addressbookStatus;
263 public void setAddressbookStatus (final AddressbokStatus addressbookStatus) {
264 this.addressbookStatus = addressbookStatus;
268 @SuppressWarnings ("ReturnOfDateField")
269 public Date getAddressbookUpdated () {
270 return this.addressbookUpdated;
274 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
275 public void setAddressbookUpdated (final Date addressbookUpdated) {
276 this.addressbookUpdated = addressbookUpdated;
280 public User getAddressbookUser () {
281 return this.addressbookUser;
285 public void setAddressbookUser (final User addressbookUser) {
286 this.addressbookUser = addressbookUser;
290 public int hashCode () {
293 hash = 59 * hash + Objects.hashCode(this.getAddressbookId());
294 hash = 59 * hash + Objects.hashCode(this.getAddressbookLastLocked());
295 hash = 59 * hash + Objects.hashCode(this.getAddressbookLastLockedReason());
296 hash = 59 * hash + Objects.hashCode(this.getAddressbookName());
297 hash = 59 * hash + Objects.hashCode(this.getAddressbookStatus());
298 hash = 59 * hash + Objects.hashCode(this.getAddressbookUser());