]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/shop/ShopWebBean.java
Continued:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / shop / ShopWebBean.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.pizzaapplication.beans.shop;
18
19 import java.util.Collections;
20 import java.util.LinkedList;
21 import java.util.List;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.ApplicationScoped;
24 import javax.faces.FacesException;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jshopcore.model.category.Category;
31 import org.mxchange.jshopcore.model.category.CategorySessionBeanRemote;
32 import org.mxchange.jshopcore.model.product.Product;
33 import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote;
34 import org.mxchange.pizzaapplication.beans.AbstractWebBean;
35
36 /**
37  * General shop controller
38  *
39  * @author Roland Haeder<roland@mxchange.org>
40  */
41 @Named ("controller")
42 @ApplicationScoped
43 public class ShopWebBean extends AbstractWebBean implements ShopWebController {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 58_137_539_530_279L;
49
50         /**
51          * "Cache" for all available products
52          */
53         private List<Product> availableProducts;
54
55         /**
56          * All categories
57          */
58         private List<Category> categories;
59
60         @Override
61         public void addCategory (final Category category) {
62                 // Add the category
63                 this.categories.add(category);
64         }
65
66         @Override
67         public void addProduct (final Product product) {
68                 // Is the product available?
69                 if (product.getProductAvailability()) {
70                         // Add it
71                         this.availableProducts.add(product);
72                 }
73         }
74
75         @Override
76         public List<Category> getAllCategories () throws FacesException {
77                 // Return it
78                 // TODO Find something better here to prevent warning
79                 return Collections.unmodifiableList(this.categories);
80         }
81
82         @Override
83         public List<Category> getAllCategoriesParent () throws FaceletException {
84                 // Get regular list
85                 List<Category> list = new LinkedList<>(this.getAllCategories());
86
87                 // Return it
88                 return list;
89         }
90
91         @Override
92         public List<Product> getAvailableProducts () throws FacesException {
93                 // Return it
94                 // TODO Find something better here to prevent warning
95                 return Collections.unmodifiableList(this.availableProducts);
96         }
97         
98         @PostConstruct
99         public void init () {
100                 try {
101                         // Get initial context
102                         Context context = new InitialContext();
103                         
104                         // Try to lookup the bean
105                         CategorySessionBeanRemote categoryBean = (CategorySessionBeanRemote) context.lookup("ejb/stateless-category"); //NOI18N
106                         
107                         // Get all categories
108                         this.categories = categoryBean.getAllCategories();
109                         
110                         // Try to lookup the bean
111                         ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("ejb/stateless-product"); //NOI18N
112                         
113                         // Get available products list
114                         this.availableProducts = productBean.getAvailableProducts();
115                 } catch (final NamingException e) {
116                         // Continued to throw
117                         throw new FacesException(e);
118                 }
119         }
120 }