2 * Copyright (C) 2016 Roland Haeder
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.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;
35 * General product controller
37 * @author Roland Haeder<roland@mxchange.org>
39 @Named ("productController")
41 public class PizzaProductWebApplicationBean implements PizzaProductWebApplicationController {
46 private static final long serialVersionUID = 58_137_539_530_279L;
49 * "Cache" for all available products
51 private List<Product> availableProducts;
54 public void addProduct (final Product product) {
55 // Is the product available?
56 if (product.getProductAvailability()) {
58 this.availableProducts.add(product);
63 public void afterShopProductAdded (@Observes final AddedProductEvent event) {
67 throw new NullPointerException("event is null"); //NOI18N
68 } else if (event.getAddedProduct()== null) {
70 throw new NullPointerException("event.addedProduct is null"); //NOI18N
71 } else if (event.getAddedProduct().getProductId() == null) {
73 throw new NullPointerException("event.addedProduct.productId is null"); //NOI18N
74 } else if (event.getAddedProduct().getProductId() < 1) {
76 throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N
80 this.addProduct(event.getAddedProduct());
84 public List<Product> getAvailableProducts () throws FacesException {
86 return Collections.unmodifiableList(this.availableProducts);
90 * Initialization of this bean
95 // Get initial context
96 Context context = new InitialContext();
98 // Try to lookup the bean
99 ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/product!org.mxchange.jshopcore.model.product.ProductSessionBeanRemote"); //NOI18N
101 // Get available products list
102 this.availableProducts = productBean.getAvailableProducts();
103 } catch (final NamingException e) {
104 // Continued to throw
105 throw new FacesException(e);