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.InvocationTargetException;
22 import java.sql.SQLException;
23 import java.text.MessageFormat;
24 import java.util.Iterator;
25 import javax.servlet.ServletContext;
26 import javax.servlet.ServletException;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29 import javax.servlet.http.HttpSession;
30 import org.mxchange.jcore.exceptions.BadTokenException;
31 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
32 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
33 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
34 import org.mxchange.pizzaapplication.category.Category;
35 import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend;
36 import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
37 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
38 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
39 import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
40 import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
41 import org.mxchange.pizzaapplication.item.AddableBasketItem;
42 import org.mxchange.pizzaapplication.product.Product;
45 * Main application class
47 * @author Roland Haeder
49 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
51 * Frontend for products
53 private ProductFrontend productFrontend;
56 * Frontend for categories
58 private CategoryFrontend categoryFrontend;
63 public PizzaServiceApplication () {
65 this.getLogger().trace("CALLED!");
69 public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
71 this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); //NOI18N
73 // context should not be null
74 if (null == context) {
76 throw new NullPointerException("context is null");
79 // Is the bundle initialized?
80 if (!this.isBundledInitialized()) {
81 // Temporary initialize default bundle
82 // @TODO The enum Gender uses this
86 // Initialize properties from context
87 this.initProperties(context);
89 // Init database frontends
90 this.initDatabaseFrontends();
93 this.getLogger().trace("EXIT!"); //NOI18N
97 * Calculates total amount of all choosen products
99 * @param request Request instance
100 * @param session Session instance
101 * @return Total amount of all choosen products
104 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
106 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
108 // Is product and session set?
109 if (null == request) {
111 throw new NullPointerException("request is null"); //NOI18N
112 } else if (null == session) {
114 throw new NullPointerException("session is null"); //NOI18N
117 // Init/declare total price and iterator
119 Iterator<Product> iterator = this.getAvailableProducts();
121 // "Walk" over all products
122 while (iterator.hasNext()) {
124 Product product = iterator.next();
127 if (this.isProductChoosen(product, request, session)) {
128 // Then add ordered amount
129 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
132 String amount = this.getAmountFromSession(product, session);
135 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
136 totalAmount += Integer.valueOf(amount);
138 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
142 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
144 // Return total price
149 * Calculates total price of all choosen products
151 * @param request Request instance
152 * @param session Session instance
153 * @return Total price of all choosen products
156 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
158 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
160 // Is product and session set?
161 if (null == request) {
163 throw new NullPointerException("request is null"); //NOI18N
164 } else if (null == session) {
166 throw new NullPointerException("session is null"); //NOI18N
170 float totalPrice = 0.00f;
173 Iterator<Product> iterator = this.getAvailableProducts();
175 // "Walk" over all products
176 while (iterator.hasNext()) {
178 Product product = iterator.next();
181 if (this.isProductChoosen(product, request, session)) {
182 // Then add product's total price
183 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getItemId())); //NOI18N
184 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
186 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getItemId(), totalPrice)); //NOI18N
190 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
192 // Return total price
197 public void doBootstrap () {
198 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
202 public void doMainLoop () {
203 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
207 public void doShutdown () {
208 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
212 * Some "getter" for amount from session
214 * @param product Product instance
215 * @param session Session instance
216 * @return Amount as string
220 public String getAmountFromSession (final Product product, final HttpSession session) {
222 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
224 // Is product and session set?
225 if (null == product) {
227 throw new NullPointerException("product is null"); //NOI18N
228 } else if (null == session) {
230 throw new NullPointerException("session is null"); //NOI18N
234 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
236 // Is the object null?
237 if (null == object) {
239 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
246 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
248 // Cast to string and return it
249 return (String) object;
253 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
255 * @param product Product instance
256 * @param request Request instance
257 * @param session Session instance
258 * @return Whether the product is choosen
261 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
263 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
265 // Is product and session set?
266 if (null == product) {
268 throw new NullPointerException("product is null"); //NOI18N
269 } else if (null == request) {
271 throw new NullPointerException("request is null"); //NOI18N
272 } else if (null == session) {
274 throw new NullPointerException("session is null"); //NOI18N
277 // First let's check if the product is choosen
278 if (this.isProductChoosen(product, request, session)) {
280 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
283 return "checked=\"checked\""; //NOI18N
286 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
294 * Some "getter" for choose from session
296 * @param product Product instance
297 * @param session Session instance
298 * @return Choose as string
302 public String getChooseFromSession (final Product product, final HttpSession session) {
304 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
306 // Is product and session set?
307 if (null == product) {
309 throw new NullPointerException("product is null"); //NOI18N
310 } else if (null == session) {
312 throw new NullPointerException("session is null"); //NOI18N
316 Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
318 // Is the object null?
319 if (null == object) {
321 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getItemId())); //NOI18N
326 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
328 // Cast to string and return it
329 return (String) object;
333 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
335 * @param request Request instance
336 * @param session Session instance
337 * @return Whether the product is choosen
340 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
342 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
344 // Is product and session set?
345 if (null == request) {
347 throw new NullPointerException("request is null"); //NOI18N
348 } else if (null == session) {
350 throw new NullPointerException("session is null"); //NOI18N
353 // Get "enabled" from request scope
354 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
357 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
359 // Is something selected?
360 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
362 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
364 // Something has been choosen
368 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
370 // Nothing choosen yet
371 return "disabled=\"disabled\""; //NOI18N
376 * Some "getter" for choosen (checkbox) from session
378 * @param product Product instance
379 * @param request Request instance
380 * @param session Session instance
381 * @return Amount as string
385 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
387 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
389 // Is product and session set?
390 if (null == product) {
392 throw new NullPointerException("product is null"); //NOI18N
393 } else if (null == request) {
395 throw new NullPointerException("request is null"); //NOI18N
396 } else if (null == session) {
398 throw new NullPointerException("session is null"); //NOI18N
402 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
403 String choosen = this.handleChooseFromRequestSession(product, request, session);
404 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
407 assert(choosen instanceof String): "choosen is null"; //NOI18N
410 if (choosen.isEmpty()) {
416 String amount = this.handleAmountFromRequestSession(product, request, session);
417 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
420 assert(amount instanceof String): "amount is null"; //NOI18N
423 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
424 // Choosen, but no amount
433 * Checks if given Product instance is available and returns a printable
434 * (human-readable) string.
436 * @param product Product instance to check
437 * @return Human-readable version of product availability
440 public String getPrintableProduktAvailability (final Product product) {
442 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
445 if (null == product) {
446 // Should not be null
447 throw new NullPointerException("product is null"); //NOI18N
451 if (product.getAvailable() == true) {
455 // Not, not for public
461 * Some getter for printable value from session or an empty string for null.
463 * @param session Session instance
464 * @param key Key to get
465 * @return Value from key, empty string for null
468 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
470 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
472 // Are both parameter not null?
473 if (null == session) {
475 throw new NullPointerException("session is null"); //NOI18N
476 } else if (null == key) {
478 throw new NullPointerException("key is null"); //NOI18N
482 Object value = this.getValueFromSession(session, key);
485 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
488 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
490 // Return actual value
491 return this.convertNullToEmpty(value);
495 * Some "getter" for a an array of only available products
497 * @return All products
500 public Iterator<Product> getAvailableProducts () throws ServletException {
501 // categoryFrontend must be set
502 if (null == this.productFrontend) {
504 throw new NullPointerException("productFrontend is null");
508 // Ask frontend for a list of products
509 return this.productFrontend.getAvailableProducts();
510 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
511 throw new ServletException(ex);
516 * Some "getter" for a an array of all products
518 * @return All products
521 public Iterator<Product> getAllProducts () throws ServletException {
523 this.getLogger().trace("CALLED!");
525 // categoryFrontend must be set
526 if (null == this.productFrontend) {
528 throw new NullPointerException("productFrontend is null");
532 // Ask frontend for a list of products
533 return this.productFrontend.getAllProducts();
534 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
535 throw new ServletException(ex);
540 * Some "getter" for a an array of all categories
542 * @return All categories
545 public Iterator<Category> getCategories () throws ServletException {
547 this.getLogger().trace("CALLED!");
549 // categoryFrontend must be set
550 if (null == this.categoryFrontend) {
552 throw new NullPointerException("categoryFrontend is null");
556 // Ask frontend for a list of categories
557 return this.categoryFrontend.getCategories();
558 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
559 throw new ServletException(ex);
564 * Some "getter" for total price of position from request or session.
565 * Single price and amount is multiplyed.
567 * @param product Product instance
568 * @param request Request instance
569 * @param session Session instance
570 * @return Amount as string
574 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
576 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
578 // Is product and session set?
579 if (null == product) {
581 throw new NullPointerException("product is null"); //NOI18N
582 } else if (null == request) {
584 throw new NullPointerException("request is null"); //NOI18N
585 } else if (null == session) {
587 throw new NullPointerException("session is null"); //NOI18N
591 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
592 String choosen = this.handleChooseFromRequestSession(product, request, session);
593 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
596 assert(choosen instanceof String): "choosen is null"; //NOI18N
599 if (choosen.isEmpty()) {
601 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getItemId(), choosen)); //NOI18N
606 String amount = this.handleAmountFromRequestSession(product, request, session);
607 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
610 assert(amount instanceof String): "amount is null"; //NOI18N
613 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
615 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getItemId(), amount)); //NOI18N
620 Integer value = null;
624 // Get amount as integer
625 value = Integer.valueOf(amount);
626 } catch (final NumberFormatException e) {
628 throw new IllegalArgumentException(e);
632 float price = (product.getPrice() * value);
635 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getItemId(), price)); //NOI18N
637 // Then multiply it with price
642 * Handler for amount from request or session
644 * @param product Product instance
645 * @param request Request instance
646 * @param session Session instance
647 * @return Amount as string
651 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
653 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
655 // Is product and session set?
656 if (null == product) {
658 throw new NullPointerException("product is null"); //NOI18N
659 } else if (null == request) {
661 throw new NullPointerException("request is null"); //NOI18N
662 } else if (null == session) {
664 throw new NullPointerException("session is null"); //NOI18N
670 // Check request method
671 if (!"POST".equals(request.getMethod())) { //NOI18N
672 // Not POST, so get from session
673 return this.getAmountFromSession(product, session);
674 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
676 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
677 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getItemId())); //NOI18N
681 // Get attribute from request
682 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getItemId()));
685 if (object instanceof String) {
686 // Try to parse it to integer
688 Integer value = Integer.valueOf((String) object);
689 } catch (final NumberFormatException ex) {
691 this.getLogger().warn(ex);
695 // Then set it in session
696 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
699 return (String) object;
703 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
705 // Get attribute from session
706 return this.getAmountFromSession(product, session);
710 * Checks whether the given product is choosen, request overules session.
712 * @param product Product instance
713 * @param request Request instance
714 * @param session Session instance
715 * @return Whether the product is choosen
719 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
721 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
723 // Is product and session set?
724 if (null == product) {
726 throw new NullPointerException("product is null"); //NOI18N
727 } else if (null == request) {
729 throw new NullPointerException("request is null"); //NOI18N
730 } else if (null == session) {
732 throw new NullPointerException("session is null"); //NOI18N
736 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
737 String choosen = this.handleChooseFromRequestSession(product, request, session);
738 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
741 assert(choosen instanceof String): "choosen is null"; //NOI18N
743 // Is it not choosen?
744 if (choosen.isEmpty()) {
750 String amount = this.handleAmountFromRequestSession(product, request, session);
753 assert(amount instanceof String): "amount is not set"; //NOI18N
756 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
758 // Must not be empty and not 0
759 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
763 * Marks all choosen products as ordered
765 * @param request Request instance
766 * @param session Session instance
770 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
772 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
775 Iterator<Product> iterator = this.getAvailableProducts();
777 // "Walk" over all products
778 while (iterator.hasNext()) {
780 Product product = iterator.next();
783 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
786 if (this.isProductChoosen(product, request, session)) {
787 // Mark product as ordered
788 this.markProductAsOrdered(product, session);
793 this.getLogger().trace("EXIT!"); //NOI18N
797 * Marks given product as choosen in session
799 * @param product Product to mark as ordered
800 * @param session Session instance
804 public void markProductAsChoosen (final Product product, final HttpSession session) {
806 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
808 // Is product and session set?
809 if (null == product) {
811 throw new NullPointerException("product is null"); //NOI18N
812 } else if (null == session) {
814 throw new NullPointerException("session is null"); //NOI18N
817 // Mark it as ordered by setting flag
818 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getItemId())); //NOI18N
819 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
822 this.getLogger().trace("EXIT!"); //NOI18N
826 * Marks given product as ordered in session
828 * @param product Product to mark as ordered
829 * @param session Session instance
833 public void markProductAsOrdered (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 (null == product) {
840 throw new NullPointerException("product is null"); //NOI18N
841 } else if (null == session) {
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 ordered.", product.getItemId())); //NOI18N
848 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
851 this.getLogger().trace("EXIT!"); //NOI18N
855 * Somewhat setter in session
857 * @param session Session instance
858 * @param key Session key to set
859 * @param value Value to set
862 public void setValueInSession (final HttpSession session, final String key, final Object value) {
864 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
866 synchronized(session) {
868 session.setAttribute(key, value);
872 this.getLogger().trace("EXIT!"); //NOI18N
876 * Unmarks given product as choosen in session
878 * @param product Product to unmark as choosen
879 * @param session Session instance
883 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
885 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
887 // Is product and session set?
888 if (null == product) {
890 throw new NullPointerException("product is null"); //NOI18N
891 } else if (null == session) {
893 throw new NullPointerException("session is null"); //NOI18N
896 // Mark it as ordered by setting flag
897 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getItemId())); //NOI18N
898 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
901 this.getLogger().trace("EXIT!"); //NOI18N
905 * Unmarks given product as ordered in session
907 * @param product Product to unmark as ordered
908 * @param session Session instance
912 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
914 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
916 // Is product and session set?
917 if (null == product) {
919 throw new NullPointerException("product is null"); //NOI18N
920 } else if (null == session) {
922 throw new NullPointerException("session is null"); //NOI18N
925 // Mark it as ordered by setting flag
926 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getItemId())); //NOI18N
927 this.clearSessionAttribute(product, session, SESSION_ORDERED);
930 this.getLogger().trace("EXIT!"); //NOI18N
934 * Clears given parameter for product in session
936 * @param product Product instance
937 * @param session Session instance
938 * @param parameter Parameter to clear
940 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
942 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
945 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
946 this.setValueInSession(product, session, parameter, null);
949 this.getLogger().trace("EXIT!"); //NOI18N
953 * Some getter for value from session
955 * @param product Product instance
956 * @param session Session instance
957 * @param attribute Attribute to get value from
958 * @return Value from session
960 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
962 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
965 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
967 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
970 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
977 * Some getter for value from session
979 * @param session Session instance
980 * @param key Key to get value from
981 * @return Value from session
983 private Object getValueFromSession (final HttpSession session, final String key) {
985 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
990 // Get it synchronized from session
991 synchronized (session) {
992 value = session.getAttribute(key);
996 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1003 * Handler for choosen (checkbox) from request or session
1005 * @param product Product instance
1006 * @param request Request instance
1007 * @param session Session instance
1008 * @return Amount as string
1010 private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
1012 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1014 // Is product and session set?
1015 if (null == product) {
1017 throw new NullPointerException("product is null"); //NOI18N
1018 } else if (null == request) {
1020 throw new NullPointerException("request is null"); //NOI18N
1021 } else if (null == session) {
1023 throw new NullPointerException("session is null"); //NOI18N
1029 // Check request method
1030 if (!"POST".equals(request.getMethod())) { //NOI18N
1031 // Not POST, so get from session
1032 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1033 return this.getChooseFromSession(product, session);
1034 } else if (this.isProductOrdered(product, session)) {
1035 // Product is ordered
1036 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1037 return this.getChooseFromSession(product, session);
1038 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1040 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1041 return this.getChooseFromSession(product, session);
1044 // Get reqzest element
1045 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getItemId()));
1046 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getItemId(), object)); //NOI18N
1049 if (null == object) {
1051 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getItemId())); //NOI18N
1052 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
1053 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1055 // Return empty string
1059 // Then set it in session
1060 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, object);
1062 // Cast to string and return it
1063 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getItemId(), object)); //NOI18N
1064 return (String) object;
1068 * Initializes database frontends.
1070 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1072 this.getLogger().trace("CALLED!"); //NOI18N
1075 this.productFrontend = new PizzaProductDatabaseFrontend();
1077 // Category frontend
1078 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1081 this.getLogger().trace("EXIT!"); //NOI18N
1085 * Checks whether given category title is already used
1087 * @param title Title of category to check
1088 * @return Whether it has been found
1090 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1091 // categoryFrontend must be set
1092 if (null == this.categoryFrontend) {
1094 throw new NullPointerException("categoryFrontend is null");
1097 // Delegate to frontend
1098 return this.categoryFrontend.isCategoryTitleUsed(title);
1102 * Checks if given product title is already used
1103 * @param title Product title to check
1104 * @return Whether the product title has already been used
1106 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1107 // categoryFrontend must be set
1108 if (null == this.productFrontend) {
1110 throw new NullPointerException("productFrontend is null");
1113 // Delegate to frontend
1114 return this.productFrontend.isProductTitleUsed(title);
1118 * Checks if the product ordered?
1120 * @param product Product instance
1121 * @param session HttpSession instance
1122 * @return Whether the product has been ordered
1124 private boolean isProductOrdered (final Product product, final HttpSession session) {
1126 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1129 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1130 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), isOrdered)); //NOI18N
1133 return ("true".equals(isOrdered)); //NOI18N
1137 * Somewhat setter in session
1139 * @param product Product instance
1140 * @param session Session instance
1141 * @param keyPart Key part to include in final key
1142 * @param value Value to set
1144 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1146 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1149 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
1150 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
1153 this.getLogger().trace("EXIT!"); //NOI18N
1157 * Adds given category data from request to database
1159 * @param request Request instance
1162 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1164 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1166 // request must not be null
1167 if (null == request) {
1169 throw new NullPointerException("request is null"); //NOI18N
1173 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
1174 String parent = request.getParameter(CategoryFrontend.COLUMN_PARENT);
1177 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
1179 // Init variables for casting
1183 if (null == title) {
1185 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1186 } else if (title.isEmpty()) {
1188 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1189 } else if ((parent != null) && (!parent.isEmpty())) {
1190 // "parent" is set, so check it
1192 id = Integer.parseInt(parent);
1193 } catch (final NumberFormatException e) {
1195 throw new IllegalArgumentException(e);
1200 // Try to check if title is used already
1201 if (this.isCategoryTitleUsed(title)) {
1202 // Title already used
1203 throw new CategoryTitleAlreadyUsedException(request);
1205 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1206 throw new ServletException(ex);
1210 // The category is not found, so add it to database
1211 this.categoryFrontend.addCategory(title, id);
1212 } catch (final SQLException | IOException ex) {
1213 // Continue to throw it
1214 throw new ServletException(ex);
1218 this.getLogger().trace("EXIT!"); //NOI18N
1222 * Adds given product data from request to database
1224 * @param request Request instance
1227 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1229 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1231 // request must not be null
1232 if (null == request) {
1234 throw new NullPointerException("request is null"); //NOI18N
1237 // Get title, price and category id
1238 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
1239 String price = request.getParameter(ProductFrontend.COLUMN_PRICE);
1240 String category = request.getParameter(ProductFrontend.COLUMN_CATEGORY);
1241 String available = request.getParameter(ProductFrontend.COLUMN_AVAILABLE);
1244 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
1246 // Variables for converting
1251 if (null == title) {
1253 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1254 } else if (title.isEmpty()) {
1256 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1257 } else if (null == price) {
1259 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_PRICE)); //NOI18N
1260 } else if (price.isEmpty()) {
1262 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_PRICE)); //NOI18N
1263 } else if (null == category) {
1265 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
1266 } else if (category.isEmpty()) {
1268 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
1269 } else if (null == available) {
1271 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
1272 } else if (available.isEmpty()) {
1274 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
1275 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
1277 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", ProductFrontend.COLUMN_AVAILABLE, available)); //NOI18N
1282 id = Long.parseLong(category);
1283 p = Float.parseFloat(price);
1284 } catch (final NumberFormatException e) {
1286 throw new IllegalArgumentException(e);
1290 Boolean a = Boolean.parseBoolean(available);
1292 // Test on product title
1294 // Try to check if title is used already
1295 if (this.isProductTitleUsed(title)) {
1296 // Title already used
1297 throw new ProductTitleAlreadyUsedException(request);
1299 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1300 throw new ServletException(ex);
1304 // The product is not found, so add it to database
1305 this.productFrontend.addProduct(title, p, id, a);
1306 } catch (final SQLException | IOException ex) {
1307 // Continue to throw it
1308 throw new ServletException(ex);
1312 this.getLogger().trace("EXIT!"); //NOI18N
1316 * Generates link HTML code for given category's parent id, if set. This
1317 * link then points to products.jsp?category_id=x
1319 * @param category Category instance
1323 public String generateLinkForParent (final Category category) {
1325 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
1327 // category must not be null
1328 if (null == category) {
1330 throw new NullPointerException("category is null"); //NOI18N
1334 Long parent = category.getParent();
1338 // Product HTML code for link
1339 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
1347 public String getPrintableProduktCategory (final Product product) throws ServletException {
1349 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
1351 // product must not be null
1352 if (null == product) {
1354 throw new NullPointerException("product is null"); //NOI18N
1361 // Get Category instance from product over the frontend
1362 category = this.categoryFrontend.getCategory(product);
1363 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1364 throw new ServletException(ex);
1368 this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
1370 String title = null;
1372 // Now get title from it and return it
1373 title = category.getDecodedTitle();
1374 } catch (final UnsupportedEncodingException ex) {
1375 // Continue to throw as cause
1376 throw new ServletException(ex);
1380 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
1387 * Checks if product's title is already used.
1389 * @param request Request instance
1390 * @return Whether the product title is already used
1391 * @throws java.io.IOException If any IO error occurs
1392 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1393 * @throws java.sql.SQLException If any SQL error occurs
1394 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1395 * @throws java.lang.NoSuchMethodException If a method was not found
1396 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1397 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1399 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1401 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1404 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
1406 // request must not be null and "title" must be found and non-empty
1407 if (null == request) {
1409 throw new NullPointerException("request is null"); //NOI18N
1410 } else if (null == title) {
1412 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1413 } else if (title.isEmpty()) {
1415 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1418 // Default is not used
1419 boolean isUsed = this.isProductTitleUsed(title);
1422 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1429 * Handles admin form requests
1430 * @param request Request instance
1431 * @param response Response instance
1432 * @throws ServletException If something unexpected happened
1435 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1437 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1439 // request and response must both be set
1440 if (null == request) {
1442 throw new NullPointerException("request is null"); //NOI18N
1443 } else if (null == response) {
1445 throw new NullPointerException("response is null"); //NOI18N
1448 // Try this operations
1451 if ("POST".equals(request.getMethod())) { //NOI18N
1452 // Is "add/edit/delete" set?
1453 if (request.getParameter("add") != null) { //NOI18N
1454 // Is it already added?
1455 if (this.isProductTitleUsed(request)) {
1457 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1459 // Already added, so redirect here, else a ServletException will be thrown
1460 response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
1463 this.doAdminAddProduct(request);
1465 } else if (request.getParameter("edit") != null) { //NOI18N
1467 } else if (request.getParameter("delete") != null) { //NOI18N
1471 // Redirect to proper URL
1472 // @TODO Commented out for developing:
1473 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1475 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1476 // Throw it as cause
1477 throw new ServletException(ex);
1481 this.getLogger().trace("EXIT!"); //NOI18N
1485 * Handles admin form requests
1486 * @param request Request instance
1487 * @param response Response instance
1488 * @throws ServletException If something unexpected happened
1491 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1493 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1495 // request and response must both be set
1496 if (null == request) {
1498 throw new NullPointerException("request is null"); //NOI18N
1499 } else if (null == response) {
1501 throw new NullPointerException("response is null"); //NOI18N
1504 // Try this operations
1507 if ("POST".equals(request.getMethod())) { //NOI18N
1508 // Is "add/edit/delete" set?
1509 if (request.getParameter("add") != null) { //NOI18N
1510 // Is the category title already used?
1511 if (this.isCategoryTitleUsed(request)) {
1513 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1515 // Already added, so redirect here, else a ServletException will be thrown
1516 response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
1519 this.doAdminAddCategory(request);
1521 } else if (request.getParameter("edit") != null) { //NOI18N
1523 } else if (request.getParameter("delete") != null) { //NOI18N
1527 // Redirect to proper URL
1528 // @TODO Commented out for developing:
1529 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1531 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1532 // Throw it as cause
1533 throw new ServletException(ex);
1537 this.getLogger().trace("EXIT!"); //NOI18N
1541 * Checks if category's title is already used.
1543 * @param request Request instance
1544 * @return Whether the product title is already used
1545 * @throws java.io.IOException If any IO error occurs
1546 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1547 * @throws java.sql.SQLException If any SQL error occurs
1548 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1549 * @throws java.lang.NoSuchMethodException If a method was not found
1550 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1551 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1553 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1555 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1558 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
1560 // request must not be null and "title" must be found and non-empty
1561 if (null == request) {
1563 throw new NullPointerException("request is null"); //NOI18N
1564 } else if (null == title) {
1566 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1567 } else if (title.isEmpty()) {
1569 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1572 // Default is not used
1573 boolean isUsed = this.isCategoryTitleUsed(title);
1576 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1583 public Product getProduct (final AddableBasketItem item) throws ServletException {
1585 this.getLogger().trace("item=" + item + " - CALLED!");
1587 // item should not be null
1590 throw new NullPointerException("item is null");
1591 } else if (null == this.productFrontend) {
1593 throw new NullPointerException("productFrontend is null");
1596 // Init product instance
1597 Product product = null;
1601 product = this.productFrontend.getProduct(item);
1602 } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1603 // Continue to throw
1604 throw new ServletException(ex);
1608 this.getLogger().trace("product=" + product + " - EXIT!");