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.category.PizzaCategoryDatabaseConstants;
36 import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend;
37 import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend;
38 import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend;
39 import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend;
40 import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants;
41 import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
42 import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
43 import org.mxchange.pizzaapplication.product.Product;
46 * Main application class
48 * @author Roland Haeder
50 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
52 * Frontend for products
54 private ProductFrontend productFrontend;
57 * Frontend for categories
59 private CategoryFrontend categoryFrontend;
62 public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
64 this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); //NOI18N
66 // Is the bundle initialized?
67 if (!this.isBundledInitialized()) {
68 // Temporary initialize default bundle
69 // @TODO The enum Gender uses this
73 // Initialize properties from context
74 this.initProperties(context);
76 // Init database frontends
77 this.initDatabaseFrontends();
80 this.getLogger().trace("EXIT!"); //NOI18N
86 public PizzaServiceApplication () {
90 * Calculates total amount of all choosen products
92 * @param request Request instance
93 * @param session Session instance
94 * @return Total amount of all choosen products
97 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
99 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
101 // Is product and session set?
102 if (null == request) {
104 throw new NullPointerException("request is null"); //NOI18N
105 } else if (null == session) {
107 throw new NullPointerException("session is null"); //NOI18N
110 // Init/declare total price and iterator
112 Iterator<Product> iterator = this.getAvailableProducts();
114 // "Walk" over all products
115 while (iterator.hasNext()) {
117 Product product = iterator.next();
120 if (this.isProductChoosen(product, request, session)) {
121 // Then add ordered amount
122 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
125 String amount = this.getAmountFromSession(product, session);
128 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
129 totalAmount += Integer.valueOf(amount);
131 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
135 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
137 // Return total price
142 * Calculates total price of all choosen products
144 * @param request Request instance
145 * @param session Session instance
146 * @return Total price of all choosen products
149 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
151 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
153 // Is product and session set?
154 if (null == request) {
156 throw new NullPointerException("request is null"); //NOI18N
157 } else if (null == session) {
159 throw new NullPointerException("session is null"); //NOI18N
163 float totalPrice = 0.00f;
166 Iterator<Product> iterator = this.getAvailableProducts();
168 // "Walk" over all products
169 while (iterator.hasNext()) {
171 Product product = iterator.next();
174 if (this.isProductChoosen(product, request, session)) {
175 // Then add product's total price
176 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getItemId())); //NOI18N
177 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
179 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getItemId(), totalPrice)); //NOI18N
183 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
185 // Return total price
190 public void doBootstrap () {
191 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
195 public void doMainLoop () {
196 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
200 public void doShutdown () {
201 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
205 * Some "getter" for amount from session
207 * @param product Product instance
208 * @param session Session instance
209 * @return Amount as string
213 public String getAmountFromSession (final Product product, final HttpSession session) {
215 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
217 // Is product and session set?
218 if (null == product) {
220 throw new NullPointerException("product is null"); //NOI18N
221 } else if (null == session) {
223 throw new NullPointerException("session is null"); //NOI18N
227 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
229 // Is the object null?
230 if (null == object) {
232 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
239 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
241 // Cast to string and return it
242 return (String) object;
246 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
248 * @param product Product instance
249 * @param request Request instance
250 * @param session Session instance
251 * @return Whether the product is choosen
254 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
256 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
258 // Is product and session set?
259 if (null == product) {
261 throw new NullPointerException("product is null"); //NOI18N
262 } else if (null == request) {
264 throw new NullPointerException("request is null"); //NOI18N
265 } else if (null == session) {
267 throw new NullPointerException("session is null"); //NOI18N
270 // First let's check if the product is choosen
271 if (this.isProductChoosen(product, request, session)) {
273 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
276 return "checked=\"checked\""; //NOI18N
279 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
287 * Some "getter" for choose from session
289 * @param product Product instance
290 * @param session Session instance
291 * @return Choose as string
295 public String getChooseFromSession (final Product product, final HttpSession session) {
297 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
299 // Is product and session set?
300 if (null == product) {
302 throw new NullPointerException("product is null"); //NOI18N
303 } else if (null == session) {
305 throw new NullPointerException("session is null"); //NOI18N
309 Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
311 // Is the object null?
312 if (null == object) {
314 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getItemId())); //NOI18N
319 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
321 // Cast to string and return it
322 return (String) object;
326 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
328 * @param request Request instance
329 * @param session Session instance
330 * @return Whether the product is choosen
333 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
335 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
337 // Is product and session set?
338 if (null == request) {
340 throw new NullPointerException("request is null"); //NOI18N
341 } else if (null == session) {
343 throw new NullPointerException("session is null"); //NOI18N
346 // Get "enabled" from request scope
347 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
350 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
352 // Is something selected?
353 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
355 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
357 // Something has been choosen
361 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
363 // Nothing choosen yet
364 return "disabled=\"disabled\""; //NOI18N
369 * Some "getter" for choosen (checkbox) from session
371 * @param product Product instance
372 * @param request Request instance
373 * @param session Session instance
374 * @return Amount as string
378 public String getPrintableChoosenFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
380 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
382 // Is product and session set?
383 if (null == product) {
385 throw new NullPointerException("product is null"); //NOI18N
386 } else if (null == request) {
388 throw new NullPointerException("request is null"); //NOI18N
389 } else if (null == session) {
391 throw new NullPointerException("session is null"); //NOI18N
395 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
396 String choosen = this.handleChooseFromRequestSession(product, request, session);
397 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
400 assert(choosen instanceof String): "choosen is null"; //NOI18N
403 if (choosen.isEmpty()) {
409 String amount = this.handleAmountFromRequestSession(product, request, session);
410 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
413 assert(amount instanceof String): "amount is null"; //NOI18N
416 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
417 // Choosen, but no amount
426 * Checks if given Product instance is available and returns a printable
427 * (human-readable) string.
429 * @param product Product instance to check
430 * @return Human-readable version of product availability
433 public String getPrintableProduktAvailability (final Product product) {
435 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
438 if (null == product) {
439 // Should not be null
440 throw new NullPointerException("product is null"); //NOI18N
444 if (product.getAvailable() == true) {
448 // Not, not for public
454 * Some getter for printable value from session or an empty string for null.
456 * @param session Session instance
457 * @param key Key to get
458 * @return Value from key, empty string for null
461 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
463 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
465 // Are both parameter not null?
466 if (null == session) {
468 throw new NullPointerException("session is null"); //NOI18N
469 } else if (null == key) {
471 throw new NullPointerException("key is null"); //NOI18N
475 Object value = this.getValueFromSession(session, key);
478 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
481 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
483 // Return actual value
484 return this.convertNullToEmpty(value);
488 * Some "getter" for a an array of only available products
490 * @return All products
493 public Iterator<Product> getAvailableProducts () throws ServletException {
495 // Ask frontend for a list of products
496 return this.productFrontend.getAvailableProducts();
497 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
498 throw new ServletException(ex);
503 * Some "getter" for a an array of all products
505 * @return All products
508 public Iterator<Product> getAllProducts () throws ServletException {
510 // Ask frontend for a list of products
511 return this.productFrontend.getAllProducts();
512 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
513 throw new ServletException(ex);
518 * Some "getter" for a an array of all categories
520 * @return All categories
523 public Iterator<Category> getCategories () throws ServletException {
525 // Ask frontend for a list of categories
526 return this.categoryFrontend.getCategories();
527 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
528 throw new ServletException(ex);
533 * Some "getter" for total price of position from request or session.
534 * Single price and amount is multiplyed.
536 * @param product Product instance
537 * @param request Request instance
538 * @param session Session instance
539 * @return Amount as string
542 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
544 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
546 // Is product and session set?
547 if (null == product) {
549 throw new NullPointerException("product is null"); //NOI18N
550 } else if (null == request) {
552 throw new NullPointerException("request is null"); //NOI18N
553 } else if (null == session) {
555 throw new NullPointerException("session is null"); //NOI18N
559 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
560 String choosen = this.handleChooseFromRequestSession(product, request, session);
561 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
564 assert(choosen instanceof String): "choosen is null"; //NOI18N
567 if (choosen.isEmpty()) {
569 this.getLogger().debug(MessageFormat.format("product={0},choosen={1} - returning zero ...", product.getItemId(), choosen)); //NOI18N
574 String amount = this.handleAmountFromRequestSession(product, request, session);
575 this.getLogger().debug(MessageFormat.format("product={0},amount={1}", product.getItemId(), amount)); //NOI18N
578 assert(amount instanceof String): "amount is null"; //NOI18N
581 if (amount.isEmpty() || "0".equals(amount)) { //NOI18N
583 this.getLogger().debug(MessageFormat.format("product={0},amount={1} - returning zero ...", product.getItemId(), amount)); //NOI18N
588 Integer value = null;
592 // Get amount as integer
593 value = Integer.valueOf(amount);
594 } catch (final NumberFormatException e) {
596 throw new IllegalArgumentException(e);
600 float price = (product.getPrice() * value);
603 this.getLogger().trace(MessageFormat.format("product={0},price={1} - EXIT!", product.getItemId(), price)); //NOI18N
605 // Then multiply it with price
610 * Handler for amount from request or session
612 * @param product Product instance
613 * @param request Request instance
614 * @param session Session instance
615 * @return Amount as string
619 public String handleAmountFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
621 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
623 // Is product and session set?
624 if (null == product) {
626 throw new NullPointerException("product is null"); //NOI18N
627 } else if (null == request) {
629 throw new NullPointerException("request is null"); //NOI18N
630 } else if (null == session) {
632 throw new NullPointerException("session is null"); //NOI18N
638 // Check request method
639 if (!"POST".equals(request.getMethod())) { //NOI18N
640 // Not POST, so get from session
641 return this.getAmountFromSession(product, session);
642 } else if (this.handleChooseFromRequestSession(product, request, session).isEmpty()) {
644 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
645 this.getLogger().debug(MessageFormat.format("Unsetting for product={0} in session, returning zero ...", product.getItemId())); //NOI18N
649 // Get attribute from request
650 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_AMOUNT, product.getItemId()));
653 if (object instanceof String) {
654 // Try to parse it to integer
656 Integer value = Integer.valueOf((String) object);
657 } catch (final NumberFormatException ex) {
659 this.getLogger().warn(ex);
663 // Then set it in session
664 this.setValueInSession(product, session, HTTP_PARAM_AMOUNT, object);
667 return (String) object;
671 this.getLogger().trace("Calling getAmountFromSession() ..."); //NOI18N
673 // Get attribute from session
674 return this.getAmountFromSession(product, session);
678 * Checks whether the given product is choosen, request overules session.
680 * @param product Product instance
681 * @param request Request instance
682 * @param session Session instance
683 * @return Whether the product is choosen
687 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
689 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
691 // Is product and session set?
692 if (null == product) {
694 throw new NullPointerException("product is null"); //NOI18N
695 } else if (null == request) {
697 throw new NullPointerException("request is null"); //NOI18N
698 } else if (null == session) {
700 throw new NullPointerException("session is null"); //NOI18N
704 this.getLogger().debug(MessageFormat.format("Calling handleChooseFromRequestSession({0},{1},{2}) ...", product.getItemId(), request, session)); //NOI18N
705 String choosen = this.handleChooseFromRequestSession(product, request, session);
706 this.getLogger().debug(MessageFormat.format("product={0},choosen={1}", product.getItemId(), choosen)); //NOI18N
709 assert(choosen instanceof String): "choosen is null"; //NOI18N
711 // Is it not choosen?
712 if (choosen.isEmpty()) {
718 String amount = this.handleAmountFromRequestSession(product, request, session);
721 assert(amount instanceof String): "amount is not set"; //NOI18N
724 this.getLogger().trace(MessageFormat.format("amount={0} - EXIT!", amount)); //NOI18N
726 // Must not be empty and not 0
727 return (!amount.isEmpty() && !"0".equals(amount)); //NOI18N
731 * Marks all choosen products as ordered
733 * @param request Request instance
734 * @param session Session instance
738 public void markAllChoosenProductsAsOrdered (final HttpServletRequest request, final HttpSession session) throws ServletException {
740 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
743 Iterator<Product> iterator = this.getAvailableProducts();
745 // "Walk" over all products
746 while (iterator.hasNext()) {
748 Product product = iterator.next();
751 this.getLogger().debug(MessageFormat.format("product={0}", product)); //NOI18N
754 if (this.isProductChoosen(product, request, session)) {
755 // Mark product as ordered
756 this.markProductAsOrdered(product, session);
761 this.getLogger().trace("EXIT!"); //NOI18N
765 * Marks given product as choosen in session
767 * @param product Product to mark as ordered
768 * @param session Session instance
772 public void markProductAsChoosen (final Product product, final HttpSession session) {
774 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
776 // Is product and session set?
777 if (null == product) {
779 throw new NullPointerException("product is null"); //NOI18N
780 } else if (null == session) {
782 throw new NullPointerException("session is null"); //NOI18N
785 // Mark it as ordered by setting flag
786 this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getItemId())); //NOI18N
787 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
790 this.getLogger().trace("EXIT!"); //NOI18N
794 * Marks given product as ordered in session
796 * @param product Product to mark as ordered
797 * @param session Session instance
801 public void markProductAsOrdered (final Product product, final HttpSession session) {
803 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
805 // Is product and session set?
806 if (null == product) {
808 throw new NullPointerException("product is null"); //NOI18N
809 } else if (null == session) {
811 throw new NullPointerException("session is null"); //NOI18N
814 // Mark it as ordered by setting flag
815 this.getLogger().debug(MessageFormat.format("Marking product={0} as ordered.", product.getItemId())); //NOI18N
816 this.setValueInSession(product, session, SESSION_ORDERED, "true"); //NOI18N
819 this.getLogger().trace("EXIT!"); //NOI18N
823 * Somewhat setter in session
825 * @param session Session instance
826 * @param key Session key to set
827 * @param value Value to set
830 public void setValueInSession (final HttpSession session, final String key, final Object value) {
832 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
834 synchronized(session) {
836 session.setAttribute(key, value);
840 this.getLogger().trace("EXIT!"); //NOI18N
844 * Unmarks given product as choosen in session
846 * @param product Product to unmark as choosen
847 * @param session Session instance
851 public void unmarkProductAsChoosen (final Product product, final HttpSession session) {
853 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
855 // Is product and session set?
856 if (null == product) {
858 throw new NullPointerException("product is null"); //NOI18N
859 } else if (null == session) {
861 throw new NullPointerException("session is null"); //NOI18N
864 // Mark it as ordered by setting flag
865 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getItemId())); //NOI18N
866 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
869 this.getLogger().trace("EXIT!"); //NOI18N
873 * Unmarks given product as ordered in session
875 * @param product Product to unmark as ordered
876 * @param session Session instance
880 public void unmarkProductAsOrdered (final Product product, final HttpSession session) {
882 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
884 // Is product and session set?
885 if (null == product) {
887 throw new NullPointerException("product is null"); //NOI18N
888 } else if (null == session) {
890 throw new NullPointerException("session is null"); //NOI18N
893 // Mark it as ordered by setting flag
894 this.getLogger().debug(MessageFormat.format("Unmarking product={0} as ordered.", product.getItemId())); //NOI18N
895 this.clearSessionAttribute(product, session, SESSION_ORDERED);
898 this.getLogger().trace("EXIT!"); //NOI18N
902 * Clears given parameter for product in session
904 * @param product Product instance
905 * @param session Session instance
906 * @param parameter Parameter to clear
908 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
910 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
913 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
914 this.setValueInSession(product, session, parameter, null);
917 this.getLogger().trace("EXIT!"); //NOI18N
921 * Some getter for value from session
923 * @param product Product instance
924 * @param session Session instance
925 * @param attribute Attribute to get value from
926 * @return Value from session
928 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
930 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
933 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
935 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
938 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
945 * Some getter for value from session
947 * @param session Session instance
948 * @param key Key to get value from
949 * @return Value from session
951 private Object getValueFromSession (final HttpSession session, final String key) {
953 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
958 // Get it synchronized from session
959 synchronized (session) {
960 value = session.getAttribute(key);
964 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
971 * Handler for choosen (checkbox) from request or session
973 * @param product Product instance
974 * @param request Request instance
975 * @param session Session instance
976 * @return Amount as string
978 private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
980 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
982 // Is product and session set?
983 if (null == product) {
985 throw new NullPointerException("product is null"); //NOI18N
986 } else if (null == request) {
988 throw new NullPointerException("request is null"); //NOI18N
989 } else if (null == session) {
991 throw new NullPointerException("session is null"); //NOI18N
997 // Check request method
998 if (!"POST".equals(request.getMethod())) { //NOI18N
999 // Not POST, so get from session
1000 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1001 return this.getChooseFromSession(product, session);
1002 } else if (this.isProductOrdered(product, session)) {
1003 // Product is ordered
1004 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1005 return this.getChooseFromSession(product, session);
1006 } else if (!this.getChooseFromSession(product, session).isEmpty()) {
1008 this.getLogger().trace(MessageFormat.format("Calling this.getChooseFromSession({0},{1}) ... - EXIT!", product.getItemId(), session)); //NOI18N
1009 return this.getChooseFromSession(product, session);
1012 // Get reqzest element
1013 object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getItemId()));
1014 this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getItemId(), object)); //NOI18N
1017 if (null == object) {
1019 this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getItemId())); //NOI18N
1020 this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
1021 this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
1023 // Return empty string
1027 // Then set it in session
1028 this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, object);
1030 // Cast to string and return it
1031 this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getItemId(), object)); //NOI18N
1032 return (String) object;
1036 * Initializes database frontends.
1038 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
1040 this.getLogger().trace("CALLED!"); //NOI18N
1043 this.productFrontend = new PizzaProductDatabaseFrontend();
1045 // Category frontend
1046 this.categoryFrontend = new PizzaCategoryDatabaseFrontend();
1049 this.getLogger().trace("EXIT!"); //NOI18N
1053 * Checks whether given category title is already used
1055 * @param title Title of category to check
1056 * @return Whether it has been found
1058 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1059 // Delegate to frontend
1060 return this.categoryFrontend.isCategoryTitleUsed(title);
1064 * Checks if given product title is already used
1065 * @param title Product title to check
1066 * @return Whether the product title has already been used
1068 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1069 // Delegate to frontend
1070 return this.productFrontend.isProductTitleUsed(title);
1074 * Checks if the product ordered?
1076 * @param product Product instance
1077 * @param session HttpSession instance
1078 * @return Whether the product has been ordered
1080 private boolean isProductOrdered (final Product product, final HttpSession session) {
1082 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
1085 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
1086 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), isOrdered)); //NOI18N
1089 return ("true".equals(isOrdered)); //NOI18N
1093 * Somewhat setter in session
1095 * @param product Product instance
1096 * @param session Session instance
1097 * @param keyPart Key part to include in final key
1098 * @param value Value to set
1100 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
1102 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
1105 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
1106 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
1109 this.getLogger().trace("EXIT!"); //NOI18N
1113 * Adds given category data from request to database
1115 * @param request Request instance
1118 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
1120 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1122 // request must not be null
1123 if (null == request) {
1125 throw new NullPointerException("request is null"); //NOI18N
1129 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1130 String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT);
1133 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
1135 // Init variables for casting
1139 if (null == title) {
1141 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1142 } else if (title.isEmpty()) {
1144 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1145 } else if ((parent != null) && (!parent.isEmpty())) {
1146 // "parent" is set, so check it
1148 id = Integer.parseInt(parent);
1149 } catch (final NumberFormatException e) {
1151 throw new IllegalArgumentException(e);
1156 // Try to check if title is used already
1157 if (this.isCategoryTitleUsed(title)) {
1158 // Title already used
1159 throw new CategoryTitleAlreadyUsedException(request);
1161 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1162 throw new ServletException(ex);
1166 // The category is not found, so add it to database
1167 this.categoryFrontend.addCategory(title, id);
1168 } catch (final SQLException | IOException ex) {
1169 // Continue to throw it
1170 throw new ServletException(ex);
1174 this.getLogger().trace("EXIT!"); //NOI18N
1178 * Adds given product data from request to database
1180 * @param request Request instance
1183 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
1185 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1187 // request must not be null
1188 if (null == request) {
1190 throw new NullPointerException("request is null"); //NOI18N
1193 // Get title, price and category id
1194 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1195 String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE);
1196 String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY);
1197 String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
1200 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
1202 // Variables for converting
1207 if (null == title) {
1209 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1210 } else if (title.isEmpty()) {
1212 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1213 } else if (null == price) {
1215 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1216 } else if (price.isEmpty()) {
1218 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); //NOI18N
1219 } else if (null == category) {
1221 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1222 } else if (category.isEmpty()) {
1224 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); //NOI18N
1225 } else if (null == available) {
1227 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1228 } else if (available.isEmpty()) {
1230 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_AVAILABLE)); //NOI18N
1231 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
1233 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", PizzaProductDatabaseConstants.COLUMN_AVAILABLE, available)); //NOI18N
1238 id = Long.parseLong(category);
1239 p = Float.parseFloat(price);
1240 } catch (final NumberFormatException e) {
1242 throw new IllegalArgumentException(e);
1246 Boolean a = Boolean.parseBoolean(available);
1248 // Test on product title
1250 // Try to check if title is used already
1251 if (this.isProductTitleUsed(title)) {
1252 // Title already used
1253 throw new ProductTitleAlreadyUsedException(request);
1255 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
1256 throw new ServletException(ex);
1260 // The product is not found, so add it to database
1261 this.productFrontend.addProduct(title, p, id, a);
1262 } catch (final SQLException | IOException ex) {
1263 // Continue to throw it
1264 throw new ServletException(ex);
1268 this.getLogger().trace("EXIT!"); //NOI18N
1272 * Generates link HTML code for given category's parent id, if set. This
1273 * link then points to products.jsp?category_id=x
1275 * @param category Category instance
1279 public String generateLinkForParent (final Category category) {
1281 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
1283 // category must not be null
1284 if (null == category) {
1286 throw new NullPointerException("category is null"); //NOI18N
1290 Long parent = category.getParent();
1294 // Product HTML code for link
1295 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
1303 public String getPrintableProduktCategory (final Product product) throws ServletException {
1305 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
1307 // product must not be null
1308 if (null == product) {
1310 throw new NullPointerException("product is null"); //NOI18N
1317 // Get Category instance from product over the frontend
1318 category = this.categoryFrontend.getCategory(product);
1319 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1320 throw new ServletException(ex);
1324 this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
1326 String title = null;
1328 // Now get title from it and return it
1329 title = category.decodedTitle();
1330 } catch (final UnsupportedEncodingException ex) {
1331 // Continue to throw as cause
1332 throw new ServletException(ex);
1336 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
1343 * Checks if product's title is already used.
1345 * @param request Request instance
1346 * @return Whether the product title is already used
1347 * @throws java.io.IOException If any IO error occurs
1348 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1349 * @throws java.sql.SQLException If any SQL error occurs
1350 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1351 * @throws java.lang.NoSuchMethodException If a method was not found
1352 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1353 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1355 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1357 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1360 String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE);
1362 // request must not be null and "title" must be found and non-empty
1363 if (null == request) {
1365 throw new NullPointerException("request is null"); //NOI18N
1366 } else if (null == title) {
1368 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1369 } else if (title.isEmpty()) {
1371 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
1374 // Default is not used
1375 boolean isUsed = this.isProductTitleUsed(title);
1378 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1385 * Handles admin form requests
1386 * @param request Request instance
1387 * @param response Response instance
1388 * @throws ServletException If something unexpected happened
1391 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1393 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1395 // request and response must both be set
1396 if (null == request) {
1398 throw new NullPointerException("request is null"); //NOI18N
1399 } else if (null == response) {
1401 throw new NullPointerException("response is null"); //NOI18N
1404 // Try this operations
1407 if ("POST".equals(request.getMethod())) { //NOI18N
1408 // Is "add/edit/delete" set?
1409 if (request.getParameter("add") != null) { //NOI18N
1410 // Is it already added?
1411 if (this.isProductTitleUsed(request)) {
1413 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1415 // Already added, so redirect here, else a ServletException will be thrown
1416 response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
1419 this.doAdminAddProduct(request);
1421 } else if (request.getParameter("edit") != null) { //NOI18N
1423 } else if (request.getParameter("delete") != null) { //NOI18N
1427 // Redirect to proper URL
1428 // @TODO Commented out for developing:
1429 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1431 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1432 // Throw it as cause
1433 throw new ServletException(ex);
1437 this.getLogger().trace("EXIT!"); //NOI18N
1441 * Handles admin form requests
1442 * @param request Request instance
1443 * @param response Response instance
1444 * @throws ServletException If something unexpected happened
1447 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1449 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1451 // request and response must both be set
1452 if (null == request) {
1454 throw new NullPointerException("request is null"); //NOI18N
1455 } else if (null == response) {
1457 throw new NullPointerException("response is null"); //NOI18N
1460 // Try this operations
1463 if ("POST".equals(request.getMethod())) { //NOI18N
1464 // Is "add/edit/delete" set?
1465 if (request.getParameter("add") != null) { //NOI18N
1466 // Is the category title already used?
1467 if (this.isCategoryTitleUsed(request)) {
1469 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1471 // Already added, so redirect here, else a ServletException will be thrown
1472 response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
1475 this.doAdminAddCategory(request);
1477 } else if (request.getParameter("edit") != null) { //NOI18N
1479 } else if (request.getParameter("delete") != null) { //NOI18N
1483 // Redirect to proper URL
1484 // @TODO Commented out for developing:
1485 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1487 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1488 // Throw it as cause
1489 throw new ServletException(ex);
1493 this.getLogger().trace("EXIT!"); //NOI18N
1497 * Checks if category's title is already used.
1499 * @param request Request instance
1500 * @return Whether the product title is already used
1501 * @throws java.io.IOException If any IO error occurs
1502 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1503 * @throws java.sql.SQLException If any SQL error occurs
1504 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1505 * @throws java.lang.NoSuchMethodException If a method was not found
1506 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1507 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1509 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1511 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1514 String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE);
1516 // request must not be null and "title" must be found and non-empty
1517 if (null == request) {
1519 throw new NullPointerException("request is null"); //NOI18N
1520 } else if (null == title) {
1522 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1523 } else if (title.isEmpty()) {
1525 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N
1528 // Default is not used
1529 boolean isUsed = this.isCategoryTitleUsed(title);
1532 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N