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.sql.SQLException;
22 import java.text.MessageFormat;
23 import java.util.Iterator;
25 import java.util.SortedMap;
26 import java.util.TreeMap;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpSession;
31 import org.mxchange.jcore.contact.Gender;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
33 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
34 import org.mxchange.pizzaapplication.customer.Customer;
35 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
36 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
37 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
38 import org.mxchange.pizzaapplication.product.Product;
42 * @author Roland Haeder
44 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
48 public static final String MAIN_TITLE = "Pizza-Service";
51 * Some singleton getter for this instance. If the instance is not set in
52 * given application, it will be created.
54 * @param context Servlet context
55 * @return This instance
56 * @throws javax.servlet.ServletException If object is not set correctly
58 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
59 // Check application instance
60 if (context == null) {
62 throw new NullPointerException("application is null"); //NOI18N
66 PizzaApplication instance = null;
68 // Get instance from servlet application (aka. "application scope")
69 Object object = context.getAttribute("app"); //NOI18N
72 if (object instanceof PizzaApplication) {
73 // Instance is set, so casting should work
74 instance = (PizzaApplication) object;
75 } else if (object instanceof Object) {
76 // Not correct instance
77 throw new ServletException("app is not set correctly"); //NOI18N
80 // "service" is null, so initialize it
81 instance = new PizzaServiceApplication(context);
82 } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
83 throw new ServletException(ex);
87 context.setAttribute("app", instance); //NOI18N
91 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
98 * For debugging purpose
100 * @param args Arguments
102 public static void main (String[] args) {
103 // Get instance and start it
104 new PizzaServiceApplication().start();
110 private final SortedMap<String, Product> products;
113 * Private constructor
115 private PizzaServiceApplication () {
116 // Init products instance
117 this.products = new TreeMap<>();
121 * Constructor with servet configuration
122 * @param context Servlet context
124 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
125 // Call other constructor first
128 // Initialize properties from config
129 this.initProperties(context);
131 // Load available products
132 this.fillProductsList();
136 * Calculates total amount of all choosen products
138 * @param request Request instance
139 * @param session Session instance
140 * @return Total amount of all choosen products
143 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
145 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
147 // Is product and session set?
148 if (request == null) {
150 throw new NullPointerException("request is null"); //NOI18N
151 } else if (session == null) {
153 throw new NullPointerException("session is null"); //NOI18N
159 // "Walk" over all products
160 for (final Product product : this.getProducts()) {
162 if (this.isProductChoosen(product, request, session)) {
163 // Then add ordered amount
164 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
167 String amount = this.getAmountFromSession(product, session);
170 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
171 totalAmount += Integer.valueOf(amount);
173 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
177 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
179 // Return total price
184 * Calculates total price of all choosen products
186 * @param request Request instance
187 * @param session Session instance
188 * @return Total price of all choosen products
191 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
193 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
195 // Is product and session set?
196 if (request == null) {
198 throw new NullPointerException("request is null"); //NOI18N
199 } else if (session == null) {
201 throw new NullPointerException("session is null"); //NOI18N
205 float totalPrice = 0.00f;
207 // "Walk" over all products
208 for (final Product product : this.getProducts()) {
210 if (this.isProductChoosen(product, request, session)) {
211 // Then add product's total price
212 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
213 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
215 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
219 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
221 // Return total price
226 public void doBootstrap () {
227 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
231 public void doMainLoop () {
232 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
236 public void doShutdown () {
237 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
241 * Some "getter" for amount from session
243 * @param product Product instance
244 * @param session Session instance
245 * @return Amount as string
248 public String getAmountFromSession (final Product product, final HttpSession session) {
250 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
252 // Is product and session set?
253 if (product == null) {
255 throw new NullPointerException("product is null"); //NOI18N
256 } else if (session == null) {
258 throw new NullPointerException("session is null"); //NOI18N
262 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
264 // Is the object null?
265 if (object == null) {
267 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
274 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
276 // Cast to string and return it
277 return (String) object;
281 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
283 * @param product Product instance
284 * @param request Request instance
285 * @param session Session instance
286 * @return Whether the product is choosen
289 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
291 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
293 // Is product and session set?
294 if (product == null) {
296 throw new NullPointerException("product is null"); //NOI18N
297 } else if (request == null) {
299 throw new NullPointerException("request is null"); //NOI18N
300 } else if (session == null) {
302 throw new NullPointerException("session is null"); //NOI18N
305 // First let's check if the product is choosen
306 if (this.isProductChoosen(product, request, session)) {
308 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
311 return "checked=\"checked\""; //NOI18N
314 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
322 * Some "getter" for choose from session
324 * @param product Product instance
325 * @param session Session instance
326 * @return Choose as string
329 public String getChooseFromSession (final Product product, final HttpSession session) {
331 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
333 // Is product and session set?
334 if (product == null) {
336 throw new NullPointerException("product is null"); //NOI18N
337 } else if (session == null) {
339 throw new NullPointerException("session is null"); //NOI18N
343 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
345 // Is the object null?
346 if (object == null) {
348 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
353 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
355 // Cast to string and return it
356 return (String) object;
360 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
362 * @param request Request instance
363 * @param session Session instance
364 * @return Whether the product is choosen
367 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
369 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
371 // Is product and session set?
372 if (request == null) {
374 throw new NullPointerException("request is null"); //NOI18N
375 } else if (session == null) {
377 throw new NullPointerException("session is null"); //NOI18N
380 // Is something selected?
381 if (this.calculateTotalAmount(request, session) > 0) {
383 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
385 // Something has been choosen
389 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
391 // Nothing choosen yet
392 return "disabled=\"disabled\""; //NOI18N
397 * Some "getter" for choosen (checkbox) from session
399 * @param product Product instance
400 * @param request Request instance
401 * @param session Session instance
402 * @return Amount as string
405 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
407 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
409 // Is product and session set?
410 if (product == null) {
412 throw new NullPointerException("product is null"); //NOI18N
413 } else if (request == null) {
415 throw new NullPointerException("request is null"); //NOI18N
416 } else if (session == null) {
418 throw new NullPointerException("session is null"); //NOI18N
422 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
423 String choosen = this.handleChooseFromRequestSession(product, request, session);
424 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
427 assert(choosen instanceof String): "choosen is null"; //NOI18N
430 if (choosen.isEmpty()) {
436 String amount = this.handleAmountFromRequestSession(product, request, session);
437 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
440 assert(amount instanceof String): "amount is null"; //NOI18N
443 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
444 // Choosen, but no amount
453 * Some getter for printable value from session or an empty string for null.
455 * @param session Session instance
456 * @param key Key to get
457 * @return Value from key, empty string for null
460 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
462 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
464 // Are both parameter not null?
465 if (session == null) {
467 throw new NullPointerException("session is null"); //NOI18N
468 } else if (key == null) {
470 throw new NullPointerException("key is null"); //NOI18N
474 Object value = this.getValueFromSession(session, key);
477 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
480 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
482 // Return actual value
483 return this.convertNullToEmpty(value);
487 * Some "getter" for a an array of all products
489 * @return Unmarked products
492 public Product[] getProducts () {
493 return this.products.values().toArray(new Product[this.products.size()]);
497 * Some "getter" for total price of position from request or session.
498 * Single price and amount is multiplyed.
500 * @param product Product instance
501 * @param request Request instance
502 * @param session Session instance
503 * @return Amount as string
506 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
508 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
510 // Is product and session set?
511 if (product == null) {
513 throw new NullPointerException("product is null"); //NOI18N
514 } else if (request == null) {
516 throw new NullPointerException("request is null"); //NOI18N
517 } else if (session == null) {
519 throw new NullPointerException("session is null"); //NOI18N
523 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
524 String choosen = this.handleChooseFromRequestSession(product, request, session);
525 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
528 assert(choosen instanceof String): "choosen is null"; //NOI18N
531 if (choosen.isEmpty()) {
533 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
538 String amount = this.handleAmountFromRequestSession(product, request, session);
539 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
542 assert(amount instanceof String): "amount is null"; //NOI18N
545 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
547 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
552 Integer value = null;
556 // Get amount as integer
557 value = Integer.valueOf(amount);
558 } catch (final NumberFormatException e) {
560 throw new IllegalArgumentException(e);
564 float price = (product.getPrice() * value);
567 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
569 // Then multiply it with price
574 * Gets an array of products from product iterator and unmarks them as ordered
576 * @param session HttpSession instance
577 * @return Unmarked products
580 public Product[] getUnmarkedProducts (final HttpSession session) {
582 Product[] array = this.getProducts();
584 // Unmark are all as ordered
585 for (final Product product : array) {
586 this.unmarkProductAsOrdered(product, session);
589 // Return finished array
594 * Handler for amount from request or session
596 * @param product Product instance
597 * @param request Request instance
598 * @param session Session instance
599 * @return Amount as string
602 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
604 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
606 // Is product and session set?
607 if (product == null) {
609 throw new NullPointerException("product is null"); //NOI18N
610 } else if (request == null) {
612 throw new NullPointerException("request is null"); //NOI18N
613 } else if (session == null) {
615 throw new NullPointerException("session is null"); //NOI18N
621 // Check request method
622 if (!"POST".equals(request.getMethod())) { //NOI18N
623 // Not POST, so get from session
624 return this.getAmountFromSession(product, session);
625 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
627 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
628 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
632 // Get attribute from request
633 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
636 if (object instanceof String) {
637 // Try to parse it to integer
639 Integer value = Integer.valueOf((String) object);
640 } catch (final NumberFormatException ex) {
642 this.getLogger().warn(ex);
646 // Then set it in session
647 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
650 return (String) object;
654 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
656 // Get attribute from session
657 return this.getAmountFromSession(product, session);
661 * Checks whether the given product is choosen, request overules session.
663 * @param product Product instance
664 * @param request Request instance
665 * @param session Session instance
666 * @return Whether the product is choosen
669 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
671 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
673 // Is product and session set?
674 if (product == null) {
676 throw new NullPointerException("product is null"); //NOI18N
677 } else if (request == null) {
679 throw new NullPointerException("request is null"); //NOI18N
680 } else if (session == null) {
682 throw new NullPointerException("session is null"); //NOI18N
686 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
687 String choosen = this.handleChooseFromRequestSession(product, request, session);
688 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
691 assert(choosen instanceof String): "choosen is null"; //NOI18N
693 // Is it not choosen?
694 if (choosen.isEmpty()) {
700 String amount = this.handleAmountFromRequestSession(product, request, session);
703 assert(amount instanceof String): "amount is not set"; //NOI18N
706 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
708 // Must not be empty and not 0
709 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
713 * Marks given product as choosen in session
715 * @param product Product to mark as ordered
716 * @param session Session instance
719 public void markProductAsChoosen (final Product product, final HttpSession session) {
721 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
723 // Is product and session set?
724 if (product == null) {
726 throw new NullPointerException("product is null"); //NOI18N
727 } else if (session == null) {
729 throw new NullPointerException("session is null"); //NOI18N
732 // Mark it as ordered by setting flag
733 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
734 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
737 this.getLogger().trace("EXIT!"); //NOI18N
741 * Marks given product as ordered in session
743 * @param product Product to mark as ordered
744 * @param session Session instance
747 public void markProductAsOrdered (final Product product, final HttpSession session) {
749 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
751 // Is product and session set?
752 if (product == null) {
754 throw new NullPointerException("product is null"); //NOI18N
755 } else if (session == null) {
757 throw new NullPointerException("session is null"); //NOI18N
760 // Mark it as ordered by setting flag
761 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
762 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
765 this.getLogger().trace("EXIT!"); //NOI18N
769 * Somewhat setter in session
771 * @param session Session instance
772 * @param key Session key to set
773 * @param value Value to set
776 public void setValueInSession (final HttpSession session, final String key, final Object value) {
778 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
780 synchronized(session) {
782 session.setAttribute(key, value);
786 this.getLogger().trace("EXIT!"); //NOI18N
790 * Unmarks given product as choosen in session
792 * @param product Product to unmark as choosen
793 * @param session Session instance
796 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
798 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
800 // Is product and session set?
801 if (product == null) {
803 throw new NullPointerException("product is null"); //NOI18N
804 } else if (session == null) {
806 throw new NullPointerException("session is null"); //NOI18N
809 // Mark it as ordered by setting flag
810 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getName())); //NOI18N
811 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
814 this.getLogger().trace("EXIT!"); //NOI18N
818 * Unmarks given product as ordered in session
820 * @param product Product to unmark as ordered
821 * @param session Session instance
824 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
826 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
828 // Is product and session set?
829 if (product == null) {
831 throw new NullPointerException("product is null"); //NOI18N
832 } else if (session == null) {
834 throw new NullPointerException("session is null"); //NOI18N
837 // Mark it as ordered by setting flag
838 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
839 this.clearSessionAttribute(product, session, SESSION_ORDERED);
842 this.getLogger().trace("EXIT!"); //NOI18N
846 * Adds given product to list or throws an exception if name is already found
848 * @param product Product instance to add
850 private void addProduct (final Product product) {
852 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
854 // Get all data from it
855 String name = product.getName();
857 // Is the name already used?
858 if (this.isProductNameUsed(name)) {
859 // Something went wrong
860 throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
864 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
867 this.products.put(product.getName(), product);
870 this.getLogger().trace("EXIT!"); //NOI18N
874 * Clears given parameter for product in session
876 * @param product Product instance
877 * @param session Session instance
878 * @param parameter Parameter to clear
880 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
882 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
885 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
886 this.setValueInSession(product, session, parameter, null);
889 this.getLogger().trace("EXIT!"); //NOI18N
893 * Fills products list
894 * @todo Very hard-coded stuff ...
896 private void fillProductsList () throws UnsupportedDatabaseBackendException, SQLException {
898 this.getLogger().trace("CALLED!"); //NOI18N
900 // Get a product frontend
901 ProductFrontend frontend = new PizzaProductDatabaseFrontend();
904 Iterator<Product> iterator = frontend.getProducts();
907 while (iterator.hasNext()) {
909 Product product = iterator.next();
912 this.addProduct(product);
916 this.getLogger().trace("EXIT!"); //NOI18N
920 * Some getter for value from session
922 * @param product Product instance
923 * @param session Session instance
924 * @param attribute Attribute to get value from
925 * @return Value from session
927 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
929 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
932 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getName()));
934 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
937 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
944 * Some getter for value from session
946 * @param session Session instance
947 * @param key Key to get value from
948 * @return Value from session
950 private Object getValueFromSession (final HttpSession session, final String key) {
952 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
957 // Get it synchronized from session
958 synchronized (session) {
959 value = session.getAttribute(key);
963 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
970 * Handler for choosen (checkbox) from request or session
972 * @param product Product instance
973 * @param request Request instance
974 * @param session Session instance
975 * @return Amount as string
977 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
979 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
981 // Is product and session set?
982 if (product == null) {
984 throw new NullPointerException("product is null"); //NOI18N
985 } else if (request == null) {
987 throw new NullPointerException("request is null"); //NOI18N
988 } else if (session == null) {
990 throw new NullPointerException("session is null"); //NOI18N
996 // Check request method
997 if (!"POST".equals(request.getMethod())) { //NOI18N
998 // Not POST, so get from session
999 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1000 return this.getChooseFromSession(product, session);
1001 } else if (this.isProductOrdered(product, session)) {
1002 // Product is ordered
1003 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1004 return this.getChooseFromSession(product, session);
1005 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1007 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1008 return this.getChooseFromSession(product, session);
1011 // Get reqzest element
1012 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
1013 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
1016 if (object == null) {
1018 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
1019 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1020 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1022 // Return empty string
1026 // Then set it in session
1027 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1029 // Cast to string and return it
1030 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
1031 return (String) object;
1035 * Checks whether given product is already used
1037 * @param name Name of product
1038 * @return Whether the given product's name is already used
1040 private boolean isProductNameUsed(final String name) {
1042 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
1045 return this.products.containsKey(name);
1049 * Checks if the product ordered?
1055 private boolean isProductOrdered(final Product product, final HttpSession session) {
1057 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1060 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1061 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
1064 return ("true".equals(isOrdered)); //NOI18N
1068 * Somewhat setter in session
1070 * @param product Product instance
1071 * @param session Session instance
1072 * @param keyPart Key part to include in final key
1073 * @param value Value to set
1075 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1077 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1080 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
1081 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getName()), value);
1084 this.getLogger().trace("EXIT!"); //NOI18N
1088 * Application starter
1090 private void start () {
1096 this.initProperties();
1098 // Fill products list
1099 this.fillProductsList();
1100 } catch (final IOException | UnsupportedDatabaseBackendException | SQLException ex) {
1102 this.abortProgramWithException(ex);
1105 // "Walk" over all products
1106 for (final Product product : this.getProducts()) {
1108 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
1111 // Generate fake Customer instance
1112 Customer customer = new PizzaServiceCustomer();
1115 * Need a least a gender ... :( See, that is why I don't like default
1116 * constructors, you can easily miss something important and bam! You
1117 * get an NPE. The fix here is, to have construtors (or factories) which
1118 * requires all required instances that needs to be set to get a
1119 * consitent object back.
1122 // Gender is MALE now
1123 customer.setGender(Gender.MALE);
1126 Iterator<Map.Entry<Field, Object>> it = null;
1129 // Get iterator on all its fields
1130 it = customer.iterator();
1131 } catch (final NoSuchMethodException ex) {
1132 this.abortProgramWithException(ex);
1136 while ((it instanceof Iterator) && (it.hasNext())) {
1137 Map.Entry<Field, Object> entry = it.next();
1138 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N