]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/PizzaProductWebApplicationBean.java
Rewrites:
[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.text.MessageFormat;
20 import java.util.Collections;
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.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jshopcore.events.product.AddedProductEvent;
31 import org.mxchange.jshopcore.model.product.Product;
32 import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote;
33
34 /**
35  * General product controller
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @Named ("productController")
40 @ApplicationScoped
41 public class PizzaProductWebApplicationBean implements PizzaProductWebApplicationController {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 58_137_539_530_279L;
47
48         /**
49          * "Cache" for all available products
50          */
51         private List<Product> availableProducts;
52
53         @Override
54         public void addProduct (final Product product) {
55                 // Is the product available?
56                 if (product.getProductAvailability()) {
57                         // Add it
58                         this.availableProducts.add(product);
59                 }
60         }
61
62         @Override
63         public void afterShopProductAdded (@Observes final AddedProductEvent event) {
64                 // Is all valid?
65                 if (null == event) {
66                         // Throw NPE
67                         throw new NullPointerException("event is null"); //NOI18N
68                 } else if (event.getAddedProduct()== null) {
69                         // Throw again ...
70                         throw new NullPointerException("event.addedProduct is null"); //NOI18N
71                 } else if (event.getAddedProduct().getProductId() == null) {
72                         // And again ...
73                         throw new NullPointerException("event.addedProduct.productId is null"); //NOI18N
74                 } else if (event.getAddedProduct().getProductId() < 1) {
75                         // Id is invalid
76                         throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N
77                 }
78
79                 // Add it here, too.
80                 this.addProduct(event.getAddedProduct());
81         }
82
83         @Override
84         public List<Product> getAvailableProducts () throws FacesException {
85                 // Return it
86                 return Collections.unmodifiableList(this.availableProducts);
87         }
88
89         /**
90          * Initialization of this bean
91          */
92         @PostConstruct
93         public void init () {
94                 try {
95                         // Get initial context
96                         Context context = new InitialContext();
97
98                         // Try to lookup the bean
99                         ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/product!org.mxchange.jshopcore.model.product.ProductSessionBeanRemote"); //NOI18N
100
101                         // Get available products list
102                         this.availableProducts = productBean.getAvailableProducts();
103                 } catch (final NamingException e) {
104                         // Continued to throw
105                         throw new FacesException(e);
106                 }
107         }
108
109 }