]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/AdminProductWebBean.java
Continued:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / product / AdminProductWebBean.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU 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.ShopWebController;
34
35 /**
36  * Main application class
37  *
38  * @author Roland Haeder<roland@mxchange.org>
39  */
40 @Named ("admin_product")
41 @RequestScoped
42 public class AdminProductWebBean implements AdminProductWebController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 5_819_375_183_472_871L;
48
49         /**
50          * Remote bean for products
51          */
52         private final AdminProductSessionBeanRemote productRemoteBean;
53
54         ////////////////////// Bean injections ///////////////////////
55         /**
56          * Shop bean
57          */
58         @Inject
59         private ShopWebController shopController;
60
61         /////////////////////// Properties /////////////////////
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          * Property productTitle
79          */
80         private String productTitle;
81
82         /**
83          * Default constructor
84          */
85         public AdminProductWebBean () {
86                 // Try it
87                 try {
88                         // Get initial context
89                         Context context = new InitialContext();
90
91                         // Try to lookup the bean
92                         this.productRemoteBean = (AdminProductSessionBeanRemote) context.lookup("ejb/stateless-admin-product"); //NOI18N
93                 } catch (final NamingException e) {
94                         // Throw it again
95                         throw new FaceletException(e);
96                 }
97         }
98
99         @Override
100         public void addProduct () throws FaceletException {
101                 try {
102                         // Create product instance
103                         Product product = new GenericProduct();
104
105                         // Add all
106                         product.setProductAvailability(this.getProductAvailability());
107                         product.setProductCategory(this.getProductCategory());
108                         product.setProductPrice(this.getProductPrice());
109                         product.setProductTitle(this.getProductTitle());
110
111                         // Call bean
112                         Product updatedProduct = this.productRemoteBean.doAdminAddProduct(product);
113
114                         // Add to shop controller
115                         this.shopController.addProduct(updatedProduct);
116
117                         // Set all to null
118                         this.setProductAvailability(Boolean.FALSE);
119                         this.setProductCategory(null);
120                         this.setProductPrice(null);
121                         this.setProductTitle(null);
122                 } catch (final ProductTitleAlreadyUsedException | CannotAddProductException ex) {
123                         // Continue to throw
124                         throw new FaceletException(ex);
125                 }
126         }
127
128         @Override
129         public List<Product> getAllProducts () throws FaceletException {
130                 // Call bean
131                 return this.productRemoteBean.getAllProducts();
132         }
133
134         @Override
135         public void setProductAvailability (final Boolean productAvailability) {
136                 this.productAvailability = productAvailability;
137         }
138
139         @Override
140         public Category getProductCategory () {
141                 return productCategory;
142         }
143
144         @Override
145         public void setProductCategory (final Category productCategory) {
146                 this.productCategory = productCategory;
147         }
148
149         @Override
150         public Float getProductPrice () {
151                 return this.productPrice;
152         }
153
154         @Override
155         public void setProductPrice (final Float productPrice) {
156                 this.productPrice = productPrice;
157         }
158
159         @Override
160         public String getProductTitle () {
161                 return this.productTitle;
162         }
163
164         @Override
165         public void setProductTitle (final String productTitle) {
166                 this.productTitle = productTitle;
167         }
168
169         @Override
170         public Boolean getProductAvailability () {
171                 return this.productAvailability;
172         }
173 }