]> git.mxchange.org Git - pizzaservice-mailer-ejb.git/blob - src/java/org/mxchange/jusercore/model/user/password_history/AddressbookUserPasswordHistorySessionBean.java
updated/fixed copyright as this is Pizza-Service
[pizzaservice-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / password_history / AddressbookUserPasswordHistorySessionBean.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 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 Haeder<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         }
44
45         @Override
46         @SuppressWarnings ("unchecked")
47         public List<PasswordHistory> getUserPasswordHistory (final User user) {
48                 // Trace message
49                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
50
51                 // user should not be null
52                 if (null == user) {
53                         // Abort here
54                         throw new NullPointerException("user is null"); //NOI18N
55                 } else if (user.getUserId() == null) {
56                         // Throw NPE again
57                         throw new NullPointerException("user.userId is null"); //NOI18N
58                 } else if (user.getUserId() < 1) {
59                         // Illegal id number
60                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not allowed.", user.getUserId())); //NOI18N
61                 }
62
63                 // Get named query
64                 Query query = this.getEntityManager().createNamedQuery("AllUsersHistoryEntries", UserPasswordHistory.class); //NOI18N
65
66                 // Set parameter
67                 query.setParameter("user", user); //NOI18N
68
69                 // Get full history
70                 List<PasswordHistory> history = query.getResultList();
71
72                 // Trace message
73                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.getUserPasswordHistory(): history.size()={1} - EXIT !", this.getClass().getSimpleName(), history.size())); //NOI18N
74
75                 // Return it
76                 return history;
77         }
78
79 }