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.product.Product;
44 * Main application class
46 * @author Roland Haeder
48 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
50 * Frontend for products
52 private ProductFrontend productFrontend;
55 * Frontend for categories
57 private CategoryFrontend categoryFrontend;
62 public PizzaServiceApplication () {
64 this.getLogger().trace("CALLED!");
68 public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
70 this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); //NOI18N
72 // context should not be null
73 if (null == context) {
75 throw new NullPointerException("context is null");
78 // Is the bundle initialized?
79 if (!this.isBundledInitialized()) {
80 // Temporary initialize default bundle
81 // @TODO The enum Gender uses this
85 // Initialize properties from context
86 this.initProperties(context);
88 // Init database frontends
89 this.initDatabaseFrontends();
92 this.getLogger().trace("EXIT!"); //NOI18N
96 * Calculates total amount of all choosen products
98 * @param request Request instance
99 * @param session Session instance
100 * @return Total amount of all choosen products
103 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
105 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
107 // Is product and session set?
108 if (null == request) {
110 throw new NullPointerException("request is null"); //NOI18N
111 } else if (null == session) {
113 throw new NullPointerException("session is null"); //NOI18N
116 // Init/declare total price and iterator
118 Iterator<Product> iterator = this.getAvailableProducts();
120 // "Walk" over all products
121 while (iterator.hasNext()) {
123 Product product = iterator.next();
126 if (this.isProductChoosen(product, request, session)) {
127 // Then add ordered amount
128 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
131 String amount = this.getAmountFromSession(product, session);
134 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
135 totalAmount += Integer.valueOf(amount);
137 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
141 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
143 // Return total price
148 * Calculates total price of all choosen products
150 * @param request Request instance
151 * @param session Session instance
152 * @return Total price of all choosen products
155 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
157 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
159 // Is product and session set?
160 if (null == request) {
162 throw new NullPointerException("request is null"); //NOI18N
163 } else if (null == session) {
165 throw new NullPointerException("session is null"); //NOI18N
169 float totalPrice = 0.00f;
172 Iterator<Product> iterator = this.getAvailableProducts();
174 // "Walk" over all products
175 while (iterator.hasNext()) {
177 Product product = iterator.next();
180 if (this.isProductChoosen(product, request, session)) {
181 // Then add product's total price
182 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getItemId())); //NOI18N
183 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
185 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getItemId(), totalPrice)); //NOI18N
189 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
191 // Return total price
196 public void doBootstrap () {
197 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
201 public void doMainLoop () {
202 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
206 public void doShutdown () {
207 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
211 * Some "getter" for amount from session
213 * @param product Product instance
214 * @param session Session instance
215 * @return Amount as string
219 public String getAmountFromSession (final Product product, final HttpSession session) {
221 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
223 // Is product and session set?
224 if (null == product) {
226 throw new NullPointerException("product is null"); //NOI18N
227 } else if (null == session) {
229 throw new NullPointerException("session is null"); //NOI18N
233 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
235 // Is the object null?
236 if (null == object) {
238 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
245 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
247 // Cast to string and return it
248 return (String) object;
252 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
254 * @param product Product instance
255 * @param request Request instance
256 * @param session Session instance
257 * @return Whether the product is choosen
260 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
262 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
264 // Is product and session set?
265 if (null == product) {
267 throw new NullPointerException("product is null"); //NOI18N
268 } else if (null == request) {
270 throw new NullPointerException("request is null"); //NOI18N
271 } else if (null == session) {
273 throw new NullPointerException("session is null"); //NOI18N
276 // First let's check if the product is choosen
277 if (this.isProductChoosen(product, request, session)) {
279 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
282 return "checked=\"checked\""; //NOI18N
285 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
293 * Some "getter" for choose from session
295 * @param product Product instance
296 * @param session Session instance
297 * @return Choose as string
301 public String getChooseFromSession (final Product product, final HttpSession session) {
303 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
305 // Is product and session set?
306 if (null == product) {
308 throw new NullPointerException("product is null"); //NOI18N
309 } else if (null == session) {
311 throw new NullPointerException("session is null"); //NOI18N
315 Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
317 // Is the object null?
318 if (null == object) {
320 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getItemId())); //NOI18N
325 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
327 // Cast to string and return it
328 return (String) object;
332 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
334 * @param request Request instance
335 * @param session Session instance
336 * @return Whether the product is choosen
339 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
341 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
343 // Is product and session set?
344 if (null == request) {
346 throw new NullPointerException("request is null"); //NOI18N
347 } else if (null == session) {
349 throw new NullPointerException("session is null"); //NOI18N
352 // Get "enabled" from request scope
353 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
356 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
358 // Is something selected?
359 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
361 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
363 // Something has been choosen
367 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
369 // Nothing choosen yet
370 return "disabled=\"disabled\""; //NOI18N
375 * Some "getter" for choosen (checkbox) from session
377 * @param product Product instance
378 * @param request Request instance
379 * @param session Session instance
380 * @return Amount as string
384 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
386 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
388 // Is product and session set?
389 if (null == product) {
391 throw new NullPointerException("product is null"); //NOI18N
392 } else if (null == request) {
394 throw new NullPointerException("request is null"); //NOI18N
395 } else if (null == session) {
397 throw new NullPointerException("session is null"); //NOI18N
401 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
402 String choosen = this.handleChooseFromRequestSession(product, request, session);
403 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
406 assert(choosen instanceof String): "choosen is null"; //NOI18N
409 if (choosen.isEmpty()) {
415 String amount = this.handleAmountFromRequestSession(product, request, session);
416 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
419 assert(amount instanceof String): "amount is null"; //NOI18N
422 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
423 // Choosen, but no amount
432 * Checks if given Product instance is available and returns a printable
433 * (human-readable) string.
435 * @param product Product instance to check
436 * @return Human-readable version of product availability
439 public String getPrintableProduktAvailability (final Product product) {
441 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
444 if (null == product) {
445 // Should not be null
446 throw new NullPointerException("product is null"); //NOI18N
450 if (product.getAvailable() == true) {
454 // Not, not for public
460 * Some getter for printable value from session or an empty string for null.
462 * @param session Session instance
463 * @param key Key to get
464 * @return Value from key, empty string for null
467 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
469 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
471 // Are both parameter not null?
472 if (null == session) {
474 throw new NullPointerException("session is null"); //NOI18N
475 } else if (null == key) {
477 throw new NullPointerException("key is null"); //NOI18N
481 Object value = this.getValueFromSession(session, key);
484 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
487 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
489 // Return actual value
490 return this.convertNullToEmpty(value);
494 * Some "getter" for a an array of only available products
496 * @return All products
499 public Iterator<Product> getAvailableProducts () throws ServletException {
500 // categoryFrontend must be set
501 if (null == this.productFrontend) {
503 throw new NullPointerException("productFrontend is null");
507 // Ask frontend for a list of products
508 return this.productFrontend.getAvailableProducts();
509 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
510 throw new ServletException(ex);
515 * Some "getter" for a an array of all products
517 * @return All products
520 public Iterator<Product> getAllProducts () throws ServletException {
522 this.getLogger().trace("CALLED!");
524 // categoryFrontend must be set
525 if (null == this.productFrontend) {
527 throw new NullPointerException("productFrontend is null");
531 // Ask frontend for a list of products
532 return this.productFrontend.getAllProducts();
533 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
534 throw new ServletException(ex);
539 * Some "getter" for a an array of all categories
541 * @return All categories
544 public Iterator<Category> getCategories () throws ServletException {
546 this.getLogger().trace("CALLED!");
548 // categoryFrontend must be set
549 if (null == this.categoryFrontend) {
551 throw new NullPointerException("categoryFrontend is null");
555 // Ask frontend for a list of categories
556 return this.categoryFrontend.getCategories();
557 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
558 throw new ServletException(ex);
563 * Some "getter" for total price of position from request or session.
564 * Single price and amount is multiplyed.
566 * @param product Product instance
567 * @param request Request instance
568 * @param session Session instance
569 * @return Amount as string
573 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
575 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
577 // Is product and session set?
578 if (null == product) {
580 throw new NullPointerException("product is null"); //NOI18N
581 } else if (null == request) {
583 throw new NullPointerException("request is null"); //NOI18N
584 } else if (null == session) {
586 throw new NullPointerException("session is null"); //NOI18N
590 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
591 String choosen = this.handleChooseFromRequestSession(product, request, session);
592 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
595 assert(choosen instanceof String): "choosen is null"; //NOI18N
598 if (choosen.isEmpty()) {
600 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getItemId(), choosen)); //NOI18N
605 String amount = this.handleAmountFromRequestSession(product, request, session);
606 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
609 assert(amount instanceof String): "amount is null"; //NOI18N
612 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
614 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getItemId(), amount)); //NOI18N
619 Integer value = null;
623 // Get amount as integer
624 value = Integer.valueOf(amount);
625 } catch (final NumberFormatException e) {
627 throw new IllegalArgumentException(e);
631 float price = (product.getPrice() * value);
634 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getItemId(), price)); //NOI18N
636 // Then multiply it with price
641 * Handler for amount from request or session
643 * @param product Product instance
644 * @param request Request instance
645 * @param session Session instance
646 * @return Amount as string
650 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
652 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
654 // Is product and session set?
655 if (null == product) {
657 throw new NullPointerException("product is null"); //NOI18N
658 } else if (null == request) {
660 throw new NullPointerException("request is null"); //NOI18N
661 } else if (null == session) {
663 throw new NullPointerException("session is null"); //NOI18N
669 // Check request method
670 if (!"POST".equals(request.getMethod())) { //NOI18N
671 // Not POST, so get from session
672 return this.getAmountFromSession(product, session);
673 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
675 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
676 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getItemId())); //NOI18N
680 // Get attribute from request
681 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getItemId()));
684 if (object instanceof String) {
685 // Try to parse it to integer
687 Integer value = Integer.valueOf((String) object);
688 } catch (final NumberFormatException ex) {
690 this.getLogger().warn(ex);
694 // Then set it in session
695 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
698 return (String) object;
702 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
704 // Get attribute from session
705 return this.getAmountFromSession(product, session);
709 * Checks whether the given product is choosen, request overules session.
711 * @param product Product instance
712 * @param request Request instance
713 * @param session Session instance
714 * @return Whether the product is choosen
718 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
720 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
722 // Is product and session set?
723 if (null == product) {
725 throw new NullPointerException("product is null"); //NOI18N
726 } else if (null == request) {
728 throw new NullPointerException("request is null"); //NOI18N
729 } else if (null == session) {
731 throw new NullPointerException("session is null"); //NOI18N
735 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
736 String choosen = this.handleChooseFromRequestSession(product, request, session);
737 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
740 assert(choosen instanceof String): "choosen is null"; //NOI18N
742 // Is it not choosen?
743 if (choosen.isEmpty()) {
749 String amount = this.handleAmountFromRequestSession(product, request, session);
752 assert(amount instanceof String): "amount is not set"; //NOI18N
755 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
757 // Must not be empty and not 0
758 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
762 * Marks all choosen products as ordered
764 * @param request Request instance
765 * @param session Session instance
769 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
771 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
774 Iterator<Product> iterator = this.getAvailableProducts();
776 // "Walk" over all products
777 while (iterator.hasNext()) {
779 Product product = iterator.next();
782 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
785 if (this.isProductChoosen(product, request, session)) {
786 // Mark product as ordered
787 this.markProductAsOrdered(product, session);
792 this.getLogger().trace("EXIT!"); //NOI18N
796 * Marks given product as choosen in session
798 * @param product Product to mark as ordered
799 * @param session Session instance
803 public void markProductAsChoosen (final Product product, final HttpSession session) {
805 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
807 // Is product and session set?
808 if (null == product) {
810 throw new NullPointerException("product is null"); //NOI18N
811 } else if (null == session) {
813 throw new NullPointerException("session is null"); //NOI18N
816 // Mark it as ordered by setting flag
817 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getItemId())); //NOI18N
818 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
821 this.getLogger().trace("EXIT!"); //NOI18N
825 * Marks given product as ordered in session
827 * @param product Product to mark as ordered
828 * @param session Session instance
832 public void markProductAsOrdered (final Product product, final HttpSession session) {
834 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
836 // Is product and session set?
837 if (null == product) {
839 throw new NullPointerException("product is null"); //NOI18N
840 } else if (null == session) {
842 throw new NullPointerException("session is null"); //NOI18N
845 // Mark it as ordered by setting flag
846 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getItemId())); //NOI18N
847 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
850 this.getLogger().trace("EXIT!"); //NOI18N
854 * Somewhat setter in session
856 * @param session Session instance
857 * @param key Session key to set
858 * @param value Value to set
861 public void setValueInSession (final HttpSession session, final String key, final Object value) {
863 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
865 synchronized(session) {
867 session.setAttribute(key, value);
871 this.getLogger().trace("EXIT!"); //NOI18N
875 * Unmarks given product as choosen in session
877 * @param product Product to unmark as choosen
878 * @param session Session instance
882 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
884 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
886 // Is product and session set?
887 if (null == product) {
889 throw new NullPointerException("product is null"); //NOI18N
890 } else if (null == session) {
892 throw new NullPointerException("session is null"); //NOI18N
895 // Mark it as ordered by setting flag
896 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getItemId())); //NOI18N
897 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
900 this.getLogger().trace("EXIT!"); //NOI18N
904 * Unmarks given product as ordered in session
906 * @param product Product to unmark as ordered
907 * @param session Session instance
911 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
913 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
915 // Is product and session set?
916 if (null == product) {
918 throw new NullPointerException("product is null"); //NOI18N
919 } else if (null == session) {
921 throw new NullPointerException("session is null"); //NOI18N
924 // Mark it as ordered by setting flag
925 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getItemId())); //NOI18N
926 this.clearSessionAttribute(product, session, SESSION_ORDERED);
929 this.getLogger().trace("EXIT!"); //NOI18N
933 * Clears given parameter for product in session
935 * @param product Product instance
936 * @param session Session instance
937 * @param parameter Parameter to clear
939 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
941 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
944 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
945 this.setValueInSession(product, session, parameter, null);
948 this.getLogger().trace("EXIT!"); //NOI18N
952 * Some getter for value from session
954 * @param product Product instance
955 * @param session Session instance
956 * @param attribute Attribute to get value from
957 * @return Value from session
959 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
961 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
964 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
966 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
969 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
976 * Some getter for value from session
978 * @param session Session instance
979 * @param key Key to get value from
980 * @return Value from session
982 private Object getValueFromSession (final HttpSession session, final String key) {
984 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
989 // Get it synchronized from session
990 synchronized (session) {
991 value = session.getAttribute(key);
995 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1002 * Handler for choosen (checkbox) from request or session
1004 * @param product Product instance
1005 * @param request Request instance
1006 * @param session Session instance
1007 * @return Amount as string
1009 private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
1011 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1013 // Is product and session set?
1014 if (null == product) {
1016 throw new NullPointerException("product is null"); //NOI18N
1017 } else if (null == request) {
1019 throw new NullPointerException("request is null"); //NOI18N
1020 } else if (null == session) {
1022 throw new NullPointerException("session is null"); //NOI18N
1028 // Check request method
1029 if (!"POST".equals(request.getMethod())) { //NOI18N
1030 // Not POST, so get from session
1031 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1032 return this.getChooseFromSession(product, session);
1033 } else if (this.isProductOrdered(product, session)) {
1034 // Product is ordered
1035 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1036 return this.getChooseFromSession(product, session);
1037 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1039 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1040 return this.getChooseFromSession(product, session);
1043 // Get reqzest element
1044 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getItemId()));
1045 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getItemId(), object)); //NOI18N
1048 if (null == object) {
1050 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getItemId())); //NOI18N
1051 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
1052 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1054 // Return empty string
1058 // Then set it in session
1059 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, object);
1061 // Cast to string and return it
1062 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getItemId(), object)); //NOI18N
1063 return (String) object;
1067 * Initializes database frontends.
1069 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1071 this.getLogger().trace("CALLED!"); //NOI18N
1074 this.productFrontend = new PizzaProductDatabaseFrontend();
1076 // Category frontend
1077 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1080 this.getLogger().trace("EXIT!"); //NOI18N
1084 * Checks whether given category title is already used
1086 * @param title Title of category to check
1087 * @return Whether it has been found
1089 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1090 // categoryFrontend must be set
1091 if (null == this.categoryFrontend) {
1093 throw new NullPointerException("categoryFrontend is null");
1096 // Delegate to frontend
1097 return this.categoryFrontend.isCategoryTitleUsed(title);
1101 * Checks if given product title is already used
1102 * @param title Product title to check
1103 * @return Whether the product title has already been used
1105 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1106 // categoryFrontend must be set
1107 if (null == this.productFrontend) {
1109 throw new NullPointerException("productFrontend is null");
1112 // Delegate to frontend
1113 return this.productFrontend.isProductTitleUsed(title);
1117 * Checks if the product ordered?
1119 * @param product Product instance
1120 * @param session HttpSession instance
1121 * @return Whether the product has been ordered
1123 private boolean isProductOrdered (final Product product, final HttpSession session) {
1125 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1128 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1129 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), isOrdered)); //NOI18N
1132 return ("true".equals(isOrdered)); //NOI18N
1136 * Somewhat setter in session
1138 * @param product Product instance
1139 * @param session Session instance
1140 * @param keyPart Key part to include in final key
1141 * @param value Value to set
1143 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1145 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1148 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
1149 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
1152 this.getLogger().trace("EXIT!"); //NOI18N
1156 * Adds given category data from request to database
1158 * @param request Request instance
1161 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1163 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1165 // request must not be null
1166 if (null == request) {
1168 throw new NullPointerException("request is null"); //NOI18N
1172 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
1173 String parent = request.getParameter(CategoryFrontend.COLUMN_PARENT);
1176 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
1178 // Init variables for casting
1182 if (null == title) {
1184 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1185 } else if (title.isEmpty()) {
1187 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1188 } else if ((parent != null) && (!parent.isEmpty())) {
1189 // "parent" is set, so check it
1191 id = Integer.parseInt(parent);
1192 } catch (final NumberFormatException e) {
1194 throw new IllegalArgumentException(e);
1199 // Try to check if title is used already
1200 if (this.isCategoryTitleUsed(title)) {
1201 // Title already used
1202 throw new CategoryTitleAlreadyUsedException(request);
1204 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1205 throw new ServletException(ex);
1209 // The category is not found, so add it to database
1210 this.categoryFrontend.addCategory(title, id);
1211 } catch (final SQLException | IOException ex) {
1212 // Continue to throw it
1213 throw new ServletException(ex);
1217 this.getLogger().trace("EXIT!"); //NOI18N
1221 * Adds given product data from request to database
1223 * @param request Request instance
1226 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1228 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1230 // request must not be null
1231 if (null == request) {
1233 throw new NullPointerException("request is null"); //NOI18N
1236 // Get title, price and category id
1237 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
1238 String price = request.getParameter(ProductFrontend.COLUMN_PRICE);
1239 String category = request.getParameter(ProductFrontend.COLUMN_CATEGORY);
1240 String available = request.getParameter(ProductFrontend.COLUMN_AVAILABLE);
1243 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
1245 // Variables for converting
1250 if (null == title) {
1252 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1253 } else if (title.isEmpty()) {
1255 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1256 } else if (null == price) {
1258 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_PRICE)); //NOI18N
1259 } else if (price.isEmpty()) {
1261 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_PRICE)); //NOI18N
1262 } else if (null == category) {
1264 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
1265 } else if (category.isEmpty()) {
1267 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
1268 } else if (null == available) {
1270 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
1271 } else if (available.isEmpty()) {
1273 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
1274 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
1276 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", ProductFrontend.COLUMN_AVAILABLE, available)); //NOI18N
1281 id = Long.parseLong(category);
1282 p = Float.parseFloat(price);
1283 } catch (final NumberFormatException e) {
1285 throw new IllegalArgumentException(e);
1289 Boolean a = Boolean.parseBoolean(available);
1291 // Test on product title
1293 // Try to check if title is used already
1294 if (this.isProductTitleUsed(title)) {
1295 // Title already used
1296 throw new ProductTitleAlreadyUsedException(request);
1298 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1299 throw new ServletException(ex);
1303 // The product is not found, so add it to database
1304 this.productFrontend.addProduct(title, p, id, a);
1305 } catch (final SQLException | IOException ex) {
1306 // Continue to throw it
1307 throw new ServletException(ex);
1311 this.getLogger().trace("EXIT!"); //NOI18N
1315 * Generates link HTML code for given category's parent id, if set. This
1316 * link then points to products.jsp?category_id=x
1318 * @param category Category instance
1322 public String generateLinkForParent (final Category category) {
1324 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
1326 // category must not be null
1327 if (null == category) {
1329 throw new NullPointerException("category is null"); //NOI18N
1333 Long parent = category.getParent();
1337 // Product HTML code for link
1338 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
1346 public String getPrintableProduktCategory (final Product product) throws ServletException {
1348 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
1350 // product must not be null
1351 if (null == product) {
1353 throw new NullPointerException("product is null"); //NOI18N
1360 // Get Category instance from product over the frontend
1361 category = this.categoryFrontend.getCategory(product);
1362 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1363 throw new ServletException(ex);
1367 this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
1369 String title = null;
1371 // Now get title from it and return it
1372 title = category.decodedTitle();
1373 } catch (final UnsupportedEncodingException ex) {
1374 // Continue to throw as cause
1375 throw new ServletException(ex);
1379 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
1386 * Checks if product's title is already used.
1388 * @param request Request instance
1389 * @return Whether the product title is already used
1390 * @throws java.io.IOException If any IO error occurs
1391 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1392 * @throws java.sql.SQLException If any SQL error occurs
1393 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1394 * @throws java.lang.NoSuchMethodException If a method was not found
1395 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1396 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1398 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1400 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1403 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
1405 // request must not be null and "title" must be found and non-empty
1406 if (null == request) {
1408 throw new NullPointerException("request is null"); //NOI18N
1409 } else if (null == title) {
1411 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1412 } else if (title.isEmpty()) {
1414 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
1417 // Default is not used
1418 boolean isUsed = this.isProductTitleUsed(title);
1421 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1428 * Handles admin form requests
1429 * @param request Request instance
1430 * @param response Response instance
1431 * @throws ServletException If something unexpected happened
1434 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1436 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1438 // request and response must both be set
1439 if (null == request) {
1441 throw new NullPointerException("request is null"); //NOI18N
1442 } else if (null == response) {
1444 throw new NullPointerException("response is null"); //NOI18N
1447 // Try this operations
1450 if ("POST".equals(request.getMethod())) { //NOI18N
1451 // Is "add/edit/delete" set?
1452 if (request.getParameter("add") != null) { //NOI18N
1453 // Is it already added?
1454 if (this.isProductTitleUsed(request)) {
1456 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1458 // Already added, so redirect here, else a ServletException will be thrown
1459 response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
1462 this.doAdminAddProduct(request);
1464 } else if (request.getParameter("edit") != null) { //NOI18N
1466 } else if (request.getParameter("delete") != null) { //NOI18N
1470 // Redirect to proper URL
1471 // @TODO Commented out for developing:
1472 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1474 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1475 // Throw it as cause
1476 throw new ServletException(ex);
1480 this.getLogger().trace("EXIT!"); //NOI18N
1484 * Handles admin form requests
1485 * @param request Request instance
1486 * @param response Response instance
1487 * @throws ServletException If something unexpected happened
1490 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1492 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1494 // request and response must both be set
1495 if (null == request) {
1497 throw new NullPointerException("request is null"); //NOI18N
1498 } else if (null == response) {
1500 throw new NullPointerException("response is null"); //NOI18N
1503 // Try this operations
1506 if ("POST".equals(request.getMethod())) { //NOI18N
1507 // Is "add/edit/delete" set?
1508 if (request.getParameter("add") != null) { //NOI18N
1509 // Is the category title already used?
1510 if (this.isCategoryTitleUsed(request)) {
1512 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1514 // Already added, so redirect here, else a ServletException will be thrown
1515 response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
1518 this.doAdminAddCategory(request);
1520 } else if (request.getParameter("edit") != null) { //NOI18N
1522 } else if (request.getParameter("delete") != null) { //NOI18N
1526 // Redirect to proper URL
1527 // @TODO Commented out for developing:
1528 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1530 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1531 // Throw it as cause
1532 throw new ServletException(ex);
1536 this.getLogger().trace("EXIT!"); //NOI18N
1540 * Checks if category's title is already used.
1542 * @param request Request instance
1543 * @return Whether the product title is already used
1544 * @throws java.io.IOException If any IO error occurs
1545 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1546 * @throws java.sql.SQLException If any SQL error occurs
1547 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1548 * @throws java.lang.NoSuchMethodException If a method was not found
1549 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1550 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1552 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1554 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1557 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
1559 // request must not be null and "title" must be found and non-empty
1560 if (null == request) {
1562 throw new NullPointerException("request is null"); //NOI18N
1563 } else if (null == title) {
1565 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1566 } else if (title.isEmpty()) {
1568 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1571 // Default is not used
1572 boolean isUsed = this.isCategoryTitleUsed(title);
1575 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N