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.application;
19 import java.io.IOException;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.contact.Gender;
31 import org.mxchange.jcore.exceptions.BadTokenException;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
33 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
34 import org.mxchange.pizzaapplication.category.Category;
35 import org.mxchange.pizzaapplication.customer.Customer;
36 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
37 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
38 import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend;
39 import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
40 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
41 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
42 import org.mxchange.pizzaapplication.product.Product;
45 * Main application class
47 * @author Roland Haeder
49 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
53 public static final String MAIN_TITLE = "Pizza-Service";
56 * Frontend for products
58 private ProductFrontend productFrontend;
61 * Frontend for categories
63 private CategoryFrontend categoryFrontend;
66 * Some singleton getter for this instance. If the instance is not set in
67 * given application, it will be created.
69 * @param context Servlet context
70 * @return This instance
71 * @throws javax.servlet.ServletException If object is not set correctly
73 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
74 // Check application instance
75 if (context == null) {
77 throw new NullPointerException("application is null"); //NOI18N
81 PizzaApplication instance = null;
83 // Get instance from servlet application (aka. "application scope")
84 Object object = context.getAttribute("app"); //NOI18N
87 if (object instanceof PizzaApplication) {
88 // Instance is set, so casting should work
89 instance = (PizzaApplication) object;
90 } else if (object instanceof Object) {
91 // Not correct instance
92 throw new ServletException("app is not set correctly"); //NOI18N
95 // "service" is null, so initialize it
96 instance = new PizzaServiceApplication(context);
97 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
98 throw new ServletException(ex);
102 context.setAttribute("app", instance); //NOI18N
106 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
113 * For debugging purpose
115 * @param args Arguments
117 public static void main (String[] args) {
118 // Get instance and start it
119 new PizzaServiceApplication().start();
123 * Constructor with servet configuration
125 * @param context Servlet context
127 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
128 // Temporary initialize default bundle
129 // @TODO The JSF may have better internatialization support
132 // Initialize properties from config
133 this.initProperties(context);
135 // Init database frontends
136 this.initDatabaseFrontends();
140 * Default constructor
142 private PizzaServiceApplication () {
146 * Calculates total amount of all choosen products
148 * @param request Request instance
149 * @param session Session instance
150 * @return Total amount of all choosen products
153 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
155 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
157 // Is product and session set?
158 if (request == null) {
160 throw new NullPointerException("request is null"); //NOI18N
161 } else if (session == null) {
163 throw new NullPointerException("session is null"); //NOI18N
166 // Init/declare total price and iterator
168 Iterator<Product> iterator = this.getProducts();
170 // "Walk" over all products
171 while (iterator.hasNext()) {
173 Product product = iterator.next();
176 if (this.isProductChoosen(product, request, session)) {
177 // Then add ordered amount
178 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
181 String amount = this.getAmountFromSession(product, session);
184 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
185 totalAmount += Integer.valueOf(amount);
187 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
191 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
193 // Return total price
198 * Calculates total price of all choosen products
200 * @param request Request instance
201 * @param session Session instance
202 * @return Total price of all choosen products
205 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
207 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
209 // Is product and session set?
210 if (request == null) {
212 throw new NullPointerException("request is null"); //NOI18N
213 } else if (session == null) {
215 throw new NullPointerException("session is null"); //NOI18N
219 float totalPrice = 0.00f;
222 Iterator<Product> iterator = this.getProducts();
224 // "Walk" over all products
225 while (iterator.hasNext()) {
227 Product product = iterator.next();
230 if (this.isProductChoosen(product, request, session)) {
231 // Then add product's total price
232 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
233 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
235 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
239 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
241 // Return total price
246 public void doBootstrap () {
247 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
251 public void doMainLoop () {
252 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
256 public void doShutdown () {
257 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
261 * Some "getter" for amount from session
263 * @param product Product instance
264 * @param session Session instance
265 * @return Amount as string
268 public String getAmountFromSession (final Product product, final HttpSession session) {
270 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
272 // Is product and session set?
273 if (product == null) {
275 throw new NullPointerException("product is null"); //NOI18N
276 } else if (session == null) {
278 throw new NullPointerException("session is null"); //NOI18N
282 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
284 // Is the object null?
285 if (object == null) {
287 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
294 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
296 // Cast to string and return it
297 return (String) object;
301 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
303 * @param product Product instance
304 * @param request Request instance
305 * @param session Session instance
306 * @return Whether the product is choosen
309 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
311 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
313 // Is product and session set?
314 if (product == null) {
316 throw new NullPointerException("product is null"); //NOI18N
317 } else if (request == null) {
319 throw new NullPointerException("request is null"); //NOI18N
320 } else if (session == null) {
322 throw new NullPointerException("session is null"); //NOI18N
325 // First let's check if the product is choosen
326 if (this.isProductChoosen(product, request, session)) {
328 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
331 return "checked=\"checked\""; //NOI18N
334 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
342 * Some "getter" for choose from session
344 * @param product Product instance
345 * @param session Session instance
346 * @return Choose as string
349 public String getChooseFromSession (final Product product, final HttpSession session) {
351 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
353 // Is product and session set?
354 if (product == null) {
356 throw new NullPointerException("product is null"); //NOI18N
357 } else if (session == null) {
359 throw new NullPointerException("session is null"); //NOI18N
363 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
365 // Is the object null?
366 if (object == null) {
368 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
373 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
375 // Cast to string and return it
376 return (String) object;
380 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
382 * @param request Request instance
383 * @param session Session instance
384 * @return Whether the product is choosen
387 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
389 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
391 // Is product and session set?
392 if (request == null) {
394 throw new NullPointerException("request is null"); //NOI18N
395 } else if (session == null) {
397 throw new NullPointerException("session is null"); //NOI18N
400 // Is something selected?
401 if (this.calculateTotalAmount(request, session) > 0) {
403 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
405 // Something has been choosen
409 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
411 // Nothing choosen yet
412 return "disabled=\"disabled\""; //NOI18N
417 * Some "getter" for choosen (checkbox) from session
419 * @param product Product instance
420 * @param request Request instance
421 * @param session Session instance
422 * @return Amount as string
425 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
427 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
429 // Is product and session set?
430 if (product == null) {
432 throw new NullPointerException("product is null"); //NOI18N
433 } else if (request == null) {
435 throw new NullPointerException("request is null"); //NOI18N
436 } else if (session == null) {
438 throw new NullPointerException("session is null"); //NOI18N
442 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
443 String choosen = this.handleChooseFromRequestSession(product, request, session);
444 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
447 assert(choosen instanceof String): "choosen is null"; //NOI18N
450 if (choosen.isEmpty()) {
456 String amount = this.handleAmountFromRequestSession(product, request, session);
457 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
460 assert(amount instanceof String): "amount is null"; //NOI18N
463 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
464 // Choosen, but no amount
473 * Checks if given Product instance is available and returns a printable
474 * (human-readable) string.
476 * @param product Product instance to check
477 * @return Human-readable version of product availability
480 public String getPrintableProduktAvailability (final Product product) {
481 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product)); //NOI18N
485 * Some getter for printable value from session or an empty string for null.
487 * @param session Session instance
488 * @param key Key to get
489 * @return Value from key, empty string for null
492 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
494 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
496 // Are both parameter not null?
497 if (session == null) {
499 throw new NullPointerException("session is null"); //NOI18N
500 } else if (key == null) {
502 throw new NullPointerException("key is null"); //NOI18N
506 Object value = this.getValueFromSession(session, key);
509 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
512 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
514 // Return actual value
515 return this.convertNullToEmpty(value);
519 * Some "getter" for a an array of all products
521 * @return All products
524 public Iterator<Product> getProducts () throws ServletException {
526 // Ask frontend for a list of products
527 return this.productFrontend.getProducts();
528 } catch (final IOException | BadTokenException | SQLException ex) {
529 throw new ServletException(ex);
534 * Some "getter" for a an array of all categories
536 * @return All categories
539 public Iterator<Category> getCategories () throws ServletException {
541 // Ask frontend for a list of categories
542 return this.categoryFrontend.getCategories();
543 } catch (final IOException | BadTokenException | SQLException ex) {
544 throw new ServletException(ex);
549 * Some "getter" for total price of position from request or session.
550 * Single price and amount is multiplyed.
552 * @param product Product instance
553 * @param request Request instance
554 * @param session Session instance
555 * @return Amount as string
558 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
560 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
562 // Is product and session set?
563 if (product == null) {
565 throw new NullPointerException("product is null"); //NOI18N
566 } else if (request == null) {
568 throw new NullPointerException("request is null"); //NOI18N
569 } else if (session == null) {
571 throw new NullPointerException("session is null"); //NOI18N
575 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
576 String choosen = this.handleChooseFromRequestSession(product, request, session);
577 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
580 assert(choosen instanceof String): "choosen is null"; //NOI18N
583 if (choosen.isEmpty()) {
585 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
590 String amount = this.handleAmountFromRequestSession(product, request, session);
591 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
594 assert(amount instanceof String): "amount is null"; //NOI18N
597 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
599 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
604 Integer value = null;
608 // Get amount as integer
609 value = Integer.valueOf(amount);
610 } catch (final NumberFormatException e) {
612 throw new IllegalArgumentException(e);
616 float price = (product.getPrice() * value);
619 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
621 // Then multiply it with price
626 * Handler for amount from request or session
628 * @param product Product instance
629 * @param request Request instance
630 * @param session Session instance
631 * @return Amount as string
634 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
636 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
638 // Is product and session set?
639 if (product == null) {
641 throw new NullPointerException("product is null"); //NOI18N
642 } else if (request == null) {
644 throw new NullPointerException("request is null"); //NOI18N
645 } else if (session == null) {
647 throw new NullPointerException("session is null"); //NOI18N
653 // Check request method
654 if (!"POST".equals(request.getMethod())) { //NOI18N
655 // Not POST, so get from session
656 return this.getAmountFromSession(product, session);
657 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
659 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
660 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
664 // Get attribute from request
665 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
668 if (object instanceof String) {
669 // Try to parse it to integer
671 Integer value = Integer.valueOf((String) object);
672 } catch (final NumberFormatException ex) {
674 this.getLogger().warn(ex);
678 // Then set it in session
679 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
682 return (String) object;
686 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
688 // Get attribute from session
689 return this.getAmountFromSession(product, session);
693 * Checks whether the given product is choosen, request overules session.
695 * @param product Product instance
696 * @param request Request instance
697 * @param session Session instance
698 * @return Whether the product is choosen
701 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
703 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
705 // Is product and session set?
706 if (product == null) {
708 throw new NullPointerException("product is null"); //NOI18N
709 } else if (request == null) {
711 throw new NullPointerException("request is null"); //NOI18N
712 } else if (session == null) {
714 throw new NullPointerException("session is null"); //NOI18N
718 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
719 String choosen = this.handleChooseFromRequestSession(product, request, session);
720 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
723 assert(choosen instanceof String): "choosen is null"; //NOI18N
725 // Is it not choosen?
726 if (choosen.isEmpty()) {
732 String amount = this.handleAmountFromRequestSession(product, request, session);
735 assert(amount instanceof String): "amount is not set"; //NOI18N
738 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
740 // Must not be empty and not 0
741 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
745 * Marks all choosen products as ordered
747 * @param request Request instance
748 * @param session Session instance
751 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
753 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
756 Iterator<Product> iterator = this.getProducts();
758 // "Walk" over all products
759 while (iterator.hasNext()) {
761 Product product = iterator.next();
764 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
767 if (this.isProductChoosen(product, request, session)) {
768 // Mark product as ordered
769 this.markProductAsOrdered(product, session);
774 this.getLogger().trace("EXIT!"); //NOI18N
778 * Marks given product as choosen in session
780 * @param product Product to mark as ordered
781 * @param session Session instance
784 public void markProductAsChoosen (final Product product, final HttpSession session) {
786 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
788 // Is product and session set?
789 if (product == null) {
791 throw new NullPointerException("product is null"); //NOI18N
792 } else if (session == null) {
794 throw new NullPointerException("session is null"); //NOI18N
797 // Mark it as ordered by setting flag
798 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
799 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
802 this.getLogger().trace("EXIT!"); //NOI18N
806 * Marks given product as ordered in session
808 * @param product Product to mark as ordered
809 * @param session Session instance
812 public void markProductAsOrdered (final Product product, final HttpSession session) {
814 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
816 // Is product and session set?
817 if (product == null) {
819 throw new NullPointerException("product is null"); //NOI18N
820 } else if (session == null) {
822 throw new NullPointerException("session is null"); //NOI18N
825 // Mark it as ordered by setting flag
826 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
827 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
830 this.getLogger().trace("EXIT!"); //NOI18N
834 * Somewhat setter in session
836 * @param session Session instance
837 * @param key Session key to set
838 * @param value Value to set
841 public void setValueInSession (final HttpSession session, final String key, final Object value) {
843 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
845 synchronized(session) {
847 session.setAttribute(key, value);
851 this.getLogger().trace("EXIT!"); //NOI18N
855 * Unmarks given product as choosen in session
857 * @param product Product to unmark as choosen
858 * @param session Session instance
861 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
863 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
865 // Is product and session set?
866 if (product == null) {
868 throw new NullPointerException("product is null"); //NOI18N
869 } else if (session == null) {
871 throw new NullPointerException("session is null"); //NOI18N
874 // Mark it as ordered by setting flag
875 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
876 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
879 this.getLogger().trace("EXIT!"); //NOI18N
883 * Unmarks given product as ordered in session
885 * @param product Product to unmark as ordered
886 * @param session Session instance
889 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
891 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
893 // Is product and session set?
894 if (product == null) {
896 throw new NullPointerException("product is null"); //NOI18N
897 } else if (session == null) {
899 throw new NullPointerException("session is null"); //NOI18N
902 // Mark it as ordered by setting flag
903 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
904 this.clearSessionAttribute(product, session, SESSION_ORDERED);
907 this.getLogger().trace("EXIT!"); //NOI18N
911 * Clears given parameter for product in session
913 * @param product Product instance
914 * @param session Session instance
915 * @param parameter Parameter to clear
917 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
919 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
922 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
923 this.setValueInSession(product, session, parameter, null);
926 this.getLogger().trace("EXIT!"); //NOI18N
930 * Some getter for value from session
932 * @param product Product instance
933 * @param session Session instance
934 * @param attribute Attribute to get value from
935 * @return Value from session
937 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
939 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
942 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
944 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
947 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
954 * Some getter for value from session
956 * @param session Session instance
957 * @param key Key to get value from
958 * @return Value from session
960 private Object getValueFromSession (final HttpSession session, final String key) {
962 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
967 // Get it synchronized from session
968 synchronized (session) {
969 value = session.getAttribute(key);
973 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
980 * Handler for choosen (checkbox) from request or session
982 * @param product Product instance
983 * @param request Request instance
984 * @param session Session instance
985 * @return Amount as string
987 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
989 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
991 // Is product and session set?
992 if (product == null) {
994 throw new NullPointerException("product is null"); //NOI18N
995 } else if (request == null) {
997 throw new NullPointerException("request is null"); //NOI18N
998 } else if (session == null) {
1000 throw new NullPointerException("session is null"); //NOI18N
1006 // Check request method
1007 if (!"POST".equals(request.getMethod())) { //NOI18N
1008 // Not POST, so get from session
1009 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1010 return this.getChooseFromSession(product, session);
1011 } else if (this.isProductOrdered(product, session)) {
1012 // Product is ordered
1013 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1014 return this.getChooseFromSession(product, session);
1015 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1017 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1018 return this.getChooseFromSession(product, session);
1021 // Get reqzest element
1022 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
1023 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1026 if (object == null) {
1028 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1029 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1030 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1032 // Return empty string
1036 // Then set it in session
1037 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1039 // Cast to string and return it
1040 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1041 return (String) object;
1045 * Initializes database frontends.
1047 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1049 this.productFrontend = new PizzaProductDatabaseFrontend();
1051 // Category frontend
1052 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1056 * Checks whether given category title is already used
1057 * @param title Title of category to check
1058 * @return Whether it has been found
1060 private boolean isCategoryUsed (final String title) throws IOException, SQLException, BadTokenException {
1061 // Delegate to frontend
1062 return this.categoryFrontend.isCategoryTitleUsed(title);
1066 * Checks if the product ordered?
1068 * @param product Product instance
1069 * @param session HttpSession instance
1072 private boolean isProductOrdered(final Product product, final HttpSession session) {
1074 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1077 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1078 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1081 return ("true".equals(isOrdered)); //NOI18N
1085 * Somewhat setter in session
1087 * @param product Product instance
1088 * @param session Session instance
1089 * @param keyPart Key part to include in final key
1090 * @param value Value to set
1092 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1094 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1097 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1098 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1101 this.getLogger().trace("EXIT!"); //NOI18N
1105 * Application starter
1107 private void start () {
1113 this.initProperties();
1114 } catch (final IOException ex) {
1116 this.abortProgramWithException(ex);
1120 Iterator<Product> iterator = null;
1124 iterator = this.getProducts();
1125 } catch (final ServletException ex) {
1126 this.abortProgramWithException(ex);
1129 // "Walk" over all products
1130 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1132 Product product = iterator.next();
1135 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1138 // Generate fake Customer instance
1139 Customer customer = new PizzaServiceCustomer();
1142 * Need a least a gender ... :( See, that is why I don't like default
1143 * constructors, you can easily miss something important and bam! You
1144 * get an NPE. The fix here is, to have construtors (or factories) which
1145 * requires all required instances that needs to be set to get a
1146 * consitent object back.
1149 // Gender is MALE now
1150 customer.setGender(Gender.MALE);
1153 Iterator<Map.Entry<Field, Object>> it = null;
1156 // Get iterator on all its fields
1157 it = customer.iterator();
1158 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1159 this.abortProgramWithException(ex);
1163 while ((it instanceof Iterator) && (it.hasNext())) {
1164 Map.Entry<Field, Object> entry = it.next();
1165 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1170 * Adds given category data from request to database
1172 * @param request Request instance
1175 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException {
1177 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1179 // request must not be null
1180 if (request == null) {
1182 throw new NullPointerException("request is null");
1185 // Check if all fields are given
1186 if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE) == null) {
1188 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
1189 } else if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE).isEmpty()) {
1191 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
1192 } else if ((request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT) != null) && (!request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT).isEmpty())) {
1193 // "parent" is set, so check it
1195 Integer dummy = Integer.parseInt(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT));
1196 } catch (final NumberFormatException e) {
1198 throw new IllegalArgumentException(e);
1202 // Try to check if title is used already
1203 if (this.isCategoryUsed(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE))) {
1204 // Title already used
1205 throw new IllegalArgumentException(MessageFormat.format("Title {0} is already used.", request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE)));
1207 } catch (final IOException | SQLException | BadTokenException ex) {
1208 throw new ServletException(ex);
1212 // Get all data from it
1213 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1214 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1217 // The category is not found, so add it to database
1218 this.categoryFrontend.addCategory(title, parent);
1219 } catch (final SQLException ex) {
1220 // Continue to throw it
1221 throw new ServletException(ex);
1225 this.getLogger().trace("EXIT!"); //NOI18N
1229 * Adds given product data from request to database
1231 * @param request Request instance
1234 public void doAdminAddProduct (final HttpServletRequest request) {
1236 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1238 // request must not be null
1239 if (request == null) {
1241 throw new NullPointerException("request is null");
1244 // Check if all fields are given
1246 this.getLogger().trace("EXIT!"); //NOI18N