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 code
106 public int calculateTotalAmount (final HttpServletRequest request, final HttpSession session) throws ServletException {
108 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
110 // Is product and session set?
111 if (null == request) {
113 throw new NullPointerException("request is null"); //NOI18N
114 } else if (null == session) {
116 throw new NullPointerException("session is null"); //NOI18N
119 // Init/declare total price and iterator
121 Iterator<Product> iterator = this.getAvailableProducts();
123 // "Walk" over all products
124 while (iterator.hasNext()) {
126 Product product = iterator.next();
129 if (this.isProductChoosen(product, request, session)) {
130 // Then add ordered amount
131 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
134 String amount = this.getAmountFromSession(product, session);
137 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
138 totalAmount += Integer.valueOf(amount);
140 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
144 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
146 // Return total price
151 * Calculates total price of all choosen products
153 * @param request Request instance
154 * @param session Session instance
155 * @return Total price of all choosen products
158 public float calculateTotalPrice (final HttpServletRequest request, final HttpSession session) throws ServletException {
160 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
162 // Is product and session set?
163 if (null == request) {
165 throw new NullPointerException("request is null"); //NOI18N
166 } else if (null == session) {
168 throw new NullPointerException("session is null"); //NOI18N
172 float totalPrice = 0.00f;
175 Iterator<Product> iterator = this.getAvailableProducts();
177 // "Walk" over all products
178 while (iterator.hasNext()) {
180 Product product = iterator.next();
183 if (this.isProductChoosen(product, request, session)) {
184 // Then add product's total price
185 this.getLogger().debug(MessageFormat.format("Calling getTotalPositionPriceFromRequestSession({0},request,session) ...", product.getItemId())); //NOI18N
186 totalPrice += this.getTotalPositionPriceFromRequestSession(product, request, session);
188 this.getLogger().debug(MessageFormat.format("product={0},totalPrice={1}", product.getItemId(), totalPrice)); //NOI18N
192 this.getLogger().trace(MessageFormat.format(" totalPrice={0} - EXIT!", totalPrice)); //NOI18N
194 // Return total price
199 public void doBootstrap () {
200 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
204 public void doMainLoop () {
205 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
209 public void doShutdown () {
210 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
214 * Some "getter" for amount from session
216 * @param product Product instance
217 * @param session Session instance
218 * @return Amount as string
222 public String getAmountFromSession (final Product product, final HttpSession session) {
224 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
226 // Is product and session set?
227 if (null == product) {
229 throw new NullPointerException("product is null"); //NOI18N
230 } else if (null == session) {
232 throw new NullPointerException("session is null"); //NOI18N
236 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
238 // Is the object null?
239 if (null == object) {
241 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
248 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
250 // Cast to string and return it
251 return (String) object;
255 * Some "getter" for HTML code 'checked="checked"' if the product is choosen
257 * @param product Product instance
258 * @param request Request instance
259 * @param session Session instance
260 * @return Whether the product is choosen
263 public String getCheckedHtmlFromProduct (final Product product, final HttpServletRequest request, final HttpSession session) {
265 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
267 // Is product and session set?
268 if (null == product) {
270 throw new NullPointerException("product is null"); //NOI18N
271 } else if (null == request) {
273 throw new NullPointerException("request is null"); //NOI18N
274 } else if (null == session) {
276 throw new NullPointerException("session is null"); //NOI18N
279 // First let's check if the product is choosen
280 if (this.isProductChoosen(product, request, session)) {
282 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
285 return "checked=\"checked\""; //NOI18N
288 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
296 * Some "getter" for choose from session
298 * @param product Product instance
299 * @param session Session instance
300 * @return Choose as string
304 public String getChooseFromSession (final Product product, final HttpSession session) {
306 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
308 // Is product and session set?
309 if (null == product) {
311 throw new NullPointerException("product is null"); //NOI18N
312 } else if (null == session) {
314 throw new NullPointerException("session is null"); //NOI18N
318 Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
320 // Is the object null?
321 if (null == object) {
323 this.getLogger().debug(MessageFormat.format("Returning empty string for product={0} ...", product.getItemId())); //NOI18N
328 this.getLogger().trace(MessageFormat.format("object={0} - CALLED!", object)); //NOI18N
330 // Cast to string and return it
331 return (String) object;
335 * Some "getter" for HTML code 'disabled="disabled"' for e.g. submit buttons
337 * @param request Request instance
338 * @param session Session instance
339 * @return Whether the product is choosen
342 public String getDisabledHtmlFromSession (final HttpServletRequest request, final HttpSession session) throws ServletException {
344 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
346 // Is product and session set?
347 if (null == request) {
349 throw new NullPointerException("request is null"); //NOI18N
350 } else if (null == session) {
352 throw new NullPointerException("session is null"); //NOI18N
355 // Get "enabled" from request scope
356 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
359 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
361 // Is something selected?
362 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
364 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
366 // Something has been choosen
370 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
372 // Nothing choosen yet
373 return "disabled=\"disabled\""; //NOI18N
378 * Checks if given Product instance is available and returns a printable
379 * (human-readable) string.
381 * @param product Product instance to check
382 * @return Human-readable version of product availability
385 public String getPrintableProduktAvailability (final Product product) {
387 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
390 if (null == product) {
391 // Should not be null
392 throw new NullPointerException("product is null"); //NOI18N
396 if (product.getAvailable() == true) {
400 // Not, not for public
406 * Some getter for printable value from session or an empty string for null.
408 * @param session Session instance
409 * @param key Key to get
410 * @return Value from key, empty string for null
413 public Object getPrintableValeFromSession (final HttpSession session, final String key) {
415 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED", session, key)); //NOI18N
417 // Are both parameter not null?
418 if (null == session) {
420 throw new NullPointerException("session is null"); //NOI18N
421 } else if (null == key) {
423 throw new NullPointerException("key is null"); //NOI18N
427 Object value = this.getValueFromSession(session, key);
430 this.getLogger().debug(MessageFormat.format("value={0}", value)); //NOI18N
433 this.getLogger().trace(MessageFormat.format("Calling this.convertNullToEmpty({0}) ... - EXIT!", value)); //NOI18N
435 // Return actual value
436 return this.convertNullToEmpty(value);
440 * Some "getter" for a an array of only available products
442 * @return All products
445 public Iterator<Product> getAvailableProducts () throws ServletException {
446 // categoryFrontend must be set
447 if (null == this.productFrontend) {
449 throw new NullPointerException("productFrontend is null");
453 // Ask frontend for a list of products
454 return this.productFrontend.getAvailableProducts();
455 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
456 throw new ServletException(ex);
461 * Some "getter" for a an array of all products
463 * @return All products
466 public Iterator<Product> getAllProducts () throws ServletException {
468 this.getLogger().trace("CALLED!");
470 // categoryFrontend must be set
471 if (null == this.productFrontend) {
473 throw new NullPointerException("productFrontend is null");
477 // Ask frontend for a list of products
478 return this.productFrontend.getAllProducts();
479 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
480 throw new ServletException(ex);
485 * Some "getter" for a an array of all categories
487 * @return All categories
490 public Iterator<Category> getAllCategories () throws ServletException {
492 this.getLogger().trace("CALLED!");
494 // categoryFrontend must be set
495 if (null == this.categoryFrontend) {
497 throw new NullPointerException("categoryFrontend is null");
501 // Ask frontend for a list of categories
502 return this.categoryFrontend.getAllCategories();
503 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
504 throw new ServletException(ex);
509 * Some "getter" for total price of position from request or session.
510 * Single price and amount is multiplyed.
512 * @param product Product instance
513 * @param request Request instance
514 * @param session Session instance
515 * @return Amount as string
519 public float getTotalPositionPriceFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) {
520 throw new UnsupportedOperationException("This method is deprecated and shall not be called.");
524 * Checks whether the given product is choosen, request overules session.
526 * @param product Product instance
527 * @param request Request instance
528 * @param session Session instance
529 * @return Whether the product is choosen
533 public boolean isProductChoosen (final Product product, final HttpServletRequest request, final HttpSession session) {
534 throw new UnsupportedOperationException("This method is deprecated and shall not be called");
538 * Somewhat setter in session
540 * @param session Session instance
541 * @param key Session key to set
542 * @param value Value to set
545 public void setValueInSession (final HttpSession session, final String key, final Object value) {
547 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
549 synchronized(session) {
551 session.setAttribute(key, value);
555 this.getLogger().trace("EXIT!"); //NOI18N
559 * Clears given parameter for product in session
561 * @param product Product instance
562 * @param session Session instance
563 * @param parameter Parameter to clear
565 private void clearSessionAttribute (final Product product, final HttpSession session, final String parameter) {
567 this.getLogger().trace(MessageFormat.format("produce={0},parameter={1},session={2} - CALLED!", product, parameter, session)); //NOI18N
570 this.getLogger().debug(MessageFormat.format("Clearing product={0},parameter={1} ...", product.getItemId(), parameter)); //NOI18N
571 this.setValueInSession(product, session, parameter, null);
574 this.getLogger().trace("EXIT!"); //NOI18N
578 * Some getter for value from session
580 * @param product Product instance
581 * @param session Session instance
582 * @param attribute Attribute to get value from
583 * @return Value from session
585 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
587 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
590 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
592 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
595 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
602 * Some getter for value from session
604 * @param session Session instance
605 * @param key Key to get value from
606 * @return Value from session
608 private Object getValueFromSession (final HttpSession session, final String key) {
610 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
615 // Get it synchronized from session
616 synchronized (session) {
617 value = session.getAttribute(key);
621 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
628 * Initializes database frontends.
630 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
632 this.getLogger().trace("CALLED!"); //NOI18N
635 this.productFrontend = new ProductDatabaseFrontend();
638 this.categoryFrontend = new CategoryDatabaseFrontend();
641 this.getLogger().trace("EXIT!"); //NOI18N
645 * Checks whether given category title is already used
647 * @param title Title of category to check
648 * @return Whether it has been found
650 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
651 // categoryFrontend must be set
652 if (null == this.categoryFrontend) {
654 throw new NullPointerException("categoryFrontend is null");
657 // Delegate to frontend
658 return this.categoryFrontend.isCategoryTitleUsed(title);
662 * Checks if given product title is already used
663 * @param title Product title to check
664 * @return Whether the product title has already been used
666 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
667 // categoryFrontend must be set
668 if (null == this.productFrontend) {
670 throw new NullPointerException("productFrontend is null");
673 // Delegate to frontend
674 return this.productFrontend.isProductTitleUsed(title);
678 * Checks if the product ordered?
680 * @param product Product instance
681 * @param session HttpSession instance
682 * @return Whether the product has been ordered
684 private boolean isProductOrdered (final Product product, final HttpSession session) {
686 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
689 Object isOrdered = this.getValueFromSession(product, session, SESSION_ORDERED);
690 this.getLogger().debug(MessageFormat.format("product={0},isOrdered={1}", product.getItemId(), isOrdered)); //NOI18N
693 return ("true".equals(isOrdered)); //NOI18N
697 * Somewhat setter in session
699 * @param product Product instance
700 * @param session Session instance
701 * @param keyPart Key part to include in final key
702 * @param value Value to set
704 private void setValueInSession (final Product product, final HttpSession session, final String keyPart, final Object value) {
706 this.getLogger().trace(MessageFormat.format("product={0},session={1},keyPart={2},value={3} - CALLED!", product, session, keyPart, value)); //NOI18N
709 this.getLogger().debug(MessageFormat.format("Setting value={0} for product={1},keyPart={2}", value, product.getItemId(), keyPart)); //NOI18N
710 this.setValueInSession(session, String.format(HTTP_PARAM_MASK, keyPart, product.getItemId()), value);
713 this.getLogger().trace("EXIT!"); //NOI18N
717 * Adds given category data from request to database
719 * @param request Request instance
722 public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
724 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
726 // request must not be null
727 if (null == request) {
729 throw new NullPointerException("request is null"); //NOI18N
733 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
734 String parent = request.getParameter(CategoryFrontend.COLUMN_PARENT);
737 this.getLogger().debug(MessageFormat.format("title={0},parent={1}", title, parent)); //NOI18N
739 // Init variables for casting
745 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
746 } else if (title.isEmpty()) {
748 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
749 } else if ((parent != null) && (!parent.isEmpty())) {
750 // "parent" is set, so check it
752 id = Integer.parseInt(parent);
753 } catch (final NumberFormatException e) {
755 throw new IllegalArgumentException(e);
760 // Try to check if title is used already
761 if (this.isCategoryTitleUsed(title)) {
762 // Title already used
763 throw new CategoryTitleAlreadyUsedException(request);
765 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
766 throw new ServletException(ex);
770 // The category is not found, so add it to database
771 this.categoryFrontend.addCategory(title, id);
772 } catch (final SQLException | IOException ex) {
773 // Continue to throw it
774 throw new ServletException(ex);
778 this.getLogger().trace("EXIT!"); //NOI18N
782 * Adds given product data from request to database
784 * @param request Request instance
787 public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
789 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
791 // request must not be null
792 if (null == request) {
794 throw new NullPointerException("request is null"); //NOI18N
797 // Get title, price and category id
798 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
799 String price = request.getParameter(ProductFrontend.COLUMN_PRICE);
800 String category = request.getParameter(ProductFrontend.COLUMN_CATEGORY);
801 String available = request.getParameter(ProductFrontend.COLUMN_AVAILABLE);
804 this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
806 // Variables for converting
813 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
814 } else if (title.isEmpty()) {
816 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
817 } else if (null == price) {
819 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_PRICE)); //NOI18N
820 } else if (price.isEmpty()) {
822 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_PRICE)); //NOI18N
823 } else if (null == category) {
825 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
826 } else if (category.isEmpty()) {
828 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
829 } else if (null == available) {
831 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
832 } else if (available.isEmpty()) {
834 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
835 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
837 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", ProductFrontend.COLUMN_AVAILABLE, available)); //NOI18N
842 id = Long.parseLong(category);
843 p = Float.parseFloat(price);
844 } catch (final NumberFormatException e) {
846 throw new IllegalArgumentException(e);
850 Boolean a = Boolean.parseBoolean(available);
852 // Test on product title
854 // Try to check if title is used already
855 if (this.isProductTitleUsed(title)) {
856 // Title already used
857 throw new ProductTitleAlreadyUsedException(request);
859 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
860 throw new ServletException(ex);
864 // The product is not found, so add it to database
865 this.productFrontend.addProduct(title, p, id, a);
866 } catch (final SQLException | IOException ex) {
867 // Continue to throw it
868 throw new ServletException(ex);
872 this.getLogger().trace("EXIT!"); //NOI18N
876 * Generates link HTML code for given category's parent id, if set. This
877 * link then points to products.jsp?category_id=x
879 * @param category Category instance
883 public String generateLinkForParent (final Category category) {
885 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
887 // category must not be null
888 if (null == category) {
890 throw new NullPointerException("category is null"); //NOI18N
894 Long parent = category.getParent();
898 // Product HTML code for link
899 throw new UnsupportedOperationException(MessageFormat.format("parent={0} - Unfinished!", parent)); //NOI18N
907 public String getPrintableProduktCategory (final Product product) throws ServletException {
909 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
911 // product must not be null
912 if (null == product) {
914 throw new NullPointerException("product is null"); //NOI18N
921 // Get Category instance from product over the frontend
922 category = this.categoryFrontend.getCategory(product);
923 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
924 throw new ServletException(ex);
928 this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
932 // Now get title from it and return it
933 title = category.getDecodedTitle();
934 } catch (final UnsupportedEncodingException ex) {
935 // Continue to throw as cause
936 throw new ServletException(ex);
940 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
947 * Checks if product's title is already used.
949 * @param request Request instance
950 * @return Whether the product title is already used
951 * @throws java.io.IOException If any IO error occurs
952 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
953 * @throws java.sql.SQLException If any SQL error occurs
954 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
955 * @throws java.lang.NoSuchMethodException If a method was not found
956 * @throws java.lang.IllegalAccessException If the method cannot be accessed
957 * @throws java.lang.reflect.InvocationTargetException Any other problems?
959 private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
961 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
964 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
966 // request must not be null and "title" must be found and non-empty
967 if (null == request) {
969 throw new NullPointerException("request is null"); //NOI18N
970 } else if (null == title) {
972 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
973 } else if (title.isEmpty()) {
975 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
978 // Default is not used
979 boolean isUsed = this.isProductTitleUsed(title);
982 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
989 * Handles admin form requests
990 * @param request Request instance
991 * @param response Response instance
992 * @throws ServletException If something unexpected happened
995 public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
997 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
999 // request and response must both be set
1000 if (null == request) {
1002 throw new NullPointerException("request is null"); //NOI18N
1003 } else if (null == response) {
1005 throw new NullPointerException("response is null"); //NOI18N
1008 // Try this operations
1011 if ("POST".equals(request.getMethod())) { //NOI18N
1012 // Is "add/edit/delete" set?
1013 if (request.getParameter("add") != null) { //NOI18N
1014 // Is it already added?
1015 if (this.isProductTitleUsed(request)) {
1017 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1019 // Already added, so redirect here, else a ServletException will be thrown
1020 response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
1023 this.doAdminAddProduct(request);
1025 } else if (request.getParameter("edit") != null) { //NOI18N
1027 } else if (request.getParameter("delete") != null) { //NOI18N
1031 // Redirect to proper URL
1032 // @TODO Commented out for developing:
1033 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1035 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1036 // Throw it as cause
1037 throw new ServletException(ex);
1041 this.getLogger().trace("EXIT!"); //NOI18N
1045 * Handles admin form requests
1046 * @param request Request instance
1047 * @param response Response instance
1048 * @throws ServletException If something unexpected happened
1051 public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException {
1053 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
1055 // request and response must both be set
1056 if (null == request) {
1058 throw new NullPointerException("request is null"); //NOI18N
1059 } else if (null == response) {
1061 throw new NullPointerException("response is null"); //NOI18N
1064 // Try this operations
1067 if ("POST".equals(request.getMethod())) { //NOI18N
1068 // Is "add/edit/delete" set?
1069 if (request.getParameter("add") != null) { //NOI18N
1070 // Is the category title already used?
1071 if (this.isCategoryTitleUsed(request)) {
1073 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
1075 // Already added, so redirect here, else a ServletException will be thrown
1076 response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
1079 this.doAdminAddCategory(request);
1081 } else if (request.getParameter("edit") != null) { //NOI18N
1083 } else if (request.getParameter("delete") != null) { //NOI18N
1087 // Redirect to proper URL
1088 // @TODO Commented out for developing:
1089 //response.sendRedirect(request.getContextPath() + "/finished.jsp");
1091 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
1092 // Throw it as cause
1093 throw new ServletException(ex);
1097 this.getLogger().trace("EXIT!"); //NOI18N
1101 * Checks if category's title is already used.
1103 * @param request Request instance
1104 * @return Whether the product title is already used
1105 * @throws java.io.IOException If any IO error occurs
1106 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
1107 * @throws java.sql.SQLException If any SQL error occurs
1108 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
1109 * @throws java.lang.NoSuchMethodException If a method was not found
1110 * @throws java.lang.IllegalAccessException If the method cannot be accessed
1111 * @throws java.lang.reflect.InvocationTargetException Any other problems?
1113 private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
1115 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
1118 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
1120 // request must not be null and "title" must be found and non-empty
1121 if (null == request) {
1123 throw new NullPointerException("request is null"); //NOI18N
1124 } else if (null == title) {
1126 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1127 } else if (title.isEmpty()) {
1129 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
1132 // Default is not used
1133 boolean isUsed = this.isCategoryTitleUsed(title);
1136 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
1143 public Product getProduct (final AddableBasketItem item) throws ServletException {
1145 this.getLogger().trace("item=" + item + " - CALLED!");
1147 // item should not be null
1150 throw new NullPointerException("item is null");
1151 } else if (null == this.productFrontend) {
1153 throw new NullPointerException("productFrontend is null");
1156 // Init product instance
1157 Product product = null;
1161 product = this.productFrontend.getProduct(item);
1162 } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
1163 // Continue to throw
1164 throw new ServletException(ex);
1168 this.getLogger().trace("product=" + product + " - EXIT!");