2 * Copyright (C) 2016 - 2018 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", nullable = false, updatable = 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 * Connection to "users" table
115 @JoinColumn (name = "addressbook_user_id", nullable = false)
116 @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH, optional = false)
117 private User addressbookUser;
120 * Public constructor with address book name
122 * @param addressbookName Address book name
123 * @param addressbookUser User instance
125 public UserAddressbook (final String addressbookName, final User addressbookUser) {
126 // Call default constructor
129 // Set name and user instance
130 this.addressbookName = addressbookName;
131 this.addressbookUser = addressbookUser;
135 * Default constructor
137 public UserAddressbook () {
141 public int compareTo (final Addressbook addressbook) {
142 // Checkparameter and return 0 if equal
143 if (null == addressbook) {
145 throw new NullPointerException("addressbook is null"); //NOI18N
146 } else if (Objects.equals(this, addressbook)) {
152 final int comparators[] = {
154 this.getAddressbookName().compareToIgnoreCase(addressbook.getAddressbookName()),
156 this.getAddressbookStatus().compareTo(addressbook.getAddressbookStatus()),
158 this.getAddressbookUser().compareTo(addressbook.getAddressbookUser())
162 final int comparison = Comparables.checkAll(comparators);
169 public boolean equals (final Object object) {
170 if (null == object) {
172 } else if (this.getClass() != object.getClass()) {
176 final Addressbook other = (Addressbook) object;
178 if (!Objects.equals(this.getAddressbookName(), other.getAddressbookName())) {
180 } else if (!Objects.equals(this.getAddressbookUser(), other.getAddressbookUser())) {
188 @SuppressWarnings ("ReturnOfDateField")
189 public Date getAddressbookCreated () {
190 return this.addressbookCreated;
194 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
195 public void setAddressbookCreated (final Date addressbookCreated) {
196 this.addressbookCreated = addressbookCreated;
200 public Long getAddressbookId () {
201 return this.addressbookId;
205 public void setAddressbookId (final Long addressbookId) {
206 this.addressbookId = addressbookId;
210 @SuppressWarnings ("ReturnOfDateField")
211 public Date getAddressbookLastLocked () {
212 return this.addressbookLastLocked;
216 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
217 public void setAddressbookLastLocked (final Date addressbookLastLocked) {
218 this.addressbookLastLocked = addressbookLastLocked;
222 public String getAddressbookLastLockedReason () {
223 return this.addressbookLastLockedReason;
227 public void setAddressbookLastLockedReason (final String addressbookLastLockedReason) {
228 this.addressbookLastLockedReason = addressbookLastLockedReason;
232 public String getAddressbookName () {
233 return this.addressbookName;
237 public void setAddressbookName (final String addressbookName) {
238 this.addressbookName = addressbookName;
242 public AddressbokStatus getAddressbookStatus () {
243 return this.addressbookStatus;
247 public void setAddressbookStatus (final AddressbokStatus addressbookStatus) {
248 this.addressbookStatus = addressbookStatus;
252 public User getAddressbookUser () {
253 return this.addressbookUser;
257 public void setAddressbookUser (final User addressbookUser) {
258 this.addressbookUser = addressbookUser;
262 public int hashCode () {
265 hash = 59 * hash + Objects.hashCode(this.getAddressbookName());
266 hash = 59 * hash + Objects.hashCode(this.getAddressbookUser());