]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project:
authorRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 06:14:47 +0000 (08:14 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 14 Aug 2015 06:20:20 +0000 (08:20 +0200)
- Added method isCategoryUsed() and implemented it rudely in frontend, too.
- doAdminAddCategory() does now check request parameter if they exist and are not empty

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

index 80983bf1337ff6f4ce6f8d07678461a55edfab5a..d9c953c58fa03e01bc4e0b6fd0bded628f313551 100644 (file)
@@ -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;
 }
index 550d3f9436fdc4a19dbc5f14fc267c80492e3b0d..2de42a69ba23c0e6efd9034916229faf90346fdc 100644 (file)
@@ -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
        }
index 831e9aec8e274d57d3e683af197528d7499df5d3..e32c7bc999ce945e60c9a9097d3017c9c381c0c4 100644 (file)
@@ -39,4 +39,15 @@ public interface CategoryFrontend extends DatabaseFrontend {
         * @throws java.sql.SQLException If any SQL error occurs
         */
        public Iterator<Category> 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;
 }
index 65b3612fa7b7e221a53797fe050af73665df1189..36ecbbe8fae0d48a9c6259b33342de74e6b33951 100644 (file)
@@ -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<? 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;
+       }
 }