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.io.UnsupportedEncodingException;
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.HttpServletResponse;
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.CorruptedDatabaseFileException;
35 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
36 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
37 import org.mxchange.pizzaapplication.category.Category;
38 import org.mxchange.pizzaapplication.customer.Customer;
39 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
40 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
41 import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend;
42 import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
43 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
44 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
45 import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
46 import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
47 import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
48 import org.mxchange.pizzaapplication.product.Product;
51 * Main application class
53 * @author Roland Haeder
55 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
59 public static final String MAIN_TITLE = "Pizza-Service";
62 * Frontend for products
64 private ProductFrontend productFrontend;
67 * Frontend for categories
69 private CategoryFrontend categoryFrontend;
72 * Some singleton getter for this instance. If the instance is not set in
73 * given application, it will be created.
75 * @param context Servlet context
76 * @return This instance
77 * @throws javax.servlet.ServletException If object is not set correctly
79 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
80 // Check application instance
81 if (context == null) {
83 throw new NullPointerException("application is null"); //NOI18N
87 PizzaApplication instance = null;
89 // Get instance from servlet application (aka. "application scope")
90 Object object = context.getAttribute("app"); //NOI18N
93 if (object instanceof PizzaApplication) {
94 // Instance is set, so casting should work
95 instance = (PizzaApplication) object;
98 instance.getLogger().debug(MessageFormat.format("Using existing instance {0} ...", object)); //NOI18N
99 } else if (object instanceof Object) {
100 // Not correct instance
101 throw new ServletException("app is not set correctly"); //NOI18N
104 // "service" is null, so initialize it
105 instance = new PizzaServiceApplication();
106 instance.init(context);
109 instance.getLogger().debug(MessageFormat.format("Created new instance {0} ...", object)); //NOI18N
110 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
111 throw new ServletException(ex);
115 context.setAttribute("app", instance); //NOI18N
119 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
126 * For debugging purpose
128 * @param args Arguments
130 public static void main (String[] args) {
131 // Get instance and start it
132 new PizzaServiceApplication().start();
136 public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
137 // Temporary initialize default bundle
138 // @TODO The JSF may have better internatialization support
141 // Initialize properties from config
142 this.initProperties(context);
144 // Init database frontends
145 this.initDatabaseFrontends();
149 * Default constructor
151 private PizzaServiceApplication () {
155 * Calculates total amount of all choosen products
157 * @param request Request instance
158 * @param session Session instance
159 * @return Total amount of all choosen products
162 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
164 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
166 // Is product and session set?
167 if (request == null) {
169 throw new NullPointerException("request is null"); //NOI18N
170 } else if (null == session) {
172 throw new NullPointerException("session is null"); //NOI18N
175 // Init/declare total price and iterator
177 Iterator<Product> iterator = this.getAvailableProducts();
179 // "Walk" over all products
180 while (iterator.hasNext()) {
182 Product product = iterator.next();
185 if (this.isProductChoosen(product, request, session)) {
186 // Then add ordered amount
187 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
190 String amount = this.getAmountFromSession(product, session);
193 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
194 totalAmount += Integer.valueOf(amount);
196 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
200 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
202 // Return total price
207 * Calculates total price of all choosen products
209 * @param request Request instance
210 * @param session Session instance
211 * @return Total price of all choosen products
214 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
216 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
218 // Is product and session set?
219 if (request == null) {
221 throw new NullPointerException("request is null"); //NOI18N
222 } else if (null == session) {
224 throw new NullPointerException("session is null"); //NOI18N
228 float totalPrice = 0.00f;
231 Iterator<Product> iterator = this.getAvailableProducts();
233 // "Walk" over all products
234 while (iterator.hasNext()) {
236 Product product = iterator.next();
239 if (this.isProductChoosen(product, request, session)) {
240 // Then add product's total price
241 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getItemId())); //NOI18N
242 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
244 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getItemId(), totalPrice)); //NOI18N
248 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
250 // Return total price
255 public void doBootstrap () {
256 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
260 public void doMainLoop () {
261 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
265 public void doShutdown () {
266 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
270 * Some "getter" for amount from session
272 * @param product Product instance
273 * @param session Session instance
274 * @return Amount as string
277 public String getAmountFromSession (final Product product, final HttpSession session) {
279 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
281 // Is product and session set?
282 if (null == product) {
284 throw new NullPointerException("product is null"); //NOI18N
285 } else if (null == session) {
287 throw new NullPointerException("session is null"); //NOI18N
291 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
293 // Is the object null?
294 if (object == null) {
296 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
303 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
305 // Cast to string and return it
306 return (String) object;
310 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
312 * @param product Product instance
313 * @param request Request instance
314 * @param session Session instance
315 * @return Whether the product is choosen
318 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
320 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
322 // Is product and session set?
323 if (null == product) {
325 throw new NullPointerException("product is null"); //NOI18N
326 } else if (request == null) {
328 throw new NullPointerException("request is null"); //NOI18N
329 } else if (null == session) {
331 throw new NullPointerException("session is null"); //NOI18N
334 // First let's check if the product is choosen
335 if (this.isProductChoosen(product, request, session)) {
337 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
340 return "checked=\"checked\""; //NOI18N
343 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
351 * Some "getter" for choose from session
353 * @param product Product instance
354 * @param session Session instance
355 * @return Choose as string
358 public String getChooseFromSession (final Product product, final HttpSession session) {
360 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
362 // Is product and session set?
363 if (null == product) {
365 throw new NullPointerException("product is null"); //NOI18N
366 } else if (null == session) {
368 throw new NullPointerException("session is null"); //NOI18N
372 Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
374 // Is the object null?
375 if (object == null) {
377 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getItemId())); //NOI18N
382 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
384 // Cast to string and return it
385 return (String) object;
389 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
391 * @param request Request instance
392 * @param session Session instance
393 * @return Whether the product is choosen
396 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
398 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
400 // Is product and session set?
401 if (request == null) {
403 throw new NullPointerException("request is null"); //NOI18N
404 } else if (null == session) {
406 throw new NullPointerException("session is null"); //NOI18N
409 // Get "enabled" from request scope
410 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
413 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
415 // Is something selected?
416 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
418 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
420 // Something has been choosen
424 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
426 // Nothing choosen yet
427 return "disabled=\"disabled\""; //NOI18N
432 * Some "getter" for choosen (checkbox) from session
434 * @param product Product instance
435 * @param request Request instance
436 * @param session Session instance
437 * @return Amount as string
440 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
442 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
444 // Is product and session set?
445 if (null == product) {
447 throw new NullPointerException("product is null"); //NOI18N
448 } else if (request == null) {
450 throw new NullPointerException("request is null"); //NOI18N
451 } else if (null == session) {
453 throw new NullPointerException("session is null"); //NOI18N
457 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
458 String choosen = this.handleChooseFromRequestSession(product, request, session);
459 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
462 assert(choosen instanceof String): "choosen is null"; //NOI18N
465 if (choosen.isEmpty()) {
471 String amount = this.handleAmountFromRequestSession(product, request, session);
472 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
475 assert(amount instanceof String): "amount is null"; //NOI18N
478 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
479 // Choosen, but no amount
488 * Checks if given Product instance is available and returns a printable
489 * (human-readable) string.
491 * @param product Product instance to check
492 * @return Human-readable version of product availability
495 public String getPrintableProduktAvailability (final Product product) {
497 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
500 if (null == product) {
501 // Should not be null
502 throw new NullPointerException("product is null"); //NOI18N
506 if (product.getAvailable() == true) {
510 // Not, not for public
516 * Some getter for printable value from session or an empty string for null.
518 * @param session Session instance
519 * @param key Key to get
520 * @return Value from key, empty string for null
523 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
525 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
527 // Are both parameter not null?
528 if (null == session) {
530 throw new NullPointerException("session is null"); //NOI18N
531 } else if (key == null) {
533 throw new NullPointerException("key is null"); //NOI18N
537 Object value = this.getValueFromSession(session, key);
540 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
543 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
545 // Return actual value
546 return this.convertNullToEmpty(value);
550 * Some "getter" for a an array of only available products
552 * @return All products
555 public Iterator<Product> getAvailableProducts () throws ServletException {
557 // Ask frontend for a list of products
558 return this.productFrontend.getAvailableProducts();
559 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
560 throw new ServletException(ex);
565 * Some "getter" for a an array of all products
567 * @return All products
570 public Iterator<Product> getAllProducts () throws ServletException {
572 // Ask frontend for a list of products
573 return this.productFrontend.getAllProducts();
574 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
575 throw new ServletException(ex);
580 * Some "getter" for a an array of all categories
582 * @return All categories
585 public Iterator<Category> getCategories () throws ServletException {
587 // Ask frontend for a list of categories
588 return this.categoryFrontend.getCategories();
589 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
590 throw new ServletException(ex);
595 * Some "getter" for total price of position from request or session.
596 * Single price and amount is multiplyed.
598 * @param product Product instance
599 * @param request Request instance
600 * @param session Session instance
601 * @return Amount as string
604 public float getTotalPositionPriceFromRequestSession (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 (null == product) {
611 throw new NullPointerException("product is null"); //NOI18N
612 } else if (request == null) {
614 throw new NullPointerException("request is null"); //NOI18N
615 } else if (null == session) {
617 throw new NullPointerException("session is null"); //NOI18N
621 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
622 String choosen = this.handleChooseFromRequestSession(product, request, session);
623 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
626 assert(choosen instanceof String): "choosen is null"; //NOI18N
629 if (choosen.isEmpty()) {
631 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getItemId(), choosen)); //NOI18N
636 String amount = this.handleAmountFromRequestSession(product, request, session);
637 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
640 assert(amount instanceof String): "amount is null"; //NOI18N
643 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
645 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getItemId(), amount)); //NOI18N
650 Integer value = null;
654 // Get amount as integer
655 value = Integer.valueOf(amount);
656 } catch (final NumberFormatException e) {
658 throw new IllegalArgumentException(e);
662 float price = (product.getPrice() * value);
665 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getItemId(), price)); //NOI18N
667 // Then multiply it with price
672 * Handler for amount from request or session
674 * @param product Product instance
675 * @param request Request instance
676 * @param session Session instance
677 * @return Amount as string
680 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
682 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
684 // Is product and session set?
685 if (null == product) {
687 throw new NullPointerException("product is null"); //NOI18N
688 } else if (request == null) {
690 throw new NullPointerException("request is null"); //NOI18N
691 } else if (null == session) {
693 throw new NullPointerException("session is null"); //NOI18N
699 // Check request method
700 if (!"POST".equals(request.getMethod())) { //NOI18N
701 // Not POST, so get from session
702 return this.getAmountFromSession(product, session);
703 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
705 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
706 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getItemId())); //NOI18N
710 // Get attribute from request
711 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getItemId()));
714 if (object instanceof String) {
715 // Try to parse it to integer
717 Integer value = Integer.valueOf((String) object);
718 } catch (final NumberFormatException ex) {
720 this.getLogger().warn(ex);
724 // Then set it in session
725 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
728 return (String) object;
732 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
734 // Get attribute from session
735 return this.getAmountFromSession(product, session);
739 * Checks whether the given product is choosen, request overules session.
741 * @param product Product instance
742 * @param request Request instance
743 * @param session Session instance
744 * @return Whether the product is choosen
747 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
749 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
751 // Is product and session set?
752 if (null == product) {
754 throw new NullPointerException("product is null"); //NOI18N
755 } else if (request == null) {
757 throw new NullPointerException("request is null"); //NOI18N
758 } else if (null == session) {
760 throw new NullPointerException("session is null"); //NOI18N
764 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
765 String choosen = this.handleChooseFromRequestSession(product, request, session);
766 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
769 assert(choosen instanceof String): "choosen is null"; //NOI18N
771 // Is it not choosen?
772 if (choosen.isEmpty()) {
778 String amount = this.handleAmountFromRequestSession(product, request, session);
781 assert(amount instanceof String): "amount is not set"; //NOI18N
784 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
786 // Must not be empty and not 0
787 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
791 * Marks all choosen products as ordered
793 * @param request Request instance
794 * @param session Session instance
797 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
799 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
802 Iterator<Product> iterator = this.getAvailableProducts();
804 // "Walk" over all products
805 while (iterator.hasNext()) {
807 Product product = iterator.next();
810 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
813 if (this.isProductChoosen(product, request, session)) {
814 // Mark product as ordered
815 this.markProductAsOrdered(product, session);
820 this.getLogger().trace("EXIT!"); //NOI18N
824 * Marks given product as choosen in session
826 * @param product Product to mark as ordered
827 * @param session Session instance
830 public void markProductAsChoosen (final Product product, final HttpSession session) {
832 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
834 // Is product and session set?
835 if (null == product) {
837 throw new NullPointerException("product is null"); //NOI18N
838 } else if (null == session) {
840 throw new NullPointerException("session is null"); //NOI18N
843 // Mark it as ordered by setting flag
844 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getItemId())); //NOI18N
845 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
848 this.getLogger().trace("EXIT!"); //NOI18N
852 * Marks given product as ordered in session
854 * @param product Product to mark as ordered
855 * @param session Session instance
858 public void markProductAsOrdered (final Product product, final HttpSession session) {
860 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
862 // Is product and session set?
863 if (null == product) {
865 throw new NullPointerException("product is null"); //NOI18N
866 } else if (null == session) {
868 throw new NullPointerException("session is null"); //NOI18N
871 // Mark it as ordered by setting flag
872 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getItemId())); //NOI18N
873 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
876 this.getLogger().trace("EXIT!"); //NOI18N
880 * Somewhat setter in session
882 * @param session Session instance
883 * @param key Session key to set
884 * @param value Value to set
887 public void setValueInSession (final HttpSession session, final String key, final Object value) {
889 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
891 synchronized(session) {
893 session.setAttribute(key, value);
897 this.getLogger().trace("EXIT!"); //NOI18N
901 * Unmarks given product as choosen in session
903 * @param product Product to unmark as choosen
904 * @param session Session instance
907 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
909 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
911 // Is product and session set?
912 if (null == product) {
914 throw new NullPointerException("product is null"); //NOI18N
915 } else if (null == session) {
917 throw new NullPointerException("session is null"); //NOI18N
920 // Mark it as ordered by setting flag
921 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getItemId())); //NOI18N
922 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
925 this.getLogger().trace("EXIT!"); //NOI18N
929 * Unmarks given product as ordered in session
931 * @param product Product to unmark as ordered
932 * @param session Session instance
935 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
937 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
939 // Is product and session set?
940 if (null == product) {
942 throw new NullPointerException("product is null"); //NOI18N
943 } else if (null == session) {
945 throw new NullPointerException("session is null"); //NOI18N
948 // Mark it as ordered by setting flag
949 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getItemId())); //NOI18N
950 this.clearSessionAttribute(product, session, SESSION_ORDERED);
953 this.getLogger().trace("EXIT!"); //NOI18N
957 * Clears given parameter for product in session
959 * @param product Product instance
960 * @param session Session instance
961 * @param parameter Parameter to clear
963 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
965 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
968 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
969 this.setValueInSession(product, session, parameter, null);
972 this.getLogger().trace("EXIT!"); //NOI18N
976 * Some getter for value from session
978 * @param product Product instance
979 * @param session Session instance
980 * @param attribute Attribute to get value from
981 * @return Value from session
983 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
985 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
988 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
990 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
993 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1000 * Some getter for value from session
1002 * @param session Session instance
1003 * @param key Key to get value from
1004 * @return Value from session
1006 private Object getValueFromSession (final HttpSession session, final String key) {
1008 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
1013 // Get it synchronized from session
1014 synchronized (session) {
1015 value = session.getAttribute(key);
1019 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1026 * Handler for choosen (checkbox) from request or session
1028 * @param product Product instance
1029 * @param request Request instance
1030 * @param session Session instance
1031 * @return Amount as string
1033 private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
1035 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1037 // Is product and session set?
1038 if (null == product) {
1040 throw new NullPointerException("product is null"); //NOI18N
1041 } else if (request == null) {
1043 throw new NullPointerException("request is null"); //NOI18N
1044 } else if (null == session) {
1046 throw new NullPointerException("session is null"); //NOI18N
1052 // Check request method
1053 if (!"POST".equals(request.getMethod())) { //NOI18N
1054 // Not POST, so get from session
1055 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1056 return this.getChooseFromSession(product, session);
1057 } else if (this.isProductOrdered(product, session)) {
1058 // Product is ordered
1059 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1060 return this.getChooseFromSession(product, session);
1061 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1063 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1064 return this.getChooseFromSession(product, session);
1067 // Get reqzest element
1068 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getItemId()));
1069 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getItemId(), object)); //NOI18N
1072 if (object == null) {
1074 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getItemId())); //NOI18N
1075 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
1076 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1078 // Return empty string
1082 // Then set it in session
1083 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, object);
1085 // Cast to string and return it
1086 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getItemId(), object)); //NOI18N
1087 return (String) object;
1091 * Initializes database frontends.
1093 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1095 this.productFrontend = new PizzaProductDatabaseFrontend();
1097 // Category frontend
1098 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1102 * Checks whether given category title is already used
1104 * @param title Title of category to check
1105 * @return Whether it has been found
1107 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1108 // Delegate to frontend
1109 return this.categoryFrontend.isCategoryTitleUsed(title);
1113 * Checks if given product title is already used
1114 * @param title Product title to check
1115 * @return Whether the product title has already been used
1117 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1118 // Delegate to frontend
1119 return this.productFrontend.isProductTitleUsed(title);
1123 * Checks if the product ordered?
1125 * @param product Product instance
1126 * @param session HttpSession instance
1127 * @return Whether the product has been ordered
1129 private boolean isProductOrdered (final Product product, final HttpSession session) {
1131 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1134 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1135 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), isOrdered)); //NOI18N
1138 return ("true".equals(isOrdered)); //NOI18N
1142 * Somewhat setter in session
1144 * @param product Product instance
1145 * @param session Session instance
1146 * @param keyPart Key part to include in final key
1147 * @param value Value to set
1149 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1151 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1154 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
1155 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
1158 this.getLogger().trace("EXIT!"); //NOI18N
1162 * Application starter
1164 private void start() {
1170 this.initProperties();
1173 this.initDatabaseFrontends();
1174 } catch (final IOException | UnsupportedDatabaseBackendException | SQLException ex) {
1176 this.abortProgramWithException(ex);
1180 Iterator<Product> iterator = null;
1184 iterator = this.getAvailableProducts();
1185 } catch (final ServletException ex) {
1186 this.abortProgramWithException(ex);
1189 // "Walk" over all products
1190 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1192 Product product = iterator.next();
1195 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getItemId(), product.getTitle(), product.getPrice())); //NOI18N
1198 // Generate fake Customer instance
1199 Customer customer = new PizzaServiceCustomer();
1202 * Need a least a gender ... :( See, that is why I don't like default
1203 * constructors, you can easily miss something important and bam! You
1204 * get an NPE. The fix here is, to have construtors (or factories) which
1205 * requires all required instances that needs to be set to get a
1206 * consitent object back.
1209 // Gender is MALE now
1210 customer.setGender(Gender.MALE);
1213 Iterator<Map.Entry<Field, Object>> it = null;
1216 // Get iterator on all its fields
1217 it = customer.iterator();
1218 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1219 this.abortProgramWithException(ex);
1223 while ((it instanceof Iterator) && (it.hasNext())) {
1224 Map.Entry<Field, Object> entry = it.next();
1225 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1230 * Adds given category data from request to database
1232 * @param request Request instance
1235 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1237 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1239 // request must not be null
1240 if (request == null) {
1242 throw new NullPointerException("request is null"); //NOI18N
1246 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1247 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1250 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
1252 // Init variables for casting
1256 if (title == null) {
1258 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1259 } else if (title.isEmpty()) {
1261 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1262 } else if ((parent != null) && (!parent.isEmpty())) {
1263 // "parent" is set, so check it
1265 id = Integer.parseInt(parent);
1266 } catch (final NumberFormatException e) {
1268 throw new IllegalArgumentException(e);
1273 // Try to check if title is used already
1274 if (this.isCategoryTitleUsed(title)) {
1275 // Title already used
1276 throw new CategoryTitleAlreadyUsedException(request);
1278 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1279 throw new ServletException(ex);
1283 // The category is not found, so add it to database
1284 this.categoryFrontend.addCategory(title, id);
1285 } catch (final SQLException | IOException ex) {
1286 // Continue to throw it
1287 throw new ServletException(ex);
1291 this.getLogger().trace("EXIT!"); //NOI18N
1295 * Adds given product data from request to database
1297 * @param request Request instance
1300 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1302 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1304 // request must not be null
1305 if (request == null) {
1307 throw new NullPointerException("request is null"); //NOI18N
1310 // Get title, price and category id
1311 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1312 String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
1313 String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
1314 String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
1317 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
1319 // Variables for converting
1324 if (title == null) {
1326 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1327 } else if (title.isEmpty()) {
1329 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1330 } else if (price == null) {
1332 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1333 } else if (price.isEmpty()) {
1335 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1336 } else if (category == null) {
1338 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1339 } else if (category.isEmpty()) {
1341 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1342 } else if (available == null) {
1344 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1345 } else if (available.isEmpty()) {
1347 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1348 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
1350 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available)); //NOI18N
1355 id = Long.parseLong(category);
1356 p = Float.parseFloat(price);
1357 } catch (final NumberFormatException e) {
1359 throw new IllegalArgumentException(e);
1363 Boolean a = Boolean.parseBoolean(available);
1365 // Test on product title
1367 // Try to check if title is used already
1368 if (this.isProductTitleUsed(title)) {
1369 // Title already used
1370 throw new ProductTitleAlreadyUsedException(request);
1372 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1373 throw new ServletException(ex);
1377 // The product is not found, so add it to database
1378 this.productFrontend.addProduct(title, p, id, a);
1379 } catch (final SQLException | IOException ex) {
1380 // Continue to throw it
1381 throw new ServletException(ex);
1385 this.getLogger().trace("EXIT!"); //NOI18N
1389 * Generates link HTML code for given category's parent id, if set. This
1390 * link then points to products.jsp?category_id=x
1392 * @param category Category instance
1396 public String generateLinkForParent (final Category category) {
1398 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
1400 // category must not be null
1401 if (category == null) {
1403 throw new NullPointerException("category is null"); //NOI18N
1407 Long parent = category.getParent();
1411 // Product HTML code for link
1412 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
1420 public String getPrintableProduktCategory (final Product product) throws ServletException {
1422 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
1424 // product must not be null
1425 if (null == product) {
1427 throw new NullPointerException("product is null"); //NOI18N
1434 // Get Category instance from product over the frontend
1435 category = this.categoryFrontend.getCategory(product);
1436 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1437 throw new ServletException(ex);
1441 this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
1443 String title = null;
1445 // Now get title from it and return it
1446 title = category.decodedTitle();
1447 } catch (final UnsupportedEncodingException ex) {
1448 // Continue to throw as cause
1449 throw new ServletException(ex);
1453 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
1460 * Checks if product's title is already used.
1462 * @param request Request instance
1463 * @return Whether the product title is already used
1464 * @throws java.io.IOException If any IO error occurs
1465 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1466 * @throws java.sql.SQLException If any SQL error occurs
1467 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1468 * @throws java.lang.NoSuchMethodException If a method was not found
1469 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1470 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1472 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1474 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1477 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1479 // request must not be null and "title" must be found and non-empty
1480 if (request == null) {
1482 throw new NullPointerException("request is null"); //NOI18N
1483 } else if (title == null) {
1485 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1486 } else if (title.isEmpty()) {
1488 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1491 // Default is not used
1492 boolean isUsed = this.isProductTitleUsed(title);
1495 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1502 * Handles admin form requests
1503 * @param request Request instance
1504 * @param response Response instance
1505 * @throws ServletException If something unexpected happened
1508 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1510 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1512 // request and response must both be set
1513 if (request == null) {
1515 throw new NullPointerException("request is null"); //NOI18N
1516 } else if (response == null) {
1518 throw new NullPointerException("response is null"); //NOI18N
1521 // Try this operations
1524 if ("POST".equals(request.getMethod())) { //NOI18N
1525 // Is "add/edit/delete" set?
1526 if (request.getParameter("add") != null) { //NOI18N
1527 // Is it already added?
1528 if (this.isProductTitleUsed(request)) {
1530 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1532 // Already added, so redirect here, else a ServletException will be thrown
1533 response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
1536 this.doAdminAddProduct(request);
1538 } else if (request.getParameter("edit") != null) { //NOI18N
1540 } else if (request.getParameter("delete") != null) { //NOI18N
1544 // Redirect to proper URL
1545 // @TODO Commented out for developing:
1546 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1548 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1549 // Throw it as cause
1550 throw new ServletException(ex);
1554 this.getLogger().trace("EXIT!"); //NOI18N
1558 * Handles admin form requests
1559 * @param request Request instance
1560 * @param response Response instance
1561 * @throws ServletException If something unexpected happened
1564 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1566 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1568 // request and response must both be set
1569 if (request == null) {
1571 throw new NullPointerException("request is null"); //NOI18N
1572 } else if (response == null) {
1574 throw new NullPointerException("response is null"); //NOI18N
1577 // Try this operations
1580 if ("POST".equals(request.getMethod())) { //NOI18N
1581 // Is "add/edit/delete" set?
1582 if (request.getParameter("add") != null) { //NOI18N
1583 // Is the category title already used?
1584 if (this.isCategoryTitleUsed(request)) {
1586 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1588 // Already added, so redirect here, else a ServletException will be thrown
1589 response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
1592 this.doAdminAddCategory(request);
1594 } else if (request.getParameter("edit") != null) { //NOI18N
1596 } else if (request.getParameter("delete") != null) { //NOI18N
1600 // Redirect to proper URL
1601 // @TODO Commented out for developing:
1602 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1604 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1605 // Throw it as cause
1606 throw new ServletException(ex);
1610 this.getLogger().trace("EXIT!"); //NOI18N
1614 * Checks if category's title is already used.
1616 * @param request Request instance
1617 * @return Whether the product title is already used
1618 * @throws java.io.IOException If any IO error occurs
1619 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1620 * @throws java.sql.SQLException If any SQL error occurs
1621 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1622 * @throws java.lang.NoSuchMethodException If a method was not found
1623 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1624 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1626 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1628 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1631 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1633 // request must not be null and "title" must be found and non-empty
1634 if (request == null) {
1636 throw new NullPointerException("request is null"); //NOI18N
1637 } else if (title == null) {
1639 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1640 } else if (title.isEmpty()) {
1642 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1645 // Default is not used
1646 boolean isUsed = this.isCategoryTitleUsed(title);
1649 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N