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;
96 } else if (object instanceof Object) {
97 // Not correct instance
98 throw new ServletException("app is not set correctly"); //NOI18N
101 // "service" is null, so initialize it
102 instance = new PizzaServiceApplication(context);
103 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
104 throw new ServletException(ex);
108 context.setAttribute("app", instance); //NOI18N
112 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
119 * For debugging purpose
121 * @param args Arguments
123 public static void main (String[] args) {
124 // Get instance and start it
125 new PizzaServiceApplication().start();
129 * Constructor with servet configuration
131 * @param context Servlet context
133 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
134 // Temporary initialize default bundle
135 // @TODO The JSF may have better internatialization support
138 // Initialize properties from config
139 this.initProperties(context);
141 // Init database frontends
142 this.initDatabaseFrontends();
146 * Default constructor
148 private PizzaServiceApplication () {
152 * Calculates total amount of all choosen products
154 * @param request Request instance
155 * @param session Session instance
156 * @return Total amount of all choosen products
159 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
161 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
163 // Is product and session set?
164 if (request == null) {
166 throw new NullPointerException("request is null"); //NOI18N
167 } else if (session == null) {
169 throw new NullPointerException("session is null"); //NOI18N
172 // Init/declare total price and iterator
174 Iterator<Product> iterator = this.getAvailableProducts();
176 // "Walk" over all products
177 while (iterator.hasNext()) {
179 Product product = iterator.next();
182 if (this.isProductChoosen(product, request, session)) {
183 // Then add ordered amount
184 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
187 String amount = this.getAmountFromSession(product, session);
190 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
191 totalAmount += Integer.valueOf(amount);
193 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
197 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
199 // Return total price
204 * Calculates total price of all choosen products
206 * @param request Request instance
207 * @param session Session instance
208 * @return Total price of all choosen products
211 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
213 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
215 // Is product and session set?
216 if (request == null) {
218 throw new NullPointerException("request is null"); //NOI18N
219 } else if (session == null) {
221 throw new NullPointerException("session is null"); //NOI18N
225 float totalPrice = 0.00f;
228 Iterator<Product> iterator = this.getAvailableProducts();
230 // "Walk" over all products
231 while (iterator.hasNext()) {
233 Product product = iterator.next();
236 if (this.isProductChoosen(product, request, session)) {
237 // Then add product's total price
238 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
239 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
241 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
245 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
247 // Return total price
252 public void doBootstrap () {
253 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
257 public void doMainLoop () {
258 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
262 public void doShutdown () {
263 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
267 * Some "getter" for amount from session
269 * @param product Product instance
270 * @param session Session instance
271 * @return Amount as string
274 public String getAmountFromSession (final Product product, final HttpSession session) {
276 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
278 // Is product and session set?
279 if (product == null) {
281 throw new NullPointerException("product is null"); //NOI18N
282 } else if (session == null) {
284 throw new NullPointerException("session is null"); //NOI18N
288 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
290 // Is the object null?
291 if (object == null) {
293 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
300 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
302 // Cast to string and return it
303 return (String) object;
307 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
309 * @param product Product instance
310 * @param request Request instance
311 * @param session Session instance
312 * @return Whether the product is choosen
315 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
317 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
319 // Is product and session set?
320 if (product == null) {
322 throw new NullPointerException("product is null"); //NOI18N
323 } else if (request == null) {
325 throw new NullPointerException("request is null"); //NOI18N
326 } else if (session == null) {
328 throw new NullPointerException("session is null"); //NOI18N
331 // First let's check if the product is choosen
332 if (this.isProductChoosen(product, request, session)) {
334 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
337 return "checked=\"checked\""; //NOI18N
340 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
348 * Some "getter" for choose from session
350 * @param product Product instance
351 * @param session Session instance
352 * @return Choose as string
355 public String getChooseFromSession (final Product product, final HttpSession session) {
357 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
359 // Is product and session set?
360 if (product == null) {
362 throw new NullPointerException("product is null"); //NOI18N
363 } else if (session == null) {
365 throw new NullPointerException("session is null"); //NOI18N
369 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
371 // Is the object null?
372 if (object == null) {
374 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
379 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
381 // Cast to string and return it
382 return (String) object;
386 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
388 * @param request Request instance
389 * @param session Session instance
390 * @return Whether the product is choosen
393 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
395 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
397 // Is product and session set?
398 if (request == null) {
400 throw new NullPointerException("request is null"); //NOI18N
401 } else if (session == null) {
403 throw new NullPointerException("session is null"); //NOI18N
406 // Is something selected?
407 if (this.calculateTotalAmount(request, session) > 0) {
409 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
411 // Something has been choosen
415 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
417 // Nothing choosen yet
418 return "disabled=\"disabled\""; //NOI18N
423 * Some "getter" for choosen (checkbox) from session
425 * @param product Product instance
426 * @param request Request instance
427 * @param session Session instance
428 * @return Amount as string
431 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
433 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
435 // Is product and session set?
436 if (product == null) {
438 throw new NullPointerException("product is null"); //NOI18N
439 } else if (request == null) {
441 throw new NullPointerException("request is null"); //NOI18N
442 } else if (session == null) {
444 throw new NullPointerException("session is null"); //NOI18N
448 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
449 String choosen = this.handleChooseFromRequestSession(product, request, session);
450 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
453 assert(choosen instanceof String): "choosen is null"; //NOI18N
456 if (choosen.isEmpty()) {
462 String amount = this.handleAmountFromRequestSession(product, request, session);
463 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
466 assert(amount instanceof String): "amount is null"; //NOI18N
469 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
470 // Choosen, but no amount
479 * Checks if given Product instance is available and returns a printable
480 * (human-readable) string.
482 * @param product Product instance to check
483 * @return Human-readable version of product availability
486 public String getPrintableProduktAvailability (final Product product) {
488 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
491 if (product == null) {
492 // Should not be null
493 throw new NullPointerException("product is null"); //NOI18N
497 if (product.getAvailable() == true) {
501 // Not, not for public
507 * Some getter for printable value from session or an empty string for null.
509 * @param session Session instance
510 * @param key Key to get
511 * @return Value from key, empty string for null
514 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
516 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
518 // Are both parameter not null?
519 if (session == null) {
521 throw new NullPointerException("session is null"); //NOI18N
522 } else if (key == null) {
524 throw new NullPointerException("key is null"); //NOI18N
528 Object value = this.getValueFromSession(session, key);
531 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
534 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
536 // Return actual value
537 return this.convertNullToEmpty(value);
541 * Some "getter" for a an array of only available products
543 * @return All products
546 public Iterator<Product> getAvailableProducts () throws ServletException {
548 // Ask frontend for a list of products
549 return this.productFrontend.getAvailableProducts();
550 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
551 throw new ServletException(ex);
556 * Some "getter" for a an array of all products
558 * @return All products
561 public Iterator<Product> getAllProducts () throws ServletException {
563 // Ask frontend for a list of products
564 return this.productFrontend.getAllProducts();
565 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
566 throw new ServletException(ex);
571 * Some "getter" for a an array of all categories
573 * @return All categories
576 public Iterator<Category> getCategories () throws ServletException {
578 // Ask frontend for a list of categories
579 return this.categoryFrontend.getCategories();
580 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
581 throw new ServletException(ex);
586 * Some "getter" for total price of position from request or session.
587 * Single price and amount is multiplyed.
589 * @param product Product instance
590 * @param request Request instance
591 * @param session Session instance
592 * @return Amount as string
595 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
597 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
599 // Is product and session set?
600 if (product == null) {
602 throw new NullPointerException("product is null"); //NOI18N
603 } else if (request == null) {
605 throw new NullPointerException("request is null"); //NOI18N
606 } else if (session == null) {
608 throw new NullPointerException("session is null"); //NOI18N
612 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
613 String choosen = this.handleChooseFromRequestSession(product, request, session);
614 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
617 assert(choosen instanceof String): "choosen is null"; //NOI18N
620 if (choosen.isEmpty()) {
622 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
627 String amount = this.handleAmountFromRequestSession(product, request, session);
628 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
631 assert(amount instanceof String): "amount is null"; //NOI18N
634 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
636 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
641 Integer value = null;
645 // Get amount as integer
646 value = Integer.valueOf(amount);
647 } catch (final NumberFormatException e) {
649 throw new IllegalArgumentException(e);
653 float price = (product.getPrice() * value);
656 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
658 // Then multiply it with price
663 * Handler for amount from request or session
665 * @param product Product instance
666 * @param request Request instance
667 * @param session Session instance
668 * @return Amount as string
671 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
673 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
675 // Is product and session set?
676 if (product == null) {
678 throw new NullPointerException("product is null"); //NOI18N
679 } else if (request == null) {
681 throw new NullPointerException("request is null"); //NOI18N
682 } else if (session == null) {
684 throw new NullPointerException("session is null"); //NOI18N
690 // Check request method
691 if (!"POST".equals(request.getMethod())) { //NOI18N
692 // Not POST, so get from session
693 return this.getAmountFromSession(product, session);
694 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
696 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
697 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
701 // Get attribute from request
702 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
705 if (object instanceof String) {
706 // Try to parse it to integer
708 Integer value = Integer.valueOf((String) object);
709 } catch (final NumberFormatException ex) {
711 this.getLogger().warn(ex);
715 // Then set it in session
716 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
719 return (String) object;
723 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
725 // Get attribute from session
726 return this.getAmountFromSession(product, session);
730 * Checks whether the given product is choosen, request overules session.
732 * @param product Product instance
733 * @param request Request instance
734 * @param session Session instance
735 * @return Whether the product is choosen
738 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
740 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
742 // Is product and session set?
743 if (product == null) {
745 throw new NullPointerException("product is null"); //NOI18N
746 } else if (request == null) {
748 throw new NullPointerException("request is null"); //NOI18N
749 } else if (session == null) {
751 throw new NullPointerException("session is null"); //NOI18N
755 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
756 String choosen = this.handleChooseFromRequestSession(product, request, session);
757 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
760 assert(choosen instanceof String): "choosen is null"; //NOI18N
762 // Is it not choosen?
763 if (choosen.isEmpty()) {
769 String amount = this.handleAmountFromRequestSession(product, request, session);
772 assert(amount instanceof String): "amount is not set"; //NOI18N
775 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
777 // Must not be empty and not 0
778 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
782 * Marks all choosen products as ordered
784 * @param request Request instance
785 * @param session Session instance
788 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
790 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
793 Iterator<Product> iterator = this.getAvailableProducts();
795 // "Walk" over all products
796 while (iterator.hasNext()) {
798 Product product = iterator.next();
801 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
804 if (this.isProductChoosen(product, request, session)) {
805 // Mark product as ordered
806 this.markProductAsOrdered(product, session);
811 this.getLogger().trace("EXIT!"); //NOI18N
815 * Marks given product as choosen in session
817 * @param product Product to mark as ordered
818 * @param session Session instance
821 public void markProductAsChoosen (final Product product, final HttpSession session) {
823 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
825 // Is product and session set?
826 if (product == null) {
828 throw new NullPointerException("product is null"); //NOI18N
829 } else if (session == null) {
831 throw new NullPointerException("session is null"); //NOI18N
834 // Mark it as ordered by setting flag
835 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
836 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
839 this.getLogger().trace("EXIT!"); //NOI18N
843 * Marks given product as ordered in session
845 * @param product Product to mark as ordered
846 * @param session Session instance
849 public void markProductAsOrdered (final Product product, final HttpSession session) {
851 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
853 // Is product and session set?
854 if (product == null) {
856 throw new NullPointerException("product is null"); //NOI18N
857 } else if (session == null) {
859 throw new NullPointerException("session is null"); //NOI18N
862 // Mark it as ordered by setting flag
863 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
864 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
867 this.getLogger().trace("EXIT!"); //NOI18N
871 * Somewhat setter in session
873 * @param session Session instance
874 * @param key Session key to set
875 * @param value Value to set
878 public void setValueInSession (final HttpSession session, final String key, final Object value) {
880 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
882 synchronized(session) {
884 session.setAttribute(key, value);
888 this.getLogger().trace("EXIT!"); //NOI18N
892 * Unmarks given product as choosen in session
894 * @param product Product to unmark as choosen
895 * @param session Session instance
898 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
900 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
902 // Is product and session set?
903 if (product == null) {
905 throw new NullPointerException("product is null"); //NOI18N
906 } else if (session == null) {
908 throw new NullPointerException("session is null"); //NOI18N
911 // Mark it as ordered by setting flag
912 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
913 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
916 this.getLogger().trace("EXIT!"); //NOI18N
920 * Unmarks given product as ordered in session
922 * @param product Product to unmark as ordered
923 * @param session Session instance
926 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
928 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
930 // Is product and session set?
931 if (product == null) {
933 throw new NullPointerException("product is null"); //NOI18N
934 } else if (session == null) {
936 throw new NullPointerException("session is null"); //NOI18N
939 // Mark it as ordered by setting flag
940 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
941 this.clearSessionAttribute(product, session, SESSION_ORDERED);
944 this.getLogger().trace("EXIT!"); //NOI18N
948 * Clears given parameter for product in session
950 * @param product Product instance
951 * @param session Session instance
952 * @param parameter Parameter to clear
954 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
956 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
959 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
960 this.setValueInSession(product, session, parameter, null);
963 this.getLogger().trace("EXIT!"); //NOI18N
967 * Some getter for value from session
969 * @param product Product instance
970 * @param session Session instance
971 * @param attribute Attribute to get value from
972 * @return Value from session
974 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
976 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
979 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
981 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
984 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
991 * Some getter for value from session
993 * @param session Session instance
994 * @param key Key to get value from
995 * @return Value from session
997 private Object getValueFromSession (final HttpSession session, final String key) {
999 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
1004 // Get it synchronized from session
1005 synchronized (session) {
1006 value = session.getAttribute(key);
1010 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1017 * Handler for choosen (checkbox) from request or session
1019 * @param product Product instance
1020 * @param request Request instance
1021 * @param session Session instance
1022 * @return Amount as string
1024 private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
1026 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1028 // Is product and session set?
1029 if (product == null) {
1031 throw new NullPointerException("product is null"); //NOI18N
1032 } else if (request == null) {
1034 throw new NullPointerException("request is null"); //NOI18N
1035 } else if (session == null) {
1037 throw new NullPointerException("session is null"); //NOI18N
1043 // Check request method
1044 if (!"POST".equals(request.getMethod())) { //NOI18N
1045 // Not POST, so get from session
1046 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1047 return this.getChooseFromSession(product, session);
1048 } else if (this.isProductOrdered(product, session)) {
1049 // Product is ordered
1050 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1051 return this.getChooseFromSession(product, session);
1052 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1054 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1055 return this.getChooseFromSession(product, session);
1058 // Get reqzest element
1059 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
1060 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1063 if (object == null) {
1065 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1066 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1067 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1069 // Return empty string
1073 // Then set it in session
1074 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1076 // Cast to string and return it
1077 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1078 return (String) object;
1082 * Initializes database frontends.
1084 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1086 this.productFrontend = new PizzaProductDatabaseFrontend();
1088 // Category frontend
1089 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1093 * Checks whether given category title is already used
1095 * @param title Title of category to check
1096 * @return Whether it has been found
1098 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1099 // Delegate to frontend
1100 return this.categoryFrontend.isCategoryTitleUsed(title);
1104 * Checks if given product title is already used
1105 * @param title Product title to check
1106 * @return Whether the product title has already been used
1108 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1109 // Delegate to frontend
1110 return this.productFrontend.isProductTitleUsed(title);
1114 * Checks if the product ordered?
1116 * @param product Product instance
1117 * @param session HttpSession instance
1120 private boolean isProductOrdered (final Product product, final HttpSession session) {
1122 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1125 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1126 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1129 return ("true".equals(isOrdered)); //NOI18N
1133 * Somewhat setter in session
1135 * @param product Product instance
1136 * @param session Session instance
1137 * @param keyPart Key part to include in final key
1138 * @param value Value to set
1140 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1142 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1145 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1146 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1149 this.getLogger().trace("EXIT!"); //NOI18N
1153 * Application starter
1155 private void start() {
1161 this.initProperties();
1162 } catch (final IOException ex) {
1164 this.abortProgramWithException(ex);
1168 Iterator<Product> iterator = null;
1172 iterator = this.getAvailableProducts();
1173 } catch (final ServletException ex) {
1174 this.abortProgramWithException(ex);
1177 // "Walk" over all products
1178 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1180 Product product = iterator.next();
1183 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1186 // Generate fake Customer instance
1187 Customer customer = new PizzaServiceCustomer();
1190 * Need a least a gender ... :( See, that is why I don't like default
1191 * constructors, you can easily miss something important and bam! You
1192 * get an NPE. The fix here is, to have construtors (or factories) which
1193 * requires all required instances that needs to be set to get a
1194 * consitent object back.
1197 // Gender is MALE now
1198 customer.setGender(Gender.MALE);
1201 Iterator<Map.Entry<Field, Object>> it = null;
1204 // Get iterator on all its fields
1205 it = customer.iterator();
1206 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1207 this.abortProgramWithException(ex);
1211 while ((it instanceof Iterator) && (it.hasNext())) {
1212 Map.Entry<Field, Object> entry = it.next();
1213 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1218 * Adds given category data from request to database
1220 * @param request Request instance
1223 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1225 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1227 // request must not be null
1228 if (request == null) {
1230 throw new NullPointerException("request is null"); //NOI18N
1234 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1235 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1238 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
1240 // Init variables for casting
1244 if (title == null) {
1246 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1247 } else if (title.isEmpty()) {
1249 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1250 } else if ((parent != null) && (!parent.isEmpty())) {
1251 // "parent" is set, so check it
1253 id = Integer.parseInt(parent);
1254 } catch (final NumberFormatException e) {
1256 throw new IllegalArgumentException(e);
1261 // Try to check if title is used already
1262 if (this.isCategoryTitleUsed(title)) {
1263 // Title already used
1264 throw new CategoryTitleAlreadyUsedException(request);
1266 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1267 throw new ServletException(ex);
1271 // The category is not found, so add it to database
1272 this.categoryFrontend.addCategory(title, id);
1273 } catch (final SQLException | IOException ex) {
1274 // Continue to throw it
1275 throw new ServletException(ex);
1279 this.getLogger().trace("EXIT!"); //NOI18N
1283 * Adds given product data from request to database
1285 * @param request Request instance
1288 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1290 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1292 // request must not be null
1293 if (request == null) {
1295 throw new NullPointerException("request is null"); //NOI18N
1298 // Get title, price and category id
1299 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1300 String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
1301 String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
1302 String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
1305 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available));
1307 // Variables for converting
1312 if (title == null) {
1314 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1315 } else if (title.isEmpty()) {
1317 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1318 } else if (price == null) {
1320 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1321 } else if (price.isEmpty()) {
1323 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1324 } else if (category == null) {
1326 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1327 } else if (category.isEmpty()) {
1329 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1330 } else if (available == null) {
1332 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1333 } else if (available.isEmpty()) {
1335 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1336 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
1338 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available)); //NOI18N
1343 id = Long.parseLong(category);
1344 p = Float.parseFloat(price);
1345 } catch (final NumberFormatException e) {
1347 throw new IllegalArgumentException(e);
1351 Boolean a = Boolean.parseBoolean(available);
1353 // Test on product title
1355 // Try to check if title is used already
1356 if (this.isProductTitleUsed(title)) {
1357 // Title already used
1358 throw new ProductTitleAlreadyUsedException(request);
1360 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1361 throw new ServletException(ex);
1365 // The product is not found, so add it to database
1366 this.productFrontend.addProduct(title, p, id, a);
1367 } catch (final SQLException | IOException ex) {
1368 // Continue to throw it
1369 throw new ServletException(ex);
1373 this.getLogger().trace("EXIT!"); //NOI18N
1377 * Generates link HTML code for given category's parent id, if set. This
1378 * link then points to products.jsp?category_id=x
1380 * @param category Category instance
1384 public String generateLinkForParent (final Category category) {
1386 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
1388 // category must not be null
1389 if (category == null) {
1391 throw new NullPointerException("category is null"); //NOI18N
1395 Long parent = category.getParent();
1399 // Product HTML code for link
1400 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
1408 public String getPrintableProduktCategory (final Product product) throws ServletException {
1410 this.getLogger().trace("product=" + product + " - CALLED!");
1412 // product must not be null
1413 if (product == null) {
1415 throw new NullPointerException("product is null");
1422 // Get Category instance from product over the frontend
1423 category = this.categoryFrontend.getCategory(product);
1424 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1425 throw new ServletException(ex);
1429 this.getLogger().debug("category=" + category);
1431 String title = null;
1433 // Now get title from it and return it
1434 title = category.decodedTitle();
1435 } catch (final UnsupportedEncodingException ex) {
1436 // Continue to throw as cause
1437 throw new ServletException(ex);
1441 this.getLogger().trace("title=" + title + " - EXIT!");
1448 * Checks if product's title is already used.
1450 * @param request Request instance
1451 * @return Whether the product title is already used
1452 * @throws java.io.IOException If any IO error occurs
1453 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1454 * @throws java.sql.SQLException If any SQL error occurs
1455 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1456 * @throws java.lang.NoSuchMethodException If a method was not found
1457 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1458 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1460 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1462 this.getLogger().trace("request=" + request + " - CALLED!");
1465 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1467 // request must not be null and "title" must be found and non-empty
1468 if (request == null) {
1470 throw new NullPointerException("request is null");
1471 } else if (title == null) {
1473 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1474 } else if (title.isEmpty()) {
1476 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1479 // Default is not used
1480 boolean isUsed = this.isProductTitleUsed(title);
1483 this.getLogger().trace("isUsed=" + isUsed + " - EXIT!");
1490 * Handles admin form requests
1491 * @param request Request instance
1492 * @param response Response instance
1493 * @throws ServletException If something unexpected happened
1496 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1498 this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!");
1500 // request and response must both be set
1501 if (request == null) {
1503 throw new NullPointerException("request is null");
1504 } else if (response == null) {
1506 throw new NullPointerException("response is null");
1509 // Try this operations
1512 if ("POST".equals(request.getMethod())) { //NOI18N
1513 // Is "add/edit/delete" set?
1514 if (request.getParameter("add") != null) { //NOI18N
1515 // Is it already added?
1516 if (this.isProductTitleUsed(request)) {
1518 this.getLogger().debug("Already used, redirecting ...");
1520 // Already added, so redirect here, else a ServletException will be thrown
1521 response.sendRedirect(request.getContextPath() + "/admin/product.jsp?already=1");
1524 this.doAdminAddProduct(request);
1526 } else if (request.getParameter("edit") != null) { //NOI18N
1528 } else if (request.getParameter("delete") != null) { //NOI18N
1532 // Redirect to proper URL
1533 // @TODO Commented out for developing:
1534 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1536 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1537 // Throw it as cause
1538 throw new ServletException(ex);
1542 this.getLogger().trace("EXIT!");
1546 * Handles admin form requests
1547 * @param request Request instance
1548 * @param response Response instance
1549 * @throws ServletException If something unexpected happened
1552 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1554 this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!");
1556 // request and response must both be set
1557 if (request == null) {
1559 throw new NullPointerException("request is null");
1560 } else if (response == null) {
1562 throw new NullPointerException("response is null");
1565 // Try this operations
1568 if ("POST".equals(request.getMethod())) { //NOI18N
1569 // Is "add/edit/delete" set?
1570 if (request.getParameter("add") != null) { //NOI18N
1571 // Is the category title already used?
1572 if (this.isCategoryTitleUsed(request)) {
1574 this.getLogger().debug("Already used, redirecting ...");
1576 // Already added, so redirect here, else a ServletException will be thrown
1577 response.sendRedirect(request.getContextPath() + "/admin/category.jsp?already=1");
1580 this.doAdminAddCategory(request);
1582 } else if (request.getParameter("edit") != null) { //NOI18N
1584 } else if (request.getParameter("delete") != null) { //NOI18N
1588 // Redirect to proper URL
1589 // @TODO Commented out for developing:
1590 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1592 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1593 // Throw it as cause
1594 throw new ServletException(ex);
1598 this.getLogger().trace("EXIT!");
1602 * Checks if category's title is already used.
1604 * @param request Request instance
1605 * @return Whether the product title is already used
1606 * @throws java.io.IOException If any IO error occurs
1607 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1608 * @throws java.sql.SQLException If any SQL error occurs
1609 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1610 * @throws java.lang.NoSuchMethodException If a method was not found
1611 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1612 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1614 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1616 this.getLogger().trace("request=" + request + " - CALLED!");
1619 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1621 // request must not be null and "title" must be found and non-empty
1622 if (request == null) {
1624 throw new NullPointerException("request is null");
1625 } else if (title == null) {
1627 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1628 } else if (title.isEmpty()) {
1630 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1633 // Default is not used
1634 boolean isUsed = this.isCategoryTitleUsed(title);
1637 this.getLogger().trace("isUsed=" + isUsed + " - EXIT!");