]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/shop/PizzaShopWebApplicationBean.java
Continued a bit:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / shop / PizzaShopWebApplicationBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License as
6  * published by the Free Software Foundation, either version 3 of the
7  * License, or (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 Affero General Public License for more details.
13  *
14  * You should have received a copy of the GNU Affero 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.enterprise.event.Observes;
25 import javax.faces.FacesException;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jshopcore.events.category.AddedCategoryEvent;
32 import org.mxchange.jshopcore.model.category.Category;
33 import org.mxchange.jshopcore.model.category.CategorySessionBeanRemote;
34 import org.mxchange.jshopcore.model.product.Product;
35 import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote;
36
37 /**
38  * General shop controller
39  * <p>
40  * @author Roland Haeder<roland@mxchange.org>
41  */
42 @Named ("shopController")
43 @ApplicationScoped
44 public class PizzaShopWebApplicationBean implements PizzaShopWebApplicationController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 58_137_539_530_279L;
50
51         /**
52          * "Cache" for all available products
53          */
54         private List<Product> availableProducts;
55
56         /**
57          * All categories
58          */
59         private List<Category> categories;
60
61         @Override
62         public void addCategory (final Category category) {
63                 // Add the category
64                 this.categories.add(category);
65         }
66
67         @Override
68         public void addProduct (final Product product) {
69                 // Is the product available?
70                 if (product.getProductAvailability()) {
71                         // Add it
72                         this.availableProducts.add(product);
73                 }
74         }
75
76         @Override
77         public void afterShopCategoryAdded (@Observes final AddedCategoryEvent event) {
78                 // Add it here, too.
79                 this.addCategory(event.getAddedCategory());
80         }
81
82         @Override
83         public List<Category> getAllCategories () throws FacesException {
84                 // Return it
85                 // TODO Find something better here to prevent warning
86                 return Collections.unmodifiableList(this.categories);
87         }
88
89         @Override
90         public List<Category> getAllCategoriesParent () throws FaceletException {
91                 // Get regular list
92                 List<Category> list = new LinkedList<>(this.getAllCategories());
93
94                 // Return it
95                 return list;
96         }
97
98         @Override
99         public List<Product> getAvailableProducts () throws FacesException {
100                 // Return it
101                 return Collections.unmodifiableList(this.availableProducts);
102         }
103
104         /**
105          * Initialization of this bean
106          */
107         @PostConstruct
108         public void init () {
109                 try {
110                         // Get initial context
111                         Context context = new InitialContext();
112
113                         // Try to lookup the bean
114                         CategorySessionBeanRemote categoryBean = (CategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/category!org.mxchange.jshopcore.model.category.CategorySessionBeanRemote"); //NOI18N
115
116                         // Get all categories
117                         this.categories = categoryBean.getAllCategories();
118
119                         // Try to lookup the bean
120                         ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/product!org.mxchange.jshopcore.model.product.ProductSessionBeanRemote"); //NOI18N
121
122                         // Get available products list
123                         this.availableProducts = productBean.getAvailableProducts();
124                 } catch (final NamingException e) {
125                         // Continued to throw
126                         throw new FacesException(e);
127                 }
128         }
129
130 }