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));
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(context);
108 instance.getLogger().debug(MessageFormat.format("Created new instance {0} ...", object));
109 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
110 throw new ServletException(ex);
114 context.setAttribute("app", instance); //NOI18N
118 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
125 * For debugging purpose
127 * @param args Arguments
129 public static void main (String[] args) {
130 // Get instance and start it
131 new PizzaServiceApplication().start();
135 * Constructor with servet configuration
137 * @param context Servlet context
139 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
140 // Temporary initialize default bundle
141 // @TODO The JSF may have better internatialization support
144 // Initialize properties from config
145 this.initProperties(context);
147 // Init database frontends
148 this.initDatabaseFrontends();
152 * Default constructor
154 private PizzaServiceApplication () {
158 * Calculates total amount of all choosen products
160 * @param request Request instance
161 * @param session Session instance
162 * @return Total amount of all choosen products
165 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
167 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
169 // Is product and session set?
170 if (request == null) {
172 throw new NullPointerException("request is null"); //NOI18N
173 } else if (session == null) {
175 throw new NullPointerException("session is null"); //NOI18N
178 // Init/declare total price and iterator
180 Iterator<Product> iterator = this.getAvailableProducts();
182 // "Walk" over all products
183 while (iterator.hasNext()) {
185 Product product = iterator.next();
188 if (this.isProductChoosen(product, request, session)) {
189 // Then add ordered amount
190 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
193 String amount = this.getAmountFromSession(product, session);
196 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
197 totalAmount += Integer.valueOf(amount);
199 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
203 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
205 // Return total price
210 * Calculates total price of all choosen products
212 * @param request Request instance
213 * @param session Session instance
214 * @return Total price of all choosen products
217 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
219 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
221 // Is product and session set?
222 if (request == null) {
224 throw new NullPointerException("request is null"); //NOI18N
225 } else if (session == null) {
227 throw new NullPointerException("session is null"); //NOI18N
231 float totalPrice = 0.00f;
234 Iterator<Product> iterator = this.getAvailableProducts();
236 // "Walk" over all products
237 while (iterator.hasNext()) {
239 Product product = iterator.next();
242 if (this.isProductChoosen(product, request, session)) {
243 // Then add product's total price
244 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
245 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
247 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
251 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
253 // Return total price
258 public void doBootstrap () {
259 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
263 public void doMainLoop () {
264 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
268 public void doShutdown () {
269 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
273 * Some "getter" for amount from session
275 * @param product Product instance
276 * @param session Session instance
277 * @return Amount as string
280 public String getAmountFromSession (final Product product, final HttpSession session) {
282 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
284 // Is product and session set?
285 if (product == null) {
287 throw new NullPointerException("product is null"); //NOI18N
288 } else if (session == null) {
290 throw new NullPointerException("session is null"); //NOI18N
294 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
296 // Is the object null?
297 if (object == null) {
299 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
306 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
308 // Cast to string and return it
309 return (String) object;
313 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
315 * @param product Product instance
316 * @param request Request instance
317 * @param session Session instance
318 * @return Whether the product is choosen
321 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
323 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
325 // Is product and session set?
326 if (product == null) {
328 throw new NullPointerException("product is null"); //NOI18N
329 } else if (request == null) {
331 throw new NullPointerException("request is null"); //NOI18N
332 } else if (session == null) {
334 throw new NullPointerException("session is null"); //NOI18N
337 // First let's check if the product is choosen
338 if (this.isProductChoosen(product, request, session)) {
340 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
343 return "checked=\"checked\""; //NOI18N
346 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
354 * Some "getter" for choose from session
356 * @param product Product instance
357 * @param session Session instance
358 * @return Choose as string
361 public String getChooseFromSession (final Product product, final HttpSession session) {
363 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
365 // Is product and session set?
366 if (product == null) {
368 throw new NullPointerException("product is null"); //NOI18N
369 } else if (session == null) {
371 throw new NullPointerException("session is null"); //NOI18N
375 Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
377 // Is the object null?
378 if (object == null) {
380 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
385 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
387 // Cast to string and return it
388 return (String) object;
392 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
394 * @param request Request instance
395 * @param session Session instance
396 * @return Whether the product is choosen
399 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
401 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
403 // Is product and session set?
404 if (request == null) {
406 throw new NullPointerException("request is null"); //NOI18N
407 } else if (session == null) {
409 throw new NullPointerException("session is null"); //NOI18N
412 // Get "enabled" from request scope
413 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
416 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
418 // Is something selected?
419 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
421 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
423 // Something has been choosen
427 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
429 // Nothing choosen yet
430 return "disabled=\"disabled\""; //NOI18N
435 * Some "getter" for choosen (checkbox) from session
437 * @param product Product instance
438 * @param request Request instance
439 * @param session Session instance
440 * @return Amount as string
443 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
445 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
447 // Is product and session set?
448 if (product == null) {
450 throw new NullPointerException("product is null"); //NOI18N
451 } else if (request == null) {
453 throw new NullPointerException("request is null"); //NOI18N
454 } else if (session == null) {
456 throw new NullPointerException("session is null"); //NOI18N
460 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
461 String choosen = this.handleChooseFromRequestSession(product, request, session);
462 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
465 assert(choosen instanceof String): "choosen is null"; //NOI18N
468 if (choosen.isEmpty()) {
474 String amount = this.handleAmountFromRequestSession(product, request, session);
475 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
478 assert(amount instanceof String): "amount is null"; //NOI18N
481 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
482 // Choosen, but no amount
491 * Checks if given Product instance is available and returns a printable
492 * (human-readable) string.
494 * @param product Product instance to check
495 * @return Human-readable version of product availability
498 public String getPrintableProduktAvailability (final Product product) {
500 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
503 if (product == null) {
504 // Should not be null
505 throw new NullPointerException("product is null"); //NOI18N
509 if (product.getAvailable() == true) {
513 // Not, not for public
519 * Some getter for printable value from session or an empty string for null.
521 * @param session Session instance
522 * @param key Key to get
523 * @return Value from key, empty string for null
526 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
528 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
530 // Are both parameter not null?
531 if (session == null) {
533 throw new NullPointerException("session is null"); //NOI18N
534 } else if (key == null) {
536 throw new NullPointerException("key is null"); //NOI18N
540 Object value = this.getValueFromSession(session, key);
543 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
546 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
548 // Return actual value
549 return this.convertNullToEmpty(value);
553 * Some "getter" for a an array of only available products
555 * @return All products
558 public Iterator<Product> getAvailableProducts () throws ServletException {
560 // Ask frontend for a list of products
561 return this.productFrontend.getAvailableProducts();
562 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
563 throw new ServletException(ex);
568 * Some "getter" for a an array of all products
570 * @return All products
573 public Iterator<Product> getAllProducts () throws ServletException {
575 // Ask frontend for a list of products
576 return this.productFrontend.getAllProducts();
577 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
578 throw new ServletException(ex);
583 * Some "getter" for a an array of all categories
585 * @return All categories
588 public Iterator<Category> getCategories () throws ServletException {
590 // Ask frontend for a list of categories
591 return this.categoryFrontend.getCategories();
592 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
593 throw new ServletException(ex);
598 * Some "getter" for total price of position from request or session.
599 * Single price and amount is multiplyed.
601 * @param product Product instance
602 * @param request Request instance
603 * @param session Session instance
604 * @return Amount as string
607 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
609 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
611 // Is product and session set?
612 if (product == null) {
614 throw new NullPointerException("product is null"); //NOI18N
615 } else if (request == null) {
617 throw new NullPointerException("request is null"); //NOI18N
618 } else if (session == null) {
620 throw new NullPointerException("session is null"); //NOI18N
624 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
625 String choosen = this.handleChooseFromRequestSession(product, request, session);
626 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
629 assert(choosen instanceof String): "choosen is null"; //NOI18N
632 if (choosen.isEmpty()) {
634 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
639 String amount = this.handleAmountFromRequestSession(product, request, session);
640 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
643 assert(amount instanceof String): "amount is null"; //NOI18N
646 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
648 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
653 Integer value = null;
657 // Get amount as integer
658 value = Integer.valueOf(amount);
659 } catch (final NumberFormatException e) {
661 throw new IllegalArgumentException(e);
665 float price = (product.getPrice() * value);
668 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
670 // Then multiply it with price
675 * Handler for amount from request or session
677 * @param product Product instance
678 * @param request Request instance
679 * @param session Session instance
680 * @return Amount as string
683 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
685 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
687 // Is product and session set?
688 if (product == null) {
690 throw new NullPointerException("product is null"); //NOI18N
691 } else if (request == null) {
693 throw new NullPointerException("request is null"); //NOI18N
694 } else if (session == null) {
696 throw new NullPointerException("session is null"); //NOI18N
702 // Check request method
703 if (!"POST".equals(request.getMethod())) { //NOI18N
704 // Not POST, so get from session
705 return this.getAmountFromSession(product, session);
706 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
708 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
709 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
713 // Get attribute from request
714 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
717 if (object instanceof String) {
718 // Try to parse it to integer
720 Integer value = Integer.valueOf((String) object);
721 } catch (final NumberFormatException ex) {
723 this.getLogger().warn(ex);
727 // Then set it in session
728 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
731 return (String) object;
735 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
737 // Get attribute from session
738 return this.getAmountFromSession(product, session);
742 * Checks whether the given product is choosen, request overules session.
744 * @param product Product instance
745 * @param request Request instance
746 * @param session Session instance
747 * @return Whether the product is choosen
750 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
752 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
754 // Is product and session set?
755 if (product == null) {
757 throw new NullPointerException("product is null"); //NOI18N
758 } else if (request == null) {
760 throw new NullPointerException("request is null"); //NOI18N
761 } else if (session == null) {
763 throw new NullPointerException("session is null"); //NOI18N
767 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
768 String choosen = this.handleChooseFromRequestSession(product, request, session);
769 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
772 assert(choosen instanceof String): "choosen is null"; //NOI18N
774 // Is it not choosen?
775 if (choosen.isEmpty()) {
781 String amount = this.handleAmountFromRequestSession(product, request, session);
784 assert(amount instanceof String): "amount is not set"; //NOI18N
787 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
789 // Must not be empty and not 0
790 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
794 * Marks all choosen products as ordered
796 * @param request Request instance
797 * @param session Session instance
800 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
802 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
805 Iterator<Product> iterator = this.getAvailableProducts();
807 // "Walk" over all products
808 while (iterator.hasNext()) {
810 Product product = iterator.next();
813 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
816 if (this.isProductChoosen(product, request, session)) {
817 // Mark product as ordered
818 this.markProductAsOrdered(product, session);
823 this.getLogger().trace("EXIT!"); //NOI18N
827 * Marks given product as choosen in session
829 * @param product Product to mark as ordered
830 * @param session Session instance
833 public void markProductAsChoosen (final Product product, final HttpSession session) {
835 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
837 // Is product and session set?
838 if (product == null) {
840 throw new NullPointerException("product is null"); //NOI18N
841 } else if (session == null) {
843 throw new NullPointerException("session is null"); //NOI18N
846 // Mark it as ordered by setting flag
847 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
848 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
851 this.getLogger().trace("EXIT!"); //NOI18N
855 * Marks given product as ordered in session
857 * @param product Product to mark as ordered
858 * @param session Session instance
861 public void markProductAsOrdered (final Product product, final HttpSession session) {
863 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
865 // Is product and session set?
866 if (product == null) {
868 throw new NullPointerException("product is null"); //NOI18N
869 } else if (session == null) {
871 throw new NullPointerException("session is null"); //NOI18N
874 // Mark it as ordered by setting flag
875 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
876 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
879 this.getLogger().trace("EXIT!"); //NOI18N
883 * Somewhat setter in session
885 * @param session Session instance
886 * @param key Session key to set
887 * @param value Value to set
890 public void setValueInSession (final HttpSession session, final String key, final Object value) {
892 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
894 synchronized(session) {
896 session.setAttribute(key, value);
900 this.getLogger().trace("EXIT!"); //NOI18N
904 * Unmarks given product as choosen in session
906 * @param product Product to unmark as choosen
907 * @param session Session instance
910 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
912 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
914 // Is product and session set?
915 if (product == null) {
917 throw new NullPointerException("product is null"); //NOI18N
918 } else if (session == null) {
920 throw new NullPointerException("session is null"); //NOI18N
923 // Mark it as ordered by setting flag
924 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
925 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
928 this.getLogger().trace("EXIT!"); //NOI18N
932 * Unmarks given product as ordered in session
934 * @param product Product to unmark as ordered
935 * @param session Session instance
938 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
940 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
942 // Is product and session set?
943 if (product == null) {
945 throw new NullPointerException("product is null"); //NOI18N
946 } else if (session == null) {
948 throw new NullPointerException("session is null"); //NOI18N
951 // Mark it as ordered by setting flag
952 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
953 this.clearSessionAttribute(product, session, SESSION_ORDERED);
956 this.getLogger().trace("EXIT!"); //NOI18N
960 * Clears given parameter for product in session
962 * @param product Product instance
963 * @param session Session instance
964 * @param parameter Parameter to clear
966 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
968 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
971 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
972 this.setValueInSession(product, session, parameter, null);
975 this.getLogger().trace("EXIT!"); //NOI18N
979 * Some getter for value from session
981 * @param product Product instance
982 * @param session Session instance
983 * @param attribute Attribute to get value from
984 * @return Value from session
986 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
988 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
991 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
993 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
996 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1003 * Some getter for value from session
1005 * @param session Session instance
1006 * @param key Key to get value from
1007 * @return Value from session
1009 private Object getValueFromSession (final HttpSession session, final String key) {
1011 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
1016 // Get it synchronized from session
1017 synchronized (session) {
1018 value = session.getAttribute(key);
1022 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1029 * Handler for choosen (checkbox) from request or session
1031 * @param product Product instance
1032 * @param request Request instance
1033 * @param session Session instance
1034 * @return Amount as string
1036 private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
1038 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1040 // Is product and session set?
1041 if (product == null) {
1043 throw new NullPointerException("product is null"); //NOI18N
1044 } else if (request == null) {
1046 throw new NullPointerException("request is null"); //NOI18N
1047 } else if (session == null) {
1049 throw new NullPointerException("session is null"); //NOI18N
1055 // Check request method
1056 if (!"POST".equals(request.getMethod())) { //NOI18N
1057 // Not POST, so get from session
1058 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1059 return this.getChooseFromSession(product, session);
1060 } else if (this.isProductOrdered(product, session)) {
1061 // Product is ordered
1062 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1063 return this.getChooseFromSession(product, session);
1064 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1066 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1067 return this.getChooseFromSession(product, session);
1070 // Get reqzest element
1071 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getId()));
1072 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1075 if (object == null) {
1077 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1078 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
1079 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1081 // Return empty string
1085 // Then set it in session
1086 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, object);
1088 // Cast to string and return it
1089 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1090 return (String) object;
1094 * Initializes database frontends.
1096 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1098 this.productFrontend = new PizzaProductDatabaseFrontend();
1100 // Category frontend
1101 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1105 * Checks whether given category title is already used
1107 * @param title Title of category to check
1108 * @return Whether it has been found
1110 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1111 // Delegate to frontend
1112 return this.categoryFrontend.isCategoryTitleUsed(title);
1116 * Checks if given product title is already used
1117 * @param title Product title to check
1118 * @return Whether the product title has already been used
1120 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1121 // Delegate to frontend
1122 return this.productFrontend.isProductTitleUsed(title);
1126 * Checks if the product ordered?
1128 * @param product Product instance
1129 * @param session HttpSession instance
1130 * @return Whether the product has been ordered
1132 private boolean isProductOrdered (final Product product, final HttpSession session) {
1134 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1137 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1138 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1141 return ("true".equals(isOrdered)); //NOI18N
1145 * Somewhat setter in session
1147 * @param product Product instance
1148 * @param session Session instance
1149 * @param keyPart Key part to include in final key
1150 * @param value Value to set
1152 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1154 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1157 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1158 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1161 this.getLogger().trace("EXIT!"); //NOI18N
1165 * Application starter
1167 private void start() {
1173 this.initProperties();
1176 this.initDatabaseFrontends();
1177 } catch (final IOException | UnsupportedDatabaseBackendException | SQLException ex) {
1179 this.abortProgramWithException(ex);
1183 Iterator<Product> iterator = null;
1187 iterator = this.getAvailableProducts();
1188 } catch (final ServletException ex) {
1189 this.abortProgramWithException(ex);
1192 // "Walk" over all products
1193 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1195 Product product = iterator.next();
1198 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1201 // Generate fake Customer instance
1202 Customer customer = new PizzaServiceCustomer();
1205 * Need a least a gender ... :( See, that is why I don't like default
1206 * constructors, you can easily miss something important and bam! You
1207 * get an NPE. The fix here is, to have construtors (or factories) which
1208 * requires all required instances that needs to be set to get a
1209 * consitent object back.
1212 // Gender is MALE now
1213 customer.setGender(Gender.MALE);
1216 Iterator<Map.Entry<Field, Object>> it = null;
1219 // Get iterator on all its fields
1220 it = customer.iterator();
1221 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1222 this.abortProgramWithException(ex);
1226 while ((it instanceof Iterator) && (it.hasNext())) {
1227 Map.Entry<Field, Object> entry = it.next();
1228 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1233 * Adds given category data from request to database
1235 * @param request Request instance
1238 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1240 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1242 // request must not be null
1243 if (request == null) {
1245 throw new NullPointerException("request is null"); //NOI18N
1249 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1250 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1253 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
1255 // Init variables for casting
1259 if (title == null) {
1261 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1262 } else if (title.isEmpty()) {
1264 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1265 } else if ((parent != null) && (!parent.isEmpty())) {
1266 // "parent" is set, so check it
1268 id = Integer.parseInt(parent);
1269 } catch (final NumberFormatException e) {
1271 throw new IllegalArgumentException(e);
1276 // Try to check if title is used already
1277 if (this.isCategoryTitleUsed(title)) {
1278 // Title already used
1279 throw new CategoryTitleAlreadyUsedException(request);
1281 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1282 throw new ServletException(ex);
1286 // The category is not found, so add it to database
1287 this.categoryFrontend.addCategory(title, id);
1288 } catch (final SQLException | IOException ex) {
1289 // Continue to throw it
1290 throw new ServletException(ex);
1294 this.getLogger().trace("EXIT!"); //NOI18N
1298 * Adds given product data from request to database
1300 * @param request Request instance
1303 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1305 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1307 // request must not be null
1308 if (request == null) {
1310 throw new NullPointerException("request is null"); //NOI18N
1313 // Get title, price and category id
1314 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1315 String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
1316 String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
1317 String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
1320 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
1322 // Variables for converting
1327 if (title == null) {
1329 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1330 } else if (title.isEmpty()) {
1332 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1333 } else if (price == null) {
1335 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1336 } else if (price.isEmpty()) {
1338 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1339 } else if (category == null) {
1341 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1342 } else if (category.isEmpty()) {
1344 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1345 } else if (available == null) {
1347 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1348 } else if (available.isEmpty()) {
1350 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1351 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
1353 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available)); //NOI18N
1358 id = Long.parseLong(category);
1359 p = Float.parseFloat(price);
1360 } catch (final NumberFormatException e) {
1362 throw new IllegalArgumentException(e);
1366 Boolean a = Boolean.parseBoolean(available);
1368 // Test on product title
1370 // Try to check if title is used already
1371 if (this.isProductTitleUsed(title)) {
1372 // Title already used
1373 throw new ProductTitleAlreadyUsedException(request);
1375 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1376 throw new ServletException(ex);
1380 // The product is not found, so add it to database
1381 this.productFrontend.addProduct(title, p, id, a);
1382 } catch (final SQLException | IOException ex) {
1383 // Continue to throw it
1384 throw new ServletException(ex);
1388 this.getLogger().trace("EXIT!"); //NOI18N
1392 * Generates link HTML code for given category's parent id, if set. This
1393 * link then points to products.jsp?category_id=x
1395 * @param category Category instance
1399 public String generateLinkForParent (final Category category) {
1401 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
1403 // category must not be null
1404 if (category == null) {
1406 throw new NullPointerException("category is null"); //NOI18N
1410 Long parent = category.getParent();
1414 // Product HTML code for link
1415 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
1423 public String getPrintableProduktCategory (final Product product) throws ServletException {
1425 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
1427 // product must not be null
1428 if (product == null) {
1430 throw new NullPointerException("product is null"); //NOI18N
1437 // Get Category instance from product over the frontend
1438 category = this.categoryFrontend.getCategory(product);
1439 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1440 throw new ServletException(ex);
1444 this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
1446 String title = null;
1448 // Now get title from it and return it
1449 title = category.decodedTitle();
1450 } catch (final UnsupportedEncodingException ex) {
1451 // Continue to throw as cause
1452 throw new ServletException(ex);
1456 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
1463 * Checks if product's title is already used.
1465 * @param request Request instance
1466 * @return Whether the product title is already used
1467 * @throws java.io.IOException If any IO error occurs
1468 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1469 * @throws java.sql.SQLException If any SQL error occurs
1470 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1471 * @throws java.lang.NoSuchMethodException If a method was not found
1472 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1473 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1475 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1477 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1480 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1482 // request must not be null and "title" must be found and non-empty
1483 if (request == null) {
1485 throw new NullPointerException("request is null"); //NOI18N
1486 } else if (title == null) {
1488 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1489 } else if (title.isEmpty()) {
1491 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1494 // Default is not used
1495 boolean isUsed = this.isProductTitleUsed(title);
1498 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1505 * Handles admin form requests
1506 * @param request Request instance
1507 * @param response Response instance
1508 * @throws ServletException If something unexpected happened
1511 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1513 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1515 // request and response must both be set
1516 if (request == null) {
1518 throw new NullPointerException("request is null"); //NOI18N
1519 } else if (response == null) {
1521 throw new NullPointerException("response is null"); //NOI18N
1524 // Try this operations
1527 if ("POST".equals(request.getMethod())) { //NOI18N
1528 // Is "add/edit/delete" set?
1529 if (request.getParameter("add") != null) { //NOI18N
1530 // Is it already added?
1531 if (this.isProductTitleUsed(request)) {
1533 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1535 // Already added, so redirect here, else a ServletException will be thrown
1536 response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
1539 this.doAdminAddProduct(request);
1541 } else if (request.getParameter("edit") != null) { //NOI18N
1543 } else if (request.getParameter("delete") != null) { //NOI18N
1547 // Redirect to proper URL
1548 // @TODO Commented out for developing:
1549 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1551 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1552 // Throw it as cause
1553 throw new ServletException(ex);
1557 this.getLogger().trace("EXIT!"); //NOI18N
1561 * Handles admin form requests
1562 * @param request Request instance
1563 * @param response Response instance
1564 * @throws ServletException If something unexpected happened
1567 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1569 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1571 // request and response must both be set
1572 if (request == null) {
1574 throw new NullPointerException("request is null"); //NOI18N
1575 } else if (response == null) {
1577 throw new NullPointerException("response is null"); //NOI18N
1580 // Try this operations
1583 if ("POST".equals(request.getMethod())) { //NOI18N
1584 // Is "add/edit/delete" set?
1585 if (request.getParameter("add") != null) { //NOI18N
1586 // Is the category title already used?
1587 if (this.isCategoryTitleUsed(request)) {
1589 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1591 // Already added, so redirect here, else a ServletException will be thrown
1592 response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
1595 this.doAdminAddCategory(request);
1597 } else if (request.getParameter("edit") != null) { //NOI18N
1599 } else if (request.getParameter("delete") != null) { //NOI18N
1603 // Redirect to proper URL
1604 // @TODO Commented out for developing:
1605 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1607 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1608 // Throw it as cause
1609 throw new ServletException(ex);
1613 this.getLogger().trace("EXIT!"); //NOI18N
1617 * Checks if category's title is already used.
1619 * @param request Request instance
1620 * @return Whether the product title is already used
1621 * @throws java.io.IOException If any IO error occurs
1622 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1623 * @throws java.sql.SQLException If any SQL error occurs
1624 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1625 * @throws java.lang.NoSuchMethodException If a method was not found
1626 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1627 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1629 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1631 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1634 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1636 // request must not be null and "title" must be found and non-empty
1637 if (request == null) {
1639 throw new NullPointerException("request is null"); //NOI18N
1640 } else if (title == null) {
1642 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1643 } else if (title.isEmpty()) {
1645 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1648 // Default is not used
1649 boolean isUsed = this.isCategoryTitleUsed(title);
1652 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N