2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.application;
19 import java.io.IOException;
20 import java.lang.reflect.Field;
21 import java.lang.reflect.InvocationTargetException;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.contact.Gender;
31 import org.mxchange.jcore.exceptions.BadTokenException;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
33 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
34 import org.mxchange.pizzaapplication.category.Category;
35 import org.mxchange.pizzaapplication.customer.Customer;
36 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
37 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
38 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
39 import org.mxchange.pizzaapplication.product.Product;
43 * @author Roland Haeder
45 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
49 public static final String MAIN_TITLE = "Pizza-Service";
52 * Frontend for products
54 private ProductFrontend productFrontend;
57 * Some singleton getter for this instance. If the instance is not set in
58 * given application, it will be created.
60 * @param context Servlet context
61 * @return This instance
62 * @throws javax.servlet.ServletException If object is not set correctly
64 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
65 // Check application instance
66 if (context == null) {
68 throw new NullPointerException("application is null"); //NOI18N
72 PizzaApplication instance = null;
74 // Get instance from servlet application (aka. "application scope")
75 Object object = context.getAttribute("app"); //NOI18N
78 if (object instanceof PizzaApplication) {
79 // Instance is set, so casting should work
80 instance = (PizzaApplication) object;
81 } else if (object instanceof Object) {
82 // Not correct instance
83 throw new ServletException("app is not set correctly"); //NOI18N
86 // "service" is null, so initialize it
87 instance = new PizzaServiceApplication(context);
88 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
89 throw new ServletException(ex);
93 context.setAttribute("app", instance); //NOI18N
97 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
104 * For debugging purpose
106 * @param args Arguments
108 public static void main (String[] args) {
109 // Get instance and start it
110 new PizzaServiceApplication().start();
114 * Constructor with servet configuration
116 * @param context Servlet context
118 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
119 // Temporary initialize default bundle
120 // @TODO The JSF may have better internatialization support
123 // Initialize properties from config
124 this.initProperties(context);
126 // Init database frontends
127 this.initDatabaseFrontends();
131 * Default constructor
133 private PizzaServiceApplication () {
137 * Calculates total amount of all choosen products
139 * @param request Request instance
140 * @param session Session instance
141 * @return Total amount of all choosen products
144 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
146 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
148 // Is product and session set?
149 if (request == null) {
151 throw new NullPointerException("request is null"); //NOI18N
152 } else if (session == null) {
154 throw new NullPointerException("session is null"); //NOI18N
157 // Init/declare total price and iterator
159 Iterator<Product> iterator;
163 iterator = this.getProducts();
164 } catch (final IOException | BadTokenException ex) {
165 throw new ServletException(ex);
168 // "Walk" over all products
169 while (iterator.hasNext()) {
171 Product product = iterator.next();
174 if (this.isProductChoosen(product, request, session)) {
175 // Then add ordered amount
176 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
179 String amount = this.getAmountFromSession(product, session);
182 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
183 totalAmount += Integer.valueOf(amount);
185 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
189 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
191 // Return total price
196 * Calculates total price of all choosen products
198 * @param request Request instance
199 * @param session Session instance
200 * @return Total price of all choosen products
203 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
205 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
207 // Is product and session set?
208 if (request == null) {
210 throw new NullPointerException("request is null"); //NOI18N
211 } else if (session == null) {
213 throw new NullPointerException("session is null"); //NOI18N
217 float totalPrice = 0.00f;
220 Iterator<Product> iterator;
222 iterator = this.getProducts();
223 } catch (final IOException | BadTokenException ex) {
224 throw new ServletException(ex);
227 // "Walk" over all products
228 while (iterator.hasNext()) {
230 Product product = iterator.next();
233 if (this.isProductChoosen(product, request, session)) {
234 // Then add product's total price
235 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
236 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
238 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
242 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
244 // Return total price
249 public void doBootstrap () {
250 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
254 public void doMainLoop () {
255 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
259 public void doShutdown () {
260 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
264 * Some "getter" for amount from session
266 * @param product Product instance
267 * @param session Session instance
268 * @return Amount as string
271 public String getAmountFromSession (final Product product, final HttpSession session) {
273 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
275 // Is product and session set?
276 if (product == null) {
278 throw new NullPointerException("product is null"); //NOI18N
279 } else if (session == null) {
281 throw new NullPointerException("session is null"); //NOI18N
285 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
287 // Is the object null?
288 if (object == null) {
290 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
297 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
299 // Cast to string and return it
300 return (String) object;
304 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
306 * @param product Product instance
307 * @param request Request instance
308 * @param session Session instance
309 * @return Whether the product is choosen
312 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
314 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
316 // Is product and session set?
317 if (product == null) {
319 throw new NullPointerException("product is null"); //NOI18N
320 } else if (request == null) {
322 throw new NullPointerException("request is null"); //NOI18N
323 } else if (session == null) {
325 throw new NullPointerException("session is null"); //NOI18N
328 // First let's check if the product is choosen
329 if (this.isProductChoosen(product, request, session)) {
331 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
334 return "checked=\"checked\""; //NOI18N
337 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
345 * Some "getter" for choose from session
347 * @param product Product instance
348 * @param session Session instance
349 * @return Choose as string
352 public String getChooseFromSession (final Product product, final HttpSession session) {
354 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
356 // Is product and session set?
357 if (product == null) {
359 throw new NullPointerException("product is null"); //NOI18N
360 } else if (session == null) {
362 throw new NullPointerException("session is null"); //NOI18N
366 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
368 // Is the object null?
369 if (object == null) {
371 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
376 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
378 // Cast to string and return it
379 return (String) object;
383 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
385 * @param request Request instance
386 * @param session Session instance
387 * @return Whether the product is choosen
390 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
392 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
394 // Is product and session set?
395 if (request == null) {
397 throw new NullPointerException("request is null"); //NOI18N
398 } else if (session == null) {
400 throw new NullPointerException("session is null"); //NOI18N
403 // Is something selected?
404 if (this.calculateTotalAmount(request, session) > 0) {
406 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
408 // Something has been choosen
412 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
414 // Nothing choosen yet
415 return "disabled=\"disabled\""; //NOI18N
420 * Some "getter" for choosen (checkbox) from session
422 * @param product Product instance
423 * @param request Request instance
424 * @param session Session instance
425 * @return Amount as string
428 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
430 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
432 // Is product and session set?
433 if (product == null) {
435 throw new NullPointerException("product is null"); //NOI18N
436 } else if (request == null) {
438 throw new NullPointerException("request is null"); //NOI18N
439 } else if (session == null) {
441 throw new NullPointerException("session is null"); //NOI18N
445 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
446 String choosen = this.handleChooseFromRequestSession(product, request, session);
447 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
450 assert(choosen instanceof String): "choosen is null"; //NOI18N
453 if (choosen.isEmpty()) {
459 String amount = this.handleAmountFromRequestSession(product, request, session);
460 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
463 assert(amount instanceof String): "amount is null"; //NOI18N
466 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
467 // Choosen, but no amount
476 * Checks if given Product instance is available and returns a printable
477 * (human-readable) string.
479 * @param product Product instance to check
480 * @return Human-readable version of product availability
483 public String getPrintableProduktAvailability (final Product product) {
484 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product));
488 * Some getter for printable value from session or an empty string for null.
490 * @param session Session instance
491 * @param key Key to get
492 * @return Value from key, empty string for null
495 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
497 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
499 // Are both parameter not null?
500 if (session == null) {
502 throw new NullPointerException("session is null"); //NOI18N
503 } else if (key == null) {
505 throw new NullPointerException("key is null"); //NOI18N
509 Object value = this.getValueFromSession(session, key);
512 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
515 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
517 // Return actual value
518 return this.convertNullToEmpty(value);
522 * Some "getter" for a an array of all products
524 * @return All products
527 public Iterator<Product> getProducts () throws IOException, BadTokenException {
528 // Ask frontend for a list of products
529 return this.productFrontend.getProducts();
533 * Some "getter" for a an array of all categories
535 * @return All categories
538 public Iterator<Category> getCategories () {
539 throw new UnsupportedOperationException("Needs refacturing ...");
543 * Some "getter" for total price of position from request or session.
544 * Single price and amount is multiplyed.
546 * @param product Product instance
547 * @param request Request instance
548 * @param session Session instance
549 * @return Amount as string
552 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
554 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
556 // Is product and session set?
557 if (product == null) {
559 throw new NullPointerException("product is null"); //NOI18N
560 } else if (request == null) {
562 throw new NullPointerException("request is null"); //NOI18N
563 } else if (session == null) {
565 throw new NullPointerException("session is null"); //NOI18N
569 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
570 String choosen = this.handleChooseFromRequestSession(product, request, session);
571 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
574 assert(choosen instanceof String): "choosen is null"; //NOI18N
577 if (choosen.isEmpty()) {
579 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
584 String amount = this.handleAmountFromRequestSession(product, request, session);
585 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
588 assert(amount instanceof String): "amount is null"; //NOI18N
591 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
593 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
598 Integer value = null;
602 // Get amount as integer
603 value = Integer.valueOf(amount);
604 } catch (final NumberFormatException e) {
606 throw new IllegalArgumentException(e);
610 float price = (product.getPrice() * value);
613 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
615 // Then multiply it with price
620 * Handler for amount from request or session
622 * @param product Product instance
623 * @param request Request instance
624 * @param session Session instance
625 * @return Amount as string
628 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
630 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
632 // Is product and session set?
633 if (product == null) {
635 throw new NullPointerException("product is null"); //NOI18N
636 } else if (request == null) {
638 throw new NullPointerException("request is null"); //NOI18N
639 } else if (session == null) {
641 throw new NullPointerException("session is null"); //NOI18N
647 // Check request method
648 if (!"POST".equals(request.getMethod())) { //NOI18N
649 // Not POST, so get from session
650 return this.getAmountFromSession(product, session);
651 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
653 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
654 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
658 // Get attribute from request
659 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
662 if (object instanceof String) {
663 // Try to parse it to integer
665 Integer value = Integer.valueOf((String) object);
666 } catch (final NumberFormatException ex) {
668 this.getLogger().warn(ex);
672 // Then set it in session
673 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
676 return (String) object;
680 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
682 // Get attribute from session
683 return this.getAmountFromSession(product, session);
687 * Checks whether the given product is choosen, request overules session.
689 * @param product Product instance
690 * @param request Request instance
691 * @param session Session instance
692 * @return Whether the product is choosen
695 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
697 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
699 // Is product and session set?
700 if (product == null) {
702 throw new NullPointerException("product is null"); //NOI18N
703 } else if (request == null) {
705 throw new NullPointerException("request is null"); //NOI18N
706 } else if (session == null) {
708 throw new NullPointerException("session is null"); //NOI18N
712 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
713 String choosen = this.handleChooseFromRequestSession(product, request, session);
714 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
717 assert(choosen instanceof String): "choosen is null"; //NOI18N
719 // Is it not choosen?
720 if (choosen.isEmpty()) {
726 String amount = this.handleAmountFromRequestSession(product, request, session);
729 assert(amount instanceof String): "amount is not set"; //NOI18N
732 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
734 // Must not be empty and not 0
735 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
739 * Marks given product as choosen in session
741 * @param product Product to mark as ordered
742 * @param session Session instance
745 public void markProductAsChoosen (final Product product, final HttpSession session) {
747 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
749 // Is product and session set?
750 if (product == null) {
752 throw new NullPointerException("product is null"); //NOI18N
753 } else if (session == null) {
755 throw new NullPointerException("session is null"); //NOI18N
758 // Mark it as ordered by setting flag
759 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
760 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
763 this.getLogger().trace("EXIT!"); //NOI18N
767 * Marks given product as ordered in session
769 * @param product Product to mark as ordered
770 * @param session Session instance
773 public void markProductAsOrdered (final Product product, final HttpSession session) {
775 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
777 // Is product and session set?
778 if (product == null) {
780 throw new NullPointerException("product is null"); //NOI18N
781 } else if (session == null) {
783 throw new NullPointerException("session is null"); //NOI18N
786 // Mark it as ordered by setting flag
787 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
788 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
791 this.getLogger().trace("EXIT!"); //NOI18N
795 * Somewhat setter in session
797 * @param session Session instance
798 * @param key Session key to set
799 * @param value Value to set
802 public void setValueInSession (final HttpSession session, final String key, final Object value) {
804 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
806 synchronized(session) {
808 session.setAttribute(key, value);
812 this.getLogger().trace("EXIT!"); //NOI18N
816 * Unmarks given product as choosen in session
818 * @param product Product to unmark as choosen
819 * @param session Session instance
822 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
824 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
826 // Is product and session set?
827 if (product == null) {
829 throw new NullPointerException("product is null"); //NOI18N
830 } else if (session == null) {
832 throw new NullPointerException("session is null"); //NOI18N
835 // Mark it as ordered by setting flag
836 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
837 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
840 this.getLogger().trace("EXIT!"); //NOI18N
844 * Unmarks given product as ordered in session
846 * @param product Product to unmark as ordered
847 * @param session Session instance
850 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
852 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
854 // Is product and session set?
855 if (product == null) {
857 throw new NullPointerException("product is null"); //NOI18N
858 } else if (session == null) {
860 throw new NullPointerException("session is null"); //NOI18N
863 // Mark it as ordered by setting flag
864 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
865 this.clearSessionAttribute(product, session, SESSION_ORDERED);
868 this.getLogger().trace("EXIT!"); //NOI18N
872 * Clears given parameter for product in session
874 * @param product Product instance
875 * @param session Session instance
876 * @param parameter Parameter to clear
878 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
880 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
883 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
884 this.setValueInSession(product, session, parameter, null);
887 this.getLogger().trace("EXIT!"); //NOI18N
891 * Some getter for value from session
893 * @param product Product instance
894 * @param session Session instance
895 * @param attribute Attribute to get value from
896 * @return Value from session
898 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
900 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
903 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
905 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
908 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
915 * Some getter for value from session
917 * @param session Session instance
918 * @param key Key to get value from
919 * @return Value from session
921 private Object getValueFromSession (final HttpSession session, final String key) {
923 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
928 // Get it synchronized from session
929 synchronized (session) {
930 value = session.getAttribute(key);
934 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
941 * Handler for choosen (checkbox) from request or session
943 * @param product Product instance
944 * @param request Request instance
945 * @param session Session instance
946 * @return Amount as string
948 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
950 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
952 // Is product and session set?
953 if (product == null) {
955 throw new NullPointerException("product is null"); //NOI18N
956 } else if (request == null) {
958 throw new NullPointerException("request is null"); //NOI18N
959 } else if (session == null) {
961 throw new NullPointerException("session is null"); //NOI18N
967 // Check request method
968 if (!"POST".equals(request.getMethod())) { //NOI18N
969 // Not POST, so get from session
970 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
971 return this.getChooseFromSession(product, session);
972 } else if (this.isProductOrdered(product, session)) {
973 // Product is ordered
974 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
975 return this.getChooseFromSession(product, session);
976 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
978 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session));
979 return this.getChooseFromSession(product, session);
982 // Get reqzest element
983 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
984 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
987 if (object == null) {
989 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
990 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
991 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
993 // Return empty string
997 // Then set it in session
998 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1000 // Cast to string and return it
1001 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1002 return (String) object;
1006 * Initializes database frontends.
1008 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1009 this.productFrontend = new PizzaProductDatabaseFrontend();
1013 * Checks if the product ordered?
1015 * @param product Product instance
1016 * @param session HttpSession instance
1019 private boolean isProductOrdered(final Product product, final HttpSession session) {
1021 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1024 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1025 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1028 return ("true".equals(isOrdered)); //NOI18N
1032 * Somewhat setter in session
1034 * @param product Product instance
1035 * @param session Session instance
1036 * @param keyPart Key part to include in final key
1037 * @param value Value to set
1039 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1041 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1044 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1045 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1048 this.getLogger().trace("EXIT!"); //NOI18N
1052 * Application starter
1054 private void start () {
1060 this.initProperties();
1061 } catch (final IOException ex) {
1063 this.abortProgramWithException(ex);
1067 Iterator<Product> iterator = null;
1071 iterator = this.getProducts();
1072 } catch (final IOException | BadTokenException ex) {
1073 this.abortProgramWithException(ex);
1076 // "Walk" over all products
1077 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1079 Product product = iterator.next();
1082 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1085 // Generate fake Customer instance
1086 Customer customer = new PizzaServiceCustomer();
1089 * Need a least a gender ... :( See, that is why I don't like default
1090 * constructors, you can easily miss something important and bam! You
1091 * get an NPE. The fix here is, to have construtors (or factories) which
1092 * requires all required instances that needs to be set to get a
1093 * consitent object back.
1096 // Gender is MALE now
1097 customer.setGender(Gender.MALE);
1100 Iterator<Map.Entry<Field, Object>> it = null;
1103 // Get iterator on all its fields
1104 it = customer.iterator();
1105 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1106 this.abortProgramWithException(ex);
1110 while ((it instanceof Iterator) && (it.hasNext())) {
1111 Map.Entry<Field, Object> entry = it.next();
1112 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N