]> git.mxchange.org Git - pizzaservice-war.git/commitdiff
Continued with project, added more checks for items and handling of already added...
authorRoland Haeder <roland@mxchange.org>
Fri, 21 Aug 2015 10:41:41 +0000 (12:41 +0200)
committerRoland Haeder <roland@mxchange.org>
Fri, 21 Aug 2015 10:41:41 +0000 (12:41 +0200)
Signed-off-by:Roland Häder <roland@mxchange.org>

src/java/org/mxchange/pizzaapplication/basket/BaseBasket.java
src/java/org/mxchange/pizzaapplication/basket/Basket.java
src/java/org/mxchange/pizzaapplication/filter/servlet/basket/BasketItemAddedFilter.java
src/java/org/mxchange/pizzaapplication/item/AddableBasketItem.java
src/java/org/mxchange/pizzaapplication/item/BaseItem.java
src/java/org/mxchange/pizzaapplication/item/basket/BasketItem.java
web/form_handler/add_item.jsp
web/index.jsp

index fa81756fb05c1de1859c272819a31537a4acf5bd..ffd62df20ceeca8be3d29ae919db83957481ce3f 100644 (file)
 package org.mxchange.pizzaapplication.basket;
 
 import java.sql.SQLException;
-import org.mxchange.pizzaapplication.database.frontend.basket.BasketDatabaseFrontend;
 import java.text.MessageFormat;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import org.mxchange.jcore.BaseFrameworkSystem;
 import org.mxchange.jcore.exceptions.UnsupportedDatabaseBackendException;
+import org.mxchange.pizzaapplication.database.frontend.basket.BasketDatabaseFrontend;
 import org.mxchange.pizzaapplication.database.frontend.basket.BasketFrontend;
 import org.mxchange.pizzaapplication.item.AddableBasketItem;
+import org.mxchange.pizzaapplication.product.Product;
 
 /**
  * A general basket class
@@ -45,7 +46,7 @@ public class BaseBasket extends BaseFrameworkSystem implements Basket<AddableBas
         */
        protected BaseBasket () throws UnsupportedDatabaseBackendException, SQLException {
                // Trace message
-               this.getLogger().trace("CALLED!");
+               this.getLogger().trace("CALLED!"); //NOI18N
 
                // Init item map instance
                this.items = new LinkedHashMap<>();
@@ -54,7 +55,7 @@ public class BaseBasket extends BaseFrameworkSystem implements Basket<AddableBas
                BasketFrontend frontend = new BasketDatabaseFrontend();
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("frontend={0} being set ...", frontend));
+               this.getLogger().debug(MessageFormat.format("frontend={0} being set ...", frontend)); //NOI18N
 
                // Instance it here
                this.setFrontend(frontend);
@@ -62,7 +63,64 @@ public class BaseBasket extends BaseFrameworkSystem implements Basket<AddableBas
 
        @Override
        public void addItem (final AddableBasketItem item) {
+               // Trace call
+               this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
+
+               // item must not be null
+               if (item == null) {
+                       // Then abort here
+                       throw new NullPointerException("item is null"); //NOI18N
+               } else if (this.isItemAdded(item)) {
+                       // Already been added
+                       throw new IllegalArgumentException("item has already been added. Did you miss to call isItemAdded()?"); //NOI18N
+               }
+
                // Add it to map
                this.items.put(item.getId(), item);
+
+               // Trace call
+               this.getLogger().trace("EXIT!"); //NOI18N
+       }
+
+       @Override
+       public AddableBasketItem getItem (final Product product) {
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("product={0} - CALLED!", product));
+
+               // product must not be null
+               if (product == null) {
+                       // Abort here
+                       throw new NullPointerException("product is null");
+               }
+
+               // Get item instance from map by product's id number (both are the same)
+               AddableBasketItem item = this.items.get(product.getId());
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("item={0} - EXIT!", item));
+
+               // Return it
+               return item;
+       }
+
+       @Override
+       public boolean isItemAdded (final AddableBasketItem item) {
+               // Trace call
+               this.getLogger().trace(MessageFormat.format("item={0} - CALLED!", item)); //NOI18N
+
+               // item must not be null
+               if (item == null) {
+                       // Then abort here
+                       throw new NullPointerException("item is null"); //NOI18N
+               }
+
+               // Call map's method
+               boolean isAdded = this.items.containsKey(item.getId());
+
+               // Trace message
+               this.getLogger().trace(MessageFormat.format("isAdded={0} - EXIT!", isAdded)); //NOI18N
+
+               // Return it
+               return isAdded;
        }
 }
index 182ea4d11f25b81a6e56ee1f4eedca4aa7cd33fb..f7b69fadbd94ab5d1de10527af28fbac8d370184 100644 (file)
@@ -18,6 +18,7 @@ package org.mxchange.pizzaapplication.basket;
 
 import org.mxchange.jcore.FrameworkInterface;
 import org.mxchange.pizzaapplication.item.AddableBasketItem;
