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.lang.reflect.Field;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
23 import java.util.SortedMap;
24 import java.util.TreeMap;
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpSession;
29 import org.mxchange.jcore.contact.Gender;
30 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
31 import org.mxchange.pizzaapplication.customer.Customer;
32 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
33 import org.mxchange.pizzaapplication.product.PizzaProduct;
34 import org.mxchange.pizzaapplication.product.Product;
38 * @author Roland Haeder
40 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
44 public static final String MAIN_TITLE = "Pizza-Service";
47 * Some singleton getter for this instance. If the instance is not set in
48 * given application, it will be created.
50 * @param application Servlet context
51 * @return This instance
52 * @throws javax.servlet.ServletException If object is not set correctly
54 public static final PizzaApplication getInstance (final ServletContext application) throws ServletException {
55 // Check application instance
56 if (application == null) {
58 throw new NullPointerException("application is null"); //NOI18N
62 PizzaApplication instance = null;
64 // Get instance from servlet application (aka. "application scope")
65 Object object = application.getAttribute("app"); //NOI18N
68 if (object instanceof PizzaApplication) {
69 // Instance is set, so casting should work
70 instance = (PizzaApplication) object;
71 } else if (object instanceof Object) {
72 // Not correct instance
73 throw new ServletException("app is not set correctly"); //NOI18N
75 // "service" is null, so initialize it
76 instance = new PizzaServiceApplication();
79 application.setAttribute("app", instance); //NOI18N
83 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
90 * For debugging purpose
92 * @param args Arguments
94 public static void main (String[] args) {
95 // Get instance and start it
96 new PizzaServiceApplication().start();
102 private final SortedMap<String, Product> products;
105 * Private constructor
107 private PizzaServiceApplication () {
108 // Init products instance
109 this.products = new TreeMap<>();
114 // Fill products list
115 this.fillProductsList();
119 * Calculates total amount of all choosen products
121 * @param request Request instance
122 * @param session Session instance
123 * @return Total amount of all choosen products
126 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
128 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
130 // Is product and session set?
131 if (request == null) {
133 throw new NullPointerException("request is null"); //NOI18N
134 } else if (session == null) {
136 throw new NullPointerException("session is null"); //NOI18N
142 // "Walk" over all products
143 for (final Product product : this.getProducts()) {
145 if (this.isProductChoosen(product, request, session)) {
146 // Then add ordered amount
147 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
150 String amount = this.getAmountFromSession(product, session);
153 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
154 totalAmount += Integer.valueOf(amount);
156 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
160 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
162 // Return total price
167 * Calculates total price of all choosen products
169 * @param request Request instance
170 * @param session Session instance
171 * @return Total price of all choosen products
174 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
176 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
178 // Is product and session set?
179 if (request == null) {
181 throw new NullPointerException("request is null"); //NOI18N
182 } else if (session == null) {
184 throw new NullPointerException("session is null"); //NOI18N
188 float totalPrice = 0.00f;
190 // "Walk" over all products
191 for (final Product product : this.getProducts()) {
193 if (this.isProductChoosen(product, request, session)) {
194 // Then add product's total price
195 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
196 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
198 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
202 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
204 // Return total price
209 public void doBootstrap () {
210 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
214 public void doMainLoop () {
215 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
219 public void doShutdown () {
220 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
224 * Some "getter" for amount from session
226 * @param product Product instance
227 * @param session Session instance
228 * @return Amount as string
231 public String getAmountFromSession (final Product product, final HttpSession session) {
233 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
235 // Is product and session set?
236 if (product == null) {
238 throw new NullPointerException("product is null"); //NOI18N
239 } else if (session == null) {
241 throw new NullPointerException("session is null"); //NOI18N
245 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
247 // Is the object null?
248 if (object == null) {
250 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
257 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
259 // Cast to string and return it
260 return (String) object;
264 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
266 * @param product Product instance
267 * @param request Request instance
268 * @param session Session instance
269 * @return Whether the product is choosen
272 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
274 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
276 // Is product and session set?
277 if (product == null) {
279 throw new NullPointerException("product is null"); //NOI18N
280 } else if (request == null) {
282 throw new NullPointerException("request is null"); //NOI18N
283 } else if (session == null) {
285 throw new NullPointerException("session is null"); //NOI18N
288 // First let's check if the product is choosen
289 if (this.isProductChoosen(product, request, session)) {
291 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
294 return "checked=\"checked\""; //NOI18N
297 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
305 * Some "getter" for choose from session
307 * @param product Product instance
308 * @param session Session instance
309 * @return Choose as string
312 public String getChooseFromSession (final Product product, final HttpSession session) {
314 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
316 // Is product and session set?
317 if (product == null) {
319 throw new NullPointerException("product is null"); //NOI18N
320 } else if (session == null) {
322 throw new NullPointerException("session is null"); //NOI18N
326 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
328 // Is the object null?
329 if (object == null) {
331 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
336 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
338 // Cast to string and return it
339 return (String) object;
343 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
345 * @param request Request instance
346 * @param session Session instance
347 * @return Whether the product is choosen
350 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
352 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
354 // Is product and session set?
355 if (request == null) {
357 throw new NullPointerException("request is null"); //NOI18N
358 } else if (session == null) {
360 throw new NullPointerException("session is null"); //NOI18N
363 // Is something selected?
364 if (this.calculateTotalAmount(request, session) > 0) {
366 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
368 // Something has been choosen
372 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
374 // Nothing choosen yet
375 return "disabled=\"disabled\""; //NOI18N
380 * Some "getter" for choosen (checkbox) from session
382 * @param product Product instance
383 * @param request Request instance
384 * @param session Session instance
385 * @return Amount as string
388 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
390 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
392 // Is product and session set?
393 if (product == null) {
395 throw new NullPointerException("product is null"); //NOI18N
396 } else if (request == null) {
398 throw new NullPointerException("request is null"); //NOI18N
399 } else if (session == null) {
401 throw new NullPointerException("session is null"); //NOI18N
405 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
406 String choosen = this.handleChooseFromRequestSession(product, request, session);
407 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
410 assert(choosen instanceof String): "choosen is null"; //NOI18N
413 if (choosen.isEmpty()) {
419 String amount = this.handleAmountFromRequestSession(product, request, session);
420 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
423 assert(amount instanceof String): "amount is null"; //NOI18N
426 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
427 // Choosen, but no amount
436 * Some getter for printable value from session or an empty string for null.
438 * @param session Session instance
439 * @param key Key to get
440 * @return Value from key, empty string for null
443 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
445 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
447 // Are both parameter not null?
448 if (session == null) {
450 throw new NullPointerException("session is null"); //NOI18N
451 } else if (key == null) {
453 throw new NullPointerException("key is null"); //NOI18N
457 Object value = this.getValueFromSession(session, key);
460 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
463 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
465 // Return actual value
466 return this.convertNullToEmpty(value);
470 * Some "getter" for a an array of all products
472 * @return Unmarked products
475 public Product[] getProducts () {
476 return this.products.values().toArray(new Product[this.products.size()]);
480 * Some "getter" for total price of position from request or session.
481 * Single price and amount is multiplyed.
483 * @param product Product instance
484 * @param request Request instance
485 * @param session Session instance
486 * @return Amount as string
489 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
491 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
493 // Is product and session set?
494 if (product == null) {
496 throw new NullPointerException("product is null"); //NOI18N
497 } else if (request == null) {
499 throw new NullPointerException("request is null"); //NOI18N
500 } else if (session == null) {
502 throw new NullPointerException("session is null"); //NOI18N
506 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
507 String choosen = this.handleChooseFromRequestSession(product, request, session);
508 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
511 assert(choosen instanceof String): "choosen is null"; //NOI18N
514 if (choosen.isEmpty()) {
516 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
521 String amount = this.handleAmountFromRequestSession(product, request, session);
522 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
525 assert(amount instanceof String): "amount is null"; //NOI18N
528 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
530 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
535 Integer value = null;
539 // Get amount as integer
540 value = Integer.valueOf(amount);
541 } catch (final NumberFormatException e) {
543 throw new IllegalArgumentException(e);
547 float price = (product.getPrice() * value);
550 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
552 // Then multiply it with price
557 * Gets an array of products from product iterator and unmarks them as ordered
559 * @param session HttpSession instance
560 * @return Unmarked products
563 public Product[] getUnmarkedProducts (final HttpSession session) {
565 Product[] array = this.getProducts();
567 // Unmark are all as ordered
568 for (final Product product : array) {
569 this.unmarkProductAsOrdered(product, session);
572 // Return finished array
577 * Handler for amount from request or session
579 * @param product Product instance
580 * @param request Request instance
581 * @param session Session instance
582 * @return Amount as string
585 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
587 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
589 // Is product and session set?
590 if (product == null) {
592 throw new NullPointerException("product is null"); //NOI18N
593 } else if (request == null) {
595 throw new NullPointerException("request is null"); //NOI18N
596 } else if (session == null) {
598 throw new NullPointerException("session is null"); //NOI18N
604 // Check request method
605 if (!"POST".equals(request.getMethod())) { //NOI18N
606 // Not POST, so get from session
607 return this.getAmountFromSession(product, session);
608 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
610 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
611 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
615 // Get attribute from request
616 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
619 if (object instanceof String) {
620 // Try to parse it to integer
622 Integer value = Integer.valueOf((String) object);
623 } catch (final NumberFormatException ex) {
625 this.getLogger().warn(ex);
629 // Then set it in session
630 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
633 return (String) object;
637 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
639 // Get attribute from session
640 return this.getAmountFromSession(product, session);
644 * Checks whether the given product is choosen, request overules session.
646 * @param product Product instance
647 * @param request Request instance
648 * @param session Session instance
649 * @return Whether the product is choosen
652 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
654 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
656 // Is product and session set?
657 if (product == null) {
659 throw new NullPointerException("product is null"); //NOI18N
660 } else if (request == null) {
662 throw new NullPointerException("request is null"); //NOI18N
663 } else if (session == null) {
665 throw new NullPointerException("session is null"); //NOI18N
669 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
670 String choosen = this.handleChooseFromRequestSession(product, request, session);
671 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
674 assert(choosen instanceof String): "choosen is null"; //NOI18N
676 // Is it not choosen?
677 if (choosen.isEmpty()) {
683 String amount = this.handleAmountFromRequestSession(product, request, session);
686 assert(amount instanceof String): "amount is not set"; //NOI18N
689 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
691 // Must not be empty and not 0
692 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
696 * Marks given product as choosen in session
698 * @param product Product to mark as ordered
699 * @param session Session instance
702 public void markProductAsChoosen (final Product product, final HttpSession session) {
704 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
706 // Is product and session set?
707 if (product == null) {
709 throw new NullPointerException("product is null"); //NOI18N
710 } else if (session == null) {
712 throw new NullPointerException("session is null"); //NOI18N
715 // Mark it as ordered by setting flag
716 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
717 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
720 this.getLogger().trace("EXIT!"); //NOI18N
724 * Marks given product as ordered in session
726 * @param product Product to mark as ordered
727 * @param session Session instance
730 public void markProductAsOrdered (final Product product, final HttpSession session) {
732 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
734 // Is product and session set?
735 if (product == null) {
737 throw new NullPointerException("product is null"); //NOI18N
738 } else if (session == null) {
740 throw new NullPointerException("session is null"); //NOI18N
743 // Mark it as ordered by setting flag
744 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
745 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
748 this.getLogger().trace("EXIT!"); //NOI18N
752 * Somewhat setter in session
754 * @param session Session instance
755 * @param key Session key to set
756 * @param value Value to set
759 public void setValueInSession (final HttpSession session, final String key, final Object value) {
761 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
763 synchronized(session) {
765 session.setAttribute(key, value);
769 this.getLogger().trace("EXIT!"); //NOI18N
773 * Unmarks given product as choosen in session
775 * @param product Product to unmark as choosen
776 * @param session Session instance
779 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
781 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
783 // Is product and session set?
784 if (product == null) {
786 throw new NullPointerException("product is null"); //NOI18N
787 } else if (session == null) {
789 throw new NullPointerException("session is null"); //NOI18N
792 // Mark it as ordered by setting flag
793 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getName())); //NOI18N
794 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
797 this.getLogger().trace("EXIT!"); //NOI18N
801 * Unmarks given product as ordered in session
803 * @param product Product to unmark as ordered
804 * @param session Session instance
807 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
809 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
811 // Is product and session set?
812 if (product == null) {
814 throw new NullPointerException("product is null"); //NOI18N
815 } else if (session == null) {
817 throw new NullPointerException("session is null"); //NOI18N
820 // Mark it as ordered by setting flag
821 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
822 this.clearSessionAttribute(product, session, SESSION_ORDERED);
825 this.getLogger().trace("EXIT!"); //NOI18N
829 * Adds given product to list or throws an exception if name is already found
831 * @param name Internal name of product
832 * @param title Product's title
835 private void addProduct (final String name, final String title, final float price) {
837 this.getLogger().trace(MessageFormat.format("name={0},title={1},price={2} - CALLED!", name, title, price)); //NOI18N
839 // Is the name already used?
840 if (this.isProductNameUsed(name)) {
841 // Something went wrong
842 throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
846 Product product = new PizzaProduct(name, title, price);
849 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
852 this.products.put(product.getName(), product);
855 this.getLogger().trace("EXIT!"); //NOI18N
859 * Clears given parameter for product in session
861 * @param product Product instance
862 * @param session Session instance
863 * @param parameter Parameter to clear
865 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
867 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
870 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
871 this.setValueInSession(product, session, parameter, null);
874 this.getLogger().trace("EXIT!"); //NOI18N
878 * Fills products list
879 * @todo Very hard-coded stuff ...
881 private void fillProductsList () {
883 this.getLogger().trace("CALLED!"); //NOI18N
886 this.addProduct("italia", "Pizza Italia", 5.50f); //NOI18N
887 this.addProduct("diablo", "Pizza Diablo", 7.80f); //NOI18N
888 this.addProduct("bolognese", "Spagetti Bolognese", 11.95f); //NOI18N
891 this.getLogger().trace("EXIT!"); //NOI18N
895 * Some getter for value from session
897 * @param product Product instance
898 * @param session Session instance
899 * @param attribute Attribute to get value from
900 * @return Value from session
902 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
904 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
907 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getName()));
909 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
912 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
919 * Some getter for value from session
921 * @param session Session instance
922 * @param key Key to get value from
923 * @return Value from session
925 private Object getValueFromSession (final HttpSession session, final String key) {
927 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
932 // Get it synchronized from session
933 synchronized (session) {
934 value = session.getAttribute(key);
938 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
945 * Handler for choosen (checkbox) from request or session
947 * @param product Product instance
948 * @param request Request instance
949 * @param session Session instance
950 * @return Amount as string
952 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
954 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
956 // Is product and session set?
957 if (product == null) {
959 throw new NullPointerException("product is null"); //NOI18N
960 } else if (request == null) {
962 throw new NullPointerException("request is null"); //NOI18N
963 } else if (session == null) {
965 throw new NullPointerException("session is null"); //NOI18N
971 // Check request method
972 if (!"POST".equals(request.getMethod())) { //NOI18N
973 // Not POST, so get from session
974 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
975 return this.getChooseFromSession(product, session);
976 } else if (this.isProductOrdered(product, session)) {
977 // Product is ordered
978 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
979 return this.getChooseFromSession(product, session);
980 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
982 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
983 return this.getChooseFromSession(product, session);
986 // Get reqzest element
987 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
988 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
991 if (object == null) {
993 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
994 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
995 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
997 // Return empty string
1001 // Then set it in session
1002 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1004 // Cast to string and return it
1005 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
1006 return (String) object;
1010 * Checks whether given product is already used
1012 * @param name Name of product
1013 * @return Whether the given product's name is already used
1015 private boolean isProductNameUsed(final String name) {
1017 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
1020 return this.products.containsKey(name);
1024 * Checks if the product ordered?
1030 private boolean isProductOrdered(final Product product, final HttpSession session) {
1032 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1035 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1036 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
1039 return ("true".equals(isOrdered)); //NOI18N
1043 * Somewhat setter in session
1045 * @param product Product instance
1046 * @param session Session instance
1047 * @param keyPart Key part to include in final key
1048 * @param value Value to set
1050 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1052 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1055 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
1056 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getName()), value);
1059 this.getLogger().trace("EXIT!"); //NOI18N
1063 * Application starter
1065 private void start () {
1066 // "Walk" over all products
1067 for (final Product product : this.getProducts()) {
1069 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
1072 // Generate fake Customer instance
1073 Customer customer = new PizzaServiceCustomer();
1076 * Need a least a gender ... :( See, that is why I don't like default
1077 * constructors, you can easily miss something important and bam! You
1078 * get an NPE. The fix here is, to have construtors (or factories) which
1079 * requires all required instances that needs to be set to get a
1080 * consitent object back.
1083 // Gender is MALE now
1084 customer.setGender(Gender.MALE);
1086 // Get iterator on all its fields
1087 Iterator<Map.Entry<Field, Object>> it = customer.iterator();
1090 while (it.hasNext()) {
1091 Map.Entry<Field, Object> entry = it.next();
1092 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N