]> git.mxchange.org Git - juser-login-core.git/blob - src/org/mxchange/jusercore/model/user/password_history/UserPasswordHistory.java
added named query for user's history entries, sorted by primary key ("unlimited")
[juser-login-core.git] / src / org / mxchange / jusercore / model / user / password_history / UserPasswordHistory.java
1 /*
2  * Copyright (C) 2016 Cho-Time GmbH
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.jusercore.model.user.password_history;
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.GeneratedValue;
26 import javax.persistence.GenerationType;
27 import javax.persistence.Id;
28 import javax.persistence.JoinColumn;
29 import javax.persistence.NamedQueries;
30 import javax.persistence.NamedQuery;
31 import javax.persistence.OneToOne;
32 import javax.persistence.Table;
33 import javax.persistence.Temporal;
34 import javax.persistence.TemporalType;
35 import javax.persistence.Transient;
36 import org.mxchange.jusercore.model.user.LoginUser;
37 import org.mxchange.jusercore.model.user.User;
38
39 /**
40  * A POJO for user password history
41  * <p>
42  * @author Roland Haeder<rhaeder@cho-time.de>
43  */
44 @Entity (name = "user_password_history")
45 @Table (
46                 name = "user_password_history"
47 )
48 @NamedQueries (
49                 {
50                         @NamedQuery (name = "AllUsersHistoryEntries", query = "SELECT h FROM user_password_history AS h WHERE h.userPasswordHistoryUser = :user ORDER BY h.userPasswordHistoryId ASC")
51                 }
52 )
53 @SuppressWarnings ("PersistenceUnitPresent")
54 public class UserPasswordHistory implements PasswordHistory {
55
56         /**
57          * Serial number
58          */
59         @Transient
60         private static final long serialVersionUID = 1L;
61
62         /**
63          * Timestamp when this entry has been created
64          */
65         @Basic (optional = false)
66         @Column (name = "history_created", nullable = false, updatable = false)
67         @Temporal (TemporalType.TIMESTAMP)
68         private Calendar userPasswordHistoryCreated;
69
70         /**
71          * Id number (primary key)
72          */
73         @Id
74         @Column (name = "history_id", updatable = false)
75         @GeneratedValue (strategy = GenerationType.IDENTITY)
76         private Long userPasswordHistoryId;
77
78         /**
79          * Password hash being used
80          */
81         @Basic (optional = false)
82         @Column (name = "history_password_hash", nullable = false, updatable = false)
83         private String userPasswordHistoryPasswordHash;
84
85         /**
86          * User instance for this history entry
87          */
88         @JoinColumn (name = "history_user_id", nullable = true, updatable = false)
89         @OneToOne (targetEntity = LoginUser.class, cascade = CascadeType.REFRESH)
90         private User userPasswordHistoryUser;
91
92         /**
93          * Default constructor
94          */
95         public UserPasswordHistory () {
96         }
97
98         /**
99          * Constructor with password hash and user instance
100          * <p>
101          * @param userPasswordHistoryPasswordHash Password hash
102          * @param userPasswordHistoryUser User instance
103          */
104         public UserPasswordHistory (final String userPasswordHistoryPasswordHash, final User userPasswordHistoryUser) {
105                 // Set all
106                 this.userPasswordHistoryPasswordHash = userPasswordHistoryPasswordHash;
107                 this.userPasswordHistoryUser = userPasswordHistoryUser;
108         }
109
110         @Override
111         public boolean equals (final Object object) {
112                 if (this == object) {
113                         return true;
114                 } else if (object == null) {
115                         return false;
116                 } else if (this.getClass() != object.getClass()) {
117                         return false;
118                 }
119
120                 final PasswordHistory other = (PasswordHistory) object;
121
122                 if (!Objects.equals(this.getUserPasswordHistoryPasswordHash(), other.getUserPasswordHistoryPasswordHash())) {
123                         return false;
124                 } else if (!Objects.equals(this.getUserPasswordHistoryId(), other.getUserPasswordHistoryId())) {
125                         return false;
126                 } else if (!Objects.equals(this.getUserPasswordHistoryUser(), other.getUserPasswordHistoryUser())) {
127                         return false;
128                 }
129
130                 return true;
131         }
132
133         @Override
134         @SuppressWarnings ("ReturnOfDateField")
135         public Calendar getUserPasswordHistoryCreated () {
136                 return this.userPasswordHistoryCreated;
137         }
138
139         @Override
140         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
141         public void setUserPasswordHistoryCreated (final Calendar userPasswordHistoryCreated) {
142                 this.userPasswordHistoryCreated = userPasswordHistoryCreated;
143         }
144
145         @Override
146         public Long getUserPasswordHistoryId () {
147                 return this.userPasswordHistoryId;
148         }
149
150         @Override
151         public void setUserPasswordHistoryId (final Long userPasswordHistoryId) {
152                 this.userPasswordHistoryId = userPasswordHistoryId;
153         }
154
155         @Override
156         public String getUserPasswordHistoryPasswordHash () {
157                 return this.userPasswordHistoryPasswordHash;
158         }
159
160         @Override
161         public void setUserPasswordHistoryPasswordHash (final String userPasswordHistoryPasswordHash) {
162                 this.userPasswordHistoryPasswordHash = userPasswordHistoryPasswordHash;
163         }
164
165         @Override
166         public User getUserPasswordHistoryUser () {
167                 return this.userPasswordHistoryUser;
168         }
169
170         @Override
171         public void setUserPasswordHistoryUser (final User userPasswordHistoryUser) {
172                 this.userPasswordHistoryUser = userPasswordHistoryUser;
173         }
174
175         @Override
176         public int hashCode () {
177                 int hash = 7;
178
179                 hash = 79 * hash + Objects.hashCode(this.getUserPasswordHistoryId());
180                 hash = 79 * hash + Objects.hashCode(this.getUserPasswordHistoryPasswordHash());
181                 hash = 79 * hash + Objects.hashCode(this.getUserPasswordHistoryUser());
182
183                 return hash;
184         }
185
186 }