+import org.mxchange.pizzaapplication.product.Product;
 
 /**
  * An interface for baskets
@@ -31,6 +32,23 @@ public interface Basket<T extends AddableBasketItem> extends FrameworkInterface
         * Adds given item instance to this basket
         * @param item Item instance to add
         */
-       public void addItem (final AddableBasketItem item);
-       
+       public void addItem (final T item);
+
+       /**
+        * Checks whether the given item as already been added. Mostly
+        * implementations may check the id number as this is mostly believed being
+        * unique.
+        *
+        * @param item Item instance to check
+        * @return Whether the given item has been found
+        */
+       public boolean isItemAdded (final T item);
+
+       /**
+        * Returns an item for given product instance or null if not found.
+        * 
+        * @param product Product instance
+        * @return Item instance or null
+        */
+       public T getItem (final Product product);
 }
index 21fecbcaa3a03576e418680259e29390e0d30483..e6c7e4e3c61ef15aac90610a857f41c10934bb56 100644 (file)
@@ -42,18 +42,18 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
        @Override
        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));
+               this.getLogger().trace(MessageFormat.format("request={0},response={1},chain={2} - CALLED!", request, response, chain)); //NOI18N
 
                // All must be set
                if (request == null) {
                        // request is null
-                       throw new NullPointerException("request is null");
+                       throw new NullPointerException("request is null"); //NOI18N
                } else if (response == null) {
                        // response is null
-                       throw new NullPointerException("response is null");
+                       throw new NullPointerException("response is null"); //NOI18N
                } else if (chain == null) {
                        // chain is null
-                       throw new NullPointerException("chain is null");
+                       throw new NullPointerException("chain is null"); //NOI18N
                }
 
                // Call doFilter to move on
@@ -63,29 +63,32 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
                HttpSession session = ((HttpServletRequest) request).getSession();
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("session={0}", session));
+               this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N
 
                // Should not be null
                if (session == null) {
                        // session is null
-                       throw new NullPointerException("session is null");
+                       throw new NullPointerException("session is null"); //NOI18N
                }
 
                // Get item instance
-               Object item = session.getAttribute("item");
+               Object object = session.getAttribute("item"); //NOI18N
 
                // Debug message
-               this.getLogger().debug(MessageFormat.format("item={0}", item));
+               this.getLogger().debug(MessageFormat.format("object={0}", object)); //NOI18N
 
                // item should not be null
-               if (item == null) {
+               if (object == null) {
                        // item is null
-                       throw new NullPointerException("item is null");
-               } else if (!(item instanceof AddableBasketItem)) {
+                       throw new NullPointerException("item is null"); //NOI18N
+               } else if (!(object instanceof AddableBasketItem)) {
                        // Not right instance
-                       throw new IllegalArgumentException("item does not implement OrderableItem");
+                       throw new IllegalArgumentException("item does not implement AddableBasketItem"); //NOI18N
                }
 
+               // Now it is secure to cast
+               AddableBasketItem item = (AddableBasketItem) object;
+
                // Get basket instance
                Basket<AddableBasketItem> basket;
                try {
@@ -95,11 +98,17 @@ public class BasketItemAddedFilter extends BaseServletFilter implements Filter {
                        throw new ServletException(ex);
                }
 
+               // Is the item already added?
+               if (basket.isItemAdded(item)) {
+                       // Yes, then throw exception here
+                       throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getId()));
+               }
+
                // Register item with it
-               basket.addItem((AddableBasketItem) item);
+               basket.addItem(item);
 
                // Trace message
-               this.getLogger().trace("EXIT!");
+               this.getLogger().trace("EXIT!"); //NOI18N
        }
 
 }
