]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/PizzaProductWebApplicationBean.java
Split of shop controller into category and product
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / product / PizzaProductWebApplicationBean.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.product;
18
19 import java.util.Collections;
20 import java.util.List;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.ApplicationScoped;
23 import javax.enterprise.event.Observes;
24 import javax.faces.FacesException;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jshopcore.events.product.AddedProductEvent;
30 import org.mxchange.jshopcore.model.product.Product;
31 import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote;
32
33 /**
34  * General product controller
35  * <p>
36  * @author Roland Haeder<roland@mxchange.org>
37  */
38 @Named ("productController")
39 @ApplicationScoped
40 public class PizzaProductWebApplicationBean implements PizzaProductWebApplicationController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 58_137_539_530_279L;
46
47         /**
48          * "Cache" for all available products
49          */
50         private List<Product> availableProducts;
51
52         @Override
53         public void addProduct (final Product product) {
54                 // Is the product available?
55                 if (product.getProductAvailability()) {
56                         // Add it
57                         this.availableProducts.add(product);
58                 }
59         }
60
61         @Override
62         public void afterShopProductAdded (@Observes final AddedProductEvent event) {
63                 // Add it here, too.
64                 this.addProduct(event.getAddedProduct());
65         }
66
67         @Override
68         public List<Product> getAvailableProducts () throws FacesException {
69                 // Return it
70                 return Collections.unmodifiableList(this.availableProducts);
71         }
72
73         /**
74          * Initialization of this bean
75          */
76         @PostConstruct
77         public void init () {
78                 try {
79                         // Get initial context
80                         Context context = new InitialContext();
81
82                         // Try to lookup the bean
83                         ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/product!org.mxchange.jshopcore.model.product.ProductSessionBeanRemote"); //NOI18N
84
85                         // Get available products list
86                         this.availableProducts = productBean.getAvailableProducts();
87                 } catch (final NamingException e) {
88                         // Continued to throw
89                         throw new FacesException(e);
90                 }
91         }
92
93 }