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 org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
20 import java.io.IOException;
21 import java.lang.reflect.Field;
22 import java.lang.reflect.InvocationTargetException;
23 import java.sql.SQLException;
24 import java.text.MessageFormat;
25 import java.util.Iterator;
27 import javax.servlet.ServletContext;
28 import javax.servlet.ServletException;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpSession;
31 import org.mxchange.jcore.contact.Gender;
32 import org.mxchange.jcore.exceptions.BadTokenException;
33 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
34 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
35 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
36 import org.mxchange.pizzaapplication.category.Category;
37 import org.mxchange.pizzaapplication.customer.Customer;
38 import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer;
39 import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants;
40 import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend;
41 import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
42 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
43 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
44 import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
45 import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
46 import org.mxchange.pizzaapplication.product.Product;
49 * Main application class
51 * @author Roland Haeder
53 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
57 public static final String MAIN_TITLE = "Pizza-Service";
60 * Frontend for products
62 private ProductFrontend productFrontend;
65 * Frontend for categories
67 private CategoryFrontend categoryFrontend;
70 * Some singleton getter for this instance. If the instance is not set in
71 * given application, it will be created.
73 * @param context Servlet context
74 * @return This instance
75 * @throws javax.servlet.ServletException If object is not set correctly
77 public static final PizzaApplication getInstance (final ServletContext context) throws ServletException {
78 // Check application instance
79 if (context == null) {
81 throw new NullPointerException("application is null"); //NOI18N
85 PizzaApplication instance = null;
87 // Get instance from servlet application (aka. "application scope")
88 Object object = context.getAttribute("app"); //NOI18N
91 if (object instanceof PizzaApplication) {
92 // Instance is set, so casting should work
93 instance = (PizzaApplication) object;
94 } else if (object instanceof Object) {
95 // Not correct instance
96 throw new ServletException("app is not set correctly"); //NOI18N
99 // "service" is null, so initialize it
100 instance = new PizzaServiceApplication(context);
101 } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
102 throw new ServletException(ex);
106 context.setAttribute("app", instance); //NOI18N
110 instance.getLogger().trace(MessageFormat.format("instance={0} - EXIT!", instance)); //NOI18N
117 * For debugging purpose
119 * @param args Arguments
121 public static void main (String[] args) {
122 // Get instance and start it
123 new PizzaServiceApplication().start();
127 * Constructor with servet configuration
129 * @param context Servlet context
131 private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
132 // Temporary initialize default bundle
133 // @TODO The JSF may have better internatialization support
136 // Initialize properties from config
137 this.initProperties(context);
139 // Init database frontends
140 this.initDatabaseFrontends();
144 * Default constructor
146 private PizzaServiceApplication () {
150 * Calculates total amount of all choosen products
152 * @param request Request instance
153 * @param session Session instance
154 * @return Total amount of all choosen products
157 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
159 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
161 // Is product and session set?
162 if (request == null) {
164 throw new NullPointerException("request is null"); //NOI18N
165 } else if (session == null) {
167 throw new NullPointerException("session is null"); //NOI18N
170 // Init/declare total price and iterator
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 ordered amount
182 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getId())); //NOI18N
185 String amount = this.getAmountFromSession(product, session);
188 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
189 totalAmount += Integer.valueOf(amount);
191 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getId(), totalAmount)); //NOI18N
195 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
197 // Return total price
202 * Calculates total price of all choosen products
204 * @param request Request instance
205 * @param session Session instance
206 * @return Total price of all choosen products
209 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
211 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
213 // Is product and session set?
214 if (request == null) {
216 throw new NullPointerException("request is null"); //NOI18N
217 } else if (session == null) {
219 throw new NullPointerException("session is null"); //NOI18N
223 float totalPrice = 0.00f;
226 Iterator<Product> iterator = this.getAvailableProducts();
228 // "Walk" over all products
229 while (iterator.hasNext()) {
231 Product product = iterator.next();
234 if (this.isProductChoosen(product, request, session)) {
235 // Then add product's total price
236 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getId())); //NOI18N
237 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
239 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getId(), totalPrice)); //NOI18N
243 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
245 // Return total price
250 public void doBootstrap () {
251 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
255 public void doMainLoop () {
256 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
260 public void doShutdown () {
261 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
265 * Some "getter" for amount from session
267 * @param product Product instance
268 * @param session Session instance
269 * @return Amount as string
272 public String getAmountFromSession (final Product product, final HttpSession session) {
274 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
276 // Is product and session set?
277 if (product == null) {
279 throw new NullPointerException("product is null"); //NOI18N
280 } else if (session == null) {
282 throw new NullPointerException("session is null"); //NOI18N
286 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
288 // Is the object null?
289 if (object == null) {
291 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
298 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
300 // Cast to string and return it
301 return (String) object;
305 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
307 * @param product Product instance
308 * @param request Request instance
309 * @param session Session instance
310 * @return Whether the product is choosen
313 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
315 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
317 // Is product and session set?
318 if (product == null) {
320 throw new NullPointerException("product is null"); //NOI18N
321 } else if (request == null) {
323 throw new NullPointerException("request is null"); //NOI18N
324 } else if (session == null) {
326 throw new NullPointerException("session is null"); //NOI18N
329 // First let's check if the product is choosen
330 if (this.isProductChoosen(product, request, session)) {
332 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
335 return "checked=\"checked\""; //NOI18N
338 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
346 * Some "getter" for choose from session
348 * @param product Product instance
349 * @param session Session instance
350 * @return Choose as string
353 public String getChooseFromSession (final Product product, final HttpSession session) {
355 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
357 // Is product and session set?
358 if (product == null) {
360 throw new NullPointerException("product is null"); //NOI18N
361 } else if (session == null) {
363 throw new NullPointerException("session is null"); //NOI18N
367 Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
369 // Is the object null?
370 if (object == null) {
372 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getId())); //NOI18N
377 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
379 // Cast to string and return it
380 return (String) object;
384 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
386 * @param request Request instance
387 * @param session Session instance
388 * @return Whether the product is choosen
391 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
393 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
395 // Is product and session set?
396 if (request == null) {
398 throw new NullPointerException("request is null"); //NOI18N
399 } else if (session == null) {
401 throw new NullPointerException("session is null"); //NOI18N
404 // Is something selected?
405 if (this.calculateTotalAmount(request, session) > 0) {
407 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
409 // Something has been choosen
413 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
415 // Nothing choosen yet
416 return "disabled=\"disabled\""; //NOI18N
421 * Some "getter" for choosen (checkbox) from session
423 * @param product Product instance
424 * @param request Request instance
425 * @param session Session instance
426 * @return Amount as string
429 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
431 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
433 // Is product and session set?
434 if (product == null) {
436 throw new NullPointerException("product is null"); //NOI18N
437 } else if (request == null) {
439 throw new NullPointerException("request is null"); //NOI18N
440 } else if (session == null) {
442 throw new NullPointerException("session is null"); //NOI18N
446 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
447 String choosen = this.handleChooseFromRequestSession(product, request, session);
448 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
451 assert(choosen instanceof String): "choosen is null"; //NOI18N
454 if (choosen.isEmpty()) {
460 String amount = this.handleAmountFromRequestSession(product, request, session);
461 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
464 assert(amount instanceof String): "amount is null"; //NOI18N
467 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
468 // Choosen, but no amount
477 * Checks if given Product instance is available and returns a printable
478 * (human-readable) string.
480 * @param product Product instance to check
481 * @return Human-readable version of product availability
484 public String getPrintableProduktAvailability (final Product product) {
486 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
489 if (product == null) {
490 // Should not be null
491 throw new NullPointerException("product is null"); //NOI18N
495 if (product.getAvailable() == true) {
499 // Not, not for public
505 * Some getter for printable value from session or an empty string for null.
507 * @param session Session instance
508 * @param key Key to get
509 * @return Value from key, empty string for null
512 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
514 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
516 // Are both parameter not null?
517 if (session == null) {
519 throw new NullPointerException("session is null"); //NOI18N
520 } else if (key == null) {
522 throw new NullPointerException("key is null"); //NOI18N
526 Object value = this.getValueFromSession(session, key);
529 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
532 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
534 // Return actual value
535 return this.convertNullToEmpty(value);
539 * Some "getter" for a an array of only available products
541 * @return All products
544 public Iterator<Product> getAvailableProducts () throws ServletException {
546 // Ask frontend for a list of products
547 return this.productFrontend.getAvailableProducts();
548 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
549 throw new ServletException(ex);
554 * Some "getter" for a an array of all products
556 * @return All products
559 public Iterator<Product> getAllProducts () throws ServletException {
561 // Ask frontend for a list of products
562 return this.productFrontend.getAllProducts();
563 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
564 throw new ServletException(ex);
569 * Some "getter" for a an array of all categories
571 * @return All categories
574 public Iterator<Category> getCategories () throws ServletException {
576 // Ask frontend for a list of categories
577 return this.categoryFrontend.getCategories();
578 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
579 throw new ServletException(ex);
584 * Some "getter" for total price of position from request or session.
585 * Single price and amount is multiplyed.
587 * @param product Product instance
588 * @param request Request instance
589 * @param session Session instance
590 * @return Amount as string
593 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
595 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
597 // Is product and session set?
598 if (product == null) {
600 throw new NullPointerException("product is null"); //NOI18N
601 } else if (request == null) {
603 throw new NullPointerException("request is null"); //NOI18N
604 } else if (session == null) {
606 throw new NullPointerException("session is null"); //NOI18N
610 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
611 String choosen = this.handleChooseFromRequestSession(product, request, session);
612 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
615 assert(choosen instanceof String): "choosen is null"; //NOI18N
618 if (choosen.isEmpty()) {
620 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getId(), choosen)); //NOI18N
625 String amount = this.handleAmountFromRequestSession(product, request, session);
626 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getId(), amount)); //NOI18N
629 assert(amount instanceof String): "amount is null"; //NOI18N
632 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
634 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getId(), amount)); //NOI18N
639 Integer value = null;
643 // Get amount as integer
644 value = Integer.valueOf(amount);
645 } catch (final NumberFormatException e) {
647 throw new IllegalArgumentException(e);
651 float price = (product.getPrice() * value);
654 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getId(), price)); //NOI18N
656 // Then multiply it with price
661 * Handler for amount from request or session
663 * @param product Product instance
664 * @param request Request instance
665 * @param session Session instance
666 * @return Amount as string
669 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
671 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
673 // Is product and session set?
674 if (product == null) {
676 throw new NullPointerException("product is null"); //NOI18N
677 } else if (request == null) {
679 throw new NullPointerException("request is null"); //NOI18N
680 } else if (session == null) {
682 throw new NullPointerException("session is null"); //NOI18N
688 // Check request method
689 if (!"POST".equals(request.getMethod())) { //NOI18N
690 // Not POST, so get from session
691 return this.getAmountFromSession(product, session);
692 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
694 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
695 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getId())); //NOI18N
699 // Get attribute from request
700 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getId()));
703 if (object instanceof String) {
704 // Try to parse it to integer
706 Integer value = Integer.valueOf((String) object);
707 } catch (final NumberFormatException ex) {
709 this.getLogger().warn(ex);
713 // Then set it in session
714 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
717 return (String) object;
721 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
723 // Get attribute from session
724 return this.getAmountFromSession(product, session);
728 * Checks whether the given product is choosen, request overules session.
730 * @param product Product instance
731 * @param request Request instance
732 * @param session Session instance
733 * @return Whether the product is choosen
736 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
738 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
740 // Is product and session set?
741 if (product == null) {
743 throw new NullPointerException("product is null"); //NOI18N
744 } else if (request == null) {
746 throw new NullPointerException("request is null"); //NOI18N
747 } else if (session == null) {
749 throw new NullPointerException("session is null"); //NOI18N
753 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getId(), request, session)); //NOI18N
754 String choosen = this.handleChooseFromRequestSession(product, request, session);
755 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getId(), choosen)); //NOI18N
758 assert(choosen instanceof String): "choosen is null"; //NOI18N
760 // Is it not choosen?
761 if (choosen.isEmpty()) {
767 String amount = this.handleAmountFromRequestSession(product, request, session);
770 assert(amount instanceof String): "amount is not set"; //NOI18N
773 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
775 // Must not be empty and not 0
776 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
780 * Marks all choosen products as ordered
782 * @param request Request instance
783 * @param session Session instance
786 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
788 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
791 Iterator<Product> iterator = this.getAvailableProducts();
793 // "Walk" over all products
794 while (iterator.hasNext()) {
796 Product product = iterator.next();
799 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
802 if (this.isProductChoosen(product, request, session)) {
803 // Mark product as ordered
804 this.markProductAsOrdered(product, session);
809 this.getLogger().trace("EXIT!"); //NOI18N
813 * Marks given product as choosen in session
815 * @param product Product to mark as ordered
816 * @param session Session instance
819 public void markProductAsChoosen (final Product product, final HttpSession session) {
821 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
823 // Is product and session set?
824 if (product == null) {
826 throw new NullPointerException("product is null"); //NOI18N
827 } else if (session == null) {
829 throw new NullPointerException("session is null"); //NOI18N
832 // Mark it as ordered by setting flag
833 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
834 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
837 this.getLogger().trace("EXIT!"); //NOI18N
841 * Marks given product as ordered in session
843 * @param product Product to mark as ordered
844 * @param session Session instance
847 public void markProductAsOrdered (final Product product, final HttpSession session) {
849 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
851 // Is product and session set?
852 if (product == null) {
854 throw new NullPointerException("product is null"); //NOI18N
855 } else if (session == null) {
857 throw new NullPointerException("session is null"); //NOI18N
860 // Mark it as ordered by setting flag
861 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getId())); //NOI18N
862 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
865 this.getLogger().trace("EXIT!"); //NOI18N
869 * Somewhat setter in session
871 * @param session Session instance
872 * @param key Session key to set
873 * @param value Value to set
876 public void setValueInSession (final HttpSession session, final String key, final Object value) {
878 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
880 synchronized(session) {
882 session.setAttribute(key, value);
886 this.getLogger().trace("EXIT!"); //NOI18N
890 * Unmarks given product as choosen in session
892 * @param product Product to unmark as choosen
893 * @param session Session instance
896 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
898 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
900 // Is product and session set?
901 if (product == null) {
903 throw new NullPointerException("product is null"); //NOI18N
904 } else if (session == null) {
906 throw new NullPointerException("session is null"); //NOI18N
909 // Mark it as ordered by setting flag
910 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
911 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
914 this.getLogger().trace("EXIT!"); //NOI18N
918 * Unmarks given product as ordered in session
920 * @param product Product to unmark as ordered
921 * @param session Session instance
924 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
926 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
928 // Is product and session set?
929 if (product == null) {
931 throw new NullPointerException("product is null"); //NOI18N
932 } else if (session == null) {
934 throw new NullPointerException("session is null"); //NOI18N
937 // Mark it as ordered by setting flag
938 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getId())); //NOI18N
939 this.clearSessionAttribute(product, session, SESSION_ORDERED);
942 this.getLogger().trace("EXIT!"); //NOI18N
946 * Clears given parameter for product in session
948 * @param product Product instance
949 * @param session Session instance
950 * @param parameter Parameter to clear
952 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
954 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
957 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getId(), parameter)); //NOI18N
958 this.setValueInSession(product, session, parameter, null);
961 this.getLogger().trace("EXIT!"); //NOI18N
965 * Some getter for value from session
967 * @param product Product instance
968 * @param session Session instance
969 * @param attribute Attribute to get value from
970 * @return Value from session
972 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
974 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
977 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getId()));
979 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getId(), attribute, value)); //NOI18N
982 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
989 * Some getter for value from session
991 * @param session Session instance
992 * @param key Key to get value from
993 * @return Value from session
995 private Object getValueFromSession (final HttpSession session, final String key) {
997 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
1002 // Get it synchronized from session
1003 synchronized (session) {
1004 value = session.getAttribute(key);
1008 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
1015 * Handler for choosen (checkbox) from request or session
1017 * @param product Product instance
1018 * @param request Request instance
1019 * @param session Session instance
1020 * @return Amount as string
1022 private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) {
1024 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
1026 // Is product and session set?
1027 if (product == null) {
1029 throw new NullPointerException("product is null"); //NOI18N
1030 } else if (request == null) {
1032 throw new NullPointerException("request is null"); //NOI18N
1033 } else if (session == null) {
1035 throw new NullPointerException("session is null"); //NOI18N
1041 // Check request method
1042 if (!"POST".equals(request.getMethod())) { //NOI18N
1043 // Not POST, so get from session
1044 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1045 return this.getChooseFromSession(product, session);
1046 } else if (this.isProductOrdered(product, session)) {
1047 // Product is ordered
1048 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1049 return this.getChooseFromSession(product, session);
1050 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1052 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getId(), session)); //NOI18N
1053 return this.getChooseFromSession(product, session);
1056 // Get reqzest element
1057 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
1058 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
1061 if (object == null) {
1063 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
1064 this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
1065 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1067 // Return empty string
1071 // Then set it in session
1072 this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
1074 // Cast to string and return it
1075 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
1076 return (String) object;
1080 * Initializes database frontends.
1082 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1084 this.productFrontend = new PizzaProductDatabaseFrontend();
1086 // Category frontend
1087 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1091 * Checks whether given category title is already used
1093 * @param title Title of category to check
1094 * @return Whether it has been found
1096 private boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
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 // Delegate to frontend
1108 return this.productFrontend.isProductTitleUsed(title);
1112 * Checks if the product ordered?
1114 * @param product Product instance
1115 * @param session HttpSession instance
1118 private boolean isProductOrdered(final Product product, final HttpSession session) {
1120 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1123 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1124 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getId(), isOrdered)); //NOI18N
1127 return ("true".equals(isOrdered)); //NOI18N
1131 * Somewhat setter in session
1133 * @param product Product instance
1134 * @param session Session instance
1135 * @param keyPart Key part to include in final key
1136 * @param value Value to set
1138 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1140 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1143 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getId(), keyPart)); //NOI18N
1144 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getId()), value);
1147 this.getLogger().trace("EXIT!"); //NOI18N
1151 * Application starter
1153 private void start () {
1159 this.initProperties();
1160 } catch (final IOException ex) {
1162 this.abortProgramWithException(ex);
1166 Iterator<Product> iterator = null;
1170 iterator = this.getAvailableProducts();
1171 } catch (final ServletException ex) {
1172 this.abortProgramWithException(ex);
1175 // "Walk" over all products
1176 while ((iterator instanceof Iterator) && (iterator.hasNext())) {
1178 Product product = iterator.next();
1181 this.getLogger().debug(MessageFormat.format("Product {0}, {1}: {2}", product.getId(), product.getTitle(), product.getPrice())); //NOI18N
1184 // Generate fake Customer instance
1185 Customer customer = new PizzaServiceCustomer();
1188 * Need a least a gender ... :( See, that is why I don't like default
1189 * constructors, you can easily miss something important and bam! You
1190 * get an NPE. The fix here is, to have construtors (or factories) which
1191 * requires all required instances that needs to be set to get a
1192 * consitent object back.
1195 // Gender is MALE now
1196 customer.setGender(Gender.MALE);
1199 Iterator<Map.Entry<Field, Object>> it = null;
1202 // Get iterator on all its fields
1203 it = customer.iterator();
1204 } catch (final NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1205 this.abortProgramWithException(ex);
1209 while ((it instanceof Iterator) && (it.hasNext())) {
1210 Map.Entry<Field, Object> entry = it.next();
1211 this.getLogger().debug(MessageFormat.format("entry {0}={1}", entry.getKey(), entry.getValue())); //NOI18N
1216 * Adds given category data from request to database
1218 * @param request Request instance
1221 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1223 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1225 // request must not be null
1226 if (request == null) {
1228 throw new NullPointerException("request is null"); //NOI18N
1232 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1233 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1236 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
1238 // Init variables for casting
1242 if (title == null) {
1244 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1245 } else if (title.isEmpty()) {
1247 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1248 } else if ((parent != null) && (!parent.isEmpty())) {
1249 // "parent" is set, so check it
1251 id = Integer.parseInt(parent);
1252 } catch (final NumberFormatException e) {
1254 throw new IllegalArgumentException(e);
1259 // Try to check if title is used already
1260 if (this.isCategoryTitleUsed(title)) {
1261 // Title already used
1262 throw new CategoryTitleAlreadyUsedException(request);
1264 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1265 throw new ServletException(ex);
1269 // The category is not found, so add it to database
1270 this.categoryFrontend.addCategory(title, id);
1271 } catch (final SQLException | IOException ex) {
1272 // Continue to throw it
1273 throw new ServletException(ex);
1277 this.getLogger().trace("EXIT!"); //NOI18N
1281 * Adds given product data from request to database
1283 * @param request Request instance
1286 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1288 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1290 // request must not be null
1291 if (request == null) {
1293 throw new NullPointerException("request is null"); //NOI18N
1296 // Get title, price and category id
1297 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1298 String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
1299 String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
1300 String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
1303 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available));
1305 // Variables for converting
1310 if (title == null) {
1312 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1313 } else if (title.isEmpty()) {
1315 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1316 } else if (price == null) {
1318 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1319 } else if (price.isEmpty()) {
1321 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1322 } else if (category == null) {
1324 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1325 } else if (category.isEmpty()) {
1327 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1328 } else if (available == null) {
1330 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1331 } else if (available.isEmpty()) {
1333 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1334 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
1336 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available)); //NOI18N
1341 id = Long.parseLong(category);
1342 p = Float.parseFloat(price);
1343 } catch (final NumberFormatException e) {
1345 throw new IllegalArgumentException(e);
1349 Boolean a = Boolean.parseBoolean(available);
1351 // Test on product title
1353 // Try to check if title is used already
1354 if (this.isProductTitleUsed(title)) {
1355 // Title already used
1356 throw new ProductTitleAlreadyUsedException(request);
1358 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1359 throw new ServletException(ex);
1363 // The product is not found, so add it to database
1364 this.productFrontend.addProduct(title, p, id, a);
1365 } catch (final SQLException | IOException ex) {
1366 // Continue to throw it
1367 throw new ServletException(ex);
1371 this.getLogger().trace("EXIT!"); //NOI18N
1375 * Generates link HTML code for given category's parent id, if set. This
1376 * link then points to products.jsp?category_id=x
1378 * @param category Category instance
1382 public String generateLinkForParent (final Category category) {
1384 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
1386 // category must not be null
1387 if (category == null) {
1389 throw new NullPointerException("category is null"); //NOI18N
1393 Long parent = category.getParent();
1397 // Product HTML code for link
1398 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N