]> git.mxchange.org Git - jbonuscard-lib.git/blob - src/org/mxchange/addressbook/model/addressbook/UserAddressbook.java
Some cascade/fetch cleanups:
[jbonuscard-lib.git] / src / org / mxchange / addressbook / model / addressbook / UserAddressbook.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.model.addressbook;
18
19 import java.util.Calendar;
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 org.mxchange.addressbook.model.addressbook.status.AddressbokStatus;
39 import org.mxchange.jusercore.model.user.LoginUser;
40 import org.mxchange.jusercore.model.user.User;
41
42 /**
43  * A POJO for user addressbooks
44  * <p>
45  * @author Roland Haeder<roland@mxchange.org>
46  */
47 @Entity (name = "addressbooks")
48 @Table (name = "addressbooks")
49 @NamedQueries (
50                 {
51                         @NamedQuery (name = "AllUsersAddressbooks", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :param ORDER BY a.addressbookId ASC"),
52                         @NamedQuery (name = "SearchUserAddressbookName", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookUser = :user AND LOWER(a.addressbookName) LIKE LOWER(:name)"),
53                         @NamedQuery (name = "SearchAddressbookById", query = "SELECT a FROM addressbooks AS a WHERE a.addressbookId = :id")
54                 }
55 )
56 public class UserAddressbook implements Addressbook, Comparable<Addressbook> {
57
58         /**
59          * Serial number
60          */
61         private static final long serialVersionUID = 176_573_148_678_169L;
62
63         /**
64          * When this address book has been created
65          */
66         @Basic (optional = false)
67         @Temporal (value = TemporalType.TIMESTAMP)
68         @Column (name = "addressbook_created", nullable = false, updatable = false)
69         private Calendar addressbookCreated;
70
71         /**
72          * Id number
73          */
74         @Id
75         @GeneratedValue (strategy = GenerationType.IDENTITY)
76         @Column (name = "addressbook_id", length = 20, nullable = false, updatable = false)
77         private Long addressbookId;
78
79         /**
80          * Last locked timestamp
81          */
82         @Temporal (TemporalType.TIMESTAMP)
83         @Column (name = "addressbook_last_locked")
84         private Calendar addressbookLastLocked;
85
86         /**
87          * Last locked timestamp
88          */
89         @Lob
90         @Column (name = "addressbook_last_locked_reason")
91         private String addressbookLastLockedReason;
92
93         /**
94          * Name for this address book
95          */
96         @Basic (optional = false)
97         @Column (name = "addressbook_name", length = 50, nullable = false)
98         private String addressbookName;
99
100         /**
101          * Adress book status
102          */
103         @Basic (optional = false)
104         @Enumerated (EnumType.STRING)
105         @Column (name = "addressbook_status", nullable = false, length = 10)
106         private AddressbokStatus addressbookStatus;
107
108         /**
109          * Connection to "users" table
110          */
111         @JoinColumn (name = "addressbook_user_id", nullable = false)
112         @OneToOne (targetEntity = LoginUser.class, optional = false, cascade = CascadeType.REFRESH)
113         private User addressbookUser;
114
115         /**
116          * Public constructor with address book name
117          * <p>
118          * @param addressbookName Address book name
119          * @param addressbookUser User instance
120          * @param addressbookCreated When this entry has been created
121          */
122         public UserAddressbook (final String addressbookName, final User addressbookUser, final Calendar addressbookCreated) {
123                 // Call default constructor
124                 this();
125
126                 // Set name, user instance and creation timestamp
127                 this.addressbookName = addressbookName;
128                 this.addressbookUser = addressbookUser;
129                 this.addressbookCreated = addressbookCreated;
130         }
131
132         /**
133          * Default constructor
134          */
135         public UserAddressbook () {
136                 // Set status to UNLOCKED
137                 this.addressbookStatus = AddressbokStatus.UNLOCKED;
138         }
139
140         @Override
141         public int compareTo (final Addressbook addressbook) {
142                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
143         }
144
145         @Override
146         public boolean equals (final Object object) {
147                 if (object == null) {
148                         return false;
149                 } else if (this.getClass() != object.getClass()) {
150                         return false;
151                 }
152
153                 final Addressbook other = (Addressbook) object;
154
155                 if (!Objects.equals(this.getAddressbookName(), other.getAddressbookName())) {
156                         return false;
157                 } else if (!Objects.equals(this.getAddressbookUser(), other.getAddressbookUser())) {
158                         return false;
159                 }
160
161                 return true;
162         }
163
164         @Override
165         public int hashCode () {
166                 int hash = 7;
167                 hash = 59 * hash + Objects.hashCode(this.getAddressbookName());
168                 hash = 59 * hash + Objects.hashCode(this.getAddressbookUser());
169                 return hash;
170         }
171
172         @Override
173         public Calendar getAddressbookCreated () {
174                 return this.addressbookCreated;
175         }
176
177         @Override
178         public void setAddressbookCreated (final Calendar addressbookCreated) {
179                 this.addressbookCreated = addressbookCreated;
180         }
181
182         @Override
183         public Long getAddressbookId () {
184                 return this.addressbookId;
185         }
186
187         @Override
188         public void setAddressbookId (final Long addressbookId) {
189                 this.addressbookId = addressbookId;
190         }
191
192         @Override
193         public Calendar getAddressbookLastLocked () {
194                 return this.addressbookLastLocked;
195         }
196
197         @Override
198         public void setAddressbookLastLocked (final Calendar addressbookLastLocked) {
199                 this.addressbookLastLocked = addressbookLastLocked;
200         }
201
202         @Override
203         public String getAddressbookLastLockedReason () {
204                 return this.addressbookLastLockedReason;
205         }
206
207         @Override
208         public void setAddressbookLastLockedReason (final String addressbookLastLockedReason) {
209                 this.addressbookLastLockedReason = addressbookLastLockedReason;
210         }
211
212         @Override
213         public String getAddressbookName () {
214                 return this.addressbookName;
215         }
216
217         @Override
218         public void setAddressbookName (final String addressbookName) {
219                 this.addressbookName = addressbookName;
220         }
221
222         @Override
223         public AddressbokStatus getAddressbookStatus () {
224                 return this.addressbookStatus;
225         }
226
227         @Override
228         public void setAddressbookStatus (final AddressbokStatus addressbookStatus) {
229                 this.addressbookStatus = addressbookStatus;
230         }
231
232         @Override
233         public User getAddressbookUser () {
234                 return this.addressbookUser;
235         }
236
237         @Override
238         public void setAddressbookUser (final User addressbookUser) {
239                 this.addressbookUser = addressbookUser;
240         }
241
242 }