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.jshop.category.Category;
34 import org.mxchange.jshop.database.frontend.category.CategoryDatabaseFrontend;
35 import org.mxchange.jshop.database.frontend.category.CategoryFrontend;
36 import org.mxchange.jshop.database.frontend.product.ProductDatabaseFrontend;
37 import org.mxchange.jshop.database.frontend.product.ProductFrontend;
38 import org.mxchange.jshop.exceptions.CategoryTitleAlreadyUsedException;
39 import org.mxchange.jshop.exceptions.ProductTitleAlreadyUsedException;
40 import org.mxchange.jshop.item.AddableBasketItem;
41 import org.mxchange.jshop.product.Product;
42 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
45 * Main application class
47 * @author Roland Haeder
49 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
51 * Frontend for products
53 private ProductFrontend productFrontend;
56 * Frontend for categories
58 private CategoryFrontend categoryFrontend;
63 public PizzaServiceApplication () {
65 this.getLogger().trace("CALLED!");
69 public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
71 this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); //NOI18N
73 // context should not be null
74 if (null == context) {
76 throw new NullPointerException("context is null");
79 // Is the bundle initialized?
80 if (!this.isBundledInitialized()) {
81 // Temporary initialize default bundle
82 // @TODO The enum Gender uses this
86 // Initialize properties from context
87 this.initProperties(context);
89 // Init database frontends
90 this.initDatabaseFrontends();
93 this.getLogger().trace("EXIT!"); //NOI18N
97 * Calculates total amount of all choosen products
99 * @param request Request instance
100 * @param session Session instance
101 * @return Total amount of all choosen products
102 * @deprecated Old lost
105 private int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
107 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
109 // Is product and session set?
110 if (null == request) {
112 throw new NullPointerException("request is null"); //NOI18N
113 } else if (null == session) {
115 throw new NullPointerException("session is null"); //NOI18N
118 // Init/declare total price and iterator
120 Iterator<Product> iterator = this.getAvailableProducts();
122 // "Walk" over all products
123 while (iterator.hasNext()) {
125 Product product = iterator.next();
128 if (this.isProductChoosen(product, request, session)) {
129 // Then add ordered amount
130 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
133 String amount = this.getAmountFromSession(product, session);
136 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
137 totalAmount += Integer.valueOf(amount);
139 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
143 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
145 // Return total price
150 public void doBootstrap () {
151 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
155 public void doMainLoop () {
156 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
160 public void doShutdown () {
161 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
165 * Some "getter" for amount from session
167 * @param product Product instance
168 * @param session Session instance
169 * @return Amount as string
173 public String getAmountFromSession (final Product product, final HttpSession session) {
175 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
177 // Is product and session set?
178 if (null == product) {
180 throw new NullPointerException("product is null"); //NOI18N
181 } else if (null == session) {
183 throw new NullPointerException("session is null"); //NOI18N
187 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
189 // Is the object null?
190 if (null == object) {
192 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
199 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
201 // Cast to string and return it
202 return (String) object;
206 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
208 * @param product Product instance
209 * @param request Request instance
210 * @param session Session instance
211 * @return Whether the product is choosen
214 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
216 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
218 // Is product and session set?
219 if (null == product) {
221 throw new NullPointerException("product is null"); //NOI18N
222 } else if (null == request) {
224 throw new NullPointerException("request is null"); //NOI18N
225 } else if (null == session) {
227 throw new NullPointerException("session is null"); //NOI18N
230 // First let's check if the product is choosen
231 if (this.isProductChoosen(product, request, session)) {
233 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
236 return "checked=\"checked\""; //NOI18N
239 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
247 * Some "getter" for choose from session
249 * @param product Product instance
250 * @param session Session instance
251 * @return Choose as string
255 public String getChooseFromSession (final Product product, final HttpSession session) {
257 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
259 // Is product and session set?
260 if (null == product) {
262 throw new NullPointerException("product is null"); //NOI18N
263 } else if (null == session) {
265 throw new NullPointerException("session is null"); //NOI18N
269 Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
271 // Is the object null?
272 if (null == object) {
274 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getItemId())); //NOI18N
279 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
281 // Cast to string and return it
282 return (String) object;
286 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
288 * @param request Request instance
289 * @param session Session instance
290 * @return Whether the product is choosen
293 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
295 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
297 // Is product and session set?
298 if (null == request) {
300 throw new NullPointerException("request is null"); //NOI18N
301 } else if (null == session) {
303 throw new NullPointerException("session is null"); //NOI18N
306 // Get "enabled" from request scope
307 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
310 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
312 // Is something selected?
313 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
315 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
317 // Something has been choosen
321 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
323 // Nothing choosen yet
324 return "disabled=\"disabled\""; //NOI18N
329 * Checks if given Product instance is available and returns a printable
330 * (human-readable) string.
332 * @param product Product instance to check
333 * @return Human-readable version of product availability
336 public String getPrintableProduktAvailability (final Product product) {
338 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
341 if (null == product) {
342 // Should not be null
343 throw new NullPointerException("product is null"); //NOI18N
347 if (product.getAvailable() == true) {
351 // Not, not for public
357 * Some getter for printable value from session or an empty string for null.
359 * @param session Session instance
360 * @param key Key to get
361 * @return Value from key, empty string for null
364 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
366 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
368 // Are both parameter not null?
369 if (null == session) {
371 throw new NullPointerException("session is null"); //NOI18N
372 } else if (null == key) {
374 throw new NullPointerException("key is null"); //NOI18N
378 Object value = this.getValueFromSession(session, key);
381 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
384 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
386 // Return actual value
387 return this.convertNullToEmpty(value);
391 * Some "getter" for a an array of only available products
393 * @return All products
396 public Iterator<Product> getAvailableProducts () throws ServletException {
397 // categoryFrontend must be set
398 if (null == this.productFrontend) {
400 throw new NullPointerException("productFrontend is null");
404 // Ask frontend for a list of products
405 return this.productFrontend.getAvailableProducts();
406 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
407 throw new ServletException(ex);
412 * Some "getter" for a an array of all products
414 * @return All products
417 public Iterator<Product> getAllProducts () throws ServletException {
419 this.getLogger().trace("CALLED!");
421 // categoryFrontend must be set
422 if (null == this.productFrontend) {
424 throw new NullPointerException("productFrontend is null");
428 // Ask frontend for a list of products
429 return this.productFrontend.getAllProducts();
430 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
431 throw new ServletException(ex);
436 * Some "getter" for a an array of all categories
438 * @return All categories
441 public Iterator<Category> getAllCategories () throws ServletException {
443 this.getLogger().trace("CALLED!");
445 // categoryFrontend must be set
446 if (null == this.categoryFrontend) {
448 throw new NullPointerException("categoryFrontend is null");
452 // Ask frontend for a list of categories
453 return this.categoryFrontend.getAllCategories();
454 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
455 throw new ServletException(ex);
460 * Some "getter" for total price of position from request or session.
461 * Single price and amount is multiplyed.
463 * @param product Product instance
464 * @param request Request instance
465 * @param session Session instance
466 * @return Amount as string
470 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
471 throw new UnsupportedOperationException("This method is deprecated and shall not be called.");
475 * Checks whether the given product is choosen, request overules session.
477 * @param product Product instance
478 * @param request Request instance
479 * @param session Session instance
480 * @return Whether the product is choosen
484 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
485 throw new UnsupportedOperationException("This method is deprecated and shall not be called");
489 * Somewhat setter in session
491 * @param session Session instance
492 * @param key Session key to set
493 * @param value Value to set
496 public void setValueInSession (final HttpSession session, final String key, final Object value) {
498 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
500 synchronized(session) {
502 session.setAttribute(key, value);
506 this.getLogger().trace("EXIT!"); //NOI18N
510 * Clears given parameter for product in session
512 * @param product Product instance
513 * @param session Session instance
514 * @param parameter Parameter to clear
516 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
518 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
521 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
522 this.setValueInSession(product, session, parameter, null);
525 this.getLogger().trace("EXIT!"); //NOI18N
529 * Some getter for value from session
531 * @param product Product instance
532 * @param session Session instance
533 * @param attribute Attribute to get value from
534 * @return Value from session
536 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
538 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
541 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
543 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
546 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
553 * Some getter for value from session
555 * @param session Session instance
556 * @param key Key to get value from
557 * @return Value from session
559 private Object getValueFromSession (final HttpSession session, final String key) {
561 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
566 // Get it synchronized from session
567 synchronized (session) {
568 value = session.getAttribute(key);
572 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
579 * Initializes database frontends.
581 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
583 this.getLogger().trace("CALLED!"); //NOI18N
586 this.productFrontend = new ProductDatabaseFrontend();
589 this.categoryFrontend = new CategoryDatabaseFrontend();
592 this.getLogger().trace("EXIT!"); //NOI18N
596 * Checks whether given category title is already used
598 * @param title Title of category to check
599 * @return Whether it has been found
601 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
602 // categoryFrontend must be set
603 if (null == this.categoryFrontend) {
605 throw new NullPointerException("categoryFrontend is null");
608 // Delegate to frontend
609 return this.categoryFrontend.isCategoryTitleUsed(title);
613 * Checks if given product title is already used
614 * @param title Product title to check
615 * @return Whether the product title has already been used
617 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
618 // categoryFrontend must be set
619 if (null == this.productFrontend) {
621 throw new NullPointerException("productFrontend is null");
624 // Delegate to frontend
625 return this.productFrontend.isProductTitleUsed(title);
629 * Checks if the product ordered?
631 * @param product Product instance
632 * @param session HttpSession instance
633 * @return Whether the product has been ordered
635 private boolean isProductOrdered (final Product product, final HttpSession session) {
637 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
640 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
641 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), isOrdered)); //NOI18N
644 return ("true".equals(isOrdered)); //NOI18N
648 * Somewhat setter in session
650 * @param product Product instance
651 * @param session Session instance
652 * @param keyPart Key part to include in final key
653 * @param value Value to set
655 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
657 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
660 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
661 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
664 this.getLogger().trace("EXIT!"); //NOI18N
668 * Adds given category data from request to database
670 * @param request Request instance
673 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
675 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
677 // request must not be null
678 if (null == request) {
680 throw new NullPointerException("request is null"); //NOI18N
684 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
685 String parent = request.getParameter(CategoryFrontend.COLUMN_PARENT);
688 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
690 // Init variables for casting
696 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
697 } else if (title.isEmpty()) {
699 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
700 } else if ((parent != null) && (!parent.isEmpty())) {
701 // "parent" is set, so check it
703 id = Integer.parseInt(parent);
704 } catch (final NumberFormatException e) {
706 throw new IllegalArgumentException(e);
711 // Try to check if title is used already
712 if (this.isCategoryTitleUsed(title)) {
713 // Title already used
714 throw new CategoryTitleAlreadyUsedException(request);
716 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
717 throw new ServletException(ex);
721 // The category is not found, so add it to database
722 this.categoryFrontend.addCategory(title, id);
723 } catch (final SQLException | IOException ex) {
724 // Continue to throw it
725 throw new ServletException(ex);
729 this.getLogger().trace("EXIT!"); //NOI18N
733 * Adds given product data from request to database
735 * @param request Request instance
738 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
740 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
742 // request must not be null
743 if (null == request) {
745 throw new NullPointerException("request is null"); //NOI18N
748 // Get title, price and category id
749 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
750 String price = request.getParameter(ProductFrontend.COLUMN_PRICE);
751 String category = request.getParameter(ProductFrontend.COLUMN_CATEGORY);
752 String available = request.getParameter(ProductFrontend.COLUMN_AVAILABLE);
755 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
757 // Variables for converting
764 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
765 } else if (title.isEmpty()) {
767 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
768 } else if (null == price) {
770 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_PRICE)); //NOI18N
771 } else if (price.isEmpty()) {
773 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_PRICE)); //NOI18N
774 } else if (null == category) {
776 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
777 } else if (category.isEmpty()) {
779 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
780 } else if (null == available) {
782 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
783 } else if (available.isEmpty()) {
785 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
786 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
788 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", ProductFrontend.COLUMN_AVAILABLE, available)); //NOI18N
793 id = Long.parseLong(category);
794 p = Float.parseFloat(price);
795 } catch (final NumberFormatException e) {
797 throw new IllegalArgumentException(e);
801 Boolean a = Boolean.parseBoolean(available);
803 // Test on product title
805 // Try to check if title is used already
806 if (this.isProductTitleUsed(title)) {
807 // Title already used
808 throw new ProductTitleAlreadyUsedException(request);
810 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
811 throw new ServletException(ex);
815 // The product is not found, so add it to database
816 this.productFrontend.addProduct(title, p, id, a);
817 } catch (final SQLException | IOException ex) {
818 // Continue to throw it
819 throw new ServletException(ex);
823 this.getLogger().trace("EXIT!"); //NOI18N
827 * Generates link HTML code for given category's parent id, if set. This
828 * link then points to products.jsp?category_id=x
830 * @param category Category instance
834 public String generateLinkForParent (final Category category) {
836 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
838 // category must not be null
839 if (null == category) {
841 throw new NullPointerException("category is null"); //NOI18N
845 Long parent = category.getParent();
849 // Product HTML code for link
850 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
858 public String getPrintableProduktCategory (final Product product) throws ServletException {
860 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
862 // product must not be null
863 if (null == product) {
865 throw new NullPointerException("product is null"); //NOI18N
872 // Get Category instance from product over the frontend
873 category = this.categoryFrontend.getCategory(product);
874 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
875 throw new ServletException(ex);
879 this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
883 // Now get title from it and return it
884 title = category.getDecodedTitle();
885 } catch (final UnsupportedEncodingException ex) {
886 // Continue to throw as cause
887 throw new ServletException(ex);
891 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
898 * Checks if product's title is already used.
900 * @param request Request instance
901 * @return Whether the product title is already used
902 * @throws java.io.IOException If any IO error occurs
903 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
904 * @throws java.sql.SQLException If any SQL error occurs
905 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
906 * @throws java.lang.NoSuchMethodException If a method was not found
907 * @throws java.lang.IllegalAccessException If the method cannot be accessed
908 * @throws java.lang.reflect.InvocationTargetException Any other problems?
910 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
912 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
915 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
917 // request must not be null and "title" must be found and non-empty
918 if (null == request) {
920 throw new NullPointerException("request is null"); //NOI18N
921 } else if (null == title) {
923 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
924 } else if (title.isEmpty()) {
926 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
929 // Default is not used
930 boolean isUsed = this.isProductTitleUsed(title);
933 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
940 * Handles admin form requests
941 * @param request Request instance
942 * @param response Response instance
943 * @throws ServletException If something unexpected happened
946 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
948 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
950 // request and response must both be set
951 if (null == request) {
953 throw new NullPointerException("request is null"); //NOI18N
954 } else if (null == response) {
956 throw new NullPointerException("response is null"); //NOI18N
959 // Try this operations
962 if ("POST".equals(request.getMethod())) { //NOI18N
963 // Is "add/edit/delete" set?
964 if (request.getParameter("add") != null) { //NOI18N
965 // Is it already added?
966 if (this.isProductTitleUsed(request)) {
968 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
970 // Already added, so redirect here, else a ServletException will be thrown
971 response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
974 this.doAdminAddProduct(request);
976 } else if (request.getParameter("edit") != null) { //NOI18N
978 } else if (request.getParameter("delete") != null) { //NOI18N
982 // Redirect to proper URL
983 // @TODO Commented out for developing:
984 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
986 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
988 throw new ServletException(ex);
992 this.getLogger().trace("EXIT!"); //NOI18N
996 * Handles admin form requests
997 * @param request Request instance
998 * @param response Response instance
999 * @throws ServletException If something unexpected happened
1002 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1004 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1006 // request and response must both be set
1007 if (null == request) {
1009 throw new NullPointerException("request is null"); //NOI18N
1010 } else if (null == response) {
1012 throw new NullPointerException("response is null"); //NOI18N
1015 // Try this operations
1018 if ("POST".equals(request.getMethod())) { //NOI18N
1019 // Is "add/edit/delete" set?
1020 if (request.getParameter("add") != null) { //NOI18N
1021 // Is the category title already used?
1022 if (this.isCategoryTitleUsed(request)) {
1024 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1026 // Already added, so redirect here, else a ServletException will be thrown
1027 response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
1030 this.doAdminAddCategory(request);
1032 } else if (request.getParameter("edit") != null) { //NOI18N
1034 } else if (request.getParameter("delete") != null) { //NOI18N
1038 // Redirect to proper URL
1039 // @TODO Commented out for developing:
1040 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1042 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1043 // Throw it as cause
1044 throw new ServletException(ex);
1048 this.getLogger().trace("EXIT!"); //NOI18N
1052 * Checks if category's title is already used.
1054 * @param request Request instance
1055 * @return Whether the product title is already used
1056 * @throws java.io.IOException If any IO error occurs
1057 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1058 * @throws java.sql.SQLException If any SQL error occurs
1059 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1060 * @throws java.lang.NoSuchMethodException If a method was not found
1061 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1062 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1064 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1066 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1069 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
1071 // request must not be null and "title" must be found and non-empty
1072 if (null == request) {
1074 throw new NullPointerException("request is null"); //NOI18N
1075 } else if (null == title) {
1077 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1078 } else if (title.isEmpty()) {
1080 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1083 // Default is not used
1084 boolean isUsed = this.isCategoryTitleUsed(title);
1087 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1094 public Product getProduct (final AddableBasketItem item) throws ServletException {
1096 this.getLogger().trace("item=" + item + " - CALLED!");
1098 // item should not be null
1101 throw new NullPointerException("item is null");
1102 } else if (null == this.productFrontend) {
1104 throw new NullPointerException("productFrontend is null");
1107 // Init product instance
1108 Product product = null;
1112 product = this.productFrontend.getProduct(item);
1113 } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1114 // Continue to throw
1115 throw new ServletException(ex);
1119 this.getLogger().trace("product=" + product + " - EXIT!");