]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/PizzaAdminProductWebRequestBean.java
fixed JNDI names
[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.clear();
121                 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
122                         // Continue to throw
123                         throw new FaceletException(ex);
124                 }
125         }
126
127         @Override
128         public List<Product> getAllProducts () throws FaceletException {
129                 // Call bean
130                 return this.productRemoteBean.getAllProducts();
131         }
132
133         @Override
134         public Boolean getProductAvailability () {
135                 return this.productAvailability;
136         }
137
138         @Override
139         public void setProductAvailability (final Boolean productAvailability) {
140                 this.productAvailability = productAvailability;
141         }
142
143         @Override
144         public Category getProductCategory () {
145                 return this.productCategory;
146         }
147
148         @Override
149         public void setProductCategory (final Category productCategory) {
150                 this.productCategory = productCategory;
151         }
152
153         @Override
154         public Float getProductPrice () {
155                 return this.productPrice;
156         }
157
158         @Override
159         public void setProductPrice (final Float productPrice) {
160                 this.productPrice = productPrice;
161         }
162
163         @Override
164         public String getProductTitle () {
165                 return this.productTitle;
166         }
167
168         @Override
169         public void setProductTitle (final String productTitle) {
170                 this.productTitle = productTitle;
171         }
172
173         /**
174          * Clears this bean (example: product has been added)
175          */
176         private void clear () {
177                 this.setProductAvailability(Boolean.FALSE);
178                 this.setProductCategory(null);
179                 this.setProductPrice(null);
180                 this.setProductTitle(null);
181         }
182
183 }