2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.controller;
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;
43 * Main application class
45 * @author Roland Haeder
49 public class PizzaServiceBean extends BaseFrameworkBean implements PizzaBean {
53 private static final long serialVersionUID = 58137539530279L;
58 private final PizzaApplication app;
64 // Get new application instance
65 this.getLogger().debug("INITIALIZER!"); //NOI18N
66 this.app = new PizzaServiceApplication();
72 public PizzaServiceBean () {
73 this.getLogger().trace("CALLED!"); //NOI18N
78 public void init () throws FacesException {
80 this.getLogger().trace(MessageFormat.format("application={0},basket={1} - CALLED!", this.getApplication(), this.getBasket())); //NOI18N
83 if (this.getApplication() == null) {
85 throw new NullPointerException("application is null"); //NOI18N
90 this.app.init(this.getApplication());
91 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
93 throw new FacesException(ex);
98 * Some "getter" for amount from session
100 * @param product Product instance
101 * @param session Session instance
102 * @return Amount as string
106 public String getAmountFromSession (final Product product, final HttpSession session) {
107 return this.app.getAmountFromSession(product, session);
111 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
113 * @param product Product instance
114 * @param request Request instance
115 * @param session Session instance
116 * @return Whether the product is choosen
119 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
120 return this.app.getCheckedHtmlFromProduct(product, request, session);
124 * Some "getter" for choose from session
126 * @param product Product instance
127 * @param session Session instance
128 * @return Choose as string
132 public String getChooseFromSession (final Product product, final HttpSession session) {
133 return this.app.getChooseFromSession(product, session);
137 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
139 * @param request Request instance
140 * @param session Session instance
141 * @return Whether the product is choosen
144 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
145 return this.app.getDisabledHtmlFromSession(request, session);
149 * Checks if given Product instance is available and returns a printable
150 * (human-readable) string.
152 * @param product Product instance to check
153 * @return Human-readable version of product availability
156 public String getPrintableProduktAvailability (final Product product) {
157 return this.app.getPrintableProduktAvailability(product);
161 * Some getter for printable value from session or an empty string for null.
163 * @param session Session instance
164 * @param key Key to get
165 * @return Value from key, empty string for null
168 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
169 return this.app.getPrintableValeFromSession(session, key);
173 * Some "getter" for a an array of only available products
175 * @return All products
178 public Iterator<Product> getAvailableProducts () throws ServletException {
179 return this.app.getAvailableProducts();
183 * Some "getter" for a an array of all products
185 * @return All products
188 public Iterator<Product> getAllProducts () throws ServletException {
189 return this.app.getAllProducts();
193 * Some "getter" for a an array of all categories
195 * @return All categories
198 public Iterator<Category> getAllCategories () throws ServletException {
199 return this.app.getAllCategories();
203 * Some "getter" for total price of position from request or session.
204 * Single price and amount is multiplyed.
206 * @param product Product instance
207 * @param request Request instance
208 * @param session Session instance
209 * @return Amount as string
213 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
214 return this.app.getTotalPositionPriceFromRequestSession(product, request, session);
218 * Checks whether the given product is choosen, request overules session.
220 * @param product Product instance
221 * @param request Request instance
222 * @param session Session instance
223 * @return Whether the product is choosen
227 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
228 return this.app.isProductChoosen(product, request, session);
232 * Somewhat setter in session
234 * @param session Session instance
235 * @param key Session key to set
236 * @param value Value to set
239 public void setValueInSession (final HttpSession session, final String key, final Object value) {
240 this.app.setValueInSession(session, key, value);
244 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
245 this.app.doAdminAddCategory(request);
249 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
250 this.app.doAdminAddProduct(request);
254 public String generateLinkForParent (final Category category) {
255 return this.app.generateLinkForParent(category);
259 public String getPrintableProduktCategory (final Product product) throws ServletException {
260 return this.app.getPrintableProduktCategory(product);
264 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
265 this.app.doAdminHandleProductForms(request, response);
269 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
270 this.app.doAdminHandleCategoryForms(request, response);
274 public Product getProduct (final AddableBasketItem item) throws ServletException {
275 // Deligate to application
276 return this.app.getProduct(item);