]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/application/PizzaServiceApplication.java
Converted private constructor to public init() method with context, still this cannot...
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / application / PizzaServiceApplication.java
index 68cf72205a75c4fb0a2d837c214d008012210c96..d8253814c2c7a000741ccc59f10bb1774ccbd29c 100644 (file)
@@ -16,8 +16,8 @@
  */
 package org.mxchange.pizzaapplication.application;
 
-import org.mxchange.pizzaapplication.exceptions.CategoryTitleAlreadyUsedException;
 import java.io.IOException;
+import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Field;
 import java.lang.reflect.InvocationTargetException;
 import java.sql.SQLException;
@@ -27,6 +27,7 @@ import java.util.Map;
 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;
@@ -42,6 +43,7 @@ import org.mxchange.pizzaapplication.database.frontend.category.PizzaCategoryDat
 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.CategoryTitleAlreadyUsedException;
 import org.mxchange.pizzaapplication.exceptions.ProductTitleAlreadyUsedException;
 import org.mxchange.pizzaapplication.product.Product;
 
@@ -91,13 +93,20 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                if (object instanceof PizzaApplication) {
                        // Instance is set, so casting should work
                        instance = (PizzaApplication) object;
+
+                       // Debug message
+                       instance.getLogger().debug(MessageFormat.format("Using existing instance {0} ...", object)); //NOI18N
                } else if (object instanceof Object) {
                        // Not correct instance
                        throw new ServletException("app is not set correctly"); //NOI18N
                } else {
                        try {
                                // "service" is null, so initialize it
-                               instance = new PizzaServiceApplication(context);
+                               instance = new PizzaServiceApplication();
+                               instance.init(context);
+
+                       // Debug message
+                       instance.getLogger().debug(MessageFormat.format("Created new instance {0} ...", object)); //NOI18N
                        } catch (final UnsupportedDatabaseBackendException | SQLException | IOException | BadTokenException ex) {
                                throw new ServletException(ex);
                        }
@@ -123,12 +132,8 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                new PizzaServiceApplication().start();
        }
 
-       /**
-        * Constructor with servet configuration
-        *
-        * @param context Servlet context
-        */
-       private PizzaServiceApplication (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
+       @Override
+       public void init (final ServletContext context) throws UnsupportedDatabaseBackendException, SQLException, IOException, BadTokenException {
                // Temporary initialize default bundle
                // @TODO The JSF may have better internatialization support
                this.initBundle();
@@ -364,7 +369,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                }
 
                // Get attribute
-               Object object = this.getValueFromSession(product, session, HTTP_PARAM_CHOOSE);
+               Object object = this.getValueFromSession(product, session, HTTP_PARAM_ITEM_ID);
 
                // Is the object null?
                if (object == null) {
@@ -401,8 +406,14 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                        throw new NullPointerException("session is null"); //NOI18N
                }
 
+               // Get "enabled" from request scope
+               Boolean enabled = Boolean.parseBoolean((String) request.getAttribute("enabled")); //NOI18N
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("enabled={0}", enabled)); //NOI18N
+
                // Is something selected?
-               if (this.calculateTotalAmount(request, session) > 0) {
+               if ((enabled) || (this.calculateTotalAmount(request, session) > 0)) {
                        // Trace message
                        this.getLogger().trace("Returning empty string - EXIT!"); //NOI18N
 
@@ -545,7 +556,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                try {
                        // Ask frontend for a list of products
                        return this.productFrontend.getAvailableProducts();
-               } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException ex) {
+               } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                        throw new ServletException(ex);
                }
        }
@@ -560,7 +571,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                try {
                        // Ask frontend for a list of products
                        return this.productFrontend.getAllProducts();
-               } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException ex) {
+               } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                        throw new ServletException(ex);
                }
        }
@@ -575,7 +586,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                try {
                        // Ask frontend for a list of categories
                        return this.categoryFrontend.getCategories();
-               } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException ex) {
+               } catch (final IOException | BadTokenException | SQLException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                        throw new ServletException(ex);
                }
        }
