]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/login/UserLoginWebBean.java
9496db3d558bb965bb84c0ff51950cd39e2cab7b
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / login / UserLoginWebBean.java
1 /*
2  * Copyright (C) 2015 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.beans.login;
18
19 import java.util.Objects;
20 import javax.enterprise.context.SessionScoped;
21 import javax.faces.view.facelets.FaceletException;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import org.mxchange.addressbook.beans.user.UserWebController;
28 import org.mxchange.jusercore.exceptions.UserNotFoundException;
29 import org.mxchange.jusercore.exceptions.UserStatusLockedException;
30 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
31 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
32 import org.mxchange.jusercore.model.user.User;
33 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
34
35 /**
36  * A web bean for user registration
37  * <p>
38  * @author Roland Haeder
39  */
40 @Named ("loginController")
41 @SessionScoped
42 public class UserLoginWebBean implements UserLoginWebController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 47_828_986_719_691_592L;
48
49         /**
50          * Logged-in user instance
51          */
52         private User loggedInUser;
53
54         /**
55          * Reemote register session bean
56          */
57         private UserLoginSessionBeanRemote loginBean;
58
59         /**
60          * User controller
61          */
62         @Inject
63         private UserWebController userController;
64
65         /**
66          * Default constructor
67          */
68         public UserLoginWebBean () {
69                 try {
70                         // Get initial context
71                         Context context = new InitialContext();
72
73                         // Try to lookup
74                         this.loginBean = (UserLoginSessionBeanRemote) context.lookup("ejb/stateless-login"); //NOI18N
75                 } catch (final NamingException ex) {
76                         // Continue to throw
77                         throw new FaceletException(ex);
78                 }
79         }
80
81         @Override
82         public String doLogin () {
83                 // Get user instance
84                 User user = this.userController.createUserInstance();
85
86                 try {
87                         // Call bean
88                         User confirmedUser = this.loginBean.validateUserAccountStatus(user);
89
90                         // All fine here so set it here
91                         this.setLoggedInUser(confirmedUser);
92                 } catch (final UserNotFoundException | UserStatusLockedException | UserStatusUnconfirmedException ex) {
93                         // Throw again
94                         throw new FaceletException(ex);
95                 }
96
97                 // All fine
98                 return "login"; //NOI18N
99         }
100
101         @Override
102         public User getLoggedInUser () {
103                 return this.loggedInUser;
104         }
105
106         @Override
107         public void setLoggedInUser (final User loggedInUser) {
108                 this.loggedInUser = loggedInUser;
109         }
110
111         @Override
112         public boolean isUserLoggedIn () {
113                 // Compare instance
114                 return ((this.getLoggedInUser() instanceof User) && (Objects.equals(this.getLoggedInUser().getUserAccountStatus(), UserAccountStatus.CONFIRMED)));
115         }
116 }