From d29bcbc50afc0a1884985fa8b6e6c037c8f47e41 Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 14 Aug 2015 08:14:47 +0200 Subject: [PATCH] Continued with project: - Added method isCategoryUsed() and implemented it rudely in frontend, too. - doAdminAddCategory() does now check request parameter if they exist and are not empty MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by:Roland Häder --- .../application/PizzaApplication.java | 6 ++- .../application/PizzaServiceApplication.java | 53 ++++++++++++++++++- .../frontend/category/CategoryFrontend.java | 11 ++++ .../PizzaCategoryDatabaseFrontend.java | 37 +++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java index 80983bf1..d9c953c5 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java @@ -236,13 +236,15 @@ public interface PizzaApplication extends Application { * Adds given category data from request to database * * @param request Request instance + * @throws javax.servlet.ServletException If something unexpected happened */ - public void doAdminAddCategory (final HttpServletRequest request); + public void doAdminAddCategory (final HttpServletRequest request) throws ServletException; /** * Adds given product data from request to database * * @param request Request instance + * @throws javax.servlet.ServletException If something unexpected happened */ - public void doAdminAddProduct (final HttpServletRequest request); + public void doAdminAddProduct (final HttpServletRequest request) throws ServletException; } diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index 550d3f94..2de42a69 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -34,6 +34,7 @@ import org.mxchange.pizzaapplication.BasePizzaServiceSystem; import org.mxchange.pizzaapplication.category.Category; import org.mxchange.pizzaapplication.customer.Customer; import org.mxchange.pizzaapplication.customer.PizzaServiceCustomer; +import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants; import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend; import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend; import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend; @@ -1051,6 +1052,16 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P this.categoryFrontend = new PizzaCategoryDatabaseFrontend(); } + /** + * Checks whether given category title is already used + * @param title Title of category to check + * @return Whether it has been found + */ + private boolean isCategoryUsed (final String title) throws IOException, SQLException, BadTokenException { + // Delegate to frontend + return this.categoryFrontend.isCategoryTitleUsed(title); + } + /** * Checks if the product ordered? * @@ -1161,10 +1172,43 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @param request Request instance */ @Override - public void doAdminAddCategory (final HttpServletRequest request) { + public void doAdminAddCategory (final HttpServletRequest request) throws ServletException { // Trace message this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N + // request must not be null + if (request == null) { + // Is null + throw new NullPointerException("request is null"); + } + + // Check if all fields are given + if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE) == null) { + // "title" not set + throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); + } else if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE).isEmpty()) { + // Is left empty + throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaCategoryDatabaseConstants.COLUMN_TITLE)); + } else if ((request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT) != null) && (!request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT).isEmpty())) { + // "parent" is set, so check it + try { + Integer dummy = Integer.parseInt(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT)); + } catch (final NumberFormatException e) { + // Not valid number + throw new IllegalArgumentException(e); + } + } else { + try { + // Try to check if title is used already + if (this.isCategoryUsed(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE))) { + // Title already used + throw new IllegalArgumentException(MessageFormat.format("Title {0} is already used.", request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE))); + } + } catch (final IOException | SQLException | BadTokenException ex) { + throw new ServletException(ex); + } + } + // Trace message this.getLogger().trace("EXIT!"); //NOI18N } @@ -1179,6 +1223,13 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P // Trace message this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N + // request must not be null + if (request == null) { + // Is null + throw new NullPointerException("request is null"); + } + + // Check if all fields are given // Trace message this.getLogger().trace("EXIT!"); //NOI18N } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java index 831e9aec..e32c7bc9 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java @@ -39,4 +39,15 @@ public interface CategoryFrontend extends DatabaseFrontend { * @throws java.sql.SQLException If any SQL error occurs */ public Iterator getCategories () throws IOException, BadTokenException, SQLException; + + /** + * Checks if given category title is already used + * + * @param title Title to check + * @return Whether the title has been 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 + */ + public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException; } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java index 65b3612f..36ecbbe8 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -30,6 +30,7 @@ import org.mxchange.jcore.database.storage.Storeable; import org.mxchange.jcore.exceptions.BadTokenException; import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException; import org.mxchange.pizzaapplication.category.Category; +import org.mxchange.pizzaapplication.database.category.PizzaCategoryDatabaseConstants; /** * Stores and retrieves Contact instances @@ -164,4 +165,40 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen private Category parseLineToCategory (final String line) { throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N } + + /** + * Checks if given category title is already used + * + * @param title Title to check + * @return Whether the title has been used + */ + @Override + public boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException { + // Trace message + this.getLogger().trace(MessageFormat.format("title={0} - CALLED!", title)); + + // Get search criteria + SearchableCritera criteria = new SearchCriteria(); + + // Add criteria + criteria.addCriteria(PizzaCategoryDatabaseConstants.COLUMN_TITLE, title); + + // Only one entry is find + criteria.setLimit(1); + + // Run it on backend + Result result = this.getBackend().doSelectByCriteria(criteria); + + // Debug log + this.getLogger().debug(MessageFormat.format("result({0}={1}", result, result.size())); + + // Now check size of the result + boolean isFound = (result.size() == 1); + + // Trace message + this.getLogger().trace(MessageFormat.format("isFound={0} - EXIT!", isFound)); + + // Return it + return isFound; + } } -- 2.39.5