@@ -831,7 +842,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 
                // Mark it as ordered by setting flag
                this.getLogger().debug(MessageFormat.format("Marking product={0} as choosen.", product.getId())); //NOI18N
-               this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, "1"); //NOI18N
+               this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, "1"); //NOI18N
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
@@ -908,7 +919,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
 
                // Mark it as ordered by setting flag
                this.getLogger().debug(MessageFormat.format("Unmarking product={0} as choosen.", product.getId())); //NOI18N
-               this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
+               this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
 
                // Trace message
                this.getLogger().trace("EXIT!"); //NOI18N
@@ -997,7 +1008,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                this.getLogger().trace(MessageFormat.format("session={0},key={1} - CALLED!", session, key)); //NOI18N
 
                // Init value
-               Object value = null;
+               Object value;
 
                // Get it synchronized from session
                synchronized (session) {
@@ -1019,7 +1030,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
 
@@ -1054,14 +1065,14 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                }
 
                // Get reqzest element
-               object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_CHOOSE, product.getId()));
+               object = request.getParameter(String.format(HTTP_PARAM_MASK, HTTP_PARAM_ITEM_ID, product.getId()));
                this.getLogger().debug(MessageFormat.format("product={0},object={1}", product.getId(), object)); //NOI18N
 
                // Is it null?
                if (object == null) {
                        // Unset session
                        this.getLogger().debug(MessageFormat.format("Unsetting session for product={0} ...", product.getId())); //NOI18N
-                       this.clearSessionAttribute(product, session, HTTP_PARAM_CHOOSE);
+                       this.clearSessionAttribute(product, session, HTTP_PARAM_ITEM_ID);
                        this.clearSessionAttribute(product, session, HTTP_PARAM_AMOUNT);
 
                        // Return empty string
@@ -1069,7 +1080,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                }
 
                // Then set it in session
-               this.setValueInSession(product, session, HTTP_PARAM_CHOOSE, object);
+               this.setValueInSession(product, session, HTTP_PARAM_ITEM_ID, object);
 
                // Cast to string and return it
                this.getLogger().debug(MessageFormat.format("product={0} - Returning {1} ...", product.getId(), object)); //NOI18N
@@ -1093,7 +1104,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 {
+       private boolean isCategoryTitleUsed(final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                // Delegate to frontend
                return this.categoryFrontend.isCategoryTitleUsed(title);
        }
@@ -1103,7 +1114,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
         * @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, CorruptedDatabaseFileException {
+       private boolean isProductTitleUsed (final String title) throws IOException, SQLException, BadTokenException, CorruptedDatabaseFileException, NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
                // Delegate to frontend
                return this.productFrontend.isProductTitleUsed(title);
        }
@@ -1113,9 +1124,9 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
         *
         * @param product Product instance
         * @param session HttpSession instance
-        * @return
+        * @return Whether the product has been ordered
         */
-       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
 
@@ -1150,14 +1161,17 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
        /**
         * Application starter
         */
-       private void start () {
+       private void start() {
                // Init bundle
                this.initBundle();
 
                try {
                        // Init properties
                        this.initProperties();
-               } catch (final IOException ex) {
+
+                       // And frontends
+                       this.initDatabaseFrontends();
+               } catch (final IOException | UnsupportedDatabaseBackendException | SQLException ex) {
                        // Abort here
                        this.abortProgramWithException(ex);
                }
@@ -1185,13 +1199,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);
 
@@ -1261,7 +1275,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                                // Title already used
                                throw new CategoryTitleAlreadyUsedException(request);
                        }
-               } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException ex) {
+               } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                        throw new ServletException(ex);
                }
 
@@ -1300,7 +1314,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                String available = request.getParameter(PizzaProductDatabaseConstants.COLUMN_AVAILABLE);
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available));
+               this.getLogger().debug(MessageFormat.format("title={0},price={1},category={2},available={3}", title, price, category, available)); //NOI18N
 
                // Variables for converting
                Long id = null;
