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
124 * @param context Servlet context
126 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
127 // Call other constructor first
130 // Initialize properties from config
131 this.initProperties(context);
133 // Load available products
134 this.fillProductsList();
138 * Calculates total amount of all choosen products
140 * @param request Request instance
141 * @param session Session instance
142 * @return Total amount of all choosen products
145 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) {
147 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
149 // Is product and session set?
150 if (request == null) {
152 throw new NullPointerException("request is null"); //NOI18N
153 } else if (session == null) {
155 throw new NullPointerException("session is null"); //NOI18N
161 // "Walk" over all products
162 for (final Product product : this.getProducts()) {
164 if (this.isProductChoosen(product, request, session)) {
165 // Then add ordered amount
166 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getName())); //NOI18N
169 String amount = this.getAmountFromSession(product, session);
172 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
173 totalAmount += Integer.valueOf(amount);
175 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getName(), totalAmount)); //NOI18N
179 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
181 // Return total price
186 * Calculates total price of all choosen products
188 * @param request Request instance
189 * @param session Session instance
190 * @return Total price of all choosen products
193 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) {
195 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
197 // Is product and session set?
198 if (request == null) {
200 throw new NullPointerException("request is null"); //NOI18N
201 } else if (session == null) {
203 throw new NullPointerException("session is null"); //NOI18N
207 float totalPrice = 0.00f;
209 // "Walk" over all products
210 for (final Product product : this.getProducts()) {
212 if (this.isProductChoosen(product, request, session)) {
213 // Then add product's total price
214 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getName())); //NOI18N
215 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
217 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getName(), totalPrice)); //NOI18N
221 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
223 // Return total price
228 public void doBootstrap () {
229 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
233 public void doMainLoop () {
234 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
238 public void doShutdown () {
239 throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
243 * Some "getter" for amount from session
245 * @param product Product instance
246 * @param session Session instance
247 * @return Amount as string
250 public String getAmountFromSession (final Product product, final HttpSession session) {
252 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
254 // Is product and session set?
255 if (product == null) {
257 throw new NullPointerException("product is null"); //NOI18N
258 } else if (session == null) {
260 throw new NullPointerException("session is null"); //NOI18N
264 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
266 // Is the object null?
267 if (object == null) {
269 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
276 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
278 // Cast to string and return it
279 return (String) object;
283 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
285 * @param product Product instance
286 * @param request Request instance
287 * @param session Session instance
288 * @return Whether the product is choosen
291 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
293 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
295 // Is product and session set?
296 if (product == null) {
298 throw new NullPointerException("product is null"); //NOI18N
299 } else if (request == null) {
301 throw new NullPointerException("request is null"); //NOI18N
302 } else if (session == null) {
304 throw new NullPointerException("session is null"); //NOI18N
307 // First let's check if the product is choosen
308 if (this.isProductChoosen(product, request, session)) {
310 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
313 return "checked=\"checked\""; //NOI18N
316 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
324 * Some "getter" for choose from session
326 * @param product Product instance
327 * @param session Session instance
328 * @return Choose as string
331 public String getChooseFromSession (final Product product, final HttpSession session) {
333 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
335 // Is product and session set?
336 if (product == null) {
338 throw new NullPointerException("product is null"); //NOI18N
339 } else if (session == null) {
341 throw new NullPointerException("session is null"); //NOI18N
345 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
347 // Is the object null?
348 if (object == null) {
350 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getName())); //NOI18N
355 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
357 // Cast to string and return it
358 return (String) object;
362 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
364 * @param request Request instance
365 * @param session Session instance
366 * @return Whether the product is choosen
369 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) {
371 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
373 // Is product and session set?
374 if (request == null) {
376 throw new NullPointerException("request is null"); //NOI18N
377 } else if (session == null) {
379 throw new NullPointerException("session is null"); //NOI18N
382 // Is something selected?
383 if (this.calculateTotalAmount(request, session) > 0) {
385 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
387 // Something has been choosen
391 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
393 // Nothing choosen yet
394 return "disabled=\"disabled\""; //NOI18N
399 * Some "getter" for choosen (checkbox) from session
401 * @param product Product instance
402 * @param request Request instance
403 * @param session Session instance
404 * @return Amount as string
407 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
409 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
411 // Is product and session set?
412 if (product == null) {
414 throw new NullPointerException("product is null"); //NOI18N
415 } else if (request == null) {
417 throw new NullPointerException("request is null"); //NOI18N
418 } else if (session == null) {
420 throw new NullPointerException("session is null"); //NOI18N
424 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
425 String choosen = this.handleChooseFromRequestSession(product, request, session);
426 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
429 assert(choosen instanceof String): "choosen is null"; //NOI18N
432 if (choosen.isEmpty()) {
438 String amount = this.handleAmountFromRequestSession(product, request, session);
439 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
442 assert(amount instanceof String): "amount is null"; //NOI18N
445 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
446 // Choosen, but no amount
455 * Some getter for printable value from session or an empty string for null.
457 * @param session Session instance
458 * @param key Key to get
459 * @return Value from key, empty string for null
462 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
464 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
466 // Are both parameter not null?
467 if (session == null) {
469 throw new NullPointerException("session is null"); //NOI18N
470 } else if (key == null) {
472 throw new NullPointerException("key is null"); //NOI18N
476 Object value = this.getValueFromSession(session, key);
479 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
482 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
484 // Return actual value
485 return this.convertNullToEmpty(value);
489 * Some "getter" for a an array of all products
491 * @return Unmarked products
494 public Product[] getProducts () {
495 return this.products.values().toArray(new Product[this.products.size()]);
499 * Some "getter" for total price of position from request or session.
500 * Single price and amount is multiplyed.
502 * @param product Product instance
503 * @param request Request instance
504 * @param session Session instance
505 * @return Amount as string
508 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
510 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
512 // Is product and session set?
513 if (product == null) {
515 throw new NullPointerException("product is null"); //NOI18N
516 } else if (request == null) {
518 throw new NullPointerException("request is null"); //NOI18N
519 } else if (session == null) {
521 throw new NullPointerException("session is null"); //NOI18N
525 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
526 String choosen = this.handleChooseFromRequestSession(product, request, session);
527 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
530 assert(choosen instanceof String): "choosen is null"; //NOI18N
533 if (choosen.isEmpty()) {
535 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getName(), choosen)); //NOI18N
540 String amount = this.handleAmountFromRequestSession(product, request, session);
541 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getName(), amount)); //NOI18N
544 assert(amount instanceof String): "amount is null"; //NOI18N
547 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
549 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getName(), amount)); //NOI18N
554 Integer value = null;
558 // Get amount as integer
559 value = Integer.valueOf(amount);
560 } catch (final NumberFormatException e) {
562 throw new IllegalArgumentException(e);
566 float price = (product.getPrice() * value);
569 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getName(), price)); //NOI18N
571 // Then multiply it with price
576 * Gets an array of products from product iterator and unmarks them as ordered
578 * @param session HttpSession instance
579 * @return Unmarked products
582 public Product[] getUnmarkedProducts (final HttpSession session) {
584 Product[] array = this.getProducts();
586 // Unmark are all as ordered
587 for (final Product product : array) {
588 this.unmarkProductAsOrdered(product, session);
591 // Return finished array
596 * Handler for amount from request or session
598 * @param product Product instance
599 * @param request Request instance
600 * @param session Session instance
601 * @return Amount as string
604 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
606 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
608 // Is product and session set?
609 if (product == null) {
611 throw new NullPointerException("product is null"); //NOI18N
612 } else if (request == null) {
614 throw new NullPointerException("request is null"); //NOI18N
615 } else if (session == null) {
617 throw new NullPointerException("session is null"); //NOI18N
623 // Check request method
624 if (!"POST".equals(request.getMethod())) { //NOI18N
625 // Not POST, so get from session
626 return this.getAmountFromSession(product, session);
627 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
629 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
630 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getName())); //NOI18N
634 // Get attribute from request
635 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getName()));
638 if (object instanceof String) {
639 // Try to parse it to integer
641 Integer value = Integer.valueOf((String) object);
642 } catch (final NumberFormatException ex) {
644 this.getLogger().warn(ex);
648 // Then set it in session
649 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
652 return (String) object;
656 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
658 // Get attribute from session
659 return this.getAmountFromSession(product, session);
663 * Checks whether the given product is choosen, request overules session.
665 * @param product Product instance
666 * @param request Request instance
667 * @param session Session instance
668 * @return Whether the product is choosen
671 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
673 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
675 // Is product and session set?
676 if (product == null) {
678 throw new NullPointerException("product is null"); //NOI18N
679 } else if (request == null) {
681 throw new NullPointerException("request is null"); //NOI18N
682 } else if (session == null) {
684 throw new NullPointerException("session is null"); //NOI18N
688 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getName(), request, session)); //NOI18N
689 String choosen = this.handleChooseFromRequestSession(product, request, session);
690 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getName(), choosen)); //NOI18N
693 assert(choosen instanceof String): "choosen is null"; //NOI18N
695 // Is it not choosen?
696 if (choosen.isEmpty()) {
702 String amount = this.handleAmountFromRequestSession(product, request, session);
705 assert(amount instanceof String): "amount is not set"; //NOI18N
708 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
710 // Must not be empty and not 0
711 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
715 * Marks given product as choosen in session
717 * @param product Product to mark as ordered
718 * @param session Session instance
721 public void markProductAsChoosen (final Product product, final HttpSession session) {
723 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
725 // Is product and session set?
726 if (product == null) {
728 throw new NullPointerException("product is null"); //NOI18N
729 } else if (session == null) {
731 throw new NullPointerException("session is null"); //NOI18N
734 // Mark it as ordered by setting flag
735 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getName())); //NOI18N
736 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
739 this.getLogger().trace("EXIT!"); //NOI18N
743 * Marks given product as ordered in session
745 * @param product Product to mark as ordered
746 * @param session Session instance
749 public void markProductAsOrdered (final Product product, final HttpSession session) {
751 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
753 // Is product and session set?
754 if (product == null) {
756 throw new NullPointerException("product is null"); //NOI18N
757 } else if (session == null) {
759 throw new NullPointerException("session is null"); //NOI18N
762 // Mark it as ordered by setting flag
763 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getName())); //NOI18N
764 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
767 this.getLogger().trace("EXIT!"); //NOI18N
771 * Somewhat setter in session
773 * @param session Session instance
774 * @param key Session key to set
775 * @param value Value to set
778 public void setValueInSession (final HttpSession session, final String key, final Object value) {
780 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
782 synchronized(session) {
784 session.setAttribute(key, value);
788 this.getLogger().trace("EXIT!"); //NOI18N
792 * Unmarks given product as choosen in session
794 * @param product Product to unmark as choosen
795 * @param session Session instance
798 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
800 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
802 // Is product and session set?
803 if (product == null) {
805 throw new NullPointerException("product is null"); //NOI18N
806 } else if (session == null) {
808 throw new NullPointerException("session is null"); //NOI18N
811 // Mark it as ordered by setting flag
812 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getName())); //NOI18N
813 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
816 this.getLogger().trace("EXIT!"); //NOI18N
820 * Unmarks given product as ordered in session
822 * @param product Product to unmark as ordered
823 * @param session Session instance
826 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
828 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
830 // Is product and session set?
831 if (product == null) {
833 throw new NullPointerException("product is null"); //NOI18N
834 } else if (session == null) {
836 throw new NullPointerException("session is null"); //NOI18N
839 // Mark it as ordered by setting flag
840 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getName())); //NOI18N
841 this.clearSessionAttribute(product, session, SESSION_ORDERED);
844 this.getLogger().trace("EXIT!"); //NOI18N
848 * Adds given product to list or throws an exception if name is already found
850 * @param product Product instance to add
852 private void addProduct (final Product product) {
854 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
856 // Get all data from it
857 String name = product.getName();
859 // Is the name already used?
860 if (this.isProductNameUsed(name)) {
861 // Something went wrong
862 throw new IllegalArgumentException(MessageFormat.format("product {0} is already used.", name)); //NOI18N
866 this.getLogger().debug(MessageFormat.format("Adding product={0} ...", product)); //NOI18N
869 this.products.put(product.getName(), product);
872 this.getLogger().trace("EXIT!"); //NOI18N
876 * Clears given parameter for product in session
878 * @param product Product instance
879 * @param session Session instance
880 * @param parameter Parameter to clear
882 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
884 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
887 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getName(), parameter)); //NOI18N
888 this.setValueInSession(product, session, parameter, null);
891 this.getLogger().trace("EXIT!"); //NOI18N
895 * Fills products list
896 * @todo Very hard-coded stuff ...
898 private void fillProductsList () throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
900 this.getLogger().trace("CALLED!"); //NOI18N
902 // Get a product frontend
903 ProductFrontend frontend = new PizzaProductDatabaseFrontend();
906 Iterator<Product> iterator = frontend.getProducts();
909 while (iterator.hasNext()) {
911 Product product = iterator.next();
914 this.addProduct(product);
918 this.getLogger().trace("EXIT!"); //NOI18N
922 * Some getter for value from session
924 * @param product Product instance
925 * @param session Session instance
926 * @param attribute Attribute to get value from
927 * @return Value from session
929 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
931 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
934 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getName()));
936 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getName(), attribute, value)); //NOI18N
939 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
946 * Some getter for value from session
948 * @param session Session instance
949 * @param key Key to get value from
950 * @return Value from session
952 private Object getValueFromSession (final HttpSession session, final String key) {
954 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
959 // Get it synchronized from session
960 synchronized (session) {
961 value = session.getAttribute(key);
965 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
972 * Handler for choosen (checkbox) from request or session
974 * @param product Product instance
975 * @param request Request instance
976 * @param session Session instance
977 * @return Amount as string
979 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
981 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
983 // Is product and session set?
984 if (product == null) {
986 throw new NullPointerException("product is null"); //NOI18N
987 } else if (request == null) {
989 throw new NullPointerException("request is null"); //NOI18N
990 } else if (session == null) {
992 throw new NullPointerException("session is null"); //NOI18N
998 // Check request method
999 if (!"POST".equals(request.getMethod())) { //NOI18N
1000 // Not POST, so get from session
1001 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1002 return this.getChooseFromSession(product, session);
1003 } else if (this.isProductOrdered(product, session)) {
1004 // Product is ordered
1005 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1006 return this.getChooseFromSession(product, session);
1007 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1009 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getName(), session));
1010 return this.getChooseFromSession(product, session);
1013 // Get reqzest element
1014 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getName()));
1015 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getName(), object)); //NOI18N
1018 if (object == null) {
1020 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getName())); //NOI18N
1021 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1022 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1024 // Return empty string
1028 // Then set it in session
1029 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1031 // Cast to string and return it
1032 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getName(), object)); //NOI18N
1033 return (String) object;
1037 * Checks whether given product is already used
1039 * @param name Name of product
1040 * @return Whether the given product's name is already used
1042 private boolean isProductNameUsed(final String name) {
1044 this.getLogger().trace(MessageFormat.format("name={0} - CALLED!", name)); //NOI18N
1047 return this.products.containsKey(name);
1051 * Checks if the product ordered?
1057 private boolean isProductOrdered(final Product product, final HttpSession session) {
1059 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1062 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1063 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getName(), isOrdered)); //NOI18N
1066 return ("true".equals(isOrdered)); //NOI18N
1070 * Somewhat setter in session
1072 * @param product Product instance
1073 * @param session Session instance
1074 * @param keyPart Key part to include in final key
1075 * @param value Value to set
1077 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1079 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1082 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getName(), keyPart)); //NOI18N
1083 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getName()), value);
1086 this.getLogger().trace("EXIT!"); //NOI18N
1090 * Application starter
1092 private void start () {
1098 this.initProperties();
1100 // Fill products list
1101 this.fillProductsList();
1102 } catch (final IOException | UnsupportedDatabaseBackendException | SQLException | BadTokenException ex) {
1104 this.abortProgramWithException(ex);
1107 // "Walk" over all products
1108 for (final Product product : this.getProducts()) {
1110 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getName(), product.getTitle(), product.getPrice())); //NOI18N
1113 // Generate fake Customer instance
1114 Customer customer = new PizzaServiceCustomer();
1117 * Need a least a gender ... :( See, that is why I don't like default
1118 * constructors, you can easily miss something important and bam! You
1119 * get an NPE. The fix here is, to have construtors (or factories) which
1120 * requires all required instances that needs to be set to get a
1121 * consitent object back.
1124 // Gender is MALE now
1125 customer.setGender(Gender.MALE);
1128 Iterator<Map.Entry<Field, Object>> it = null;
1131 // Get iterator on all its fields
1132 it = customer.iterator();
1133 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1134 this.abortProgramWithException(ex);
1138 while ((it instanceof Iterator) && (it.hasNext())) {
1139 Map.Entry<Field, Object> entry = it.next();
1140 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N