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.product.PizzaProductDatabaseFrontend;
38 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
39 import org.mxchange.pizzaapplication.product.Product;
43 * @author Roland Haeder
45 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
49 public static final String MAIN_TITLE = "Pizza-Service";
52 * Frontend for products
54 private ProductFrontend productFrontend;
57 * Some singleton getter for this instance. If the instance is not set in
58 * given application, it will be created.
60 * @param context Servlet context
61 * @return This instance
62 * @throws javax.servlet.ServletException If object is not set correctly
64 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
65 // Check application instance
66 if (context == null) {
68 throw new NullPointerException("application is null"); //NOI18N
72 PizzaApplication instance = null;
74 // Get instance from servlet application (aka. "application scope")
75 Object object = context.getAttribute("app"); //NOI18N
78 if (object instanceof PizzaApplication) {
79 // Instance is set, so casting should work
80 instance = (PizzaApplication) object;
81 } else if (object instanceof Object) {
82 // Not correct instance
83 throw new ServletException("app is not set correctly"); //NOI18N
86 // "service" is null, so initialize it
87 instance = new PizzaServiceApplication(context);
88 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
89 throw new ServletException(ex);
93 context.setAttribute("app", instance); //NOI18N
97 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
104 * For debugging purpose
106 * @param args Arguments
108 public static void main (String[] args) {
109 // Get instance and start it
110 new PizzaServiceApplication().start();
114 * Constructor with servet configuration
116 * @param context Servlet context
118 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
119 // Temporary initialize default bundle
120 // @TODO The JSF may have better internatialization support
123 // Initialize properties from config
124 this.initProperties(context);
126 // Init database frontends
127 this.initDatabaseFrontends();
131 * Default constructor
133 private PizzaServiceApplication () {
137 * Calculates total amount of all choosen products
139 * @param request Request instance
140 * @param session Session instance
141 * @return Total amount of all choosen products
144 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
146 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
148 // Is product and session set?
149 if (request == null) {
151 throw new NullPointerException("request is null"); //NOI18N
152 } else if (session == null) {
154 throw new NullPointerException("session is null"); //NOI18N
160 // "Walk" over all products
161 for (final Product product : this.getProducts()) {
163 if (this.isProductChoosen(product, request, session)) {
164 // Then add ordered amount
165 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
168 String amount = this.getAmountFromSession(product, session);
171 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
172 totalAmount += Integer.valueOf(amount);
174 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
178 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
180 // Return total price
185 * Calculates total price of all choosen products
187 * @param request Request instance
188 * @param session Session instance
189 * @return Total price of all choosen products
192 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
194 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
196 // Is product and session set?
197 if (request == null) {
199 throw new NullPointerException("request is null"); //NOI18N
200 } else if (session == null) {
202 throw new NullPointerException("session is null"); //NOI18N
206 float totalPrice = 0.00f;
208 // "Walk" over all products
209 for (final Product product : this.getProducts()) {
211 if (this.isProductChoosen(product, request, session)) {
212 // Then add product's total price
213 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
214 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
216 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
220 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
222 // Return total price
227 public void doBootstrap () {
228 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
232 public void doMainLoop () {
233 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
237 public void doShutdown () {
238 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
242 * Some "getter" for amount from session
244 * @param product Product instance
245 * @param session Session instance
246 * @return Amount as string
249 public String getAmountFromSession (final Product product, final HttpSession session) {
251 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
253 // Is product and session set?
254 if (product == null) {
256 throw new NullPointerException("product is null"); //NOI18N
257 } else if (session == null) {
259 throw new NullPointerException("session is null"); //NOI18N
263 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
265 // Is the object null?
266 if (object == null) {
268 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
275 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
277 // Cast to string and return it
278 return (String) object;
282 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
284 * @param product Product instance
285 * @param request Request instance
286 * @param session Session instance
287 * @return Whether the product is choosen
290 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
292 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
294 // Is product and session set?
295 if (product == null) {
297 throw new NullPointerException("product is null"); //NOI18N
298 } else if (request == null) {
300 throw new NullPointerException("request is null"); //NOI18N
301 } else if (session == null) {
303 throw new NullPointerException("session is null"); //NOI18N
306 // First let's check if the product is choosen
307 if (this.isProductChoosen(product, request, session)) {
309 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
312 return "checked=\"checked\""; //NOI18N
315 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
323 * Some "getter" for choose from session
325 * @param product Product instance
326 * @param session Session instance
327 * @return Choose as string
330 public String getChooseFromSession (final Product product, final HttpSession session) {
332 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
334 // Is product and session set?
335 if (product == null) {
337 throw new NullPointerException("product is null"); //NOI18N
338 } else if (session == null) {
340 throw new NullPointerException("session is null"); //NOI18N
344 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
346 // Is the object null?
347 if (object == null) {
349 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
354 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
356 // Cast to string and return it
357 return (String) object;
361 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
363 * @param request Request instance
364 * @param session Session instance
365 * @return Whether the product is choosen
368 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
370 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
372 // Is product and session set?
373 if (request == null) {
375 throw new NullPointerException("request is null"); //NOI18N
376 } else if (session == null) {
378 throw new NullPointerException("session is null"); //NOI18N
381 // Is something selected?
382 if (this.calculateTotalAmount(request, session) > 0) {
384 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
386 // Something has been choosen
390 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
392 // Nothing choosen yet
393 return "disabled=\"disabled\""; //NOI18N
398 * Some "getter" for choosen (checkbox) from session
400 * @param product Product instance
401 * @param request Request instance
402 * @param session Session instance
403 * @return Amount as string
406 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
408 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
410 // Is product and session set?
411 if (product == null) {
413 throw new NullPointerException("product is null"); //NOI18N
414 } else if (request == null) {
416 throw new NullPointerException("request is null"); //NOI18N
417 } else if (session == null) {
419 throw new NullPointerException("session is null"); //NOI18N
423 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
424 String choosen = this.handleChooseFromRequestSession(product, request, session);
425 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
428 assert(choosen instanceof String): "choosen is null"; //NOI18N
431 if (choosen.isEmpty()) {
437 String amount = this.handleAmountFromRequestSession(product, request, session);
438 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
441 assert(amount instanceof String): "amount is null"; //NOI18N
444 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
445 // Choosen, but no amount
454 * Checks if given Product instance is available and returns a printable
455 * (human-readable) string.
457 * @param product Product instance to check
458 * @return Human-readable version of product availability
461 public String getPrintableProduktAvailability (final Product product) {
462 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product));
466 * Some getter for printable value from session or an empty string for null.
468 * @param session Session instance
469 * @param key Key to get
470 * @return Value from key, empty string for null
473 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
475 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
477 // Are both parameter not null?
478 if (session == null) {
480 throw new NullPointerException("session is null"); //NOI18N
481 } else if (key == null) {
483 throw new NullPointerException("key is null"); //NOI18N
487 Object value = this.getValueFromSession(session, key);
490 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
493 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
495 // Return actual value
496 return this.convertNullToEmpty(value);
500 * Some "getter" for a an array of all products
502 * @return All products
505 public Iterable<Product> getProducts () {
506 throw new UnsupportedOperationException("Needs refacturing ...");
510 * Some "getter" for a an array of all categories
512 * @return All categories
515 public Iterable<Category> getCategories () {
516 throw new UnsupportedOperationException("Needs refacturing ...");
520 * Some "getter" for total price of position from request or session.
521 * Single price and amount is multiplyed.
523 * @param product Product instance
524 * @param request Request instance
525 * @param session Session instance
526 * @return Amount as string
529 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
531 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
533 // Is product and session set?
534 if (product == null) {
536 throw new NullPointerException("product is null"); //NOI18N
537 } else if (request == null) {
539 throw new NullPointerException("request is null"); //NOI18N
540 } else if (session == null) {
542 throw new NullPointerException("session is null"); //NOI18N
546 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
547 String choosen = this.handleChooseFromRequestSession(product, request, session);
548 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
551 assert(choosen instanceof String): "choosen is null"; //NOI18N
554 if (choosen.isEmpty()) {
556 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
561 String amount = this.handleAmountFromRequestSession(product, request, session);
562 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
565 assert(amount instanceof String): "amount is null"; //NOI18N
568 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
570 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
575 Integer value = null;
579 // Get amount as integer
580 value = Integer.valueOf(amount);
581 } catch (final NumberFormatException e) {
583 throw new IllegalArgumentException(e);
587 float price = (product.getPrice() * value);
590 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
592 // Then multiply it with price
597 * Handler for amount from request or session
599 * @param product Product instance
600 * @param request Request instance
601 * @param session Session instance
602 * @return Amount as string
605 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
607 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
609 // Is product and session set?
610 if (product == null) {
612 throw new NullPointerException("product is null"); //NOI18N
613 } else if (request == null) {
615 throw new NullPointerException("request is null"); //NOI18N
616 } else if (session == null) {
618 throw new NullPointerException("session is null"); //NOI18N
624 // Check request method
625 if (!"POST".equals(request.getMethod())) { //NOI18N
626 // Not POST, so get from session
627 return this.getAmountFromSession(product, session);
628 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
630 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
631 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
635 // Get attribute from request
636 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
639 if (object instanceof String) {
640 // Try to parse it to integer
642 Integer value = Integer.valueOf((String) object);
643 } catch (final NumberFormatException ex) {
645 this.getLogger().warn(ex);
649 // Then set it in session
650 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
653 return (String) object;
657 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
659 // Get attribute from session
660 return this.getAmountFromSession(product, session);
664 * Checks whether the given product is choosen, request overules session.
666 * @param product Product instance
667 * @param request Request instance
668 * @param session Session instance
669 * @return Whether the product is choosen
672 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
674 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
676 // Is product and session set?
677 if (product == null) {
679 throw new NullPointerException("product is null"); //NOI18N
680 } else if (request == null) {
682 throw new NullPointerException("request is null"); //NOI18N
683 } else if (session == null) {
685 throw new NullPointerException("session is null"); //NOI18N
689 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
690 String choosen = this.handleChooseFromRequestSession(product, request, session);
691 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
694 assert(choosen instanceof String): "choosen is null"; //NOI18N
696 // Is it not choosen?
697 if (choosen.isEmpty()) {
703 String amount = this.handleAmountFromRequestSession(product, request, session);
706 assert(amount instanceof String): "amount is not set"; //NOI18N
709 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
711 // Must not be empty and not 0
712 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
716 * Marks given product as choosen in session
718 * @param product Product to mark as ordered
719 * @param session Session instance
722 public void markProductAsChoosen (final Product product, final HttpSession session) {
724 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
726 // Is product and session set?
727 if (product == null) {
729 throw new NullPointerException("product is null"); //NOI18N
730 } else if (session == null) {
732 throw new NullPointerException("session is null"); //NOI18N
735 // Mark it as ordered by setting flag
736 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
737 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
740 this.getLogger().trace("EXIT!"); //NOI18N
744 * Marks given product as ordered in session
746 * @param product Product to mark as ordered
747 * @param session Session instance
750 public void markProductAsOrdered (final Product product, final HttpSession session) {
752 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
754 // Is product and session set?
755 if (product == null) {
757 throw new NullPointerException("product is null"); //NOI18N
758 } else if (session == null) {
760 throw new NullPointerException("session is null"); //NOI18N
763 // Mark it as ordered by setting flag
764 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
765 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
768 this.getLogger().trace("EXIT!"); //NOI18N
772 * Somewhat setter in session
774 * @param session Session instance
775 * @param key Session key to set
776 * @param value Value to set
779 public void setValueInSession (final HttpSession session, final String key, final Object value) {
781 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
783 synchronized(session) {
785 session.setAttribute(key, value);
789 this.getLogger().trace("EXIT!"); //NOI18N
793 * Unmarks given product as choosen in session
795 * @param product Product to unmark as choosen
796 * @param session Session instance
799 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
801 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
803 // Is product and session set?
804 if (product == null) {
806 throw new NullPointerException("product is null"); //NOI18N
807 } else if (session == null) {
809 throw new NullPointerException("session is null"); //NOI18N
812 // Mark it as ordered by setting flag
813 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
814 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
817 this.getLogger().trace("EXIT!"); //NOI18N
821 * Unmarks given product as ordered in session
823 * @param product Product to unmark as ordered
824 * @param session Session instance
827 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
829 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
831 // Is product and session set?
832 if (product == null) {
834 throw new NullPointerException("product is null"); //NOI18N
835 } else if (session == null) {
837 throw new NullPointerException("session is null"); //NOI18N
840 // Mark it as ordered by setting flag
841 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
842 this.clearSessionAttribute(product, session, SESSION_ORDERED);
845 this.getLogger().trace("EXIT!"); //NOI18N
849 * Clears given parameter for product in session
851 * @param product Product instance
852 * @param session Session instance
853 * @param parameter Parameter to clear
855 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
857 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
860 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
861 this.setValueInSession(product, session, parameter, null);
864 this.getLogger().trace("EXIT!"); //NOI18N
868 * Some getter for value from session
870 * @param product Product instance
871 * @param session Session instance
872 * @param attribute Attribute to get value from
873 * @return Value from session
875 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
877 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
880 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
882 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
885 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
892 * Some getter for value from session
894 * @param session Session instance
895 * @param key Key to get value from
896 * @return Value from session
898 private Object getValueFromSession (final HttpSession session, final String key) {
900 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
905 // Get it synchronized from session
906 synchronized (session) {
907 value = session.getAttribute(key);
911 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
918 * Handler for choosen (checkbox) from request or session
920 * @param product Product instance
921 * @param request Request instance
922 * @param session Session instance
923 * @return Amount as string
925 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
927 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
929 // Is product and session set?
930 if (product == null) {
932 throw new NullPointerException("product is null"); //NOI18N
933 } else if (request == null) {
935 throw new NullPointerException("request is null"); //NOI18N
936 } else if (session == null) {
938 throw new NullPointerException("session is null"); //NOI18N
944 // Check request method
945 if (!"POST".equals(request.getMethod())) { //NOI18N
946 // Not POST, so get from session
947 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
948 return this.getChooseFromSession(product, session);
949 } else if (this.isProductOrdered(product, session)) {
950 // Product is ordered
951 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
952 return this.getChooseFromSession(product, session);
953 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
955 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
956 return this.getChooseFromSession(product, session);
959 // Get reqzest element
960 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
961 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
964 if (object == null) {
966 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
967 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
968 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
970 // Return empty string
974 // Then set it in session
975 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
977 // Cast to string and return it
978 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
979 return (String) object;
983 * Initializes database frontends.
985 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
986 this.productFrontend = new PizzaProductDatabaseFrontend();
990 * Checks if the product ordered?
992 * @param product Product instance
993 * @param session HttpSession instance
996 private boolean isProductOrdered(final Product product, final HttpSession session) {
998 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1001 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1002 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1005 return ("true".equals(isOrdered)); //NOI18N
1009 * Somewhat setter in session
1011 * @param product Product instance
1012 * @param session Session instance
1013 * @param keyPart Key part to include in final key
1014 * @param value Value to set
1016 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1018 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1021 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1022 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1025 this.getLogger().trace("EXIT!"); //NOI18N
1029 * Application starter
1031 private void start () {
1037 this.initProperties();
1038 } catch (final IOException ex) {
1040 this.abortProgramWithException(ex);
1043 // "Walk" over all products
1044 for (final Product product : this.getProducts()) {
1046 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1049 // Generate fake Customer instance
1050 Customer customer = new PizzaServiceCustomer();
1053 * Need a least a gender ... :( See, that is why I don't like default
1054 * constructors, you can easily miss something important and bam! You
1055 * get an NPE. The fix here is, to have construtors (or factories) which
1056 * requires all required instances that needs to be set to get a
1057 * consitent object back.
1060 // Gender is MALE now
1061 customer.setGender(Gender.MALE);
1064 Iterator<Map.Entry<Field, Object>> it = null;
1067 // Get iterator on all its fields
1068 it = customer.iterator();
1069 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1070 this.abortProgramWithException(ex);
1074 while ((it instanceof Iterator) && (it.hasNext())) {
1075 Map.Entry<Field, Object> entry = it.next();
1076 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N