]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/controller/PizzaServiceBean.java
Continued with project:
[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.ServletRequest;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.exceptions.BadTokenException;
31 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
32 import org.mxchange.jshop.beans.BaseFrameworkBean;
33 import org.mxchange.jshop.category.Category;
34 import org.mxchange.jshop.exceptions.CategoryTitleAlreadyUsedException;
35 import org.mxchange.jshop.exceptions.ProductTitleAlreadyUsedException;
36 import org.mxchange.jshop.item.AddableBasketItem;
37 import org.mxchange.jshop.product.Product;
38 import org.mxchange.pizzaapplication.application.PizzaApplication;
39 import org.mxchange.pizzaapplication.application.PizzaServiceApplication;
40
41 /**
42  * Main application class
43  *
44  * @author Roland Haeder
45  */
46 @Named("controller")
47 @SessionScoped
48 public class PizzaServiceBean extends BaseFrameworkBean implements PizzaBean {
49         /**
50          * Serial id
51          */
52         private static final long serialVersionUID = 58137539530279L;
53
54         /**
55          * Pizza application
56          */
57         private final PizzaApplication app;
58
59         /**
60          * Initializer block
61          */
62         {
63                 // Get new application instance
64                 this.getLogger().debug("INITIALIZER!"); //NOI18N
65                 this.app = new PizzaServiceApplication();
66         }
67
68         /**
69          * Default constructor
70          */
71         public PizzaServiceBean () {
72                 this.getLogger().trace("CALLED!"); //NOI18N
73         }
74
75         @Override
76         @PostConstruct
77         public void init () throws FacesException {
78                 // Trace message
79                 this.getLogger().trace(MessageFormat.format("application={0} - CALLED!", this.getApplication())); //NOI18N
80
81                 // Must not be null
82                 if (this.getApplication() == null) {
83                         // Abort here
84                         throw new NullPointerException("application is null"); //NOI18N
85                 }
86
87                 try {
88                         // Call init method
89                         this.app.init(this.getApplication());
90                 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
91                         // Continue to throw
92                         throw new FacesException(ex);
93                 }
94         }
95
96         /**
97          * Some "getter" for HTML code 'checked="checked"' if the product is choosen
98          * 
99          * @param product Product instance
100          * @param request Request instance
101          * @param session Session instance
102          * @return Whether the product is choosen
103          */
104         @Override
105         public String getCheckedHtmlFromProduct (final Product product, final ServletRequest request, final HttpSession session) {
106                 return this.app.getCheckedHtmlFromProduct(product, request, session);
107         }
108
109         /**
110          * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
111          *
112          * @param request Request instance
113          * @param session Session instance
114          * @return Whether the product is choosen
115          */
116         @Override
117         public String getDisabledHtmlFromSession (final ServletRequest request, final HttpSession session) throws ServletException {
118                 return this.app.getDisabledHtmlFromSession(request, session);
119         }
120
121         /**
122          * Checks if given Product instance is available and returns a printable
123          * (human-readable) string.
124          * 
125          * @param product Product instance to check
126          * @return Human-readable version of product availability
127          */
128         @Override
129         public String getPrintableProduktAvailability (final Product product) {
130                 return this.app.getPrintableProduktAvailability(product);
131         }
132
133         /**
134          * Some "getter" for a an array of only available products
135          *
136          * @return All products
137          */
138         @Override
139         public Iterator<Product> getAvailableProducts () throws ServletException {
140                 return this.app.getAvailableProducts();
141         }
142
143         /**
144          * Some "getter" for a an array of all products
145          *
146          * @return All products
147          */
148         @Override
149         public Iterator<Product> getAllProducts () throws ServletException {
150                 return this.app.getAllProducts();
151         }
152
153         /**
154          * Some "getter" for a an array of all categories
155          *
156          * @return All categories
157          */
158         @Override
159         public Iterator<Category> getAllCategories () throws ServletException {
160                 return this.app.getAllCategories();
161         }
162
163         /**
164          * Somewhat setter in session
165          *
166          * @param session Session instance
167          * @param key Session key to set
168          * @param value Value to set
169          */
170         @Override
171         public void setValueInSession (final HttpSession session, final String key, final Object value) {
172                 this.app.setValueInSession(session, key, value);
173         }
174
175         @Override
176         public void doAdminAddCategory (final ServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
177                 this.app.doAdminAddCategory(request);
178         }
179
180         @Override
181         public void doAdminAddProduct (final ServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
182                 this.app.doAdminAddProduct(request);
183         }
184
185         @Override
186         public String generateLinkForParent (final Category category) {
187                 return this.app.generateLinkForParent(category);
188         }
189
190         @Override
191         public String getPrintableProduktCategory (final Product product) throws ServletException {
192                 return this.app.getPrintableProduktCategory(product);
193         }
194
195         @Override
196         public void doAdminHandleProductForms () throws ServletException {
197                 this.app.doAdminHandleProductForms(this.getRequest(), this.getResponse());
198         }
199
200         @Override
201         public void doAdminHandleCategoryForms () throws ServletException {
202                 this.app.doAdminHandleCategoryForms(this.getRequest(), this.getResponse());
203         }
204
205         @Override
206         public Product getProduct (final AddableBasketItem item) throws ServletException {
207                 // Deligate to application
208                 return this.app.getProduct(item);
209         }
210 }