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 * Checks if given Product instance is available and returns a printable
461 * (human-readable) string.
463 * @param product Product instance to check
464 * @return Human-readable version of product availability
467 public String getPrintableProduktAvailability (final Product product) {
468 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product));
472 * Some getter for printable value from session or an empty string for null.
474 * @param session Session instance
475 * @param key Key to get
476 * @return Value from key, empty string for null
479 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
481 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
483 // Are both parameter not null?
484 if (session == null) {
486 throw new NullPointerException("session is null"); //NOI18N
487 } else if (key == null) {
489 throw new NullPointerException("key is null"); //NOI18N
493 Object value = this.getValueFromSession(session, key);
496 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
499 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
501 // Return actual value
502 return this.convertNullToEmpty(value);
506 * Some "getter" for a an array of all products
508 * @return Unmarked products
511 public Product[] getProducts () {
512 return this.products.values().toArray(new Product[this.products.size()]);
516 * Some "getter" for total price of position from request or session.
517 * Single price and amount is multiplyed.
519 * @param product Product instance
520 * @param request Request instance
521 * @param session Session instance
522 * @return Amount as string
525 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
527 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
529 // Is product and session set?
530 if (product == null) {
532 throw new NullPointerException("product is null"); //NOI18N
533 } else if (request == null) {
535 throw new NullPointerException("request is null"); //NOI18N
536 } else if (session == null) {
538 throw new NullPointerException("session is null"); //NOI18N
542 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
543 String choosen = this.handleChooseFromRequestSession(product, request, session);
544 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
547 assert(choosen instanceof String): "choosen is null"; //NOI18N
550 if (choosen.isEmpty()) {
552 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
557 String amount = this.handleAmountFromRequestSession(product, request, session);
558 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
561 assert(amount instanceof String): "amount is null"; //NOI18N
564 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
566 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
571 Integer value = null;
575 // Get amount as integer
576 value = Integer.valueOf(amount);
577 } catch (final NumberFormatException e) {
579 throw new IllegalArgumentException(e);
583 float price = (product.getPrice() * value);
586 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
588 // Then multiply it with price
593 * Gets an array of products from product iterator and unmarks them as ordered
595 * @param session HttpSession instance
596 * @return Unmarked products
599 public Product[] getUnmarkedProducts (final HttpSession session) {
601 Product[] array = this.getProducts();
603 // Unmark are all as ordered
604 for (final Product product : array) {
605 this.unmarkProductAsOrdered(product, session);
608 // Return finished array
613 * Handler for amount from request or session
615 * @param product Product instance
616 * @param request Request instance
617 * @param session Session instance
618 * @return Amount as string
621 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
623 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
625 // Is product and session set?
626 if (product == null) {
628 throw new NullPointerException("product is null"); //NOI18N
629 } else if (request == null) {
631 throw new NullPointerException("request is null"); //NOI18N
632 } else if (session == null) {
634 throw new NullPointerException("session is null"); //NOI18N
640 // Check request method
641 if (!"POST".equals(request.getMethod())) { //NOI18N
642 // Not POST, so get from session
643 return this.getAmountFromSession(product, session);
644 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
646 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
647 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
651 // Get attribute from request
652 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
655 if (object instanceof String) {
656 // Try to parse it to integer
658 Integer value = Integer.valueOf((String) object);
659 } catch (final NumberFormatException ex) {
661 this.getLogger().warn(ex);
665 // Then set it in session
666 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
669 return (String) object;
673 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
675 // Get attribute from session
676 return this.getAmountFromSession(product, session);
680 * Checks whether the given product is choosen, request overules session.
682 * @param product Product instance
683 * @param request Request instance
684 * @param session Session instance
685 * @return Whether the product is choosen
688 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
690 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
692 // Is product and session set?
693 if (product == null) {
695 throw new NullPointerException("product is null"); //NOI18N
696 } else if (request == null) {
698 throw new NullPointerException("request is null"); //NOI18N
699 } else if (session == null) {
701 throw new NullPointerException("session is null"); //NOI18N
705 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
706 String choosen = this.handleChooseFromRequestSession(product, request, session);
707 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
710 assert(choosen instanceof String): "choosen is null"; //NOI18N
712 // Is it not choosen?
713 if (choosen.isEmpty()) {
719 String amount = this.handleAmountFromRequestSession(product, request, session);
722 assert(amount instanceof String): "amount is not set"; //NOI18N
725 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
727 // Must not be empty and not 0
728 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
732 * Marks given product as choosen in session
734 * @param product Product to mark as ordered
735 * @param session Session instance
738 public void markProductAsChoosen (final Product product, final HttpSession session) {
740 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
742 // Is product and session set?
743 if (product == null) {
745 throw new NullPointerException("product is null"); //NOI18N
746 } else if (session == null) {
748 throw new NullPointerException("session is null"); //NOI18N
751 // Mark it as ordered by setting flag
752 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
753 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
756 this.getLogger().trace("EXIT!"); //NOI18N
760 * Marks given product as ordered in session
762 * @param product Product to mark as ordered
763 * @param session Session instance
766 public void markProductAsOrdered (final Product product, final HttpSession session) {
768 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
770 // Is product and session set?
771 if (product == null) {
773 throw new NullPointerException("product is null"); //NOI18N
774 } else if (session == null) {
776 throw new NullPointerException("session is null"); //NOI18N
779 // Mark it as ordered by setting flag
780 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
781 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
784 this.getLogger().trace("EXIT!"); //NOI18N
788 * Somewhat setter in session
790 * @param session Session instance
791 * @param key Session key to set
792 * @param value Value to set
795 public void setValueInSession (final HttpSession session, final String key, final Object value) {
797 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
799 synchronized(session) {
801 session.setAttribute(key, value);
805 this.getLogger().trace("EXIT!"); //NOI18N
809 * Unmarks given product as choosen in session
811 * @param product Product to unmark as choosen
812 * @param session Session instance
815 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
817 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
819 // Is product and session set?
820 if (product == null) {
822 throw new NullPointerException("product is null"); //NOI18N
823 } else if (session == null) {
825 throw new NullPointerException("session is null"); //NOI18N
828 // Mark it as ordered by setting flag
829 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getName())); //NOI18N
830 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
833 this.getLogger().trace("EXIT!"); //NOI18N
837 * Unmarks given product as ordered in session
839 * @param product Product to unmark as ordered
840 * @param session Session instance
843 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
845 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
847 // Is product and session set?
848 if (product == null) {
850 throw new NullPointerException("product is null"); //NOI18N
851 } else if (session == null) {
853 throw new NullPointerException("session is null"); //NOI18N
856 // Mark it as ordered by setting flag
857 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
858 this.clearSessionAttribute(product, session, SESSION_ORDERED);
861 this.getLogger().trace("EXIT!"); //NOI18N
865 * Adds given product to list or throws an exception if name is already found
867 * @param product Product instance to add
869 private void addProduct (final Product product) {
871 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
873 // Get all data from it
874 String name = product.getName();
876 // Is the name already used?
877 if (this.isProductNameUsed(name)) {
878 // Something went wrong
879 throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
883 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
886 this.products.put(product.getName(), product);
889 this.getLogger().trace("EXIT!"); //NOI18N
893 * Clears given parameter for product in session
895 * @param product Product instance
896 * @param session Session instance
897 * @param parameter Parameter to clear
899 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
901 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
904 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
905 this.setValueInSession(product, session, parameter, null);
908 this.getLogger().trace("EXIT!"); //NOI18N
912 * Fills products list
913 * @todo Very hard-coded stuff ...
915 private void fillProductsList () throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
917 this.getLogger().trace("CALLED!"); //NOI18N
919 // Get a product frontend
920 ProductFrontend frontend = new PizzaProductDatabaseFrontend();
923 Iterator<Product> iterator = frontend.getProducts();
926 while (iterator.hasNext()) {
928 Product product = iterator.next();
931 this.addProduct(product);
935 this.getLogger().trace("EXIT!"); //NOI18N
939 * Some getter for value from session
941 * @param product Product instance
942 * @param session Session instance
943 * @param attribute Attribute to get value from
944 * @return Value from session
946 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
948 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
951 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getName()));
953 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
956 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
963 * Some getter for value from session
965 * @param session Session instance
966 * @param key Key to get value from
967 * @return Value from session
969 private Object getValueFromSession (final HttpSession session, final String key) {
971 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
976 // Get it synchronized from session
977 synchronized (session) {
978 value = session.getAttribute(key);
982 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
989 * Handler for choosen (checkbox) from request or session
991 * @param product Product instance
992 * @param request Request instance
993 * @param session Session instance
994 * @return Amount as string
996 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
998 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1000 // Is product and session set?
1001 if (product == null) {
1003 throw new NullPointerException("product is null"); //NOI18N
1004 } else if (request == null) {
1006 throw new NullPointerException("request is null"); //NOI18N
1007 } else if (session == null) {
1009 throw new NullPointerException("session is null"); //NOI18N
1015 // Check request method
1016 if (!"POST".equals(request.getMethod())) { //NOI18N
1017 // Not POST, so get from session
1018 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1019 return this.getChooseFromSession(product, session);
1020 } else if (this.isProductOrdered(product, session)) {
1021 // Product is ordered
1022 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1023 return this.getChooseFromSession(product, session);
1024 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1026 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1027 return this.getChooseFromSession(product, session);
1030 // Get reqzest element
1031 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
1032 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
1035 if (object == null) {
1037 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
1038 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1039 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1041 // Return empty string
1045 // Then set it in session
1046 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1048 // Cast to string and return it
1049 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
1050 return (String) object;
1054 * Checks whether given product is already used
1056 * @param name Name of product
1057 * @return Whether the given product's name is already used
1059 private boolean isProductNameUsed(final String name) {
1061 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
1064 return this.products.containsKey(name);
1068 * Checks if the product ordered?
1074 private boolean isProductOrdered(final Product product, final HttpSession session) {
1076 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1079 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1080 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
1083 return ("true".equals(isOrdered)); //NOI18N
1087 * Somewhat setter in session
1089 * @param product Product instance
1090 * @param session Session instance
1091 * @param keyPart Key part to include in final key
1092 * @param value Value to set
1094 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1096 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1099 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
1100 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getName()), value);
1103 this.getLogger().trace("EXIT!"); //NOI18N
1107 * Application starter
1109 private void start () {
1115 this.initProperties();
1117 // Fill products list
1118 this.fillProductsList();
1119 } catch (final IOException | UnsupportedDatabaseBackendException | SQLException | BadTokenException ex) {
1121 this.abortProgramWithException(ex);
1124 // "Walk" over all products
1125 for (final Product product : this.getProducts()) {
1127 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
1130 // Generate fake Customer instance
1131 Customer customer = new PizzaServiceCustomer();
1134 * Need a least a gender ... :( See, that is why I don't like default
1135 * constructors, you can easily miss something important and bam! You
1136 * get an NPE. The fix here is, to have construtors (or factories) which
1137 * requires all required instances that needs to be set to get a
1138 * consitent object back.
1141 // Gender is MALE now
1142 customer.setGender(Gender.MALE);
1145 Iterator<Map.Entry<Field, Object>> it = null;
1148 // Get iterator on all its fields
1149 it = customer.iterator();
1150 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1151 this.abortProgramWithException(ex);
1155 while ((it instanceof Iterator) && (it.hasNext())) {
1156 Map.Entry<Field, Object> entry = it.next();
1157 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N