@@ -1355,7 +1369,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                                // Title already used
                                throw new ProductTitleAlreadyUsedException(request);
                        }
-               } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException ex) {
+               } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                        throw new ServletException(ex);
                }
 
@@ -1390,7 +1404,7 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                }
 
                // Get parent id
-               Integer parent = category.getParent();
+               Long parent = category.getParent();
 
                // Is the id set?
                if (parent > 0) {
@@ -1401,4 +1415,240 @@ public class PizzaServiceApplication extends BasePizzaServiceSystem implements P
                // No parent set
                return "Keine";
        }
+
+       @Override
+       public String getPrintableProduktCategory (final Product product) throws ServletException {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product)); //NOI18N
+
+               // product must not be null
+               if (product == null) {
+                       // Abort here
+                       throw new NullPointerException("product is null"); //NOI18N
+               }
+
+               // Declare category
+               Category category;
+
+               try {
+                       // Get Category instance from product over the frontend
+                       category = this.categoryFrontend.getCategory(product);
+               } catch (final IOException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
+                       throw new ServletException(ex);
+               }
+
+               // Debug message
+               this.getLogger().debug(MessageFormat.format("category={0}", category)); //NOI18N
+
+               String title = null;
+               try {
+                       // Now get title from it and return it
+                       title = category.decodedTitle();
+               } catch (final UnsupportedEncodingException ex) {
+                       // Continue to throw as cause
+                       throw new ServletException(ex);
+               }
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("title={0} - EXIT!", title)); //NOI18N
+
+               // Return it
+               return title;
+       }
+
+       /**
+        * 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(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+
+               // Init title
+               String title = request.getParameter(PizzaProductDatabaseConstants.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"); //NOI18N
+               } else if (title == null) {
+                       // title is not set
+                       throw new IllegalArgumentException(MessageFormat.format("{0} is not set.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+               } else if (title.isEmpty()) {
+                       // Is left empty
+                       throw new IllegalArgumentException(MessageFormat.format("{0} is empty.", PizzaProductDatabaseConstants.COLUMN_TITLE)); //NOI18N
+               }
+
+               // Default is not used
+               boolean isUsed = this.isProductTitleUsed(title);
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
+
+               // 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(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
+
+               // request and response must both be set
+               if (request == null) {
+                       // request is null
+                       throw new NullPointerException("request is null"); //NOI18N
+               } else if (response == null) {
+                       // response is null
+                       throw new NullPointerException("response is null"); //NOI18N
+               }
+
+               // 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 it already added?
+                                       if (this.isProductTitleUsed(request)) {
+                                               // Debug message
+                                               this.getLogger().debug("Already used, redirecting ..."); //NOI18N
+
+                                               // Already added, so redirect here, else a ServletException will be thrown
+                                               response.sendRedirect(String.format("%s/admin/product.jsp?already=1", request.getContextPath())); //NOI18N
+                                       } 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!"); //NOI18N
+       }
+
+       /**
+        * 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(MessageFormat.format("request={0},response={1} - CALLED!", request, response)); //NOI18N
+
+               // request and response must both be set
+               if (request == null) {
+                       // request is null
+                       throw new NullPointerException("request is null"); //NOI18N
+               } else if (response == null) {
+                       // response is null
+                       throw new NullPointerException("response is null"); //NOI18N
+               }
+
+               // 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 ..."); //NOI18N
+
+                                               // Already added, so redirect here, else a ServletException will be thrown
+                                               response.sendRedirect(String.format("%s/admin/category.jsp?already=1", request.getContextPath())); //NOI18N
+                                       } 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!"); //NOI18N
+       }
+
+       /**
+        * 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(MessageFormat.format("request={0} - CALLED!", request)); //NOI18N
+
+               // 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"); //NOI18N
+               } 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(MessageFormat.format("isUsed={0} - EXIT!", isUsed)); //NOI18N
+
+               // Return it
+               return isUsed;
+       }
 }