]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/PizzaAdminProductWebRequestBean.java
6b95e73f59fd04f3632465acc5991f77e0dbb3cf
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / product / PizzaAdminProductWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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.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.jproduct.events.product.AddedProductEvent;
32 import org.mxchange.jproduct.events.product.ProductAddedEvent;
33 import org.mxchange.jproduct.exceptions.CannotAddProductException;
34 import org.mxchange.jproduct.exceptions.ProductTitleAlreadyUsedException;
35 import org.mxchange.jproduct.model.category.Category;
36 import org.mxchange.jproduct.model.product.AdminProductSessionBeanRemote;
37 import org.mxchange.jproduct.model.product.GenericProduct;
38 import org.mxchange.jproduct.model.product.Product;
39 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
40
41 /**
42  * Main application class
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 @Named ("adminProductController")
47 @RequestScoped
48 public class PizzaAdminProductWebRequestBean extends BasePizzaBean implements PizzaAdminProductWebRequestController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 5_819_375_183_472_871L;
54
55         /**
56          * Event for added product
57          */
58         @Inject
59         @Any
60         private Event<AddedProductEvent> addedProductEvent;
61
62         /**
63          * Available
64          */
65         private Boolean productAvailability;
66
67         /**
68          * Category instance
69          */
70         private Category productCategory;
71
72         /**
73          * Property productPrice
74          */
75         private Float productPrice;
76
77         /**
78          * Remote bean for products
79          */
80         private AdminProductSessionBeanRemote productRemoteBean;
81
82         /**
83          * Property productTitle
84          */
85         private String productTitle;
86
87         /**
88          * Cache for all products
89          */
90         private List<Product> products;
91
92         /**
93          * Default constructor
94          */
95         public PizzaAdminProductWebRequestBean () {
96                 // Try it
97                 try {
98                         // Get initial context
99                         Context context = new InitialContext();
100
101                         // Try to lookup the bean
102                         this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminProduct!org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote"); //NOI18N
103                 } catch (final NamingException e) {
104                         // Throw it again
105                         throw new FaceletException(e);
106                 }
107         }
108
109         @Override
110         public void addProduct () throws FaceletException {
111                 try {
112                         // Create product instance
113                         Product product = new GenericProduct();
114
115                         // Add all
116                         product.setProductAvailability(this.getProductAvailability());
117                         product.setProductCategory(this.getProductCategory());
118                         product.setProductPrice(this.getProductPrice());
119                         product.setProductTitle(this.getProductTitle());
120
121                         // Call bean
122                         Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
123
124                         // Add updated product to local list
125                         this.products.add(updatedProduct);
126
127                         // Fire event
128                         this.addedProductEvent.fire(new ProductAddedEvent(updatedProduct));
129
130                         // Set all to null
131                         this.clear();
132                 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
133                         // Continue to throw
134                         throw new FaceletException(ex);
135                 }
136         }
137
138         @Override
139         public List<Product> getAllProducts () throws FaceletException {
140                 // Call bean
141                 return Collections.unmodifiableList(this.products);
142         }
143
144         @Override
145         public Boolean getProductAvailability () {
146                 return this.productAvailability;
147         }
148
149         @Override
150         public void setProductAvailability (final Boolean productAvailability) {
151                 this.productAvailability = productAvailability;
152         }
153
154         @Override
155         public Category getProductCategory () {
156                 return this.productCategory;
157         }
158
159         @Override
160         public void setProductCategory (final Category productCategory) {
161                 this.productCategory = productCategory;
162         }
163
164         @Override
165         public Float getProductPrice () {
166                 return this.productPrice;
167         }
168
169         @Override
170         public void setProductPrice (final Float productPrice) {
171                 this.productPrice = productPrice;
172         }
173
174         @Override
175         public String getProductTitle () {
176                 return this.productTitle;
177         }
178
179         @Override
180         public void setProductTitle (final String productTitle) {
181                 this.productTitle = productTitle;
182         }
183
184         /**
185          * Initializer method
186          */
187         @PostConstruct
188         public void init () {
189                 // Initialize list
190                 this.products = this.productRemoteBean.getAllProducts();
191         }
192
193         /**
194          * Clears this bean (example: product has been added)
195          */
196         private void clear () {
197                 this.setProductAvailability(Boolean.FALSE);
198                 this.setProductCategory(null);
199                 this.setProductPrice(null);
200                 this.setProductTitle(null);
201         }
202
203 }