]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project:
authorRoland Haeder <roland@mxchange.org>
Sat, 15 Aug 2015 11:24:47 +0000 (13:24 +0200)
committerRoland Haeder <roland@mxchange.org>
Sat, 15 Aug 2015 11:25:08 +0000 (13:25 +0200)
- 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 <roland@mxchange.org>

src/java/org/mxchange/pizzaapplication/application/PizzaApplication.java
src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
web/form_handler/admin/do_category.jsp
web/form_handler/admin/do_product.jsp

index c1ebe25236b649f57b64e7096987071f3c0a4c20..c7545bef5c2c12450a18f283e1e2ec21f1205353 100644 (file)
@@ -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;
 }
index 2c389deb7ff149d486bd658d0edde77f3f22eca3..c74d2592255c9d2053d95ddbc95e20c8997e471c 100644 (file)
@@ -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!");
 
index ddd04d64f8e23596b04007340ebb401f78e8e785..1bb64d71579ee5976996507a66acacf03fd56370 100644 (file)
        // 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);
 %>
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
index 87afa0196ba09b48b06bab22028e886e12032951..65fb17cfa9b76e7d00b9887246b5b8460abf2104 100644 (file)
        // 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);
 %>
 
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">