index 34acdc083c093b0f59a7992380298b3db37900e7..4c0acd383e037d6ca7e85a6d53214b466730098e 100644 (file)
@@ -25,6 +25,18 @@ import org.mxchange.jcore.FrameworkInterface;
  */
 public interface AddableBasketItem extends FrameworkInterface {
 
+       /**
+        * Item amount
+        * @return the amount
+        */
+       public Long getAmount ();
+
+       /**
+        * Item amount
+        * @param amount the amount to set
+        */
+       public void setAmount (final Long amount);
+
        /**
         * @return the id
         */
index 636fd3ca0487c4149c6e914842ce64d903400dd0..8500a4557762de0975186eb23f2494f13a873e5a 100644 (file)
@@ -23,6 +23,11 @@ import org.mxchange.jcore.BaseFrameworkSystem;
  * @author Roland Haeder
  */
 public class BaseItem extends BaseFrameworkSystem implements AddableBasketItem {
+       /**
+        * Item amount
+        */
+       private Long amount;
+
        /**
         * Item id number
         */
@@ -33,6 +38,22 @@ public class BaseItem extends BaseFrameworkSystem implements AddableBasketItem {
         */
        private String type;
 
+       /**
+        * Item amount
+        * @return the amount
+        */
+       public final Long getAmount () {
+               return this.amount;
+       }
+
+       /**
+        * Item amount
+        * @param amount the amount to set
+        */
+       public final void setAmount (final Long amount) {
+               this.amount = amount;
+       }
+
        /**
         * @return the id
         */
index ae149e9fab41ae82c5d27c40caffbeb77acbc628..f968c75bcddd7ac3bef64e82d26dbb166d40814a 100644 (file)
  */
 package org.mxchange.pizzaapplication.item.basket;
 
+import java.text.MessageFormat;
+import org.mxchange.pizzaapplication.item.AddableBasketItem;
 import org.mxchange.pizzaapplication.item.BaseItem;
+import org.mxchange.pizzaapplication.product.Product;
 
 /**
  * A basket item for Pizza service
  *
  * @author Roland Haeder
  */
-public class BasketItem extends BaseItem {
+public class BasketItem extends BaseItem implements AddableBasketItem {
+       /**
+        * Default constructor
+        */
+       public BasketItem () {
+       }
+
+       /**
+        * Constructor for an item from given Product instance
+        * 
+        * @param product Product instance
+        */
+       public BasketItem (final Product product) {
+               // Trace message
+               this.getLogger().debug(MessageFormat.format("product={0} - CALLED!", product));
+
+               // product must not be null
+               if (product == null) {
+                       // Abort here
+                       throw new NullPointerException("product is null");
+               }
+
+               // Copy all neccessary values
+               this.setId(product.getId());
+               this.setType("Product"); //NOI18N
+       }
 }
index aa17cca551f50a208b539a404cbec65dd7ff2b7c..3e033fc6b1ace7c3aa19d58aba2be3ccde8c03ae 100644 (file)
        PizzaApplication app = PizzaServiceApplication.getInstance(application);
 
        // Redirect to proper URL
-       // @TODO Commented out for debugging
-       //response.sendRedirect(request.getContextPath() + "/?add=1");
+       response.sendRedirect(request.getContextPath() + "/?add=1");
 %>
-<jsp:forward page="../index.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">
index 916ccd5022233022677e31e9ea21d6b72c5404d3..37824419eaa74e2785cb269c4ddd6583697b407c 100644 (file)
 <%@page import="org.mxchange.pizzaapplication.application.PizzaServiceApplication"%>
 <%@page import="org.mxchange.pizzaapplication.application.PizzaApplication"%>
 <%@page import="org.mxchange.pizzaapplication.product.Product"%>
+<%@page import="org.mxchange.pizzaapplication.basket.Basket"%>
+<%@page import="org.mxchange.pizzaapplication.basket.item.ItemBasket"%>
+<%@page import="org.mxchange.pizzaapplication.item.basket.BasketItem"%>
+<%@page import="org.mxchange.pizzaapplication.item.AddableBasketItem"%>
 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
 
                                        // Get product instance
                                        Product product = iterator.next();
 
+                                       // Get basket instance
+                                       Basket<AddableBasketItem> basket = ItemBasket.getInstance(session);
+
+                                       // Create an item instance form this product
+                                       AddableBasketItem item = basket.getItem(product);
+
+                                       // Has it been already added to the basket?
+                                       if (item != null) {
+                                               // Some nice output ...
+                                               %>
+                               <table class="table">
+                                       <tbody>
+                                                       <tr>
+                                                               <td class="table_data_column">
+                                                                       <a href="<%=request.getContextPath()%>/basket.jsp" title="Zum Warenkorb">Warenkorb</a>
+                                                               </td>
+                                                               <td class="table_data_column">
+                                                                       <%=item.getAmount()%>
+                                                               </td>
+                                                               <td class="table_data_column">
+                                                                       <%=product.getTitle()%>
+                                                               </td>
+                                                               <td class="table_data_column" align="right">
+                                                                       <fmt:formatNumber type="currency"><%=product.getPrice()%></fmt:formatNumber>
+                                                               </td>
+                                                       </tr>
+                                       </tbody>
+                               </table>
+                                               <%
+                                               // Then skip this item
+                                               continue;
+                                       }
+
                                        // Unmark as ordered
                                        app.unmarkProductAsOrdered(product, session);
                                        %>
                                                                <td class="table_data_column">
                                                                        <input class="submit" type="submit" name="add" value="Hinzuf&uuml;gen" />
                                                                        <input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_ID%>" value="<%=product.getId()%>" />
-                                                                       <input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_TYPE%>" value="<%=Product.class%>" />
+                                                                       <input class="input" type="hidden" name="<%=PizzaApplication.HTTP_PARAM_ITEM_TYPE%>" value="Product" />
                                                                </td>
                                                                <td class="table_data_column">
                                                                        <input class="input" type="text" name="<%=PizzaApplication.HTTP_PARAM_AMOUNT%>" size="3" maxlength="20" />