]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java
Continued:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / addressbook / AddressbookWebBean.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.addressbook;
18
19 import java.util.ArrayList;
20 import java.util.List;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.SessionScoped;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.addressbook.beans.login.UserLoginWebController;
30 import org.mxchange.addressbook.model.addressbook.Addressbook;
31 import org.mxchange.addressbook.model.addressbook.AddressbookSessionBeanRemote;
32
33 /**
34  * A user bean (controller)
35  * <p>
36  * @author Roland Haeder<roland@mxchange.org>
37  */
38 @Named ("addressbookController")
39 @SessionScoped
40 public class AddressbookWebBean implements AddressbookWebController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 185_781_756_712_969L;
46
47         /////////////////////// Properties /////////////////////
48         /**
49          * A list of all user's addressbooks
50          */
51         private List<Addressbook> addressbookList;
52
53         /**
54          * Remote addressbook bean
55          */
56         private final AddressbookSessionBeanRemote addressbookBean;
57
58         /**
59          * Login controller
60          */
61         @Inject
62         private UserLoginWebController loginController;
63
64         /**
65          * Default constructor
66          */
67         public AddressbookWebBean () {
68                 // Try it
69                 try {
70                         // Get initial context
71                         Context context = new InitialContext();
72
73                         // Try to lookup
74                         this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook"); //NOI18N
75                 } catch (final NamingException e) {
76                         // Throw again
77                         throw new FaceletException(e);
78                 }
79         }
80
81         @PostConstruct
82         public void init () {
83                 // Init list
84                 this.addressbookList = new ArrayList<>(0);
85
86                 // Is the user logged-in?
87                 if (this.loginController.isLoggedIn()) {
88                         // Fill list with entries
89                         this.addressbookList = this.addressbookBean.getUsersList(this.loginController.getLoggedInUser());
90                 }
91         }
92
93         @Override
94         public boolean hasCreatedAddressbooks () {
95                 // Is the user logged in?
96                 if (!this.loginController.isLoggedIn()) {
97                         // Not logged in
98                         throw new FaceletException("This method can only be called as logged-in user.");
99                 }
100
101                 // Check if the list is filled
102                 return (!this.addressbookList.isEmpty());
103         }
104 }