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;
41 * Main application class
43 * @author Roland Haeder<roland@mxchange.org>
45 @Named ("adminProductController")
47 public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequestController {
52 private static final long serialVersionUID = 5_819_375_183_472_871L;
55 * Event for added product
59 private Event<AddedProductEvent> addedProductEvent;
64 private Boolean productAvailability;
69 private Category productCategory;
72 * Property productPrice
74 private Float productPrice;
77 * Remote bean for products
79 private AdminProductSessionBeanRemote productRemoteBean;
82 * Property productTitle
84 private String productTitle;
87 * Cache for all products
89 private List<Product> products;
94 public PizzaAdminProductWebRequestBean () {
97 // Get initial context
98 Context context = new InitialContext();
100 // Try to lookup the bean
101 this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/admin_product!org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote"); //NOI18N
102 } catch (final NamingException e) {
104 throw new FaceletException(e);
109 public void addProduct () throws FaceletException {
111 // Create product instance
112 Product product = new GenericProduct();
115 product.setProductAvailability(this.getProductAvailability());
116 product.setProductCategory(this.getProductCategory());
117 product.setProductPrice(this.getProductPrice());
118 product.setProductTitle(this.getProductTitle());
121 Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
123 // Add updated product to local list
124 this.products.add(updatedProduct);
127 this.addedProductEvent.fire(new ShopProductAddedEvent(updatedProduct));
131 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
133 throw new FaceletException(ex);
138 public List<Product> getAllProducts () throws FaceletException {
140 return Collections.unmodifiableList(this.products);
144 public Boolean getProductAvailability () {
145 return this.productAvailability;
149 public void setProductAvailability (final Boolean productAvailability) {
150 this.productAvailability = productAvailability;
154 public Category getProductCategory () {
155 return this.productCategory;
159 public void setProductCategory (final Category productCategory) {
160 this.productCategory = productCategory;
164 public Float getProductPrice () {
165 return this.productPrice;
169 public void setProductPrice (final Float productPrice) {
170 this.productPrice = productPrice;
174 public String getProductTitle () {
175 return this.productTitle;
179 public void setProductTitle (final String productTitle) {
180 this.productTitle = productTitle;
187 public void init () {
189 this.products = this.productRemoteBean.getAllProducts();
193 * Clears this bean (example: product has been added)
195 private void clear () {
196 this.setProductAvailability(Boolean.FALSE);
197 this.setProductCategory(null);
198 this.setProductPrice(null);
199 this.setProductTitle(null);