]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java
Get the basket bean directly from session scope
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / filter / servlet / basket / BasketItemAddedFilter.java
index 13b96c95a2b2e48703a7239e25b6bd0449cdcd08..66089c88c7a743bcc878954ad92cdbd7bad04f41 100644 (file)
@@ -26,14 +26,13 @@ import javax.servlet.ServletException;
 import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpSession;
 import org.mxchange.jcore.exceptions.BadTokenException;
 import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
-import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
-import org.mxchange.pizzaapplication.basket.Basket;
-import org.mxchange.pizzaapplication.basket.item.ItemBasket;
+import org.mxchange.jshop.beans.basket.BasketBean;
+import org.mxchange.jshop.item.AddableBasketItem;
 import org.mxchange.pizzaapplication.filter.servlet.BaseServletFilter;
-import org.mxchange.pizzaapplication.item.AddableBasketItem;
 
 /**
  * A filter for handling added basket items
@@ -43,18 +42,19 @@ import org.mxchange.pizzaapplication.item.AddableBasketItem;
 public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
 
        @Override
+       @SuppressWarnings ("unchecked")
        public void doFilter (final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
                // Trace message
                this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain)); //NOI18N
 
                // All must be set
-               if (request == null) {
+               if (null == request) {
                        // request is null
                        throw new NullPointerException("request is null"); //NOI18N
-               } else if (response == null) {
+               } else if (null == response) {
                        // response is null
                        throw new NullPointerException("response is null"); //NOI18N
-               } else if (chain == null) {
+               } else if (null == chain) {
                        // chain is null
                        throw new NullPointerException("chain is null"); //NOI18N
                }
@@ -69,25 +69,26 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
                this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
 
                // item should not be null
-               if (object == null) {
+               if (null == object) {
                        // item is null
-                       throw new NullPointerException("item is null"); //NOI18N
+                       throw new NullPointerException("object is null"); //NOI18N
                } else if (!(object instanceof AddableBasketItem)) {
                        // Not right instance
-                       throw new IllegalArgumentException("item does not implement AddableBasketItem"); //NOI18N
+                       throw new IllegalArgumentException("object does not implement AddableBasketItem"); //NOI18N
                }
 
                // Now it is secure to cast
                AddableBasketItem item = (AddableBasketItem) object;
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("item.id={0},item.itemId={1},item.itemType={2},item.amount={3}", item.getId(), item.getItemId(), item.getItemType(), item.getAmount()));
+               this.getLogger().debug(MessageFormat.format("item.id={0},item.itemId={1},item.itemType={2},item.amount={3}", item.getId(), item.getItemId(), item.getItemType(), item.getAmount())); //NOI18N
 
-               // Init instance
-               Basket<AddableBasketItem> basket;
                try {
+                       // Cast to servlet request
+                       HttpServletRequest servletRequest = (HttpServletRequest) request;
+
                        // Get session instance
-                       HttpSession session = ((HttpServletRequest) request).getSession();
+                       HttpSession session = servletRequest.getSession();
 
                        // Debug message
                        this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N
@@ -99,7 +100,10 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
                        }
 
                        // Get basket instance
-                       basket = ItemBasket.getInstance(session);
+                       BasketBean basket = (BasketBean) session.getAttribute("basket"); //NOI18N
+
+                       // Debug message
+                       this.getLogger().debug(MessageFormat.format("basket={0}", basket)); //NOI18N
 
                        // Is the item already added?
                        if (item.getItemId() == null) {
@@ -110,7 +114,7 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
                                throw new NullPointerException(MessageFormat.format("item type of item={0} is null", item)); //NOI18N
                        } else if ((item.getAmount() == null) || (item.getAmount() == 0)) {
                                // Debug message
-                               this.getLogger().debug(MessageFormat.format("Amount for item {0} is null", item)); //NOI18N
+                               this.getLogger().debug(MessageFormat.format("Amount for item {0} is null - EXIT!", item)); //NOI18N
 
                                // Amount is not entered
                                return;
@@ -121,7 +125,16 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
 
                        // Register item with it
                        basket.addItem(item);
-               } catch (final UnsupportedDatabaseBackendException | SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
+
+                       // Is amount null or zero?
+                       if ((item.getAmount() == null) || (item.getAmount() == 0)) {
+                               // Then redirect to added=0
+                               ((HttpServletResponse) response).sendRedirect(servletRequest.getContextPath() + "/?add=0"); //NOI18N
+                       } else {
+                               // Redirect to proper URL
+                               ((HttpServletResponse) response).sendRedirect(servletRequest.getContextPath() + "/?add=1"); //NOI18N
+                       }
+               } catch (final SQLException | BadTokenException | CorruptedDatabaseFileException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
                        // Continue to throw
                        throw new ServletException(ex);
                }