From: Roland Haeder Date: Sat, 15 Aug 2015 11:24:47 +0000 (+0200) Subject: Continued with project: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=5d01eaa7ecfc3b341eb72f8342ad2cd5a582564a;p=pizzaservice-war.git Continued with project: - added doAdminHandleCategoryForms() and doAdminHandleProductForms() - isCategoryTitleUsed() and isProductTitleUsed() are now private and continue to throw all "low-level" exceptions and never a ServletException as this is thrown by publicc access methods (which servets may invoke) Signed-off-by:Roland Häder --- diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java index c1ebe252..c7545bef 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java @@ -19,6 +19,7 @@ package org.mxchange.pizzaapplication.application; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.mxchange.jcore.application.Application; import org.mxchange.pizzaapplication.category.Category; @@ -269,15 +270,6 @@ public interface PizzaApplication extends Application { */ public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException; - /** - * Checks if product's title is already used. - * - * @param request Request instance - * @return Whether the product's title has already been used - * @throws javax.servlet.ServletException If something unexpected happened - */ - public boolean isProductTitleUsed (final HttpServletRequest request) throws ServletException; - /** * Generates link HTML code for given category's parent id, if set. This * link then points to products.jsp?category_id=x @@ -286,4 +278,22 @@ public interface PizzaApplication extends Application { * @return HTML code */ public String generateLinkForParent (final Category category); + + /** + * Handles admin product form requests + * + * @param request Request instance + * @param response Response instance + * @throws ServletException If something unexpected happened + */ + public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException; + + /** + * Handles admin category form requests + * + * @param request Request instance + * @param response Response instance + * @throws ServletException If something unexpected happened + */ + public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException; } diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index 2c389deb..c74d2592 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -24,11 +24,10 @@ import java.sql.SQLException; import java.text.MessageFormat; import java.util.Iterator; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.mxchange.jcore.contact.Gender; import org.mxchange.jcore.exceptions.BadTokenException; @@ -1022,7 +1021,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @param session Session instance * @return Amount as string */ - private String handleChooseFromRequestSession(final Product product, final HttpServletRequest request, final HttpSession session) { + private String handleChooseFromRequestSession (final Product product, final HttpServletRequest request, final HttpSession session) { // Trace message this.getLogger().trace(MessageFormat.format("product={0},request={1},session={2} - CALLED!", product, request, session)); //NOI18N @@ -1096,7 +1095,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @param title Title of category to check * @return Whether it has been found */ - private boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Delegate to frontend return this.categoryFrontend.isCategoryTitleUsed(title); } @@ -1118,7 +1117,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @param session HttpSession instance * @return */ - private boolean isProductOrdered(final Product product, final HttpSession session) { + private boolean isProductOrdered (final Product product, final HttpSession session) { // Trace message this.getLogger().trace(MessageFormat.format("product={0},session={1} - CALLED!", product, session)); //NOI18N @@ -1153,7 +1152,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P /** * Application starter */ - private void start () { + private void start() { // Init bundle this.initBundle(); @@ -1188,13 +1187,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P Customer customer = new PizzaServiceCustomer(); /* - * Need a least a gender ... :( See, that is why I don't like default - * constructors, you can easily miss something important and bam! You - * get an NPE. The fix here is, to have construtors (or factories) which - * requires all required instances that needs to be set to get a - * consitent object back. - */ - + * Need a least a gender ... :( See, that is why I don't like default + * constructors, you can easily miss something important and bam! You + * get an NPE. The fix here is, to have construtors (or factories) which + * requires all required instances that needs to be set to get a + * consitent object back. + */ + // Gender is MALE now customer.setGender(Gender.MALE); @@ -1445,8 +1444,20 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P return title; } - @Override - public boolean isProductTitleUsed (final HttpServletRequest request) throws ServletException { + /** + * Checks if product's title is already used. + * + * @param request Request instance + * @return Whether the product title is already used + * @throws java.io.IOException If any IO error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-) + * @throws java.sql.SQLException If any SQL error occurs + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? + */ + private boolean isProductTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { // Trace message this.getLogger().trace("request=" + request + " - CALLED!"); @@ -1466,16 +1477,162 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P } // Default is not used - boolean isUsed = false; + boolean isUsed = this.isProductTitleUsed(title); + + // Trace message + this.getLogger().trace("isUsed=" + isUsed + " - EXIT!"); + + // Return it + return isUsed; + } + + /** + * Handles admin form requests + * @param request Request instance + * @param response Response instance + * @throws ServletException If something unexpected happened + */ + @Override + public void doAdminHandleProductForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException { + // Trace message + this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!"); + // request and response must both be set + if (request == null) { + // request is null + throw new NullPointerException("request is null"); + } else if (response == null) { + // response is null + throw new NullPointerException("response is null"); + } + + // Try this operations try { - // So that all is tested, try to find the product - isUsed = this.isProductTitleUsed(title); - } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { - // Continue to throw all as cause + // Is it post? + if ("POST".equals(request.getMethod())) { //NOI18N + // Is "add/edit/delete" set? + if (request.getParameter("add") != null) { //NOI18N + // Is it already added? + if (this.isProductTitleUsed(request)) { + // Debug message + this.getLogger().debug("Already used, redirecting ..."); + + // Already added, so redirect here, else a ServletException will be thrown + response.sendRedirect(request.getContextPath() + "/admin/product.jsp?already=1"); + } else { + // Add new product + this.doAdminAddProduct(request); + } + } else if (request.getParameter("edit") != null) { //NOI18N + // @TODO + } else if (request.getParameter("delete") != null) { //NOI18N + // @TODO + } + + // Redirect to proper URL + // @TODO Commented out for developing: + //response.sendRedirect(request.getContextPath() + "/finished.jsp"); + } + } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | ProductTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) { + // Throw it as cause throw new ServletException(ex); } + // Trace message + this.getLogger().trace("EXIT!"); + } + + /** + * Handles admin form requests + * @param request Request instance + * @param response Response instance + * @throws ServletException If something unexpected happened + */ + @Override + public void doAdminHandleCategoryForms (final HttpServletRequest request, final HttpServletResponse response) throws ServletException { + // Trace message + this.getLogger().trace("request=" + request + ",response=" + response + " - CALLED!"); + + // request and response must both be set + if (request == null) { + // request is null + throw new NullPointerException("request is null"); + } else if (response == null) { + // response is null + throw new NullPointerException("response is null"); + } + + // Try this operations + try { + // Is it post? + if ("POST".equals(request.getMethod())) { //NOI18N + // Is "add/edit/delete" set? + if (request.getParameter("add") != null) { //NOI18N + // Is the category title already used? + if (this.isCategoryTitleUsed(request)) { + // Debug message + this.getLogger().debug("Already used, redirecting ..."); + + // Already added, so redirect here, else a ServletException will be thrown + response.sendRedirect(request.getContextPath() + "/admin/category.jsp?already=1"); + } else { + // Add new category + this.doAdminAddCategory(request); + } + } else if (request.getParameter("edit") != null) { //NOI18N + // @TODO + } else if (request.getParameter("delete") != null) { //NOI18N + // @TODO + } + + // Redirect to proper URL + // @TODO Commented out for developing: + //response.sendRedirect(request.getContextPath() + "/finished.jsp"); + } + } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | CategoryTitleAlreadyUsedException | NoSuchMethodException | IllegalAccessException | InvocationTargetException | IllegalArgumentException ex) { + // Throw it as cause + throw new ServletException(ex); + } + + // Trace message + this.getLogger().trace("EXIT!"); + } + + /** + * Checks if category's title is already used. + * + * @param request Request instance + * @return Whether the product title is already used + * @throws java.io.IOException If any IO error occurs + * @throws org.mxchange.jcore.exceptions.BadTokenException If a bad token was found in a file-based database backend's file ... ;-) + * @throws java.sql.SQLException If any SQL error occurs + * @throws org.mxchange.jcore.exceptions.CorruptedDatabaseFileException If the database file is damaged + * @throws java.lang.NoSuchMethodException If a method was not found + * @throws java.lang.IllegalAccessException If the method cannot be accessed + * @throws java.lang.reflect.InvocationTargetException Any other problems? + */ + private boolean isCategoryTitleUsed (final HttpServletRequest request) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + // Trace message + this.getLogger().trace("request=" + request + " - CALLED!"); + + // Init title + String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE); + + // request must not be null and "title" must be found and non-empty + if (request == null) { + // Abort here + throw new NullPointerException("request is null"); + } else if (title == null) { + // title is not set + throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N + } else if (title.isEmpty()) { + // Is left empty + throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); //NOI18N + } + + // Default is not used + boolean isUsed = this.isCategoryTitleUsed(title); + // Trace message this.getLogger().trace("isUsed=" + isUsed + " - EXIT!"); diff --git a/web/form_handler/admin/do_category.jsp b/web/form_handler/admin/do_category.jsp index ddd04d64..1bb64d71 100644 --- a/web/form_handler/admin/do_category.jsp +++ b/web/form_handler/admin/do_category.jsp @@ -16,27 +16,8 @@ // Init application instance PizzaApplication app = PizzaServiceApplication.getInstance(application); - // Is it post? - if ("POST".equals(request.getMethod())) { //NOI18N - // Is "add/edit/delete" set? - if (request.getParameter("add") != null) { //NOI18N - // Add new category - try { - app.doAdminAddCategory(request); - } catch (final CategoryTitleAlreadyUsedException e) { - // Already used! - response.sendRedirect(request.getContextPath() + "/admin/category.jsp?already=1"); //NOI18N - } - } else if (request.getParameter("edit") != null) { //NOI18N - // @TODO - } else if (request.getParameter("delete") != null) { //NOI18N - // @TODO - } - - // Redirect to proper URL - // @TODO Commented out for developing: - //response.sendRedirect(request.getContextPath() + "/finished.jsp"); - } + // Handle forms + app.doAdminHandleCategoryForms(request, response); %> diff --git a/web/form_handler/admin/do_product.jsp b/web/form_handler/admin/do_product.jsp index 87afa019..65fb17cf 100644 --- a/web/form_handler/admin/do_product.jsp +++ b/web/form_handler/admin/do_product.jsp @@ -15,31 +15,8 @@ // Init application instance PizzaApplication app = PizzaServiceApplication.getInstance(application); - // Is it post? - if ("POST".equals(request.getMethod())) { //NOI18N - // Is "add/edit/delete" set? - if (request.getParameter("add") != null) { //NOI18N - // Is it already added? - if (app.isProductTitleUsed(request)) { - // Debug message - app.getLogger().debug("Already used, redirecting ..."); - - // Already added, so redirect here, else a ServletException will be thrown - response.sendRedirect(request.getContextPath() + "/admin/product.jsp?already=1"); - } else { - // Add new product - app.doAdminAddProduct(request); - } - } else if (request.getParameter("edit") != null) { //NOI18N - // @TODO - } else if (request.getParameter("delete") != null) { //NOI18N - // @TODO - } - - // Redirect to proper URL - // @TODO Commented out for developing: - //response.sendRedirect(request.getContextPath() + "/finished.jsp"); - } + // Handle forms + app.doAdminHandleProductForms(request, response); %>