]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/shop/PizzaShopWebApplicationBean.java
also fire an event when a new product has been added, rather than calling addProduct...
[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.events.product.AddedProductEvent;
33 import org.mxchange.jshopcore.model.category.Category;
34 import org.mxchange.jshopcore.model.category.CategorySessionBeanRemote;
35 import org.mxchange.jshopcore.model.product.Product;
36 import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote;
37
38 /**
39  * General shop controller
40  * <p>
41  * @author Roland Haeder<roland@mxchange.org>
42  */
43 @Named ("shopController")
44 @ApplicationScoped
45 public class PizzaShopWebApplicationBean implements PizzaShopWebApplicationController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 58_137_539_530_279L;
51
52         /**
53          * "Cache" for all available products
54          */
55         private List<Product> availableProducts;
56
57         /**
58          * All categories
59          */
60         private List<Category> categories;
61
62         @Override
63         public void addCategory (final Category category) {
64                 // Add the category
65                 this.categories.add(category);
66         }
67
68         @Override
69         public void addProduct (final Product product) {
70                 // Is the product available?
71                 if (product.getProductAvailability()) {
72                         // Add it
73                         this.availableProducts.add(product);
74                 }
75         }
76
77         @Override
78         public void afterShopCategoryAdded (@Observes final AddedCategoryEvent event) {
79                 // Add it here, too.
80                 this.addCategory(event.getAddedCategory());
81         }
82
83         @Override
84         public void afterShopProductAdded (@Observes final AddedProductEvent event) {
85                 // Add it here, too.
86                 this.addProduct(event.getAddedProduct());
87         }
88
89         @Override
90         public List<Category> getAllCategories () throws FacesException {
91                 // Return it
92                 // TODO Find something better here to prevent warning
93                 return Collections.unmodifiableList(this.categories);
94         }
95
96         @Override
97         public List<Category> getAllCategoriesParent () throws FaceletException {
98                 // Get regular list
99                 List<Category> list = new LinkedList<>(this.getAllCategories());
100
101                 // Return it
102                 return list;
103         }
104
105         @Override
106         public List<Product> getAvailableProducts () throws FacesException {
107                 // Return it
108                 return Collections.unmodifiableList(this.availableProducts);
109         }
110
111         /**
112          * Initialization of this bean
113          */
114         @PostConstruct
115         public void init () {
116                 try {
117                         // Get initial context
118                         Context context = new InitialContext();
119
120                         // Try to lookup the bean
121                         CategorySessionBeanRemote categoryBean = (CategorySessionBeanRemote) context.lookup("java:global/jshop-ejb/category!org.mxchange.jshopcore.model.category.CategorySessionBeanRemote"); //NOI18N
122
123                         // Get all categories
124                         this.categories = categoryBean.getAllCategories();
125
126                         // Try to lookup the bean
127                         ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/product!org.mxchange.jshopcore.model.product.ProductSessionBeanRemote"); //NOI18N
128
129                         // Get available products list
130                         this.availableProducts = productBean.getAvailableProducts();
131                 } catch (final NamingException e) {
132                         // Continued to throw
133                         throw new FacesException(e);
134                 }
135         }
136
137 }