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 java.util.SortedMap;
27 import java.util.TreeMap;
28 import javax.servlet.ServletContext;
29 import javax.servlet.ServletException;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpSession;
32 import org.mxchange.jcore.contact.Gender;
33 import org.mxchange.jcore.exceptions.BadTokenException;
34 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
35 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
36 import org.mxchange.pizzaapplication.customer.Customer;
37 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
38 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
39 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
40 import org.mxchange.pizzaapplication.product.Product;
44 * @author Roland Haeder
46 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
50 public static final String MAIN_TITLE = "Pizza-Service";
53 * Some singleton getter for this instance. If the instance is not set in
54 * given application, it will be created.
56 * @param context Servlet context
57 * @return This instance
58 * @throws javax.servlet.ServletException If object is not set correctly
60 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
61 // Check application instance
62 if (context == null) {
64 throw new NullPointerException("application is null"); //NOI18N
68 PizzaApplication instance = null;
70 // Get instance from servlet application (aka. "application scope")
71 Object object = context.getAttribute("app"); //NOI18N
74 if (object instanceof PizzaApplication) {
75 // Instance is set, so casting should work
76 instance = (PizzaApplication) object;
77 } else if (object instanceof Object) {
78 // Not correct instance
79 throw new ServletException("app is not set correctly"); //NOI18N
82 // "service" is null, so initialize it
83 instance = new PizzaServiceApplication(context);
84 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
85 throw new ServletException(ex);
89 context.setAttribute("app", instance); //NOI18N
93 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
100 * For debugging purpose
102 * @param args Arguments
104 public static void main (String[] args) {
105 // Get instance and start it
106 new PizzaServiceApplication().start();
112 private final SortedMap<String, Product> products;
115 * Private constructor
117 private PizzaServiceApplication () {
118 // Init products instance
119 this.products = new TreeMap<>();
123 * Constructor with servet configuration
125 * @param context Servlet context
127 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
128 // Call other constructor first
131 // Temporary initialize default bundle
132 // @TODO The JSF may have better internatialization support
135 // Initialize properties from config
136 this.initProperties(context);
138 // Load available products
139 this.fillProductsList();
143 * Calculates total amount of all choosen products
145 * @param request Request instance
146 * @param session Session instance
147 * @return Total amount of all choosen products
150 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
152 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
154 // Is product and session set?
155 if (request == null) {
157 throw new NullPointerException("request is null"); //NOI18N
158 } else if (session == null) {
160 throw new NullPointerException("session is null"); //NOI18N
166 // "Walk" over all products
167 for (final Product product : this.getProducts()) {
169 if (this.isProductChoosen(product, request, session)) {
170 // Then add ordered amount
171 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
174 String amount = this.getAmountFromSession(product, session);
177 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
178 totalAmount += Integer.valueOf(amount);
180 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
184 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
186 // Return total price
191 * Calculates total price of all choosen products
193 * @param request Request instance
194 * @param session Session instance
195 * @return Total price of all choosen products
198 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
200 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
202 // Is product and session set?
203 if (request == null) {
205 throw new NullPointerException("request is null"); //NOI18N
206 } else if (session == null) {
208 throw new NullPointerException("session is null"); //NOI18N
212 float totalPrice = 0.00f;
214 // "Walk" over all products
215 for (final Product product : this.getProducts()) {
217 if (this.isProductChoosen(product, request, session)) {
218 // Then add product's total price
219 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
220 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
222 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
226 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
228 // Return total price
233 public void doBootstrap () {
234 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
238 public void doMainLoop () {
239 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
243 public void doShutdown () {
244 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
248 * Some "getter" for amount from session
250 * @param product Product instance
251 * @param session Session instance
252 * @return Amount as string
255 public String getAmountFromSession (final Product product, final HttpSession session) {
257 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
259 // Is product and session set?
260 if (product == null) {
262 throw new NullPointerException("product is null"); //NOI18N
263 } else if (session == null) {
265 throw new NullPointerException("session is null"); //NOI18N
269 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
271 // Is the object null?
272 if (object == null) {
274 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
281 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
283 // Cast to string and return it
284 return (String) object;
288 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
290 * @param product Product instance
291 * @param request Request instance
292 * @param session Session instance
293 * @return Whether the product is choosen
296 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
298 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
300 // Is product and session set?
301 if (product == null) {
303 throw new NullPointerException("product is null"); //NOI18N
304 } else if (request == null) {
306 throw new NullPointerException("request is null"); //NOI18N
307 } else if (session == null) {
309 throw new NullPointerException("session is null"); //NOI18N
312 // First let's check if the product is choosen
313 if (this.isProductChoosen(product, request, session)) {
315 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
318 return "checked=\"checked\""; //NOI18N
321 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
329 * Some "getter" for choose from session
331 * @param product Product instance
332 * @param session Session instance
333 * @return Choose as string
336 public String getChooseFromSession (final Product product, final HttpSession session) {
338 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
340 // Is product and session set?
341 if (product == null) {
343 throw new NullPointerException("product is null"); //NOI18N
344 } else if (session == null) {
346 throw new NullPointerException("session is null"); //NOI18N
350 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
352 // Is the object null?
353 if (object == null) {
355 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
360 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
362 // Cast to string and return it
363 return (String) object;
367 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
369 * @param request Request instance
370 * @param session Session instance
371 * @return Whether the product is choosen
374 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
376 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
378 // Is product and session set?
379 if (request == null) {
381 throw new NullPointerException("request is null"); //NOI18N
382 } else if (session == null) {
384 throw new NullPointerException("session is null"); //NOI18N
387 // Is something selected?
388 if (this.calculateTotalAmount(request, session) > 0) {
390 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
392 // Something has been choosen
396 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
398 // Nothing choosen yet
399 return "disabled=\"disabled\""; //NOI18N
404 * Some "getter" for choosen (checkbox) from session
406 * @param product Product instance
407 * @param request Request instance
408 * @param session Session instance
409 * @return Amount as string
412 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
414 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
416 // Is product and session set?
417 if (product == null) {
419 throw new NullPointerException("product is null"); //NOI18N
420 } else if (request == null) {
422 throw new NullPointerException("request is null"); //NOI18N
423 } else if (session == null) {
425 throw new NullPointerException("session is null"); //NOI18N
429 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
430 String choosen = this.handleChooseFromRequestSession(product, request, session);
431 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
434 assert(choosen instanceof String): "choosen is null"; //NOI18N
437 if (choosen.isEmpty()) {
443 String amount = this.handleAmountFromRequestSession(product, request, session);
444 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
447 assert(amount instanceof String): "amount is null"; //NOI18N
450 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
451 // Choosen, but no amount
460 * Some getter for printable value from session or an empty string for null.
462 * @param session Session instance
463 * @param key Key to get
464 * @return Value from key, empty string for null
467 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
469 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
471 // Are both parameter not null?
472 if (session == null) {
474 throw new NullPointerException("session is null"); //NOI18N
475 } else if (key == null) {
477 throw new NullPointerException("key is null"); //NOI18N
481 Object value = this.getValueFromSession(session, key);
484 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
487 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
489 // Return actual value
490 return this.convertNullToEmpty(value);
494 * Some "getter" for a an array of all products
496 * @return Unmarked products
499 public Product[] getProducts () {
500 return this.products.values().toArray(new Product[this.products.size()]);
504 * Some "getter" for total price of position from request or session.
505 * Single price and amount is multiplyed.
507 * @param product Product instance
508 * @param request Request instance
509 * @param session Session instance
510 * @return Amount as string
513 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
515 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
517 // Is product and session set?
518 if (product == null) {
520 throw new NullPointerException("product is null"); //NOI18N
521 } else if (request == null) {
523 throw new NullPointerException("request is null"); //NOI18N
524 } else if (session == null) {
526 throw new NullPointerException("session is null"); //NOI18N
530 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
531 String choosen = this.handleChooseFromRequestSession(product, request, session);
532 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
535 assert(choosen instanceof String): "choosen is null"; //NOI18N
538 if (choosen.isEmpty()) {
540 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
545 String amount = this.handleAmountFromRequestSession(product, request, session);
546 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
549 assert(amount instanceof String): "amount is null"; //NOI18N
552 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
554 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
559 Integer value = null;
563 // Get amount as integer
564 value = Integer.valueOf(amount);
565 } catch (final NumberFormatException e) {
567 throw new IllegalArgumentException(e);
571 float price = (product.getPrice() * value);
574 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
576 // Then multiply it with price
581 * Gets an array of products from product iterator and unmarks them as ordered
583 * @param session HttpSession instance
584 * @return Unmarked products
587 public Product[] getUnmarkedProducts (final HttpSession session) {
589 Product[] array = this.getProducts();
591 // Unmark are all as ordered
592 for (final Product product : array) {
593 this.unmarkProductAsOrdered(product, session);
596 // Return finished array
601 * Handler for amount from request or session
603 * @param product Product instance
604 * @param request Request instance
605 * @param session Session instance
606 * @return Amount as string
609 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
611 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
613 // Is product and session set?
614 if (product == null) {
616 throw new NullPointerException("product is null"); //NOI18N
617 } else if (request == null) {
619 throw new NullPointerException("request is null"); //NOI18N
620 } else if (session == null) {
622 throw new NullPointerException("session is null"); //NOI18N
628 // Check request method
629 if (!"POST".equals(request.getMethod())) { //NOI18N
630 // Not POST, so get from session
631 return this.getAmountFromSession(product, session);
632 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
634 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
635 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
639 // Get attribute from request
640 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
643 if (object instanceof String) {
644 // Try to parse it to integer
646 Integer value = Integer.valueOf((String) object);
647 } catch (final NumberFormatException ex) {
649 this.getLogger().warn(ex);
653 // Then set it in session
654 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
657 return (String) object;
661 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
663 // Get attribute from session
664 return this.getAmountFromSession(product, session);
668 * Checks whether the given product is choosen, request overules session.
670 * @param product Product instance
671 * @param request Request instance
672 * @param session Session instance
673 * @return Whether the product is choosen
676 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
678 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
680 // Is product and session set?
681 if (product == null) {
683 throw new NullPointerException("product is null"); //NOI18N
684 } else if (request == null) {
686 throw new NullPointerException("request is null"); //NOI18N
687 } else if (session == null) {
689 throw new NullPointerException("session is null"); //NOI18N
693 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
694 String choosen = this.handleChooseFromRequestSession(product, request, session);
695 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
698 assert(choosen instanceof String): "choosen is null"; //NOI18N
700 // Is it not choosen?
701 if (choosen.isEmpty()) {
707 String amount = this.handleAmountFromRequestSession(product, request, session);
710 assert(amount instanceof String): "amount is not set"; //NOI18N
713 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
715 // Must not be empty and not 0
716 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
720 * Marks given product as choosen in session
722 * @param product Product to mark as ordered
723 * @param session Session instance
726 public void markProductAsChoosen (final Product product, final HttpSession session) {
728 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
730 // Is product and session set?
731 if (product == null) {
733 throw new NullPointerException("product is null"); //NOI18N
734 } else if (session == null) {
736 throw new NullPointerException("session is null"); //NOI18N
739 // Mark it as ordered by setting flag
740 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
741 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
744 this.getLogger().trace("EXIT!"); //NOI18N
748 * Marks given product as ordered in session
750 * @param product Product to mark as ordered
751 * @param session Session instance
754 public void markProductAsOrdered (final Product product, final HttpSession session) {
756 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
758 // Is product and session set?
759 if (product == null) {
761 throw new NullPointerException("product is null"); //NOI18N
762 } else if (session == null) {
764 throw new NullPointerException("session is null"); //NOI18N
767 // Mark it as ordered by setting flag
768 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
769 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
772 this.getLogger().trace("EXIT!"); //NOI18N
776 * Somewhat setter in session
778 * @param session Session instance
779 * @param key Session key to set
780 * @param value Value to set
783 public void setValueInSession (final HttpSession session, final String key, final Object value) {
785 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
787 synchronized(session) {
789 session.setAttribute(key, value);
793 this.getLogger().trace("EXIT!"); //NOI18N
797 * Unmarks given product as choosen in session
799 * @param product Product to unmark as choosen
800 * @param session Session instance
803 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
805 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
807 // Is product and session set?
808 if (product == null) {
810 throw new NullPointerException("product is null"); //NOI18N
811 } else if (session == null) {
813 throw new NullPointerException("session is null"); //NOI18N
816 // Mark it as ordered by setting flag
817 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getName())); //NOI18N
818 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
821 this.getLogger().trace("EXIT!"); //NOI18N
825 * Unmarks given product as ordered in session
827 * @param product Product to unmark as ordered
828 * @param session Session instance
831 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
833 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
835 // Is product and session set?
836 if (product == null) {
838 throw new NullPointerException("product is null"); //NOI18N
839 } else if (session == null) {
841 throw new NullPointerException("session is null"); //NOI18N
844 // Mark it as ordered by setting flag
845 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
846 this.clearSessionAttribute(product, session, SESSION_ORDERED);
849 this.getLogger().trace("EXIT!"); //NOI18N
853 * Adds given product to list or throws an exception if name is already found
855 * @param product Product instance to add
857 private void addProduct (final Product product) {
859 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
861 // Get all data from it
862 String name = product.getName();
864 // Is the name already used?
865 if (this.isProductNameUsed(name)) {
866 // Something went wrong
867 throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
871 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
874 this.products.put(product.getName(), product);
877 this.getLogger().trace("EXIT!"); //NOI18N
881 * Clears given parameter for product in session
883 * @param product Product instance
884 * @param session Session instance
885 * @param parameter Parameter to clear
887 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
889 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
892 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
893 this.setValueInSession(product, session, parameter, null);
896 this.getLogger().trace("EXIT!"); //NOI18N
900 * Fills products list
901 * @todo Very hard-coded stuff ...
903 private void fillProductsList () throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
905 this.getLogger().trace("CALLED!"); //NOI18N
907 // Get a product frontend
908 ProductFrontend frontend = new PizzaProductDatabaseFrontend();
911 Iterator<Product> iterator = frontend.getProducts();
914 while (iterator.hasNext()) {
916 Product product = iterator.next();
919 this.addProduct(product);
923 this.getLogger().trace("EXIT!"); //NOI18N
927 * Some getter for value from session
929 * @param product Product instance
930 * @param session Session instance
931 * @param attribute Attribute to get value from
932 * @return Value from session
934 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
936 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
939 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getName()));
941 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
944 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
951 * Some getter for value from session
953 * @param session Session instance
954 * @param key Key to get value from
955 * @return Value from session
957 private Object getValueFromSession (final HttpSession session, final String key) {
959 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
964 // Get it synchronized from session
965 synchronized (session) {
966 value = session.getAttribute(key);
970 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
977 * Handler for choosen (checkbox) from request or session
979 * @param product Product instance
980 * @param request Request instance
981 * @param session Session instance
982 * @return Amount as string
984 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
986 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
988 // Is product and session set?
989 if (product == null) {
991 throw new NullPointerException("product is null"); //NOI18N
992 } else if (request == null) {
994 throw new NullPointerException("request is null"); //NOI18N
995 } else if (session == null) {
997 throw new NullPointerException("session is null"); //NOI18N
1003 // Check request method
1004 if (!"POST".equals(request.getMethod())) { //NOI18N
1005 // Not POST, so get from session
1006 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1007 return this.getChooseFromSession(product, session);
1008 } else if (this.isProductOrdered(product, session)) {
1009 // Product is ordered
1010 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1011 return this.getChooseFromSession(product, session);
1012 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1014 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1015 return this.getChooseFromSession(product, session);
1018 // Get reqzest element
1019 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
1020 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
1023 if (object == null) {
1025 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
1026 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1027 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1029 // Return empty string
1033 // Then set it in session
1034 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1036 // Cast to string and return it
1037 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
1038 return (String) object;
1042 * Checks whether given product is already used
1044 * @param name Name of product
1045 * @return Whether the given product's name is already used
1047 private boolean isProductNameUsed(final String name) {
1049 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
1052 return this.products.containsKey(name);
1056 * Checks if the product ordered?
1062 private boolean isProductOrdered(final Product product, final HttpSession session) {
1064 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1067 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1068 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
1071 return ("true".equals(isOrdered)); //NOI18N
1075 * Somewhat setter in session
1077 * @param product Product instance
1078 * @param session Session instance
1079 * @param keyPart Key part to include in final key
1080 * @param value Value to set
1082 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1084 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1087 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
1088 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getName()), value);
1091 this.getLogger().trace("EXIT!"); //NOI18N
1095 * Application starter
1097 private void start () {
1103 this.initProperties();
1105 // Fill products list
1106 this.fillProductsList();
1107 } catch (final IOException | UnsupportedDatabaseBackendException | SQLException | BadTokenException ex) {
1109 this.abortProgramWithException(ex);
1112 // "Walk" over all products
1113 for (final Product product : this.getProducts()) {
1115 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
1118 // Generate fake Customer instance
1119 Customer customer = new PizzaServiceCustomer();
1122 * Need a least a gender ... :( See, that is why I don't like default
1123 * constructors, you can easily miss something important and bam! You
1124 * get an NPE. The fix here is, to have construtors (or factories) which
1125 * requires all required instances that needs to be set to get a
1126 * consitent object back.
1129 // Gender is MALE now
1130 customer.setGender(Gender.MALE);
1133 Iterator<Map.Entry<Field, Object>> it = null;
1136 // Get iterator on all its fields
1137 it = customer.iterator();
1138 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1139 this.abortProgramWithException(ex);
1143 while ((it instanceof Iterator) && (it.hasNext())) {
1144 Map.Entry<Field, Object> entry = it.next();
1145 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N