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 org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
20 import java.io.IOException;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.InvocationTargetException;
23 import java.sql.SQLException;
24 import java.text.MessageFormat;
25 import java.util.Iterator;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpSession;
31 import org.mxchange.jcore.contact.Gender;
32 import org.mxchange.jcore.exceptions.BadTokenException;
33 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
34 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
35 import org.mxchange.pizzaapplication.category.Category;
36 import org.mxchange.pizzaapplication.customer.Customer;
37 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
38 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
39 import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend;
40 import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
41 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
42 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
43 import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
44 import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
45 import org.mxchange.pizzaapplication.product.Product;
48 * Main application class
50 * @author Roland Haeder
52 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
56 public static final String MAIN_TITLE = "Pizza-Service";
59 * Frontend for products
61 private ProductFrontend productFrontend;
64 * Frontend for categories
66 private CategoryFrontend categoryFrontend;
69 * Some singleton getter for this instance. If the instance is not set in
70 * given application, it will be created.
72 * @param context Servlet context
73 * @return This instance
74 * @throws javax.servlet.ServletException If object is not set correctly
76 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
77 // Check application instance
78 if (context == null) {
80 throw new NullPointerException("application is null"); //NOI18N
84 PizzaApplication instance = null;
86 // Get instance from servlet application (aka. "application scope")
87 Object object = context.getAttribute("app"); //NOI18N
90 if (object instanceof PizzaApplication) {
91 // Instance is set, so casting should work
92 instance = (PizzaApplication) object;
93 } else if (object instanceof Object) {
94 // Not correct instance
95 throw new ServletException("app is not set correctly"); //NOI18N
98 // "service" is null, so initialize it
99 instance = new PizzaServiceApplication(context);
100 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
101 throw new ServletException(ex);
105 context.setAttribute("app", instance); //NOI18N
109 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
116 * For debugging purpose
118 * @param args Arguments
120 public static void main (String[] args) {
121 // Get instance and start it
122 new PizzaServiceApplication().start();
126 * Constructor with servet configuration
128 * @param context Servlet context
130 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
131 // Temporary initialize default bundle
132 // @TODO The JSF may have better internatialization support
135 // Initialize properties from config
136 this.initProperties(context);
138 // Init database frontends
139 this.initDatabaseFrontends();
143 * Default constructor
145 private PizzaServiceApplication () {
149 * Calculates total amount of all choosen products
151 * @param request Request instance
152 * @param session Session instance
153 * @return Total amount of all choosen products
156 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
158 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
160 // Is product and session set?
161 if (request == null) {
163 throw new NullPointerException("request is null"); //NOI18N
164 } else if (session == null) {
166 throw new NullPointerException("session is null"); //NOI18N
169 // Init/declare total price and iterator
171 Iterator<Product> iterator = this.getAvailableProducts();
173 // "Walk" over all products
174 while (iterator.hasNext()) {
176 Product product = iterator.next();
179 if (this.isProductChoosen(product, request, session)) {
180 // Then add ordered amount
181 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
184 String amount = this.getAmountFromSession(product, session);
187 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
188 totalAmount += Integer.valueOf(amount);
190 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
194 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
196 // Return total price
201 * Calculates total price of all choosen products
203 * @param request Request instance
204 * @param session Session instance
205 * @return Total price of all choosen products
208 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
210 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
212 // Is product and session set?
213 if (request == null) {
215 throw new NullPointerException("request is null"); //NOI18N
216 } else if (session == null) {
218 throw new NullPointerException("session is null"); //NOI18N
222 float totalPrice = 0.00f;
225 Iterator<Product> iterator = this.getAvailableProducts();
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."); //NOI18N
254 public void doMainLoop () {
255 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
259 public void doShutdown () {
260 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
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) {
485 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product));
488 if (product == null) {
489 // Should not be null
490 throw new NullPointerException("product is null");
494 if (product.getAvailable() == true) {
498 // Not, not for public
504 * Some getter for printable value from session or an empty string for null.
506 * @param session Session instance
507 * @param key Key to get
508 * @return Value from key, empty string for null
511 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
513 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
515 // Are both parameter not null?
516 if (session == null) {
518 throw new NullPointerException("session is null"); //NOI18N
519 } else if (key == null) {
521 throw new NullPointerException("key is null"); //NOI18N
525 Object value = this.getValueFromSession(session, key);
528 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
531 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
533 // Return actual value
534 return this.convertNullToEmpty(value);
538 * Some "getter" for a an array of only available products
540 * @return All products
543 public Iterator<Product> getAvailableProducts () throws ServletException {
545 // Ask frontend for a list of products
546 return this.productFrontend.getAvailableProducts();
547 } catch (final IOException | BadTokenException | SQLException ex) {
548 throw new ServletException(ex);
553 * Some "getter" for a an array of all products
555 * @return All products
558 public Iterator<Product> getAllProducts () throws ServletException {
560 // Ask frontend for a list of products
561 return this.productFrontend.getAllProducts();
562 } catch (final IOException | BadTokenException | SQLException ex) {
563 throw new ServletException(ex);
568 * Some "getter" for a an array of all categories
570 * @return All categories
573 public Iterator<Category> getCategories () throws ServletException {
575 // Ask frontend for a list of categories
576 return this.categoryFrontend.getCategories();
577 } catch (final IOException | BadTokenException | SQLException ex) {
578 throw new ServletException(ex);
583 * Some "getter" for total price of position from request or session.
584 * Single price and amount is multiplyed.
586 * @param product Product instance
587 * @param request Request instance
588 * @param session Session instance
589 * @return Amount as string
592 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
594 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
596 // Is product and session set?
597 if (product == null) {
599 throw new NullPointerException("product is null"); //NOI18N
600 } else if (request == null) {
602 throw new NullPointerException("request is null"); //NOI18N
603 } else if (session == null) {
605 throw new NullPointerException("session is null"); //NOI18N
609 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
610 String choosen = this.handleChooseFromRequestSession(product, request, session);
611 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
614 assert(choosen instanceof String): "choosen is null"; //NOI18N
617 if (choosen.isEmpty()) {
619 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
624 String amount = this.handleAmountFromRequestSession(product, request, session);
625 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
628 assert(amount instanceof String): "amount is null"; //NOI18N
631 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
633 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
638 Integer value = null;
642 // Get amount as integer
643 value = Integer.valueOf(amount);
644 } catch (final NumberFormatException e) {
646 throw new IllegalArgumentException(e);
650 float price = (product.getPrice() * value);
653 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
655 // Then multiply it with price
660 * Handler for amount from request or session
662 * @param product Product instance
663 * @param request Request instance
664 * @param session Session instance
665 * @return Amount as string
668 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
670 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
672 // Is product and session set?
673 if (product == null) {
675 throw new NullPointerException("product is null"); //NOI18N
676 } else if (request == null) {
678 throw new NullPointerException("request is null"); //NOI18N
679 } else if (session == null) {
681 throw new NullPointerException("session is null"); //NOI18N
687 // Check request method
688 if (!"POST".equals(request.getMethod())) { //NOI18N
689 // Not POST, so get from session
690 return this.getAmountFromSession(product, session);
691 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
693 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
694 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
698 // Get attribute from request
699 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
702 if (object instanceof String) {
703 // Try to parse it to integer
705 Integer value = Integer.valueOf((String) object);
706 } catch (final NumberFormatException ex) {
708 this.getLogger().warn(ex);
712 // Then set it in session
713 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
716 return (String) object;
720 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
722 // Get attribute from session
723 return this.getAmountFromSession(product, session);
727 * Checks whether the given product is choosen, request overules session.
729 * @param product Product instance
730 * @param request Request instance
731 * @param session Session instance
732 * @return Whether the product is choosen
735 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
737 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
739 // Is product and session set?
740 if (product == null) {
742 throw new NullPointerException("product is null"); //NOI18N
743 } else if (request == null) {
745 throw new NullPointerException("request is null"); //NOI18N
746 } else if (session == null) {
748 throw new NullPointerException("session is null"); //NOI18N
752 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
753 String choosen = this.handleChooseFromRequestSession(product, request, session);
754 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
757 assert(choosen instanceof String): "choosen is null"; //NOI18N
759 // Is it not choosen?
760 if (choosen.isEmpty()) {
766 String amount = this.handleAmountFromRequestSession(product, request, session);
769 assert(amount instanceof String): "amount is not set"; //NOI18N
772 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
774 // Must not be empty and not 0
775 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
779 * Marks all choosen products as ordered
781 * @param request Request instance
782 * @param session Session instance
785 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
787 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
790 Iterator<Product> iterator = this.getAvailableProducts();
792 // "Walk" over all products
793 while (iterator.hasNext()) {
795 Product product = iterator.next();
798 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
801 if (this.isProductChoosen(product, request, session)) {
802 // Mark product as ordered
803 this.markProductAsOrdered(product, session);
808 this.getLogger().trace("EXIT!"); //NOI18N
812 * Marks given product as choosen in session
814 * @param product Product to mark as ordered
815 * @param session Session instance
818 public void markProductAsChoosen (final Product product, final HttpSession session) {
820 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
822 // Is product and session set?
823 if (product == null) {
825 throw new NullPointerException("product is null"); //NOI18N
826 } else if (session == null) {
828 throw new NullPointerException("session is null"); //NOI18N
831 // Mark it as ordered by setting flag
832 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
833 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
836 this.getLogger().trace("EXIT!"); //NOI18N
840 * Marks given product as ordered in session
842 * @param product Product to mark as ordered
843 * @param session Session instance
846 public void markProductAsOrdered (final Product product, final HttpSession session) {
848 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
850 // Is product and session set?
851 if (product == null) {
853 throw new NullPointerException("product is null"); //NOI18N
854 } else if (session == null) {
856 throw new NullPointerException("session is null"); //NOI18N
859 // Mark it as ordered by setting flag
860 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
861 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
864 this.getLogger().trace("EXIT!"); //NOI18N
868 * Somewhat setter in session
870 * @param session Session instance
871 * @param key Session key to set
872 * @param value Value to set
875 public void setValueInSession (final HttpSession session, final String key, final Object value) {
877 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
879 synchronized(session) {
881 session.setAttribute(key, value);
885 this.getLogger().trace("EXIT!"); //NOI18N
889 * Unmarks given product as choosen in session
891 * @param product Product to unmark as choosen
892 * @param session Session instance
895 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
897 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
899 // Is product and session set?
900 if (product == null) {
902 throw new NullPointerException("product is null"); //NOI18N
903 } else if (session == null) {
905 throw new NullPointerException("session is null"); //NOI18N
908 // Mark it as ordered by setting flag
909 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
910 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
913 this.getLogger().trace("EXIT!"); //NOI18N
917 * Unmarks given product as ordered in session
919 * @param product Product to unmark as ordered
920 * @param session Session instance
923 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
925 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
927 // Is product and session set?
928 if (product == null) {
930 throw new NullPointerException("product is null"); //NOI18N
931 } else if (session == null) {
933 throw new NullPointerException("session is null"); //NOI18N
936 // Mark it as ordered by setting flag
937 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
938 this.clearSessionAttribute(product, session, SESSION_ORDERED);
941 this.getLogger().trace("EXIT!"); //NOI18N
945 * Clears given parameter for product in session
947 * @param product Product instance
948 * @param session Session instance
949 * @param parameter Parameter to clear
951 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
953 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
956 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
957 this.setValueInSession(product, session, parameter, null);
960 this.getLogger().trace("EXIT!"); //NOI18N
964 * Some getter for value from session
966 * @param product Product instance
967 * @param session Session instance
968 * @param attribute Attribute to get value from
969 * @return Value from session
971 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
973 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
976 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
978 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
981 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
988 * Some getter for value from session
990 * @param session Session instance
991 * @param key Key to get value from
992 * @return Value from session
994 private Object getValueFromSession (final HttpSession session, final String key) {
996 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
1001 // Get it synchronized from session
1002 synchronized (session) {
1003 value = session.getAttribute(key);
1007 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1014 * Handler for choosen (checkbox) from request or session
1016 * @param product Product instance
1017 * @param request Request instance
1018 * @param session Session instance
1019 * @return Amount as string
1021 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
1023 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1025 // Is product and session set?
1026 if (product == null) {
1028 throw new NullPointerException("product is null"); //NOI18N
1029 } else if (request == null) {
1031 throw new NullPointerException("request is null"); //NOI18N
1032 } else if (session == null) {
1034 throw new NullPointerException("session is null"); //NOI18N
1040 // Check request method
1041 if (!"POST".equals(request.getMethod())) { //NOI18N
1042 // Not POST, so get from session
1043 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1044 return this.getChooseFromSession(product, session);
1045 } else if (this.isProductOrdered(product, session)) {
1046 // Product is ordered
1047 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1048 return this.getChooseFromSession(product, session);
1049 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1051 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1052 return this.getChooseFromSession(product, session);
1055 // Get reqzest element
1056 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
1057 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1060 if (object == null) {
1062 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1063 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1064 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1066 // Return empty string
1070 // Then set it in session
1071 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1073 // Cast to string and return it
1074 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1075 return (String) object;
1079 * Initializes database frontends.
1081 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1083 this.productFrontend = new PizzaProductDatabaseFrontend();
1085 // Category frontend
1086 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1090 * Checks whether given category title is already used
1092 * @param title Title of category to check
1093 * @return Whether it has been found
1095 private boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
1096 // Delegate to frontend
1097 return this.categoryFrontend.isCategoryTitleUsed(title);
1101 * Checks if given product title is already used
1102 * @param title Product title to check
1103 * @return Whether the product title has already been used
1105 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
1106 // Delegate to frontend
1107 return this.productFrontend.isProductTitleUsed(title);
1111 * Checks if the product ordered?
1113 * @param product Product instance
1114 * @param session HttpSession instance
1117 private boolean isProductOrdered(final Product product, final HttpSession session) {
1119 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1122 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1123 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1126 return ("true".equals(isOrdered)); //NOI18N
1130 * Somewhat setter in session
1132 * @param product Product instance
1133 * @param session Session instance
1134 * @param keyPart Key part to include in final key
1135 * @param value Value to set
1137 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1139 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1142 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1143 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1146 this.getLogger().trace("EXIT!"); //NOI18N
1150 * Application starter
1152 private void start () {
1158 this.initProperties();
1159 } catch (final IOException ex) {
1161 this.abortProgramWithException(ex);
1165 Iterator<Product> iterator = null;
1169 iterator = this.getAvailableProducts();
1170 } catch (final ServletException ex) {
1171 this.abortProgramWithException(ex);
1174 // "Walk" over all products
1175 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1177 Product product = iterator.next();
1180 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1183 // Generate fake Customer instance
1184 Customer customer = new PizzaServiceCustomer();
1187 * Need a least a gender ... :( See, that is why I don't like default
1188 * constructors, you can easily miss something important and bam! You
1189 * get an NPE. The fix here is, to have construtors (or factories) which
1190 * requires all required instances that needs to be set to get a
1191 * consitent object back.
1194 // Gender is MALE now
1195 customer.setGender(Gender.MALE);
1198 Iterator<Map.Entry<Field, Object>> it = null;
1201 // Get iterator on all its fields
1202 it = customer.iterator();
1203 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1204 this.abortProgramWithException(ex);
1208 while ((it instanceof Iterator) && (it.hasNext())) {
1209 Map.Entry<Field, Object> entry = it.next();
1210 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1215 * Adds given category data from request to database
1217 * @param request Request instance
1220 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1222 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1224 // request must not be null
1225 if (request == null) {
1227 throw new NullPointerException("request is null");
1231 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1232 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1236 if (title == null) {
1238 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
1239 } else if (title.isEmpty()) {
1241 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
1242 } else if ((parent != null) && (!parent.isEmpty())) {
1243 // "parent" is set, so check it
1245 id = Integer.parseInt(parent);
1246 } catch (final NumberFormatException e) {
1248 throw new IllegalArgumentException(e);
1252 // Try to check if title is used already
1253 if (this.isCategoryTitleUsed(title)) {
1254 // Title already used
1255 throw new CategoryTitleAlreadyUsedException(request);
1257 } catch (final IOException | SQLException | BadTokenException ex) {
1258 throw new ServletException(ex);
1262 // The category is not found, so add it to database
1263 this.categoryFrontend.addCategory(title, id);
1264 } catch (final SQLException ex) {
1265 // Continue to throw it
1266 throw new ServletException(ex);
1270 this.getLogger().trace("EXIT!"); //NOI18N
1274 * Adds given product data from request to database
1276 * @param request Request instance
1279 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1281 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1283 // request must not be null
1284 if (request == null) {
1286 throw new NullPointerException("request is null");
1289 // Get title, price and category id
1290 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1291 String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
1292 String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
1293 String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
1300 if (title == null) {
1302 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE));
1303 } else if (title.isEmpty()) {
1305 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE));
1306 } else if (price == null) {
1308 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE));
1309 } else if (price.isEmpty()) {
1311 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE));
1312 } else if (category == null) {
1314 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY));
1315 } else if (category.isEmpty()) {
1317 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY));
1318 } else if (available == null) {
1320 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE));
1321 } else if (available.isEmpty()) {
1323 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE));
1324 } else if ((!"true".equals(available)) && (!"false".equals(available))) {
1326 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available));
1331 id = Long.parseLong(category);
1332 p = Float.parseFloat(price);
1333 } catch (final NumberFormatException e) {
1335 throw new IllegalArgumentException(e);
1339 a = Boolean.parseBoolean(available);
1341 // Test on product title
1343 // Try to check if title is used already
1344 if (this.isProductTitleUsed(title)) {
1345 // Title already used
1346 throw new ProductTitleAlreadyUsedException(request);
1348 } catch (final IOException | SQLException | BadTokenException ex) {
1349 throw new ServletException(ex);
1353 // The product is not found, so add it to database
1354 this.productFrontend.addProduct(title, p, id, a);
1355 } catch (final SQLException ex) {
1356 // Continue to throw it
1357 throw new ServletException(ex);
1361 this.getLogger().trace("EXIT!"); //NOI18N
1365 * Generates link HTML code for given category's parent id, if set. This
1366 * link then points to products.jsp?category_id=x
1368 * @param category Category instance
1372 public String generateLinkForParent (final Category category) {
1374 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category));
1376 // category must not be null
1377 if (category == null) {
1379 throw new NullPointerException("category is null");
1383 Integer parent = category.getParent();
1387 // Product HTML code for link
1388 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent));