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.List;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jshopcore.events.product.AddedProductEvent;
30 import org.mxchange.jshopcore.events.product.ShopProductAddedEvent;
31 import org.mxchange.jshopcore.exceptions.CannotAddProductException;
32 import org.mxchange.jshopcore.exceptions.ProductTitleAlreadyUsedException;
33 import org.mxchange.jshopcore.model.category.Category;
34 import org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote;
35 import org.mxchange.jshopcore.model.product.GenericProduct;
36 import org.mxchange.jshopcore.model.product.Product;
39 * Main application class
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("adminProductController")
45 public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequestController {
50 private static final long serialVersionUID = 5_819_375_183_472_871L;
53 * Event for added product
57 private Event<AddedProductEvent> addedProductEvent;
62 private Boolean productAvailability;
67 private Category productCategory;
70 * Property productPrice
72 private Float productPrice;
75 * Remote bean for products
77 private AdminProductSessionBeanRemote productRemoteBean;
80 * Property productTitle
82 private String productTitle;
87 public PizzaAdminProductWebRequestBean () {
90 // Get initial context
91 Context context = new InitialContext();
93 // Try to lookup the bean
94 this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("java:global/jshop-ejb/admin_product!org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote"); //NOI18N
95 } catch (final NamingException e) {
97 throw new FaceletException(e);
102 public void addProduct () throws FaceletException {
104 // Create product instance
105 Product product = new GenericProduct();
108 product.setProductAvailability(this.getProductAvailability());
109 product.setProductCategory(this.getProductCategory());
110 product.setProductPrice(this.getProductPrice());
111 product.setProductTitle(this.getProductTitle());
114 Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
117 this.addedProductEvent.fire(new ShopProductAddedEvent(updatedProduct));
120 this.setProductAvailability(Boolean.FALSE);
121 this.setProductCategory(null);
122 this.setProductPrice(null);
123 this.setProductTitle(null);
124 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
126 throw new FaceletException(ex);
131 public List<Product> getAllProducts () throws FaceletException {
133 return this.productRemoteBean.getAllProducts();
137 public Boolean getProductAvailability () {
138 return this.productAvailability;
142 public void setProductAvailability (final Boolean productAvailability) {
143 this.productAvailability = productAvailability;
147 public Category getProductCategory () {
148 return this.productCategory;
152 public void setProductCategory (final Category productCategory) {
153 this.productCategory = productCategory;
157 public Float getProductPrice () {
158 return this.productPrice;
162 public void setProductPrice (final Float productPrice) {
163 this.productPrice = productPrice;
167 public String getProductTitle () {
168 return this.productTitle;
172 public void setProductTitle (final String productTitle) {
173 this.productTitle = productTitle;