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