]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project:
authorRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 12:46:08 +0000 (14:46 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 12:46:08 +0000 (14:46 +0200)
- Used "category" instead of "id" for category selection box
- Added method addProduct()
- Added method isProductTitleUsed()
- Added new exception ProductTitleAlreadyUsedException
- Changed parameter type of "parent" to Integer as it can be done (better type-hint)
- Rewrote addCategory() a bit for this
- Added database columns "category", "price" and "title" for products
- Renamed method isCategoryUsed() to isCategoryTitleUsed()
- Deleted do_products.jsp
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java
src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
src/java/org/mxchange/pizzaapplication/database/frontend/category/CategoryFrontend.java
src/java/org/mxchange/pizzaapplication/database/frontend/category/PizzaCategoryDatabaseFrontend.java
src/java/org/mxchange/pizzaapplication/database/frontend/product/PizzaProductDatabaseFrontend.java
src/java/org/mxchange/pizzaapplication/database/frontend/product/ProductFrontend.java
src/java/org/mxchange/pizzaapplication/database/product/PizzaProductDatabaseConstants.java
src/java/org/mxchange/pizzaapplication/exceptions/ProductTitleAlreadyUsedException.java [new file with mode: 0644]
web/admin/product.jsp
web/form_handler/admin/do_products.jsp [deleted file]
web/static/admin/category_selection_box.jsp

index be6e2a64530ce01fa8af49bbabe82709ba988889..eac4882f9ed8172f2a51f813b9d3e24bced2e60d 100644 (file)
  */
 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
index c82f914b1cdff3ada5fb6efcdfdd1fbe9cc27ce9..914711b9353034399e312567c52b3e40b7ca233b 100644 (file)
@@ -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
        }
index b638db51cbdafa297ed5a4a81e29c6e9b0a8785d..6302554de3eda24e7080d6f682cdeebebab634e0 100644 (file)
@@ -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
index 74b7a802fc8ed48995919682db9f72988d11ca45..d491d03102baf3b5049bd5460cb22159e23acdf8 100644 (file)
@@ -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));
 
index 7076a74980332389b6cc475ca9ba5db413fd6466..633e44a491f7fccc8e09f0a4e9cee3ef45002f11 100644 (file)
@@ -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<? extends Storeable> 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<? extends Storeable> result = this.doInsertDataSet();
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("result={0}", result));
+
+               // Trace message
+               this.getLogger().trace("EXIT!");
+       }
 }
index 0ceadaa6244d72890789b1a9fe016a0551c2a6e3..4363aac3fa1b0224bf3e24cc9564c4630e095de9 100644 (file)
@@ -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<Product> 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;
 }
index 719dbf047258a3fc6d9eb0acc44331f1a69a4301..8d3646f784cfb30d57dbfb14b7c92543ca5fc2ed 100644 (file)
@@ -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 (file)
index 0000000..e650924
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+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)));
+       }
+       
+}
index 4383f963ddea30d25e1510c1e072620f2804f045..4fa59cbcd85e5344402e9ad3c50bd39754044990 100644 (file)
@@ -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
                                                                </div>
 
                                                                <div class="table_right">
-                                                                       <input type="text" name="title" size="10" maxlength="255" />
+                                                                       <input type="text" name="<%=PizzaProductDatabaseConstants.COLUMN_TITLE%>" size="10" maxlength="255" />
                                                                </div>
 
                                                                <div class="clear"></div>
                                                                </div>
 
                                                                <div class="table_right">
-                                                                       <input type="text" name="price" size="10" maxlength="255" />
+                                                                       <input type="text" name="<%=PizzaProductDatabaseConstants.COLUMN_PRICE%>" size="10" maxlength="255" />
                                                                </div>
 
                                                                <div class="clear"></div>
diff --git a/web/form_handler/admin/do_products.jsp b/web/form_handler/admin/do_products.jsp
deleted file mode 100644 (file)
index fe2de2f..0000000
+++ /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");
-       }
-%>
-
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de">
-       <head>
-               <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
-               <link rel="stylesheet" href="<%=request.getContextPath()%>/style.css" type="text/css"/>
-               <title><%=PizzaServiceApplication.MAIN_TITLE%> - Form-Handler - Administration</title>
-       </head>
-
-       <body>
-               <div id="title">
-                       <h1><%=PizzaServiceApplication.MAIN_TITLE%> - Form-Handler - Administration</h1>
-               </div>
-
-               <jsp:include page="/static/admin/menu.jsp" flush="true" />
-
-               <div id="content_outer">
-                       <div id="content_title">
-                               <h2>Bitte nicht direkt aufrufen:</h2>
-                       </div>
-
-                       <div id="content">
-                               Bitte rufen Sie diese Seite nicht direkt auf.
-                       </div>
-               </div>
-       </body>
-</html>
index ea09c0b6deb71ff15aa7459ec8de842a9701ab3f..6e278ef45df1d72ee3d731adbe3447c8b507da21 100644 (file)
@@ -14,7 +14,7 @@
        PizzaApplication app = PizzaServiceApplication.getInstance(application);
 %>
 
-<select name="<%=PizzaCategoryDatabaseConstants.COLUMN_ID%>" size="1">
+<select name="category" size="1">
        <c:forEach var="category" items="<%=app.getCategories()%>">
                <option value="${category.getId()}">${category.decodedTitle()}</option>
        </c:forEach>