]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/controller/PizzaServiceBean.java
Refacturing:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / controller / PizzaServiceBean.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.controller;
18
19 import java.io.IOException;
20 import java.sql.SQLException;
21 import java.text.MessageFormat;
22 import java.util.Iterator;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.SessionScoped;
25 import javax.faces.FacesException;
26 import javax.inject.Named;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpServletResponse;
30 import javax.servlet.http.HttpSession;
31 import org.mxchange.jcore.exceptions.BadTokenException;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
33 import org.mxchange.jshop.beans.BaseFrameworkBean;
34 import org.mxchange.jshop.category.Category;
35 import org.mxchange.jshop.exceptions.CategoryTitleAlreadyUsedException;
36 import org.mxchange.jshop.exceptions.ProductTitleAlreadyUsedException;
37 import org.mxchange.jshop.item.AddableBasketItem;
38 import org.mxchange.jshop.product.Product;
39 import org.mxchange.pizzaapplication.application.PizzaApplication;
40 import org.mxchange.pizzaapplication.application.PizzaServiceApplication;
41
42 /**
43  * Main application class
44  *
45  * @author Roland Haeder
46  */
47 @Named("controller")
48 @SessionScoped
49 public class PizzaServiceBean extends BaseFrameworkBean implements PizzaBean {
50         /**
51          * Serial id
52          */
53         private static final long serialVersionUID = 58137539530279L;
54
55         /**
56          * Pizza application
57          */
58         private final PizzaApplication app;
59
60         /**
61          * Initializer block
62          */
63         {
64                 // Get new application instance
65                 this.getLogger().debug("INITIALIZER!"); //NOI18N
66                 this.app = new PizzaServiceApplication();
67         }
68
69         /**
70          * Default constructor
71          */
72         public PizzaServiceBean () {
73                 this.getLogger().trace("CALLED!"); //NOI18N
74         }
75
76         @Override
77         @PostConstruct
78         public void init () throws FacesException {
79                 // Trace message
80                 this.getLogger().trace(MessageFormat.format("application={0},basket={1} - CALLED!", this.getApplication(), this.getBasket())); //NOI18N
81
82                 // Must not be null
83                 if (this.getApplication() == null) {
84                         // Abort here
85                         throw new NullPointerException("application is null"); //NOI18N
86                 }
87
88                 try {
89                         // Call init method
90                         this.app.init(this.getApplication());
91                 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
92                         // Continue to throw
93                         throw new FacesException(ex);
94                 }
95         }
96
97         /**
98          * Calculates total amount of all choosen products
99          *
100          * @param request Request instance
101          * @param session Session instance
102          * @return Total amount of all choosen products
103          */
104         @Override
105         public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
106                 return this.app.calculateTotalAmount(request, session);
107         }
108
109         /**
110          * Calculates total price of all choosen products
111          *
112          * @param request Request instance
113          * @param session Session instance
114          * @return Total price of all choosen products
115          */
116         @Override
117         public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
118                 return this.app.calculateTotalPrice(request, session);
119         }
120
121         /**
122          * Some "getter" for amount from session
123          *
124          * @param product Product instance
125          * @param session Session instance
126          * @return Amount as string
127          */
128         @Override
129         @Deprecated
130         public String getAmountFromSession (final Product product, final HttpSession session) {
131                 return this.app.getAmountFromSession(product, session);
132         }
133
134         /**
135          * Some "getter" for HTML code 'checked="checked"' if the product is choosen
136          * 
137          * @param product Product instance
138          * @param request Request instance
139          * @param session Session instance
140          * @return Whether the product is choosen
141          */
142         @Override
143         public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
144                 return this.app.getCheckedHtmlFromProduct(product, request, session);
145         }
146
147         /**
148          * Some "getter" for choose from session
149          * 
150          * @param product Product instance
151          * @param session Session instance
152          * @return Choose as string
153          */
154         @Override
155         @Deprecated
156         public String getChooseFromSession (final Product product, final HttpSession session) {
157                 return this.app.getChooseFromSession(product, session);
158         }
159
160         /**
161          * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
162          *
163          * @param request Request instance
164          * @param session Session instance
165          * @return Whether the product is choosen
166          */
167         @Override
168         public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
169                 return this.app.getDisabledHtmlFromSession(request, session);
170         }
171
172         /**
173          * Some "getter" for choosen (checkbox) from session
174          *
175          * @param product Product instance
176          * @param request Request instance
177          * @param session Session instance
178          * @return Amount as string
179          */
180         @Override
181         @Deprecated
182         public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
183                 return this.app.getPrintableChoosenFromRequestSession(product, request, session);
184         }
185
186         /**
187          * Checks if given Product instance is available and returns a printable
188          * (human-readable) string.
189          * 
190          * @param product Product instance to check
191          * @return Human-readable version of product availability
192          */
193         @Override
194         public String getPrintableProduktAvailability (final Product product) {
195                 return this.app.getPrintableProduktAvailability(product);
196         }
197
198         /**
199          * Some getter for printable value from session or an empty string for null.
200          *
201          * @param session Session instance
202          * @param key Key to get
203          * @return Value from key, empty string for null
204          */
205         @Override
206         public Object getPrintableValeFromSession (final HttpSession session, final String key) {
207                 return this.app.getPrintableValeFromSession(session, key);
208         }
209
210         /**
211          * Some "getter" for a an array of only available products
212          *
213          * @return All products
214          */
215         @Override
216         public Iterator<Product> getAvailableProducts () throws ServletException {
217                 return this.app.getAvailableProducts();
218         }
219
220         /**
221          * Some "getter" for a an array of all products
222          *
223          * @return All products
224          */
225         @Override
226         public Iterator<Product> getAllProducts () throws ServletException {
227                 return this.app.getAllProducts();
228         }
229
230         /**
231          * Some "getter" for a an array of all categories
232          *
233          * @return All categories
234          */
235         @Override
236         public Iterator<Category> getAllCategories () throws ServletException {
237                 return this.app.getAllCategories();
238         }
239
240         /**
241          * Some "getter" for total price of position from request or session.
242          * Single price and amount is multiplyed.
243          *
244          * @param product Product instance
245          * @param request Request instance
246          * @param session Session instance
247          * @return Amount as string
248          */
249         @Override
250         @Deprecated
251         public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
252                 return this.app.getTotalPositionPriceFromRequestSession(product, request, session);
253         }
254
255         /**
256          * Handler for amount from request or session
257          *
258          * @param product Product instance
259          * @param request Request instance
260          * @param session Session instance
261          * @return Amount as string
262          */
263         @Override
264         @Deprecated
265         public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
266                 return this.app.handleAmountFromRequestSession(product, request, session);
267         }
268
269         /**
270          * Checks whether the given product is choosen, request overules session.
271          *
272          * @param product Product instance
273          * @param request Request instance
274          * @param session Session instance
275          * @return Whether the product is choosen
276          */
277         @Override
278         @Deprecated
279         public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
280                 return this.app.isProductChoosen(product, request, session);
281         }
282
283         /**
284          * Marks all choosen products as ordered
285          *
286          * @param request Request instance
287          * @param session Session instance
288          */
289         @Override
290         @Deprecated
291         public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
292                 this.app.markAllChoosenProductsAsOrdered(request, session);
293         }
294
295         /**
296          * Marks given product as choosen in session
297          *
298          * @param product Product to mark as ordered
299          * @param session Session instance
300          */
301         @Override
302         @Deprecated
303         public void markProductAsChoosen (final Product product, final HttpSession session) {
304                 this.app.markProductAsChoosen(product, session);
305         }
306
307         /**
308          * Marks given product as ordered in session
309          *
310          * @param product Product to mark as ordered
311          * @param session Session instance
312          */
313         @Override
314         @Deprecated
315         public void markProductAsOrdered (final Product product, final HttpSession session) {
316                 this.app.markProductAsOrdered(product, session);
317         }
318
319         /**
320          * Somewhat setter in session
321          *
322          * @param session Session instance
323          * @param key Session key to set
324          * @param value Value to set
325          */
326         @Override
327         public void setValueInSession (final HttpSession session, final String key, final Object value) {
328                 this.app.setValueInSession(session, key, value);
329         }
330
331         /**
332          * Unmarks given product as choosen in session
333          *
334          * @param product Product to unmark as choosen
335          * @param session Session instance
336          */
337         @Override
338         @Deprecated
339         public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
340                 this.app.unmarkProductAsChoosen(product, session);
341         }
342
343         /**
344          * Unmarks given product as ordered in session
345          *
346          * @param product Product to unmark as ordered
347          * @param session Session instance
348          */
349         @Override
350         @Deprecated
351         public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
352                 this.app.unmarkProductAsOrdered(product, session);
353         }
354
355         @Override
356         public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
357                 this.app.doAdminAddCategory(request);
358         }
359
360         @Override
361         public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
362                 this.app.doAdminAddProduct(request);
363         }
364
365         @Override
366         public String generateLinkForParent (final Category category) {
367                 return this.app.generateLinkForParent(category);
368         }
369
370         @Override
371         public String getPrintableProduktCategory (final Product product) throws ServletException {
372                 return this.app.getPrintableProduktCategory(product);
373         }
374
375         @Override
376         public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
377                 this.app.doAdminHandleProductForms(request, response);
378         }
379
380         @Override
381         public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
382                 this.app.doAdminHandleCategoryForms(request, response);
383         }
384
385         @Override
386         public Product getProduct (final AddableBasketItem item) throws ServletException {
387                 // Deligate to application
388                 return this.app.getProduct(item);
389         }
390 }