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
*/
protected BaseBasket () throws UnsupportedDatabaseBackendException, SQLException {
// Trace message
- this.getLogger().trace("CALLED!");
+ this.getLogger().trace("CALLED!"); //NOI18N
// Init item map instance
this.items = new LinkedHashMap<>();
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);
@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;
}
}
import org.mxchange.jcore.FrameworkInterface;
import org.mxchange.pizzaapplication.item.AddableBasketItem;
+import org.mxchange.pizzaapplication.product.Product;
/**
* An interface for baskets
* 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);
}
@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
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 {
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
}
}
*/
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
*/
* @author Roland Haeder
*/
public class BaseItem extends BaseFrameworkSystem implements AddableBasketItem {
+ /**
+ * Item amount
+ */
+ private Long amount;
+
/**
* Item id number
*/
*/
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
*/
*/
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
+ }
}
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">
<%@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ü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" />