]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/PizzaAdminProductWebRequestBean.java
3d115e0945612a0d918a0ee6732a5879c7b48954
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / product / PizzaAdminProductWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.beans.product;
18
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;
37
38 /**
39  * Main application class
40  * <p>
41  * @author Roland Haeder<roland@mxchange.org>
42  */
43 @Named ("adminProductController")
44 @RequestScoped
45 public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequestController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 5_819_375_183_472_871L;
51
52         /**
53          * Event for added product
54          */
55         @Inject
56         @Any
57         private Event<AddedProductEvent> addedProductEvent;
58
59         /**
60          * Available
61          */
62         private Boolean productAvailability;
63
64         /**
65          * Category instance
66          */
67         private Category productCategory;
68
69         /**
70          * Property productPrice
71          */
72         private Float productPrice;
73
74         /**
75          * Remote bean for products
76          */
77         private AdminProductSessionBeanRemote productRemoteBean;
78
79         /**
80          * Property productTitle
81          */
82         private String productTitle;
83
84         /**
85          * Default constructor
86          */
87         public PizzaAdminProductWebRequestBean () {
88                 // Try it
89                 try {
90                         // Get initial context
91                         Context context = new InitialContext();
92
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) {
96                         // Throw it again
97                         throw new FaceletException(e);
98                 }
99         }
100
101         @Override
102         public void addProduct () throws FaceletException {
103                 try {
104                         // Create product instance
105                         Product product = new GenericProduct();
106
107                         // Add all
108                         product.setProductAvailability(this.getProductAvailability());
109                         product.setProductCategory(this.getProductCategory());
110                         product.setProductPrice(this.getProductPrice());
111                         product.setProductTitle(this.getProductTitle());
112
113                         // Call bean
114                         Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
115
116                         // Fire event
117                         this.addedProductEvent.fire(new ShopProductAddedEvent(updatedProduct));
118
119                         // Set all to null
120                         this.setProductAvailability(Boolean.FALSE);
121                         this.setProductCategory(null);
122                         this.setProductPrice(null);
123                         this.setProductTitle(null);
124                 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
125                         // Continue to throw
126                         throw new FaceletException(ex);
127                 }
128         }
129
130         @Override
131         public List<Product> getAllProducts () throws FaceletException {
132                 // Call bean
133                 return this.productRemoteBean.getAllProducts();
134         }
135
136         @Override
137         public Boolean getProductAvailability () {
138                 return this.productAvailability;
139         }
140
141         @Override
142         public void setProductAvailability (final Boolean productAvailability) {
143                 this.productAvailability = productAvailability;
144         }
145
146         @Override
147         public Category getProductCategory () {
148                 return this.productCategory;
149         }
150
151         @Override
152         public void setProductCategory (final Category productCategory) {
153                 this.productCategory = productCategory;
154         }
155
156         @Override
157         public Float getProductPrice () {
158                 return this.productPrice;
159         }
160
161         @Override
162         public void setProductPrice (final Float productPrice) {
163                 this.productPrice = productPrice;
164         }
165
166         @Override
167         public String getProductTitle () {
168                 return this.productTitle;
169         }
170
171         @Override
172         public void setProductTitle (final String productTitle) {
173                 this.productTitle = productTitle;
174         }
175
176 }