]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/PizzaAdminProductWebRequestBean.java
introduced new lib jphone-lib.jar which holds remote interfaces for jphone-core
[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.faces.view.facelets.FaceletException;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
26 import javax.naming.NamingException;
27 import org.mxchange.jshopcore.exceptions.CannotAddProductException;
28 import org.mxchange.jshopcore.exceptions.ProductTitleAlreadyUsedException;
29 import org.mxchange.jshopcore.model.category.Category;
30 import org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote;
31 import org.mxchange.jshopcore.model.product.GenericProduct;
32 import org.mxchange.jshopcore.model.product.Product;
33 import org.mxchange.pizzaapplication.beans.shop.PizzaShopWebApplicationController;
34
35 /**
36  * Main application class
37  * <p>
38  * @author Roland Haeder<roland@mxchange.org>
39  */
40 @Named ("adminProductController")
41 @RequestScoped
42 public class PizzaAdminProductWebRequestBean implements PizzaAdminProductWebRequestController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 5_819_375_183_472_871L;
48
49         /////////////////////// Properties /////////////////////
50         /**
51          * Available
52          */
53         private Boolean productAvailability;
54
55         /**
56          * Category instance
57          */
58         private Category productCategory;
59
60         /**
61          * Property productPrice
62          */
63         private Float productPrice;
64
65         /**
66          * Remote bean for products
67          */
68         private AdminProductSessionBeanRemote productRemoteBean;
69
70         /**
71          * Property productTitle
72          */
73         private String productTitle;
74
75         ////////////////////// Bean injections ///////////////////////
76
77         /**
78          * Shop bean
79          */
80         @Inject
81         private PizzaShopWebApplicationController shopController;
82
83         /**
84          * Default constructor
85          */
86         public PizzaAdminProductWebRequestBean () {
87                 // Try it
88                 try {
89                         // Get initial context
90                         Context context = new InitialContext();
91
92                         // Try to lookup the bean
93                         this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("ejb/stateless-admin-product"); //NOI18N
94                 } catch (final NamingException e) {
95                         // Throw it again
96                         throw new FaceletException(e);
97                 }
98         }
99
100         @Override
101         public void addProduct () throws FaceletException {
102                 try {
103                         // Create product instance
104                         Product product = new GenericProduct();
105
106                         // Add all
107                         product.setProductAvailability(this.getProductAvailability());
108                         product.setProductCategory(this.getProductCategory());
109                         product.setProductPrice(this.getProductPrice());
110                         product.setProductTitle(this.getProductTitle());
111
112                         // Call bean
113                         Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
114
115                         // Add to shop controller
116                         this.shopController.addProduct(updatedProduct);
117
118                         // Set all to null
119                         this.setProductAvailability(Boolean.FALSE);
120                         this.setProductCategory(null);
121                         this.setProductPrice(null);
122                         this.setProductTitle(null);
123                 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
124                         // Continue to throw
125                         throw new FaceletException(ex);
126                 }
127         }
128
129         @Override
130         public List<Product> getAllProducts () throws FaceletException {
131                 // Call bean
132                 return this.productRemoteBean.getAllProducts();
133         }
134
135         @Override
136         public Boolean getProductAvailability () {
137                 return this.productAvailability;
138         }
139
140         @Override
141         public void setProductAvailability (final Boolean productAvailability) {
142                 this.productAvailability = productAvailability;
143         }
144
145         @Override
146         public Category getProductCategory () {
147                 return this.productCategory;
148         }
149
150         @Override
151         public void setProductCategory (final Category productCategory) {
152                 this.productCategory = productCategory;
153         }
154
155         @Override
156         public Float getProductPrice () {
157                 return this.productPrice;
158         }
159
160         @Override
161         public void setProductPrice (final Float productPrice) {
162                 this.productPrice = productPrice;
163         }
164
165         @Override
166         public String getProductTitle () {
167                 return this.productTitle;
168         }
169
170         @Override
171         public void setProductTitle (final String productTitle) {
172                 this.productTitle = productTitle;
173         }
174
175 }