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