]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/password_history/AddressbookUserPasswordHistorySessionBean.java
Please cherry-pick:
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / password_history / AddressbookUserPasswordHistorySessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero 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.text.MessageFormat;
20 import java.util.List;
21 import javax.ejb.Stateless;
22 import javax.persistence.Query;
23 import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
24 import org.mxchange.jusercore.model.user.User;
25
26 /**
27  * A user password history EJB
28  * <p>
29  * @author Roland Häder<roland@mxchange.org>
30  */
31 @Stateless (name = "userPasswordHistory", description = "A stateless EJB for user's password history. This bean does return the full user's password history and not limited. The application then needs to limit it to it's purpose.")
32 public class AddressbookUserPasswordHistorySessionBean extends BaseAddressbookDatabaseBean implements UserPasswordHistorySessionBeanRemote {
33
34         /**
35          * Serial number
36          */
37         private static final long serialVersionUID = 395_767_546_195_014L;
38
39         /**
40          * Default constructor
41          */
42         public AddressbookUserPasswordHistorySessionBean () {
43                 // Call super constructor
44                 super();
45         }
46
47         @Override
48         @SuppressWarnings ("unchecked")
49         public List<PasswordHistory> getUserPasswordHistory (final User user) {
50                 // Trace message
51                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
52
53                 // user should not be null
54                 if (null == user) {
55                         // Abort here
56                         throw new NullPointerException("user is null"); //NOI18N
57                 } else if (user.getUserId() == null) {
58                         // Throw NPE again
59                         throw new NullPointerException("user.userId is null"); //NOI18N
60                 } else if (user.getUserId() < 1) {
61                         // Illegal id number
62                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not allowed.", user.getUserId())); //NOI18N
63                 }
64
65                 // Get named query
66                 Query query = this.getEntityManager().createNamedQuery("AllUsersHistoryEntries", UserPasswordHistory.class); //NOI18N
67
68                 // Set parameter
69                 query.setParameter("user", user); //NOI18N
70
71                 // Get full history
72                 List<PasswordHistory> history = query.getResultList();
73
74                 // Trace message
75                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): history.size()={1} - EXIT !", this.getClass().getSimpleName(), history.size())); //NOI18N
76
77                 // Return it
78                 return history;
79         }
80
81 }