]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/product/AdminProductWebBean.java
updated jars + switched from Deque to List
[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.jcoreee.beans.BaseFrameworkBean;
28 import org.mxchange.jshopcore.exceptions.CannotAddProductException;
29 import org.mxchange.jshopcore.exceptions.ProductTitleAlreadyUsedException;
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.controller.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 extends BaseFrameworkBean 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 productBean;
53
54         ////////////////////// Bean injections ///////////////////////
55         /**
56          * Shop bean
57          */
58         @Inject
59         private ShopWebController controller;
60
61         /////////////////////// Properties /////////////////////
62         /**
63          * Available
64          */
65         private Boolean available;
66
67         /**
68          * Category id
69          */
70         private Long id;
71
72         /**
73          * Property price
74          */
75         private Float price;
76
77         /**
78          * Property title
79          */
80         private String title;
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.productBean = (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.setAvailable(this.getAvailable());
107                         product.setId(this.getId());
108                         product.setPrice(this.getPrice());
109                         product.setTitle(this.getTitle());
110
111                         // Call bean
112                         this.productBean.doAdminAddProduct(product);
113
114                         // Add to shop controller
115                         this.controller.addProduct(product);
116
117                         // Set all to null
118                         this.setAvailable(Boolean.FALSE);
119                         this.setId(null);
120                         this.setPrice(null);
121                         this.setTitle(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.productBean.getAllProducts();
132         }
133
134         @Override
135         public void setAvailable (final Boolean available) {
136                 this.available = available;
137         }
138
139         @Override
140         public Long getId () {
141                 return id;
142         }
143
144         @Override
145         public void setId (final Long id) {
146                 this.id = id;
147         }
148
149         @Override
150         public Float getPrice () {
151                 return this.price;
152         }
153
154         @Override
155         public void setPrice (final Float price) {
156                 this.price = price;
157         }
158
159         @Override
160         public String getTitle () {
161                 return this.title;
162         }
163
164         @Override
165         public void setTitle (final String title) {
166                 this.title = title;
167         }
168
169         @Override
170         public Boolean getAvailable () {
171                 return this.available;
172         }
173 }