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.getProducts();
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.getProducts();
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) {
484 throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: product={0}", product)); //NOI18N
488 * Some getter for printable value from session or an empty string for null.
490 * @param session Session instance
491 * @param key Key to get
492 * @return Value from key, empty string for null
495 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
497 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
499 // Are both parameter not null?
500 if (session == null) {
502 throw new NullPointerException("session is null"); //NOI18N
503 } else if (key == null) {
505 throw new NullPointerException("key is null"); //NOI18N
509 Object value = this.getValueFromSession(session, key);
512 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
515 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
517 // Return actual value
518 return this.convertNullToEmpty(value);
522 * Some "getter" for a an array of all products
524 * @return All products
527 public Iterator<Product> getProducts () throws ServletException {
529 // Ask frontend for a list of products
530 return this.productFrontend.getProducts();
531 } catch (final IOException | BadTokenException | SQLException ex) {
532 throw new ServletException(ex);
537 * Some "getter" for a an array of all categories
539 * @return All categories
542 public Iterator<Category> getCategories () throws ServletException {
544 // Ask frontend for a list of categories
545 return this.categoryFrontend.getCategories();
546 } catch (final IOException | BadTokenException | SQLException ex) {
547 throw new ServletException(ex);
552 * Some "getter" for total price of position from request or session.
553 * Single price and amount is multiplyed.
555 * @param product Product instance
556 * @param request Request instance
557 * @param session Session instance
558 * @return Amount as string
561 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
563 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
565 // Is product and session set?
566 if (product == null) {
568 throw new NullPointerException("product is null"); //NOI18N
569 } else if (request == null) {
571 throw new NullPointerException("request is null"); //NOI18N
572 } else if (session == null) {
574 throw new NullPointerException("session is null"); //NOI18N
578 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
579 String choosen = this.handleChooseFromRequestSession(product, request, session);
580 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
583 assert(choosen instanceof String): "choosen is null"; //NOI18N
586 if (choosen.isEmpty()) {
588 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
593 String amount = this.handleAmountFromRequestSession(product, request, session);
594 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
597 assert(amount instanceof String): "amount is null"; //NOI18N
600 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
602 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
607 Integer value = null;
611 // Get amount as integer
612 value = Integer.valueOf(amount);
613 } catch (final NumberFormatException e) {
615 throw new IllegalArgumentException(e);
619 float price = (product.getPrice() * value);
622 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
624 // Then multiply it with price
629 * Handler for amount from request or session
631 * @param product Product instance
632 * @param request Request instance
633 * @param session Session instance
634 * @return Amount as string
637 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
639 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
641 // Is product and session set?
642 if (product == null) {
644 throw new NullPointerException("product is null"); //NOI18N
645 } else if (request == null) {
647 throw new NullPointerException("request is null"); //NOI18N
648 } else if (session == null) {
650 throw new NullPointerException("session is null"); //NOI18N
656 // Check request method
657 if (!"POST".equals(request.getMethod())) { //NOI18N
658 // Not POST, so get from session
659 return this.getAmountFromSession(product, session);
660 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
662 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
663 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
667 // Get attribute from request
668 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
671 if (object instanceof String) {
672 // Try to parse it to integer
674 Integer value = Integer.valueOf((String) object);
675 } catch (final NumberFormatException ex) {
677 this.getLogger().warn(ex);
681 // Then set it in session
682 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
685 return (String) object;
689 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
691 // Get attribute from session
692 return this.getAmountFromSession(product, session);
696 * Checks whether the given product is choosen, request overules session.
698 * @param product Product instance
699 * @param request Request instance
700 * @param session Session instance
701 * @return Whether the product is choosen
704 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
706 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
708 // Is product and session set?
709 if (product == null) {
711 throw new NullPointerException("product is null"); //NOI18N
712 } else if (request == null) {
714 throw new NullPointerException("request is null"); //NOI18N
715 } else if (session == null) {
717 throw new NullPointerException("session is null"); //NOI18N
721 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
722 String choosen = this.handleChooseFromRequestSession(product, request, session);
723 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
726 assert(choosen instanceof String): "choosen is null"; //NOI18N
728 // Is it not choosen?
729 if (choosen.isEmpty()) {
735 String amount = this.handleAmountFromRequestSession(product, request, session);
738 assert(amount instanceof String): "amount is not set"; //NOI18N
741 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
743 // Must not be empty and not 0
744 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
748 * Marks all choosen products as ordered
750 * @param request Request instance
751 * @param session Session instance
754 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
756 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
759 Iterator<Product> iterator = this.getProducts();
761 // "Walk" over all products
762 while (iterator.hasNext()) {
764 Product product = iterator.next();
767 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
770 if (this.isProductChoosen(product, request, session)) {
771 // Mark product as ordered
772 this.markProductAsOrdered(product, session);
777 this.getLogger().trace("EXIT!"); //NOI18N
781 * Marks given product as choosen in session
783 * @param product Product to mark as ordered
784 * @param session Session instance
787 public void markProductAsChoosen (final Product product, final HttpSession session) {
789 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
791 // Is product and session set?
792 if (product == null) {
794 throw new NullPointerException("product is null"); //NOI18N
795 } else if (session == null) {
797 throw new NullPointerException("session is null"); //NOI18N
800 // Mark it as ordered by setting flag
801 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
802 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
805 this.getLogger().trace("EXIT!"); //NOI18N
809 * Marks given product as ordered in session
811 * @param product Product to mark as ordered
812 * @param session Session instance
815 public void markProductAsOrdered (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("Marking product={0} as ordered.", product.getId())); //NOI18N
830 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
833 this.getLogger().trace("EXIT!"); //NOI18N
837 * Somewhat setter in session
839 * @param session Session instance
840 * @param key Session key to set
841 * @param value Value to set
844 public void setValueInSession (final HttpSession session, final String key, final Object value) {
846 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
848 synchronized(session) {
850 session.setAttribute(key, value);
854 this.getLogger().trace("EXIT!"); //NOI18N
858 * Unmarks given product as choosen in session
860 * @param product Product to unmark as choosen
861 * @param session Session instance
864 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
866 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
868 // Is product and session set?
869 if (product == null) {
871 throw new NullPointerException("product is null"); //NOI18N
872 } else if (session == null) {
874 throw new NullPointerException("session is null"); //NOI18N
877 // Mark it as ordered by setting flag
878 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
879 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
882 this.getLogger().trace("EXIT!"); //NOI18N
886 * Unmarks given product as ordered in session
888 * @param product Product to unmark as ordered
889 * @param session Session instance
892 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
894 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
896 // Is product and session set?
897 if (product == null) {
899 throw new NullPointerException("product is null"); //NOI18N
900 } else if (session == null) {
902 throw new NullPointerException("session is null"); //NOI18N
905 // Mark it as ordered by setting flag
906 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
907 this.clearSessionAttribute(product, session, SESSION_ORDERED);
910 this.getLogger().trace("EXIT!"); //NOI18N
914 * Clears given parameter for product in session
916 * @param product Product instance
917 * @param session Session instance
918 * @param parameter Parameter to clear
920 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
922 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
925 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
926 this.setValueInSession(product, session, parameter, null);
929 this.getLogger().trace("EXIT!"); //NOI18N
933 * Some getter for value from session
935 * @param product Product instance
936 * @param session Session instance
937 * @param attribute Attribute to get value from
938 * @return Value from session
940 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
942 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
945 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
947 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
950 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
957 * Some getter for value from session
959 * @param session Session instance
960 * @param key Key to get value from
961 * @return Value from session
963 private Object getValueFromSession (final HttpSession session, final String key) {
965 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
970 // Get it synchronized from session
971 synchronized (session) {
972 value = session.getAttribute(key);
976 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
983 * Handler for choosen (checkbox) from request or session
985 * @param product Product instance
986 * @param request Request instance
987 * @param session Session instance
988 * @return Amount as string
990 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
992 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
994 // Is product and session set?
995 if (product == null) {
997 throw new NullPointerException("product is null"); //NOI18N
998 } else if (request == null) {
1000 throw new NullPointerException("request is null"); //NOI18N
1001 } else if (session == null) {
1003 throw new NullPointerException("session is null"); //NOI18N
1009 // Check request method
1010 if (!"POST".equals(request.getMethod())) { //NOI18N
1011 // Not POST, so get from session
1012 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1013 return this.getChooseFromSession(product, session);
1014 } else if (this.isProductOrdered(product, session)) {
1015 // Product is ordered
1016 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1017 return this.getChooseFromSession(product, session);
1018 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1020 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1021 return this.getChooseFromSession(product, session);
1024 // Get reqzest element
1025 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
1026 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1029 if (object == null) {
1031 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1032 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1033 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1035 // Return empty string
1039 // Then set it in session
1040 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1042 // Cast to string and return it
1043 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1044 return (String) object;
1048 * Initializes database frontends.
1050 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1052 this.productFrontend = new PizzaProductDatabaseFrontend();
1054 // Category frontend
1055 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1059 * Checks whether given category title is already used
1061 * @param title Title of category to check
1062 * @return Whether it has been found
1064 private boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
1065 // Delegate to frontend
1066 return this.categoryFrontend.isCategoryTitleUsed(title);
1070 * Checks if given product title is already used
1071 * @param title Product title to check
1072 * @return Whether the product title has already been used
1074 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException {
1075 // Delegate to frontend
1076 return this.productFrontend.isProductTitleUsed(title);
1080 * Checks if the product ordered?
1082 * @param product Product instance
1083 * @param session HttpSession instance
1086 private boolean isProductOrdered(final Product product, final HttpSession session) {
1088 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1091 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1092 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1095 return ("true".equals(isOrdered)); //NOI18N
1099 * Somewhat setter in session
1101 * @param product Product instance
1102 * @param session Session instance
1103 * @param keyPart Key part to include in final key
1104 * @param value Value to set
1106 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1108 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1111 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1112 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1115 this.getLogger().trace("EXIT!"); //NOI18N
1119 * Application starter
1121 private void start () {
1127 this.initProperties();
1128 } catch (final IOException ex) {
1130 this.abortProgramWithException(ex);
1134 Iterator<Product> iterator = null;
1138 iterator = this.getProducts();
1139 } catch (final ServletException ex) {
1140 this.abortProgramWithException(ex);
1143 // "Walk" over all products
1144 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1146 Product product = iterator.next();
1149 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1152 // Generate fake Customer instance
1153 Customer customer = new PizzaServiceCustomer();
1156 * Need a least a gender ... :( See, that is why I don't like default
1157 * constructors, you can easily miss something important and bam! You
1158 * get an NPE. The fix here is, to have construtors (or factories) which
1159 * requires all required instances that needs to be set to get a
1160 * consitent object back.
1163 // Gender is MALE now
1164 customer.setGender(Gender.MALE);
1167 Iterator<Map.Entry<Field, Object>> it = null;
1170 // Get iterator on all its fields
1171 it = customer.iterator();
1172 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1173 this.abortProgramWithException(ex);
1177 while ((it instanceof Iterator) && (it.hasNext())) {
1178 Map.Entry<Field, Object> entry = it.next();
1179 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1184 * Adds given category data from request to database
1186 * @param request Request instance
1189 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1191 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1193 // request must not be null
1194 if (request == null) {
1196 throw new NullPointerException("request is null");
1200 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1201 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1205 if (title == null) {
1207 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
1208 } else if (title.isEmpty()) {
1210 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE));
1211 } else if ((parent != null) && (!parent.isEmpty())) {
1212 // "parent" is set, so check it
1214 id = Integer.parseInt(parent);
1215 } catch (final NumberFormatException e) {
1217 throw new IllegalArgumentException(e);
1221 // Try to check if title is used already
1222 if (this.isCategoryTitleUsed(title)) {
1223 // Title already used
1224 throw new CategoryTitleAlreadyUsedException(request);
1226 } catch (final IOException | SQLException | BadTokenException ex) {
1227 throw new ServletException(ex);
1231 // The category is not found, so add it to database
1232 this.categoryFrontend.addCategory(title, id);
1233 } catch (final SQLException ex) {
1234 // Continue to throw it
1235 throw new ServletException(ex);
1239 this.getLogger().trace("EXIT!"); //NOI18N
1243 * Adds given product data from request to database
1245 * @param request Request instance
1248 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1250 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1252 // request must not be null
1253 if (request == null) {
1255 throw new NullPointerException("request is null");
1258 // Get title, price and category id
1259 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1260 String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
1261 String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
1266 if (title == null) {
1268 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE));
1269 } else if (title.isEmpty()) {
1271 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE));
1272 } else if (price == null) {
1274 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE));
1275 } else if (price.isEmpty()) {
1277 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE));
1278 } else if (category == null) {
1280 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY));
1281 } else if (category.isEmpty()) {
1283 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY));
1285 // "parent" is set, so check it
1287 id = Integer.parseInt(category);
1288 p = Float.parseFloat(price);
1289 } catch (final NumberFormatException e) {
1291 throw new IllegalArgumentException(e);
1295 // Test on product title
1297 // Try to check if title is used already
1298 if (this.isProductTitleUsed(title)) {
1299 // Title already used
1300 throw new ProductTitleAlreadyUsedException(request);
1302 } catch (final IOException | SQLException | BadTokenException ex) {
1303 throw new ServletException(ex);
1307 // The product is not found, so add it to database
1308 this.productFrontend.addProduct(title, p, id);
1309 } catch (final SQLException ex) {
1310 // Continue to throw it
1311 throw new ServletException(ex);
1315 this.getLogger().trace("EXIT!"); //NOI18N
1319 * Generates link HTML code for given category's parent id, if set. This
1320 * link then points to products.jsp?category_id=x
1322 * @param category Category instance
1326 public String generateLinkForParent (final Category category) {
1328 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category));
1330 // category must not be null
1331 if (category == null) {
1333 throw new NullPointerException("category is null");
1337 Integer parent = category.getParent();
1341 // Product HTML code for link
1342 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent));