From 55a79f8336b44a108d217e3394d78ee16adb6e5d Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Fri, 14 Aug 2015 14:46:08 +0200 Subject: [PATCH] =?utf8?q?Continued=20with=20project:=20-=20Used=20"catego?= =?utf8?q?ry"=20instead=20of=20"id"=20for=20category=20selection=20box=20-?= =?utf8?q?=20Added=20method=20addProduct()=20-=20Added=20method=20isProduc?= =?utf8?q?tTitleUsed()=20-=20Added=20new=20exception=20ProductTitleAlready?= =?utf8?q?UsedException=20-=20Changed=20parameter=20type=20of=20"parent"?= =?utf8?q?=20to=20Integer=20as=20it=20can=20be=20done=20(better=20type-hin?= =?utf8?q?t)=20-=20Rewrote=20addCategory()=20a=20bit=20for=20this=20-=20Ad?= =?utf8?q?ded=20database=20columns=20"category",=20"price"=20and=20"title"?= =?utf8?q?=20for=20products=20-=20Renamed=20method=20isCategoryUsed()=20to?= =?utf8?q?=20isCategoryTitleUsed()=20-=20Deleted=20do=5Fproducts.jsp=20Sig?= =?utf8?q?ned-off-by:Roland=20H=C3=A4der=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../application/PizzaApplication.java | 8 +- .../application/PizzaServiceApplication.java | 112 ++++++++++++++---- .../frontend/category/CategoryFrontend.java | 2 +- .../PizzaCategoryDatabaseFrontend.java | 2 +- .../product/PizzaProductDatabaseFrontend.java | 81 +++++++++++++ .../frontend/product/ProductFrontend.java | 20 ++++ .../PizzaProductDatabaseConstants.java | 15 +++ .../ProductTitleAlreadyUsedException.java | 40 +++++++ web/admin/product.jsp | 7 +- web/form_handler/admin/do_products.jsp | 53 --------- web/static/admin/category_selection_box.jsp | 2 +- 11 files changed, 258 insertions(+), 84 deletions(-) create mode 100644 src/java/org/mxchange/pizzaapplication/exceptions/ProductTitleAlreadyUsedException.java delete mode 100644 web/form_handler/admin/do_products.jsp diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java index be6e2a64..eac4882f 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java @@ -16,13 +16,14 @@ */ package org.mxchange.pizzaapplication.application; -import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException; import java.util.Iterator; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.mxchange.jcore.application.Application; import org.mxchange.pizzaapplication.category.Category; +import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException; +import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException; import org.mxchange.pizzaapplication.product.Product; /** @@ -238,7 +239,7 @@ public interface PizzaApplication extends Application { * * @param request Request instance * @throws javax.servlet.ServletException If something unexpected happened - * @throws org.mxchange.pizzaapplication.application.CategoryTitleAlreadyUsedException If the given title is already used + * @throws org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException If the given title is already used */ public void doAdminAddCategory (final HttpServletRequest request) throws ServletException, CategoryTitleAlreadyUsedException; @@ -247,8 +248,9 @@ public interface PizzaApplication extends Application { * * @param request Request instance * @throws javax.servlet.ServletException If something unexpected happened + * @throws org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException If the given product title is already used */ - public void doAdminAddProduct (final HttpServletRequest request) throws ServletException; + public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException; /** * Generates link HTML code for given category's parent id, if set. This diff --git a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java index c82f914b..914711b9 100644 --- a/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java +++ b/src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java @@ -40,6 +40,8 @@ import org.mxchange.pizzaapplication.database.frontend.category.CategoryFrontend import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDatabaseFrontend; import org.mxchange.pizzaapplication.database.frontend.product.PizzaProductDatabaseFrontend; import org.mxchange.pizzaapplication.database.frontend.product.ProductFrontend; +import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants; +import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException; import org.mxchange.pizzaapplication.product.Product; /** @@ -1055,14 +1057,25 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P /** * 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 { + private boolean isCategoryTitleUsed (final String title) throws IOException, SQLException, BadTokenException { // Delegate to frontend return this.categoryFrontend.isCategoryTitleUsed(title); } + /** + * Checks if given product title is already used + * @param title Product title to check + * @return Whether the product title has already been used + */ + private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException { + // Delegate to frontend + return this.productFrontend.isProductTitleUsed(title); + } + /** * Checks if the product ordered? * @@ -1183,40 +1196,40 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P throw new NullPointerException("request is null"); } - // Check if all fields are given - if (request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE) == null) { + // Get all fields + String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE); + String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT); + Integer id = null; + + // Check all fields + if (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()) { + } else if (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())) { + } else if ((parent != null) && (!parent.isEmpty())) { // "parent" is set, so check it try { - Integer dummy = Integer.parseInt(request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT)); + id = Integer.parseInt(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 CategoryTitleAlreadyUsedException(request); - } - } catch (final IOException | SQLException | BadTokenException ex) { - throw new ServletException(ex); + } + try { + // Try to check if title is used already + if (this.isCategoryTitleUsed(title)) { + // Title already used + throw new CategoryTitleAlreadyUsedException(request); } + } catch (final IOException | SQLException | BadTokenException ex) { + throw new ServletException(ex); } - // Get all data from it - String title = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_TITLE); - String parent = request.getParameter(PizzaCategoryDatabaseConstants.COLUMN_PARENT); - try { // The category is not found, so add it to database - this.categoryFrontend.addCategory(title, parent); + this.categoryFrontend.addCategory(title, id); } catch (final SQLException ex) { // Continue to throw it throw new ServletException(ex); @@ -1232,7 +1245,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P * @param request Request instance */ @Override - public void doAdminAddProduct (final HttpServletRequest request) { + public void doAdminAddProduct (final HttpServletRequest request) throws ServletException, ProductTitleAlreadyUsedException { // Trace message this.getLogger().trace(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N @@ -1242,7 +1255,62 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P throw new NullPointerException("request is null"); } - // Check if all fields are given + // Get title, price and category id + String title = request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE); + String price = request.getParameter(PizzaProductDatabaseConstants.COLUMN_PRICE); + String category = request.getParameter(PizzaProductDatabaseConstants.COLUMN_CATEGORY); + Integer id = null; + Float p = null; + + // Check all fields + if (title == null) { + // "title" not set + throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); + } else if (title.isEmpty()) { + // Is left empty + throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); + } else if (price == null) { + // "price" not set + throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_PRICE)); + } else if (price.isEmpty()) { + // Is left empty + throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_PRICE)); + } else if (category == null) { + // "title" not set + throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); + } else if (category.isEmpty()) { + // Is left empty + throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_CATEGORY)); + } else { + // "parent" is set, so check it + try { + id = Integer.parseInt(category); + p = Float.parseFloat(price); + } catch (final NumberFormatException e) { + // Not valid number + throw new IllegalArgumentException(e); + } + } + + // Test on product title + try { + // Try to check if title is used already + if (this.isProductTitleUsed(title)) { + // Title already used + throw new ProductTitleAlreadyUsedException(request); + } + } catch (final IOException | SQLException | BadTokenException ex) { + throw new ServletException(ex); + } + + try { + // The product is not found, so add it to database + this.productFrontend.addProduct(title, p, id); + } catch (final SQLException ex) { + // Continue to throw it + throw new ServletException(ex); + } + // 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 b638db51..6302554d 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java @@ -37,7 +37,7 @@ public interface CategoryFrontend extends DatabaseFrontend { * @param parent Parent id or null if not selected * @throws java.sql.SQLException If any SQL error occurs */ - public void addCategory (final String title, final String parent) throws SQLException; + public void addCategory (final String title, final Integer parent) throws SQLException; /** * An iterator on all categories 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 74b7a802..d491d031 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java @@ -62,7 +62,7 @@ public class PizzaCategoryDatabaseFrontend extends BaseDatabaseFrontend implemen * @param parent Parent id or null if not selected */ @Override - public void addCategory (final String title, final String parent) throws SQLException { + public void addCategory (final String title, final Integer parent) throws SQLException { // Trace message this.getLogger().trace(MessageFormat.format("title={0},parent={1} - CALLED!", title, parent)); diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java index 7076a749..633e44a4 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java @@ -167,6 +167,45 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement return result; } + /** + * Checks wether the given product title is already used. + * + * @param title Product title + * @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 errors occur + */ + @Override + public boolean isProductTitleUsed (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(PizzaProductDatabaseConstants.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; + } + /** * Parses given line from database backend into a Storeable instance. Please * note that not all backends need this. @@ -198,4 +237,46 @@ public class PizzaProductDatabaseFrontend extends BaseDatabaseFrontend implement private Product parseLineToProduct (final String line) { throw new UnsupportedOperationException(MessageFormat.format("Not supported yet: line={0}", line)); //NOI18N } + + /** + * Adds product to database by given title, price and category id + * @param title Product title + * @param price Product price + * @param category Product category id + * @throws java.sql.SQLException If any SQL errors occur + */ + @Override + public void addProduct (final String title, final Float price, final Integer category) throws SQLException { + // Trace message + this.getLogger().trace(MessageFormat.format("title={0},price={1},category={2} - CALLED!", title, price, category)); + + // Title should not be null + if (title == null) { + // Abort here + throw new NullPointerException("title is null"); + } else if (price == null) { + // Abort here + throw new NullPointerException("price is null"); + } else if (category == null) { + // Abort here + throw new NullPointerException("category is null"); + } + + // Clear dataset from previous usage + this.clearDataSet(); + + // Add title and parent + this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_TITLE, title); + this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_PRICE, price); + this.addToDataSet(PizzaProductDatabaseConstants.COLUMN_CATEGORY, category); + + // Handle this over to the backend + Result result = this.doInsertDataSet(); + + // Debug message + this.getLogger().debug(MessageFormat.format("result={0}", result)); + + // Trace message + this.getLogger().trace("EXIT!"); + } } diff --git a/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java index 0ceadaa6..4363aac3 100644 --- a/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java +++ b/src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java @@ -30,6 +30,15 @@ import org.mxchange.pizzaapplication.product.Product; */ public interface ProductFrontend extends DatabaseFrontend { + /** + * Adds product to database by given title, price and category id + * @param title Product title + * @param price Product price + * @param category Product category id + * @throws java.sql.SQLException If any SQL errors occur + */ + public void addProduct (final String title, final Float price, final Integer category) throws SQLException; + /** * An iterator on all products * @@ -39,4 +48,15 @@ public interface ProductFrontend extends DatabaseFrontend { * @throws java.sql.SQLException If any SQL errors occur */ public Iterator getProducts () throws IOException, BadTokenException, SQLException; + + /** + * Checks wether the given product title is already used. + * + * @param title Product title + * @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 errors occur + */ + public boolean isProductTitleUsed (String title) throws IOException, SQLException, BadTokenException; } diff --git a/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java b/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java index 719dbf04..8d3646f7 100644 --- a/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java +++ b/src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java @@ -27,6 +27,21 @@ public final class PizzaProductDatabaseConstants { */ public static final String COLUMN_AVAILABLE = "available"; + /** + * Column name for "category" + */ + public static final String COLUMN_CATEGORY = "category"; + + /** + * Column name for "price" + */ + public static final String COLUMN_PRICE = "price"; + + /** + * Column name for "title" + */ + public static final String COLUMN_TITLE = "title"; + /** * No instance from this class */ diff --git a/src/java/org/mxchange/pizzaapplication/exceptions/ProductTitleAlreadyUsedException.java b/src/java/org/mxchange/pizzaapplication/exceptions/ProductTitleAlreadyUsedException.java new file mode 100644 index 00000000..e6509248 --- /dev/null +++ b/src/java/org/mxchange/pizzaapplication/exceptions/ProductTitleAlreadyUsedException.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2015 Roland Haeder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.pizzaapplication.exceptions; + +import java.text.MessageFormat; +import javax.servlet.http.HttpServletRequest; +import org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants; + +/** + * An exception thrown when the given title is already used + * + * @author Roland Haeder + */ +public class ProductTitleAlreadyUsedException extends Exception { + + /** + * Constructor with HttpServletRequest instance + * + * @param request A HttpServletRequest instance + */ + public ProductTitleAlreadyUsedException (final HttpServletRequest request) { + // Call super constructor + super(MessageFormat.format("Title {0} is already used.", request.getParameter(PizzaProductDatabaseConstants.COLUMN_TITLE))); + } + +} diff --git a/web/admin/product.jsp b/web/admin/product.jsp index 4383f963..4fa59cbc 100644 --- a/web/admin/product.jsp +++ b/web/admin/product.jsp @@ -6,13 +6,14 @@ <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%--<%@page errorPage="errorHandler.jsp" %>--%> +<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@page import="java.util.Iterator"%> <%@page import="java.util.Map"%> <%@page import="org.mxchange.jcore.contact.Gender"%> <%@page import="org.mxchange.pizzaapplication.product.Product"%> <%@page import="org.mxchange.pizzaapplication.application.PizzaApplication"%> <%@page import="org.mxchange.pizzaapplication.application.PizzaServiceApplication"%> -<%@page contentType="text/html" pageEncoding="UTF-8"%> +<%@page import="org.mxchange.pizzaapplication.database.product.PizzaProductDatabaseConstants"%> <% // Init application instance @@ -105,7 +106,7 @@
- +
@@ -118,7 +119,7 @@
- +
diff --git a/web/form_handler/admin/do_products.jsp b/web/form_handler/admin/do_products.jsp deleted file mode 100644 index fe2de2f3..00000000 --- a/web/form_handler/admin/do_products.jsp +++ /dev/null @@ -1,53 +0,0 @@ -<%-- - Document : order - Created on : 07.08.2015, 14:58:21 - Author : Roland Haeder ---%> - -<%--<%@page errorPage="errorHandler.jsp" %>--%> -<%@page contentType="text/html" pageEncoding="UTF-8"%> -<%@page import="org.mxchange.pizzaapplication.application.PizzaServiceApplication"%> -<%@page import="org.mxchange.pizzaapplication.application.PizzaApplication"%> -<%@page import="org.mxchange.pizzaapplication.beans.CustomerBean" %> -<%@page import="org.mxchange.pizzaapplication.product.Product"%> - -<% - // Init application instance - PizzaApplication app = PizzaServiceApplication.getInstance(application); - - // Is it post? - if ("POST".equals(request.getMethod())) { //NOI18N - // Handle saving customer data and such things - - // Redirect to proper URL - // @TODO Commented out for developing: - //response.sendRedirect(request.getContextPath() + "/finished.jsp"); - } -%> - - - - - - - <%=PizzaServiceApplication.MAIN_TITLE%> - Form-Handler - Administration - - - -
-

<%=PizzaServiceApplication.MAIN_TITLE%> - Form-Handler - Administration

-
- - - -
-
-

Bitte nicht direkt aufrufen:

-
- -
- Bitte rufen Sie diese Seite nicht direkt auf. -
-
- - diff --git a/web/static/admin/category_selection_box.jsp b/web/static/admin/category_selection_box.jsp index ea09c0b6..6e278ef4 100644 --- a/web/static/admin/category_selection_box.jsp +++ b/web/static/admin/category_selection_box.jsp @@ -14,7 +14,7 @@ PizzaApplication app = PizzaServiceApplication.getInstance(application); %> - -- 2.39.5