2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.product;
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;
36 * General product controller
38 * @author Roland Häder<roland@mxchange.org>
40 @Named ("productController")
42 public class PizzaProductWebRequestBean extends BasePizzaBean implements PizzaProductWebRequestController {
47 private static final long serialVersionUID = 58_137_539_530_279L;
50 * "Cache" for all available products
52 private List<Product> availableProducts;
55 public void afterShopProductAdded (@Observes final AddedProductEvent event) {
59 throw new NullPointerException("event is null"); //NOI18N
60 } else if (event.getAddedProduct()== null) {
62 throw new NullPointerException("event.addedProduct is null"); //NOI18N
63 } else if (event.getAddedProduct().getProductId() == null) {
65 throw new NullPointerException("event.addedProduct.productId is null"); //NOI18N
66 } else if (event.getAddedProduct().getProductId() < 1) {
68 throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N
71 // Is the product available?
72 if (event.getAddedProduct().getProductAvailability()) {
74 this.availableProducts.add(event.getAddedProduct());
79 public List<Product> getAvailableProducts () throws FacesException {
81 return Collections.unmodifiableList(this.availableProducts);
85 * Initialization of this bean
90 // Get initial context
91 Context context = new InitialContext();
93 // Try to lookup the bean
94 ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/product!org.mxchange.jshopcore.model.product.ProductSessionBeanRemote"); //NOI18N
96 // Get available products list
97 this.availableProducts = productBean.getAvailableProducts();
98 } catch (final NamingException e) {
100 throw new FacesException(e);