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.Deque;
25 import java.util.Iterator;
26 import javax.servlet.ServletContext;
27 import javax.servlet.ServletException;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.ServletResponse;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.servlet.http.HttpSession;
33 import org.mxchange.jcore.exceptions.BadTokenException;
34 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
35 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
36 import org.mxchange.jshop.category.Category;
37 import org.mxchange.jshop.database.frontend.category.CategoryDatabaseFrontend;
38 import org.mxchange.jshop.database.frontend.category.CategoryFrontend;
39 import org.mxchange.jshop.database.frontend.product.ProductDatabaseFrontend;
40 import org.mxchange.jshop.database.frontend.product.ProductFrontend;
41 import org.mxchange.jshop.exceptions.CategoryTitleAlreadyUsedException;
42 import org.mxchange.jshop.exceptions.ProductTitleAlreadyUsedException;
43 import org.mxchange.jshop.item.AddableBasketItem;
44 import org.mxchange.jshop.product.Product;
45 import org.mxchange.pizzaapplication.BasePizzaServiceSystem;
48 * Main application class
50 * @author Roland Haeder
52 public class PizzaServiceApplication extends BasePizzaServiceSystem implements PizzaApplication {
54 * Database frontend for products
56 private ProductFrontend productFrontend;
59 * Database frontend for categories
61 private CategoryFrontend categoryFrontend;
66 public PizzaServiceApplication () {
68 this.getLogger().trace("CALLED!"); //NOI18N
72 public Deque<Category> getAllCategories () throws ServletException {
74 // Deligate to frontend
75 return this.categoryFrontend.getAllCategories();
76 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
78 throw new ServletException(ex);
83 public Deque<Product> getAllProducts () throws ServletException {
85 // Deligate to frontend
86 return this.productFrontend.getAllProducts();
87 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
89 throw new ServletException(ex);
94 public Deque<Product> getAvailableProducts () throws ServletException {
96 // Deligate to frontend
97 return this.productFrontend.getAllAvailableProducts();
98 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
100 throw new ServletException(ex);
105 public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException {
107 this.getLogger().trace(MessageFormat.format("context={0} - CALLED!", context)); //NOI18N
109 // context should not be null
110 if (null == context) {
112 throw new NullPointerException("context is null"); //NOI18N
115 // Is the bundle initialized?
116 if (!this.isBundledInitialized()) {
117 // Temporary initialize default bundle
118 // TODO The enum Gender uses this
122 // Initialize properties from context
123 this.initProperties(context);
125 // Init database frontends
126 this.initDatabaseFrontends();
129 this.getLogger().trace("EXIT!"); //NOI18N
133 * Calculates total amount of all choosen products
135 * @param request Request instance
136 * @param session Session instance
137 * @return Total amount of all choosen products
138 * @deprecated Old lost
141 private int calculateTotalAmount (final ServletRequest request, final HttpSession session) throws ServletException {
143 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
145 // Is product and session set?
146 if (null == request) {
148 throw new NullPointerException("request is null"); //NOI18N
149 } else if (null == session) {
151 throw new NullPointerException("session is null"); //NOI18N
154 // Init/declare total price and iterator
156 Iterator<Product> iterator = this.getAvailableProductsIterator();
158 // "Walk" over all products
159 while (iterator.hasNext()) {
161 Product product = iterator.next();
164 if (this.isProductChoosen(product, request, session)) {
165 // Then add ordered amount
166 this.getLogger().debug(MessageFormat.format("Counting {0} ...", product.getItemId())); //NOI18N
169 String amount = this.getAmountFromSession(product, session);
172 this.getLogger().debug(MessageFormat.format("amount={0}", amount)); //NOI18N
173 totalAmount += Integer.valueOf(amount);
175 this.getLogger().debug(MessageFormat.format("product={0},totalAmount={1}", product.getItemId(), totalAmount)); //NOI18N
179 this.getLogger().trace(MessageFormat.format("totalAmount={0} - EXIT!", totalAmount)); //NOI18N
181 // Return total price
186 public void doBootstrap () {
187 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
191 public void doMainLoop () {
192 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
196 public void doShutdown () {
197 throw new UnsupportedOperationException("Not supported yet."); //NOI18N
201 * Some "getter" for amount from session
203 * @param product Product instance
204 * @param session Session instance
205 * @return Amount as string
206 * @deprecated Old lost code
209 private String getAmountFromSession (final Product product, final HttpSession session) {
211 this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N
213 // Is product and session set?
214 if (null == product) {
216 throw new NullPointerException("product is null"); //NOI18N
217 } else if (null == session) {
219 throw new NullPointerException("session is null"); //NOI18N
223 Object object = this.getValueFromSession(product, session, HTTP_PARAM_AMOUNT);
225 // Is the object null?
226 if (null == object) {
228 this.getLogger().trace("Returning 0 - EXIT!"); //NOI18N
235 this.getLogger().trace(MessageFormat.format("object={0} - EXIT!", object)); //NOI18N
237 // Cast to string and return it
238 return (String) object;
242 public String getCheckedHtmlFromProduct (final Product product, final ServletRequest request, final HttpSession session) {
244 this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N
246 // Is product and session set?
247 if (null == product) {
249 throw new NullPointerException("product is null"); //NOI18N
250 } else if (null == request) {
252 throw new NullPointerException("request is null"); //NOI18N
253 } else if (null == session) {
255 throw new NullPointerException("session is null"); //NOI18N
258 // First let's check if the product is choosen
259 if (this.isProductChoosen(product, request, session)) {
261 this.getLogger().trace("Returning checked=\"checked\" - EXIT!"); //NOI18N
264 return "checked=\"checked\""; //NOI18N
267 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
275 public String getDisabledHtmlFromSession (final ServletRequest request, final HttpSession session) throws ServletException {
277 this.getLogger().trace(MessageFormat.format("request={0},session={1} - CALLED!", request, session)); //NOI18N
279 // Is product and session set?
280 if (null == request) {
282 throw new NullPointerException("request is null"); //NOI18N
283 } else if (null == session) {
285 throw new NullPointerException("session is null"); //NOI18N
288 // Get "enabled" from request scope
289 Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
292 this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
294 // Is something selected?
295 if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
297 this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
299 // Something has been choosen
303 this.getLogger().trace("Returning disabled=\"disabled\" - EXIT!"); //NOI18N
305 // Nothing choosen yet
306 return "disabled=\"disabled\""; //NOI18N
311 public String getPrintableProduktAvailability (final Product product) {
313 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
316 if (null == product) {
317 // Should not be null
318 throw new NullPointerException("product is null"); //NOI18N
322 if (product.getAvailable() == true) {
326 // Not, not for public
332 @SuppressWarnings ("unchecked")
333 public Iterator<Product> getAvailableProductsIterator () throws ServletException {
334 // categoryFrontend must be set
335 if (null == this.productFrontend) {
337 throw new NullPointerException("productFrontend is null"); //NOI18N
341 // Ask frontend for a list of products
342 return (Iterator<Product>) this.productFrontend.getAvailableProductsIterator();
343 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
344 throw new ServletException(ex);
349 @SuppressWarnings ("unchecked")
350 public Iterator<Product> getAllProductsIterator () throws ServletException {
352 this.getLogger().trace("CALLED!"); //NOI18N
354 // categoryFrontend must be set
355 if (null == this.productFrontend) {
357 throw new NullPointerException("productFrontend is null"); //NOI18N
361 // Ask frontend for a list of products
362 return (Iterator<Product>) this.productFrontend.getAllProductsIterator();
363 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
364 throw new ServletException(ex);
369 @SuppressWarnings ("unchecked")
370 public Iterator<Category> getAllCategoriesIterator () throws ServletException {
372 this.getLogger().trace("CALLED!"); //NOI18N
374 // categoryFrontend must be set
375 if (null == this.categoryFrontend) {
377 throw new NullPointerException("categoryFrontend is null"); //NOI18N
381 // Ask frontend for a list of categories
382 return (Iterator<Category>) this.categoryFrontend.getAllCategoriesIterator();
383 } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
384 throw new ServletException(ex);
389 private boolean isProductChoosen (final Product product, final ServletRequest request, final HttpSession session) {
390 throw new UnsupportedOperationException("This method is deprecated and shall not be called"); //NOI18N
394 public void setValueInSession (final HttpSession session, final String key, final Object value) {
396 this.getLogger().trace(MessageFormat.format("session={0},key={1},value={2} - CALLED!", session, key, value)); //NOI18N
398 synchronized(session) {
400 session.setAttribute(key, value);
404 this.getLogger().trace("EXIT!"); //NOI18N
408 * Some getter for value from session
410 * @param product Product instance
411 * @param session Session instance
412 * @param attribute Attribute to get value from
413 * @return Value from session
414 * @deprecated Old lost code
417 private Object getValueFromSession (final Product product, final HttpSession session, final String attribute) {
419 this.getLogger().trace(MessageFormat.format("product={0},session={1},attribute={2} - CALLED!", product, session, attribute)); //NOI18N
422 Object value = this.getValueFromSession(session, String.format(HTTP_PARAM_MASK, attribute, product.getItemId()));
424 this.getLogger().debug(MessageFormat.format("product={0},attribute={1},value={2}", product.getItemId(), attribute, value)); //NOI18N
427 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
434 * Some getter for value from session
436 * @param session Session instance
437 * @param key Key to get value from
438 * @return Value from session
440 private Object getValueFromSession (final HttpSession session, final String key) {
442 this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
447 // Get it synchronized from session
448 synchronized (session) {
449 value = session.getAttribute(key);
453 this.getLogger().trace(MessageFormat.format("value={0} - EXIT!", value)); //NOI18N
460 * Initializes database frontends.
462 private void initDatabaseFrontends () throws UnsupportedDatabaseBackendException, SQLException {
464 this.getLogger().trace("CALLED!"); //NOI18N
467 this.productFrontend = new ProductDatabaseFrontend();
470 this.categoryFrontend = new CategoryDatabaseFrontend();
473 this.getLogger().trace("EXIT!"); //NOI18N
477 * Checks whether given category title is already used
479 * @param title Title of category to check
480 * @return Whether it has been found
482 private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
483 // categoryFrontend must be set
484 if (null == this.categoryFrontend) {
486 throw new NullPointerException("categoryFrontend is null"); //NOI18N
489 // Delegate to frontend
490 return this.categoryFrontend.isCategoryTitleUsed(title);
494 * Checks if given product title is already used
495 * @param title Product title to check
496 * @return Whether the product title has already been used
498 private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
499 // categoryFrontend must be set
500 if (null == this.productFrontend) {
502 throw new NullPointerException("productFrontend is null"); //NOI18N
505 // Delegate to frontend
506 return this.productFrontend.isProductTitleUsed(title);
510 public void doAdminAddCategory (final ServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException {
512 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
514 // request must not be null
515 if (null == request) {
517 throw new NullPointerException("request is null"); //NOI18N
521 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
522 String parentId = request.getParameter(CategoryFrontend.COLUMN_PARENT);
525 this.getLogger().debug(MessageFormat.format("title={0},parentId={1}", title, parentId)); //NOI18N
527 // Init variables for casting
533 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
534 } else if (title.isEmpty()) {
536 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
537 } else if ((parentId != null) && (!parentId.isEmpty())) {
538 // "parent" is set, so check it
540 id = Integer.parseInt(parentId);
541 } catch (final NumberFormatException e) {
543 throw new IllegalArgumentException(e);
548 // Try to check if title is used already
549 if (this.isCategoryTitleUsed(title)) {
550 // Title already used
551 throw new CategoryTitleAlreadyUsedException(request);
553 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
554 throw new ServletException(ex);
558 // The category is not found, so add it to database
559 this.categoryFrontend.addCategory(title, id);
560 } catch (final SQLException | IOException ex) {
561 // Continue to throw it
562 throw new ServletException(ex);
566 this.getLogger().trace("EXIT!"); //NOI18N
570 public void doAdminAddProduct (final ServletRequest request) throws ServletException, ProductTitleAlreadyUsedException {
572 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
574 // request must not be null
575 if (null == request) {
577 throw new NullPointerException("request is null"); //NOI18N
580 // Get title, price and category id
581 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
582 String price = request.getParameter(ProductFrontend.COLUMN_PRICE);
583 String categoryId = request.getParameter(ProductFrontend.COLUMN_CATEGORY);
584 String available = request.getParameter(ProductFrontend.COLUMN_AVAILABLE);
587 this.getLogger().debug(MessageFormat.format("title={0},price={1},categoryId={2},available={3}", title, price, categoryId, available)); //NOI18N
589 // Variables for converting
596 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
597 } else if (title.isEmpty()) {
599 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
600 } else if (null == price) {
602 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_PRICE)); //NOI18N
603 } else if (price.isEmpty()) {
605 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_PRICE)); //NOI18N
606 } else if (null == categoryId) {
608 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
609 } else if (categoryId.isEmpty()) {
611 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_CATEGORY)); //NOI18N
612 } else if (null == available) {
614 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
615 } else if (available.isEmpty()) {
617 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_AVAILABLE)); //NOI18N
618 } else if ((!"true".equals(available)) && (!"false".equals(available))) { //NOI18N
620 throw new IllegalArgumentException(MessageFormat.format("{0} is invalid: {1}", ProductFrontend.COLUMN_AVAILABLE, available)); //NOI18N
625 id = Long.parseLong(categoryId);
626 p = Float.parseFloat(price);
627 } catch (final NumberFormatException e) {
629 throw new IllegalArgumentException(e);
633 Boolean a = Boolean.parseBoolean(available);
635 // Test on product title
637 // Try to check if title is used already
638 if (this.isProductTitleUsed(title)) {
639 // Title already used
640 throw new ProductTitleAlreadyUsedException(request);
642 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
643 throw new ServletException(ex);
647 // The product is not found, so add it to database
648 this.productFrontend.addProduct(title, p, id, a);
649 } catch (final SQLException | IOException ex) {
650 // Continue to throw it
651 throw new ServletException(ex);
655 this.getLogger().trace("EXIT!"); //NOI18N
659 public String generateLinkForParent (final Category category) {
661 this.getLogger().trace(MessageFormat.format("category={0} - CALLED!", category)); //NOI18N
663 // category must not be null
664 if (null == category) {
666 throw new NullPointerException("category is null"); //NOI18N
670 Long parentId = category.getParentId();
674 // Product HTML code for link
675 throw new UnsupportedOperationException(MessageFormat.format("parentId={0} - Unfinished!", parentId)); //NOI18N
683 public String getPrintableProduktCategory (final Product product) throws ServletException {
685 this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
687 // product must not be null
688 if (null == product) {
690 throw new NullPointerException("product is null"); //NOI18N
697 // Get Category instance from product over the frontend
698 category = this.categoryFrontend.getCategory(product);
699 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
700 throw new ServletException(ex);
704 this.getLogger().debug(MessageFormat.format("categoryId={0}", category)); //NOI18N
708 // Now get title from it and return it
709 title = category.getDecodedTitle();
710 } catch (final UnsupportedEncodingException ex) {
711 // Continue to throw as cause
712 throw new ServletException(ex);
716 this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
723 * Checks if product's title is already used.
725 * @param request Request instance
726 * @return Whether the product title is already used
727 * @throws java.io.IOException If any IO error occurs
728 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
729 * @throws java.sql.SQLException If any SQL error occurs
730 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
731 * @throws java.lang.NoSuchMethodException If a method was not found
732 * @throws java.lang.IllegalAccessException If the method cannot be accessed
733 * @throws java.lang.reflect.InvocationTargetException Any other problems?
735 private boolean isProductTitleUsed (final ServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
737 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
740 String title = request.getParameter(ProductFrontend.COLUMN_TITLE);
742 // request must not be null and "title" must be found and non-empty
743 if (null == request) {
745 throw new NullPointerException("request is null"); //NOI18N
746 } else if (null == title) {
748 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", ProductFrontend.COLUMN_TITLE)); //NOI18N
749 } else if (title.isEmpty()) {
751 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", ProductFrontend.COLUMN_TITLE)); //NOI18N
754 // Default is not used
755 boolean isUsed = this.isProductTitleUsed(title);
758 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
765 public void doAdminHandleProductForms (final ServletRequest request, final ServletResponse response) throws ServletException {
767 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
769 // request and response must both be set
770 if (null == request) {
772 throw new NullPointerException("request is null"); //NOI18N
773 } else if (null == response) {
775 throw new NullPointerException("response is null"); //NOI18N
778 // Try this operations
781 if ("POST".equals(((HttpServletRequest) request).getMethod())) { //NOI18N
782 // Is "add/edit/delete" set?
783 if (request.getParameter("add") != null) { //NOI18N
784 // Is it already added?
785 if (this.isProductTitleUsed(request)) {
787 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
789 // Already added, so redirect here, else a ServletException will be thrown
790 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/product.jsp?already=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
793 this.doAdminAddProduct(request);
795 // Redirect to proper URL
796 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/product.jsp?added=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
798 } else if (request.getParameter("edit") != null) { //NOI18N
800 } else if (request.getParameter("delete") != null) { //NOI18N
804 // Request not handled
805 throw new ServletException("request not handled."); //NOI18N
807 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
809 throw new ServletException(ex);
813 this.getLogger().trace("EXIT!"); //NOI18N
817 public void doAdminHandleCategoryForms (final ServletRequest request, final ServletResponse response) throws ServletException {
819 this.getLogger().trace(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
821 // request and response must both be set
822 if (null == request) {
824 throw new NullPointerException("request is null"); //NOI18N
825 } else if (null == response) {
827 throw new NullPointerException("response is null"); //NOI18N
830 // Try this operations
833 if ("POST".equals(((HttpServletRequest) request).getMethod())) { //NOI18N
834 // Is "add/edit/delete" set?
835 if (request.getParameter("add") != null) { //NOI18N
836 // Is the category title already used?
837 if (this.isCategoryTitleUsed(request)) {
839 this.getLogger().debug("Already used, redirecting ..."); //NOI18N
841 // Already added, so redirect here, else a ServletException will be thrown
842 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/category.jsp?already=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
845 this.doAdminAddCategory(request);
847 // Redirect to proper URL
848 ((HttpServletResponse) response).sendRedirect(String.format("%s/admin/category.jsp?added=1", ((HttpServletRequest) request).getContextPath())); //NOI18N
850 } else if (request.getParameter("edit") != null) { //NOI18N
852 } else if (request.getParameter("delete") != null) { //NOI18N
856 // Request not handled
857 throw new ServletException("request not handled."); //NOI18N
859 } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) {
861 throw new ServletException(ex);
865 this.getLogger().trace("EXIT!"); //NOI18N
869 * Checks if category's title is already used.
871 * @param request Request instance
872 * @return Whether the product title is already used
873 * @throws java.io.IOException If any IO error occurs
874 * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-)
875 * @throws java.sql.SQLException If any SQL error occurs
876 * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged
877 * @throws java.lang.NoSuchMethodException If a method was not found
878 * @throws java.lang.IllegalAccessException If the method cannot be accessed
879 * @throws java.lang.reflect.InvocationTargetException Any other problems?
881 private boolean isCategoryTitleUsed (final ServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
883 this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
886 String title = request.getParameter(CategoryFrontend.COLUMN_TITLE);
888 // request must not be null and "title" must be found and non-empty
889 if (null == request) {
891 throw new NullPointerException("request is null"); //NOI18N
892 } else if (null == title) {
894 throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
895 } else if (title.isEmpty()) {
897 throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", CategoryFrontend.COLUMN_TITLE)); //NOI18N
900 // Default is not used
901 boolean isUsed = this.isCategoryTitleUsed(title);
904 this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
911 public Product getProduct (final AddableBasketItem item) throws ServletException {
913 this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
915 // item should not be null
918 throw new NullPointerException("item is null"); //NOI18N
919 } else if (null == this.productFrontend) {
921 throw new NullPointerException("productFrontend is null"); //NOI18N
924 // Init product instance
925 Product product = null;
929 product = this.productFrontend.getProduct(item);
930 } catch (final SQLException | IOException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
932 throw new ServletException(ex);
936 this.getLogger().trace(MessageFormat.format("product={0} - EXIT!", product)); //NOI18N //NOI18N