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.frontend.category.CategoryFrontend;
38 import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
39 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
40 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
41 import org.mxchange.pizzaapplication.product.Product;
44 * Main application class
46 * @author Roland Haeder
48 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
52 public static final String MAIN_TITLE = "Pizza-Service";
55 * Frontend for products
57 private ProductFrontend productFrontend;
60 * Frontend for categories
62 private CategoryFrontend categoryFrontend;
65 * Some singleton getter for this instance. If the instance is not set in
66 * given application, it will be created.
68 * @param context Servlet context
69 * @return This instance
70 * @throws javax.servlet.ServletException If object is not set correctly
72 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
73 // Check application instance
74 if (context == null) {
76 throw new NullPointerException("application is null"); //NOI18N
80 PizzaApplication instance = null;
82 // Get instance from servlet application (aka. "application scope")
83 Object object = context.getAttribute("app"); //NOI18N
86 if (object instanceof PizzaApplication) {
87 // Instance is set, so casting should work
88 instance = (PizzaApplication) object;
89 } else if (object instanceof Object) {
90 // Not correct instance
91 throw new ServletException("app is not set correctly"); //NOI18N
94 // "service" is null, so initialize it
95 instance = new PizzaServiceApplication(context);
96 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
97 throw new ServletException(ex);
101 context.setAttribute("app", instance); //NOI18N
105 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
112 * For debugging purpose
114 * @param args Arguments
116 public static void main (String[] args) {
117 // Get instance and start it
118 new PizzaServiceApplication().start();
122 * Constructor with servet configuration
124 * @param context Servlet context
126 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
127 // Temporary initialize default bundle
128 // @TODO The JSF may have better internatialization support
131 // Initialize properties from config
132 this.initProperties(context);
134 // Init database frontends
135 this.initDatabaseFrontends();
139 * Default constructor
141 private PizzaServiceApplication () {
145 * Calculates total amount of all choosen products
147 * @param request Request instance
148 * @param session Session instance
149 * @return Total amount of all choosen products
152 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
154 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
156 // Is product and session set?
157 if (request == null) {
159 throw new NullPointerException("request is null"); //NOI18N
160 } else if (session == null) {
162 throw new NullPointerException("session is null"); //NOI18N
165 // Init/declare total price and iterator
167 Iterator<Product> iterator = this.getProducts();
169 // "Walk" over all products
170 while (iterator.hasNext()) {
172 Product product = iterator.next();
175 if (this.isProductChoosen(product, request, session)) {
176 // Then add ordered amount
177 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
180 String amount = this.getAmountFromSession(product, session);
183 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
184 totalAmount += Integer.valueOf(amount);
186 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
190 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
192 // Return total price
197 * Calculates total price of all choosen products
199 * @param request Request instance
200 * @param session Session instance
201 * @return Total price of all choosen products
204 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
206 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
208 // Is product and session set?
209 if (request == null) {
211 throw new NullPointerException("request is null"); //NOI18N
212 } else if (session == null) {
214 throw new NullPointerException("session is null"); //NOI18N
218 float totalPrice = 0.00f;
221 Iterator<Product> iterator = this.getProducts();
223 // "Walk" over all products
224 while (iterator.hasNext()) {
226 Product product = iterator.next();
229 if (this.isProductChoosen(product, request, session)) {
230 // Then add product's total price
231 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
232 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
234 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
238 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
240 // Return total price
245 public void doBootstrap () {
246 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
250 public void doMainLoop () {
251 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
255 public void doShutdown () {
256 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
260 * Some "getter" for amount from session
262 * @param product Product instance
263 * @param session Session instance
264 * @return Amount as string
267 public String getAmountFromSession (final Product product, final HttpSession session) {
269 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
271 // Is product and session set?
272 if (product == null) {
274 throw new NullPointerException("product is null"); //NOI18N
275 } else if (session == null) {
277 throw new NullPointerException("session is null"); //NOI18N
281 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
283 // Is the object null?
284 if (object == null) {
286 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
293 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
295 // Cast to string and return it
296 return (String) object;
300 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
302 * @param product Product instance
303 * @param request Request instance
304 * @param session Session instance
305 * @return Whether the product is choosen
308 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
310 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
312 // Is product and session set?
313 if (product == null) {
315 throw new NullPointerException("product is null"); //NOI18N
316 } else if (request == null) {
318 throw new NullPointerException("request is null"); //NOI18N
319 } else if (session == null) {
321 throw new NullPointerException("session is null"); //NOI18N
324 // First let's check if the product is choosen
325 if (this.isProductChoosen(product, request, session)) {
327 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
330 return "checked=\"checked\""; //NOI18N
333 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
341 * Some "getter" for choose from session
343 * @param product Product instance
344 * @param session Session instance
345 * @return Choose as string
348 public String getChooseFromSession (final Product product, final HttpSession session) {
350 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
352 // Is product and session set?
353 if (product == null) {
355 throw new NullPointerException("product is null"); //NOI18N
356 } else if (session == null) {
358 throw new NullPointerException("session is null"); //NOI18N
362 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
364 // Is the object null?
365 if (object == null) {
367 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
372 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
374 // Cast to string and return it
375 return (String) object;
379 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
381 * @param request Request instance
382 * @param session Session instance
383 * @return Whether the product is choosen
386 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
388 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
390 // Is product and session set?
391 if (request == null) {
393 throw new NullPointerException("request is null"); //NOI18N
394 } else if (session == null) {
396 throw new NullPointerException("session is null"); //NOI18N
399 // Is something selected?
400 if (this.calculateTotalAmount(request, session) > 0) {
402 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
404 // Something has been choosen
408 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
410 // Nothing choosen yet
411 return "disabled=\"disabled\""; //NOI18N
416 * Some "getter" for choosen (checkbox) from session
418 * @param product Product instance
419 * @param request Request instance
420 * @param session Session instance
421 * @return Amount as string
424 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
426 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
428 // Is product and session set?
429 if (product == null) {
431 throw new NullPointerException("product is null"); //NOI18N
432 } else if (request == null) {
434 throw new NullPointerException("request is null"); //NOI18N
435 } else if (session == null) {
437 throw new NullPointerException("session is null"); //NOI18N
441 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
442 String choosen = this.handleChooseFromRequestSession(product, request, session);
443 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
446 assert(choosen instanceof String): "choosen is null"; //NOI18N
449 if (choosen.isEmpty()) {
455 String amount = this.handleAmountFromRequestSession(product, request, session);
456 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
459 assert(amount instanceof String): "amount is null"; //NOI18N
462 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
463 // Choosen, but no amount
472 * Checks if given Product instance is available and returns a printable
473 * (human-readable) string.
475 * @param product Product instance to check
476 * @return Human-readable version of product availability
479 public String getPrintableProduktAvailability (final Product product) {
480 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product));
484 * Some getter for printable value from session or an empty string for null.
486 * @param session Session instance
487 * @param key Key to get
488 * @return Value from key, empty string for null
491 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
493 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
495 // Are both parameter not null?
496 if (session == null) {
498 throw new NullPointerException("session is null"); //NOI18N
499 } else if (key == null) {
501 throw new NullPointerException("key is null"); //NOI18N
505 Object value = this.getValueFromSession(session, key);
508 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
511 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
513 // Return actual value
514 return this.convertNullToEmpty(value);
518 * Some "getter" for a an array of all products
520 * @return All products
523 public Iterator<Product> getProducts () throws ServletException {
525 // Ask frontend for a list of products
526 return this.productFrontend.getProducts();
527 } catch (final IOException | BadTokenException | SQLException ex) {
528 throw new ServletException(ex);
533 * Some "getter" for a an array of all categories
535 * @return All categories
538 public Iterator<Category> getCategories () throws ServletException {
540 // Ask frontend for a list of categories
541 return this.categoryFrontend.getCategories();
542 } catch (final IOException | BadTokenException | SQLException ex) {
543 throw new ServletException(ex);
548 * Some "getter" for total price of position from request or session.
549 * Single price and amount is multiplyed.
551 * @param product Product instance
552 * @param request Request instance
553 * @param session Session instance
554 * @return Amount as string
557 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
559 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
561 // Is product and session set?
562 if (product == null) {
564 throw new NullPointerException("product is null"); //NOI18N
565 } else if (request == null) {
567 throw new NullPointerException("request is null"); //NOI18N
568 } else if (session == null) {
570 throw new NullPointerException("session is null"); //NOI18N
574 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
575 String choosen = this.handleChooseFromRequestSession(product, request, session);
576 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
579 assert(choosen instanceof String): "choosen is null"; //NOI18N
582 if (choosen.isEmpty()) {
584 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
589 String amount = this.handleAmountFromRequestSession(product, request, session);
590 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
593 assert(amount instanceof String): "amount is null"; //NOI18N
596 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
598 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
603 Integer value = null;
607 // Get amount as integer
608 value = Integer.valueOf(amount);
609 } catch (final NumberFormatException e) {
611 throw new IllegalArgumentException(e);
615 float price = (product.getPrice() * value);
618 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
620 // Then multiply it with price
625 * Handler for amount from request or session
627 * @param product Product instance
628 * @param request Request instance
629 * @param session Session instance
630 * @return Amount as string
633 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
635 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
637 // Is product and session set?
638 if (product == null) {
640 throw new NullPointerException("product is null"); //NOI18N
641 } else if (request == null) {
643 throw new NullPointerException("request is null"); //NOI18N
644 } else if (session == null) {
646 throw new NullPointerException("session is null"); //NOI18N
652 // Check request method
653 if (!"POST".equals(request.getMethod())) { //NOI18N
654 // Not POST, so get from session
655 return this.getAmountFromSession(product, session);
656 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
658 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
659 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
663 // Get attribute from request
664 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
667 if (object instanceof String) {
668 // Try to parse it to integer
670 Integer value = Integer.valueOf((String) object);
671 } catch (final NumberFormatException ex) {
673 this.getLogger().warn(ex);
677 // Then set it in session
678 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
681 return (String) object;
685 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
687 // Get attribute from session
688 return this.getAmountFromSession(product, session);
692 * Checks whether the given product is choosen, request overules session.
694 * @param product Product instance
695 * @param request Request instance
696 * @param session Session instance
697 * @return Whether the product is choosen
700 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
702 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
704 // Is product and session set?
705 if (product == null) {
707 throw new NullPointerException("product is null"); //NOI18N
708 } else if (request == null) {
710 throw new NullPointerException("request is null"); //NOI18N
711 } else if (session == null) {
713 throw new NullPointerException("session is null"); //NOI18N
717 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
718 String choosen = this.handleChooseFromRequestSession(product, request, session);
719 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
722 assert(choosen instanceof String): "choosen is null"; //NOI18N
724 // Is it not choosen?
725 if (choosen.isEmpty()) {
731 String amount = this.handleAmountFromRequestSession(product, request, session);
734 assert(amount instanceof String): "amount is not set"; //NOI18N
737 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
739 // Must not be empty and not 0
740 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
744 * Marks all choosen products as ordered
746 * @param request Request instance
747 * @param session Session instance
750 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
752 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session));
755 Iterator<Product> iterator = this.getProducts();
757 // "Walk" over all products
758 while (iterator.hasNext()) {
760 Product product = iterator.next();
763 this.getLogger().debug(MessageFormat.format("product={0}", product));
766 if (this.isProductChoosen(product, request, session)) {
767 // Mark product as ordered
768 this.markProductAsOrdered(product, session);
773 this.getLogger().trace("EXIT!");
777 * Marks given product as choosen in session
779 * @param product Product to mark as ordered
780 * @param session Session instance
783 public void markProductAsChoosen (final Product product, final HttpSession session) {
785 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
787 // Is product and session set?
788 if (product == null) {
790 throw new NullPointerException("product is null"); //NOI18N
791 } else if (session == null) {
793 throw new NullPointerException("session is null"); //NOI18N
796 // Mark it as ordered by setting flag
797 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
798 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
801 this.getLogger().trace("EXIT!"); //NOI18N
805 * Marks given product as ordered in session
807 * @param product Product to mark as ordered
808 * @param session Session instance
811 public void markProductAsOrdered (final Product product, final HttpSession session) {
813 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
815 // Is product and session set?
816 if (product == null) {
818 throw new NullPointerException("product is null"); //NOI18N
819 } else if (session == null) {
821 throw new NullPointerException("session is null"); //NOI18N
824 // Mark it as ordered by setting flag
825 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
826 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
829 this.getLogger().trace("EXIT!"); //NOI18N
833 * Somewhat setter in session
835 * @param session Session instance
836 * @param key Session key to set
837 * @param value Value to set
840 public void setValueInSession (final HttpSession session, final String key, final Object value) {
842 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
844 synchronized(session) {
846 session.setAttribute(key, value);
850 this.getLogger().trace("EXIT!"); //NOI18N
854 * Unmarks given product as choosen in session
856 * @param product Product to unmark as choosen
857 * @param session Session instance
860 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
862 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
864 // Is product and session set?
865 if (product == null) {
867 throw new NullPointerException("product is null"); //NOI18N
868 } else if (session == null) {
870 throw new NullPointerException("session is null"); //NOI18N
873 // Mark it as ordered by setting flag
874 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
875 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
878 this.getLogger().trace("EXIT!"); //NOI18N
882 * Unmarks given product as ordered in session
884 * @param product Product to unmark as ordered
885 * @param session Session instance
888 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
890 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
892 // Is product and session set?
893 if (product == null) {
895 throw new NullPointerException("product is null"); //NOI18N
896 } else if (session == null) {
898 throw new NullPointerException("session is null"); //NOI18N
901 // Mark it as ordered by setting flag
902 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
903 this.clearSessionAttribute(product, session, SESSION_ORDERED);
906 this.getLogger().trace("EXIT!"); //NOI18N
910 * Clears given parameter for product in session
912 * @param product Product instance
913 * @param session Session instance
914 * @param parameter Parameter to clear
916 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
918 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
921 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
922 this.setValueInSession(product, session, parameter, null);
925 this.getLogger().trace("EXIT!"); //NOI18N
929 * Some getter for value from session
931 * @param product Product instance
932 * @param session Session instance
933 * @param attribute Attribute to get value from
934 * @return Value from session
936 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
938 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
941 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
943 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
946 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
953 * Some getter for value from session
955 * @param session Session instance
956 * @param key Key to get value from
957 * @return Value from session
959 private Object getValueFromSession (final HttpSession session, final String key) {
961 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
966 // Get it synchronized from session
967 synchronized (session) {
968 value = session.getAttribute(key);
972 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
979 * Handler for choosen (checkbox) from request or session
981 * @param product Product instance
982 * @param request Request instance
983 * @param session Session instance
984 * @return Amount as string
986 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
988 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
990 // Is product and session set?
991 if (product == null) {
993 throw new NullPointerException("product is null"); //NOI18N
994 } else if (request == null) {
996 throw new NullPointerException("request is null"); //NOI18N
997 } else if (session == null) {
999 throw new NullPointerException("session is null"); //NOI18N
1005 // Check request method
1006 if (!"POST".equals(request.getMethod())) { //NOI18N
1007 // Not POST, so get from session
1008 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
1009 return this.getChooseFromSession(product, session);
1010 } else if (this.isProductOrdered(product, session)) {
1011 // Product is ordered
1012 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
1013 return this.getChooseFromSession(product, session);
1014 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1016 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
1017 return this.getChooseFromSession(product, session);
1020 // Get reqzest element
1021 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
1022 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1025 if (object == null) {
1027 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1028 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1029 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1031 // Return empty string
1035 // Then set it in session
1036 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1038 // Cast to string and return it
1039 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1040 return (String) object;
1044 * Initializes database frontends.
1046 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1048 this.productFrontend = new PizzaProductDatabaseFrontend();
1050 // Category frontend
1051 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1055 * Checks if the product ordered?
1057 * @param product Product instance
1058 * @param session HttpSession instance
1061 private boolean isProductOrdered(final Product product, final HttpSession session) {
1063 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1066 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1067 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1070 return ("true".equals(isOrdered)); //NOI18N
1074 * Somewhat setter in session
1076 * @param product Product instance
1077 * @param session Session instance
1078 * @param keyPart Key part to include in final key
1079 * @param value Value to set
1081 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1083 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1086 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1087 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1090 this.getLogger().trace("EXIT!"); //NOI18N
1094 * Application starter
1096 private void start () {
1102 this.initProperties();
1103 } catch (final IOException ex) {
1105 this.abortProgramWithException(ex);
1109 Iterator<Product> iterator = null;
1113 iterator = this.getProducts();
1114 } catch (final ServletException ex) {
1115 this.abortProgramWithException(ex);
1118 // "Walk" over all products
1119 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1121 Product product = iterator.next();
1124 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1127 // Generate fake Customer instance
1128 Customer customer = new PizzaServiceCustomer();
1131 * Need a least a gender ... :( See, that is why I don't like default
1132 * constructors, you can easily miss something important and bam! You
1133 * get an NPE. The fix here is, to have construtors (or factories) which
1134 * requires all required instances that needs to be set to get a
1135 * consitent object back.
1138 // Gender is MALE now
1139 customer.setGender(Gender.MALE);
1142 Iterator<Map.Entry<Field, Object>> it = null;
1145 // Get iterator on all its fields
1146 it = customer.iterator();
1147 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1148 this.abortProgramWithException(ex);
1152 while ((it instanceof Iterator) && (it.hasNext())) {
1153 Map.Entry<Field, Object> entry = it.next();
1154 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N