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 afterShopProductAdded (@Observes final AddedProductEvent event) {
58 throw new NullPointerException("event is null"); //NOI18N
59 } else if (event.getAddedProduct()== null) {
61 throw new NullPointerException("event.addedProduct is null"); //NOI18N
62 } else if (event.getAddedProduct().getProductId() == null) {
64 throw new NullPointerException("event.addedProduct.productId is null"); //NOI18N
65 } else if (event.getAddedProduct().getProductId() < 1) {
67 throw new IllegalArgumentException(MessageFormat.format("event.addedProduct.productId={0} is not valid.", event.getAddedProduct().getProductId())); //NOI18N
70 // Is the product available?
71 if (event.getAddedProduct().getProductAvailability()) {
73 this.availableProducts.add(event.getAddedProduct());
78 public List<Product> getAvailableProducts () throws FacesException {
80 return Collections.unmodifiableList(this.availableProducts);
84 * Initialization of this bean
89 // Get initial context
90 Context context = new InitialContext();
92 // Try to lookup the bean
93 ProductSessionBeanRemote productBean = (ProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/product!org.mxchange.jshopcore.model.product.ProductSessionBeanRemote"); //NOI18N
95 // Get available products list
96 this.availableProducts = productBean.getAvailableProducts();
97 } catch (final NamingException e) {
99 throw new FacesException(e);