]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/addressbook/AddressbookWebBean.java
5661a9259d95c2e03a838780c963b38deaa83c6d
[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          * Name of the address book
55          */
56         private String addressbookName;
57
58         /**
59          * Remote addressbook bean
60          */
61         private final AddressbookSessionBeanRemote addressbookBean;
62
63         /**
64          * Login controller
65          */
66         @Inject
67         private UserLoginWebController loginController;
68
69         /**
70          * Default constructor
71          */
72         public AddressbookWebBean () {
73                 // Try it
74                 try {
75                         // Get initial context
76                         Context context = new InitialContext();
77
78                         // Try to lookup
79                         this.addressbookBean = (AddressbookSessionBeanRemote) context.lookup("ejb/stateless-addressbook"); //NOI18N
80                 } catch (final NamingException e) {
81                         // Throw again
82                         throw new FaceletException(e);
83                 }
84         }
85
86         @PostConstruct
87         public void init () {
88                 // Init list
89                 this.addressbookList = new ArrayList<>(0);
90
91                 // Is the user logged-in?
92                 if (this.loginController.isLoggedIn()) {
93                         // Fill list with entries
94                         this.addressbookList = this.addressbookBean.getUsersList(this.loginController.getLoggedInUser());
95                 }
96         }
97
98         @Override
99         public boolean hasCreatedAddressbooks () {
100                 // Is the user logged in?
101                 if (!this.loginController.isLoggedIn()) {
102                         // Not logged in
103                         throw new FaceletException("This method can only be called as logged-in user.");
104                 }
105
106                 // Check if the list is filled
107                 return (!this.addressbookList.isEmpty());
108         }
109
110         @Override
111         public void addAddressbook() {
112                 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
113         }
114
115         @Override
116         public String getAddressbookName() {
117                 return this.addressbookName;
118         }
119
120         @Override
121         public void setAddressbookName(final String addressbookName) {
122                 this.addressbookName = addressbookName;
123         }
124 }