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.util.Collections;
20 import java.util.List;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jshopcore.events.product.AddedProductEvent;
32 import org.mxchange.jshopcore.events.product.ShopProductAddedEvent;
33 import org.mxchange.jshopcore.exceptions.CannotAddProductException;
34 import org.mxchange.jshopcore.exceptions.ProductTitleAlreadyUsedException;
35 import org.mxchange.jshopcore.model.category.Category;
36 import org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote;
37 import org.mxchange.jshopcore.model.product.GenericProduct;
38 import org.mxchange.jshopcore.model.product.Product;
39 import org.mxchange.pizzaapplication.beans.BasePizzaController;
42 * Main application class
44 * @author Roland Haeder<roland@mxchange.org>
46 @Named ("adminProductController")
48 public class PizzaAdminProductWebRequestBean extends BasePizzaController implements PizzaAdminProductWebRequestController {
53 private static final long serialVersionUID = 5_819_375_183_472_871L;
56 * Event for added product
60 private Event<AddedProductEvent> addedProductEvent;
65 private Boolean productAvailability;
70 private Category productCategory;
73 * Property productPrice
75 private Float productPrice;
78 * Remote bean for products
80 private AdminProductSessionBeanRemote productRemoteBean;
83 * Property productTitle
85 private String productTitle;
88 * Cache for all products
90 private List<Product> products;
95 public PizzaAdminProductWebRequestBean () {
98 // Get initial context
99 Context context = new InitialContext();
101 // Try to lookup the bean
102 this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/admin_product!org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote"); //NOI18N
103 } catch (final NamingException e) {
105 throw new FaceletException(e);
110 public void addProduct () throws FaceletException {
112 // Create product instance
113 Product product = new GenericProduct();
116 product.setProductAvailability(this.getProductAvailability());
117 product.setProductCategory(this.getProductCategory());
118 product.setProductPrice(this.getProductPrice());
119 product.setProductTitle(this.getProductTitle());
122 Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
124 // Add updated product to local list
125 this.products.add(updatedProduct);
128 this.addedProductEvent.fire(new ShopProductAddedEvent(updatedProduct));
132 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
134 throw new FaceletException(ex);
139 public List<Product> getAllProducts () throws FaceletException {
141 return Collections.unmodifiableList(this.products);
145 public Boolean getProductAvailability () {
146 return this.productAvailability;
150 public void setProductAvailability (final Boolean productAvailability) {
151 this.productAvailability = productAvailability;
155 public Category getProductCategory () {
156 return this.productCategory;
160 public void setProductCategory (final Category productCategory) {
161 this.productCategory = productCategory;
165 public Float getProductPrice () {
166 return this.productPrice;
170 public void setProductPrice (final Float productPrice) {
171 this.productPrice = productPrice;
175 public String getProductTitle () {
176 return this.productTitle;
180 public void setProductTitle (final String productTitle) {
181 this.productTitle = productTitle;
188 public void init () {
190 this.products = this.productRemoteBean.getAllProducts();
194 * Clears this bean (example: product has been added)
196 private void clear () {
197 this.setProductAvailability(Boolean.FALSE);
198 this.setProductCategory(null);
199 this.setProductPrice(null);
200 this.setProductTitle(null);