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