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;
42 * Main application class
44 * @author Roland Haeder
46 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
50 public static final String MAIN_TITLE = "Pizza-Service";
53 * Frontend for products
55 private ProductFrontend productFrontend;
58 * Some singleton getter for this instance. If the instance is not set in
59 * given application, it will be created.
61 * @param context Servlet context
62 * @return This instance
63 * @throws javax.servlet.ServletException If object is not set correctly
65 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
66 // Check application instance
67 if (context == null) {
69 throw new NullPointerException("application is null"); //NOI18N
73 PizzaApplication instance = null;
75 // Get instance from servlet application (aka. "application scope")
76 Object object = context.getAttribute("app"); //NOI18N
79 if (object instanceof PizzaApplication) {
80 // Instance is set, so casting should work
81 instance = (PizzaApplication) object;
82 } else if (object instanceof Object) {
83 // Not correct instance
84 throw new ServletException("app is not set correctly"); //NOI18N
87 // "service" is null, so initialize it
88 instance = new PizzaServiceApplication(context);
89 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
90 throw new ServletException(ex);
94 context.setAttribute("app", instance); //NOI18N
98 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
105 * For debugging purpose
107 * @param args Arguments
109 public static void main (String[] args) {
110 // Get instance and start it
111 new PizzaServiceApplication().start();
115 * Constructor with servet configuration
117 * @param context Servlet context
119 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
120 // Temporary initialize default bundle
121 // @TODO The JSF may have better internatialization support
124 // Initialize properties from config
125 this.initProperties(context);
127 // Init database frontends
128 this.initDatabaseFrontends();
132 * Default constructor
134 private PizzaServiceApplication () {
138 * Calculates total amount of all choosen products
140 * @param request Request instance
141 * @param session Session instance
142 * @return Total amount of all choosen products
145 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
147 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
149 // Is product and session set?
150 if (request == null) {
152 throw new NullPointerException("request is null"); //NOI18N
153 } else if (session == null) {
155 throw new NullPointerException("session is null"); //NOI18N
158 // Init/declare total price and iterator
160 Iterator<Product> iterator;
164 iterator = this.getProducts();
165 } catch (final IOException | BadTokenException ex) {
166 throw new ServletException(ex);
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;
223 iterator = this.getProducts();
224 } catch (final IOException | BadTokenException ex) {
225 throw new ServletException(ex);
228 // "Walk" over all products
229 while (iterator.hasNext()) {
231 Product product = iterator.next();
234 if (this.isProductChoosen(product, request, session)) {
235 // Then add product's total price
236 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
237 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
239 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
243 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
245 // Return total price
250 public void doBootstrap () {
251 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
255 public void doMainLoop () {
256 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
260 public void doShutdown () {
261 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
265 * Some "getter" for amount from session
267 * @param product Product instance
268 * @param session Session instance
269 * @return Amount as string
272 public String getAmountFromSession (final Product product, final HttpSession session) {
274 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
276 // Is product and session set?
277 if (product == null) {
279 throw new NullPointerException("product is null"); //NOI18N
280 } else if (session == null) {
282 throw new NullPointerException("session is null"); //NOI18N
286 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
288 // Is the object null?
289 if (object == null) {
291 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
298 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
300 // Cast to string and return it
301 return (String) object;
305 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
307 * @param product Product instance
308 * @param request Request instance
309 * @param session Session instance
310 * @return Whether the product is choosen
313 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
315 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
317 // Is product and session set?
318 if (product == null) {
320 throw new NullPointerException("product is null"); //NOI18N
321 } else if (request == null) {
323 throw new NullPointerException("request is null"); //NOI18N
324 } else if (session == null) {
326 throw new NullPointerException("session is null"); //NOI18N
329 // First let's check if the product is choosen
330 if (this.isProductChoosen(product, request, session)) {
332 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
335 return "checked=\"checked\""; //NOI18N
338 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
346 * Some "getter" for choose from session
348 * @param product Product instance
349 * @param session Session instance
350 * @return Choose as string
353 public String getChooseFromSession (final Product product, final HttpSession session) {
355 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
357 // Is product and session set?
358 if (product == null) {
360 throw new NullPointerException("product is null"); //NOI18N
361 } else if (session == null) {
363 throw new NullPointerException("session is null"); //NOI18N
367 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
369 // Is the object null?
370 if (object == null) {
372 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
377 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
379 // Cast to string and return it
380 return (String) object;
384 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
386 * @param request Request instance
387 * @param session Session instance
388 * @return Whether the product is choosen
391 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
393 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
395 // Is product and session set?
396 if (request == null) {
398 throw new NullPointerException("request is null"); //NOI18N
399 } else if (session == null) {
401 throw new NullPointerException("session is null"); //NOI18N
404 // Is something selected?
405 if (this.calculateTotalAmount(request, session) > 0) {
407 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
409 // Something has been choosen
413 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
415 // Nothing choosen yet
416 return "disabled=\"disabled\""; //NOI18N
421 * Some "getter" for choosen (checkbox) from session
423 * @param product Product instance
424 * @param request Request instance
425 * @param session Session instance
426 * @return Amount as string
429 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
431 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
433 // Is product and session set?
434 if (product == null) {
436 throw new NullPointerException("product is null"); //NOI18N
437 } else if (request == null) {
439 throw new NullPointerException("request is null"); //NOI18N
440 } else if (session == null) {
442 throw new NullPointerException("session is null"); //NOI18N
446 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
447 String choosen = this.handleChooseFromRequestSession(product, request, session);
448 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
451 assert(choosen instanceof String): "choosen is null"; //NOI18N
454 if (choosen.isEmpty()) {
460 String amount = this.handleAmountFromRequestSession(product, request, session);
461 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
464 assert(amount instanceof String): "amount is null"; //NOI18N
467 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
468 // Choosen, but no amount
477 * Checks if given Product instance is available and returns a printable
478 * (human-readable) string.
480 * @param product Product instance to check
481 * @return Human-readable version of product availability
484 public String getPrintableProduktAvailability (final Product product) {
485 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product));
489 * Some getter for printable value from session or an empty string for null.
491 * @param session Session instance
492 * @param key Key to get
493 * @return Value from key, empty string for null
496 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
498 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
500 // Are both parameter not null?
501 if (session == null) {
503 throw new NullPointerException("session is null"); //NOI18N
504 } else if (key == null) {
506 throw new NullPointerException("key is null"); //NOI18N
510 Object value = this.getValueFromSession(session, key);
513 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
516 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
518 // Return actual value
519 return this.convertNullToEmpty(value);
523 * Some "getter" for a an array of all products
525 * @return All products
528 public Iterator<Product> getProducts () throws IOException, BadTokenException {
529 // Ask frontend for a list of products
530 return this.productFrontend.getProducts();
534 * Some "getter" for a an array of all categories
536 * @return All categories
539 public Iterator<Category> getCategories () {
540 throw new UnsupportedOperationException("Needs refacturing ...");
544 * Some "getter" for total price of position from request or session.
545 * Single price and amount is multiplyed.
547 * @param product Product instance
548 * @param request Request instance
549 * @param session Session instance
550 * @return Amount as string
553 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
555 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
557 // Is product and session set?
558 if (product == null) {
560 throw new NullPointerException("product is null"); //NOI18N
561 } else if (request == null) {
563 throw new NullPointerException("request is null"); //NOI18N
564 } else if (session == null) {
566 throw new NullPointerException("session is null"); //NOI18N
570 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
571 String choosen = this.handleChooseFromRequestSession(product, request, session);
572 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
575 assert(choosen instanceof String): "choosen is null"; //NOI18N
578 if (choosen.isEmpty()) {
580 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
585 String amount = this.handleAmountFromRequestSession(product, request, session);
586 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
589 assert(amount instanceof String): "amount is null"; //NOI18N
592 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
594 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
599 Integer value = null;
603 // Get amount as integer
604 value = Integer.valueOf(amount);
605 } catch (final NumberFormatException e) {
607 throw new IllegalArgumentException(e);
611 float price = (product.getPrice() * value);
614 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
616 // Then multiply it with price
621 * Handler for amount from request or session
623 * @param product Product instance
624 * @param request Request instance
625 * @param session Session instance
626 * @return Amount as string
629 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
631 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
633 // Is product and session set?
634 if (product == null) {
636 throw new NullPointerException("product is null"); //NOI18N
637 } else if (request == null) {
639 throw new NullPointerException("request is null"); //NOI18N
640 } else if (session == null) {
642 throw new NullPointerException("session is null"); //NOI18N
648 // Check request method
649 if (!"POST".equals(request.getMethod())) { //NOI18N
650 // Not POST, so get from session
651 return this.getAmountFromSession(product, session);
652 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
654 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
655 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
659 // Get attribute from request
660 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
663 if (object instanceof String) {
664 // Try to parse it to integer
666 Integer value = Integer.valueOf((String) object);
667 } catch (final NumberFormatException ex) {
669 this.getLogger().warn(ex);
673 // Then set it in session
674 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
677 return (String) object;
681 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
683 // Get attribute from session
684 return this.getAmountFromSession(product, session);
688 * Checks whether the given product is choosen, request overules session.
690 * @param product Product instance
691 * @param request Request instance
692 * @param session Session instance
693 * @return Whether the product is choosen
696 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
698 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
700 // Is product and session set?
701 if (product == null) {
703 throw new NullPointerException("product is null"); //NOI18N
704 } else if (request == null) {
706 throw new NullPointerException("request is null"); //NOI18N
707 } else if (session == null) {
709 throw new NullPointerException("session is null"); //NOI18N
713 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
714 String choosen = this.handleChooseFromRequestSession(product, request, session);
715 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
718 assert(choosen instanceof String): "choosen is null"; //NOI18N
720 // Is it not choosen?
721 if (choosen.isEmpty()) {
727 String amount = this.handleAmountFromRequestSession(product, request, session);
730 assert(amount instanceof String): "amount is not set"; //NOI18N
733 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
735 // Must not be empty and not 0
736 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
740 * Marks all choosen products as ordered
742 * @param request Request instance
743 * @param session Session instance
746 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
748 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session));
751 Iterator<Product> iterator;
755 iterator = this.getProducts();
756 } catch (final IOException | BadTokenException ex) {
757 throw new ServletException(ex);
760 // "Walk" over all products
761 while (iterator.hasNext()) {
763 Product product = iterator.next();
766 this.getLogger().debug(MessageFormat.format("product={0}", product));
769 if (this.isProductChoosen(product, request, session)) {
770 // Mark product as ordered
771 this.markProductAsOrdered(product, session);
776 this.getLogger().trace("EXIT!");
780 * Marks given product as choosen in session
782 * @param product Product to mark as ordered
783 * @param session Session instance
786 public void markProductAsChoosen (final Product product, final HttpSession session) {
788 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
790 // Is product and session set?
791 if (product == null) {
793 throw new NullPointerException("product is null"); //NOI18N
794 } else if (session == null) {
796 throw new NullPointerException("session is null"); //NOI18N
799 // Mark it as ordered by setting flag
800 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
801 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
804 this.getLogger().trace("EXIT!"); //NOI18N
808 * Marks given product as ordered in session
810 * @param product Product to mark as ordered
811 * @param session Session instance
814 public void markProductAsOrdered (final Product product, final HttpSession session) {
816 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
818 // Is product and session set?
819 if (product == null) {
821 throw new NullPointerException("product is null"); //NOI18N
822 } else if (session == null) {
824 throw new NullPointerException("session is null"); //NOI18N
827 // Mark it as ordered by setting flag
828 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
829 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
832 this.getLogger().trace("EXIT!"); //NOI18N
836 * Somewhat setter in session
838 * @param session Session instance
839 * @param key Session key to set
840 * @param value Value to set
843 public void setValueInSession (final HttpSession session, final String key, final Object value) {
845 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
847 synchronized(session) {
849 session.setAttribute(key, value);
853 this.getLogger().trace("EXIT!"); //NOI18N
857 * Unmarks given product as choosen in session
859 * @param product Product to unmark as choosen
860 * @param session Session instance
863 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
865 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
867 // Is product and session set?
868 if (product == null) {
870 throw new NullPointerException("product is null"); //NOI18N
871 } else if (session == null) {
873 throw new NullPointerException("session is null"); //NOI18N
876 // Mark it as ordered by setting flag
877 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
878 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
881 this.getLogger().trace("EXIT!"); //NOI18N
885 * Unmarks given product as ordered in session
887 * @param product Product to unmark as ordered
888 * @param session Session instance
891 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
893 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
895 // Is product and session set?
896 if (product == null) {
898 throw new NullPointerException("product is null"); //NOI18N
899 } else if (session == null) {
901 throw new NullPointerException("session is null"); //NOI18N
904 // Mark it as ordered by setting flag
905 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
906 this.clearSessionAttribute(product, session, SESSION_ORDERED);
909 this.getLogger().trace("EXIT!"); //NOI18N
913 * Clears given parameter for product in session
915 * @param product Product instance
916 * @param session Session instance
917 * @param parameter Parameter to clear
919 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
921 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
924 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
925 this.setValueInSession(product, session, parameter, null);
928 this.getLogger().trace("EXIT!"); //NOI18N
932 * Some getter for value from session
934 * @param product Product instance
935 * @param session Session instance
936 * @param attribute Attribute to get value from
937 * @return Value from session
939 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
941 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
944 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
946 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
949 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
956 * Some getter for value from session
958 * @param session Session instance
959 * @param key Key to get value from
960 * @return Value from session
962 private Object getValueFromSession (final HttpSession session, final String key) {
964 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
969 // Get it synchronized from session
970 synchronized (session) {
971 value = session.getAttribute(key);
975 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
982 * Handler for choosen (checkbox) from request or session
984 * @param product Product instance
985 * @param request Request instance
986 * @param session Session instance
987 * @return Amount as string
989 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
991 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
993 // Is product and session set?
994 if (product == null) {
996 throw new NullPointerException("product is null"); //NOI18N
997 } else if (request == null) {
999 throw new NullPointerException("request is null"); //NOI18N
1000 } else if (session == null) {
1002 throw new NullPointerException("session is null"); //NOI18N
1008 // Check request method
1009 if (!"POST".equals(request.getMethod())) { //NOI18N
1010 // Not POST, so get from session
1011 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
1012 return this.getChooseFromSession(product, session);
1013 } else if (this.isProductOrdered(product, session)) {
1014 // Product is ordered
1015 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
1016 return this.getChooseFromSession(product, session);
1017 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1019 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
1020 return this.getChooseFromSession(product, session);
1023 // Get reqzest element
1024 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
1025 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1028 if (object == null) {
1030 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1031 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1032 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1034 // Return empty string
1038 // Then set it in session
1039 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1041 // Cast to string and return it
1042 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1043 return (String) object;
1047 * Initializes database frontends.
1049 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1050 this.productFrontend = new PizzaProductDatabaseFrontend();
1054 * Checks if the product ordered?
1056 * @param product Product instance
1057 * @param session HttpSession instance
1060 private boolean isProductOrdered(final Product product, final HttpSession session) {
1062 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1065 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1066 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1069 return ("true".equals(isOrdered)); //NOI18N
1073 * Somewhat setter in session
1075 * @param product Product instance
1076 * @param session Session instance
1077 * @param keyPart Key part to include in final key
1078 * @param value Value to set
1080 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1082 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1085 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1086 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1089 this.getLogger().trace("EXIT!"); //NOI18N
1093 * Application starter
1095 private void start () {
1101 this.initProperties();
1102 } catch (final IOException ex) {
1104 this.abortProgramWithException(ex);
1108 Iterator<Product> iterator = null;
1112 iterator = this.getProducts();
1113 } catch (final IOException | BadTokenException ex) {
1114 this.abortProgramWithException(ex);
1117 // "Walk" over all products
1118 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1120 Product product = iterator.next();
1123 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1126 // Generate fake Customer instance
1127 Customer customer = new PizzaServiceCustomer();
1130 * Need a least a gender ... :( See, that is why I don't like default
1131 * constructors, you can easily miss something important and bam! You
1132 * get an NPE. The fix here is, to have construtors (or factories) which
1133 * requires all required instances that needs to be set to get a
1134 * consitent object back.
1137 // Gender is MALE now
1138 customer.setGender(Gender.MALE);
1141 Iterator<Map.Entry<Field, Object>> it = null;
1144 // Get iterator on all its fields
1145 it = customer.iterator();
1146 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1147 this.abortProgramWithException(ex);
1151 while ((it instanceof Iterator) && (it.hasNext())) {
1152 Map.Entry<Field, Object> entry = it.next();
1153 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N