package org.mxchange.pizzaapplication.filter.servlet.basket;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.text.MessageFormat;
import javax.servlet.Filter;
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.UnsupportedDatabaseBackendException;
-import org.mxchange.pizzaapplication.basket.Basket;
-import org.mxchange.pizzaapplication.basket.item.ItemBasket;
+import org.mxchange.jcore.exceptions.BadTokenException;
+import org.mxchange.jcore.exceptions.CorruptedDatabaseFileException;
+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
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));
+ 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");
- } else if (response == null) {
+ throw new NullPointerException("request is null"); //NOI18N
+ } else if (null == response) {
// response is null
- throw new NullPointerException("response is null");
- } else if (chain == null) {
+ throw new NullPointerException("response is null"); //NOI18N
+ } else if (null == chain) {
// chain is null
- throw new NullPointerException("chain is null");
+ throw new NullPointerException("chain is null"); //NOI18N
}
// Call doFilter to move on
chain.doFilter(request, response);
- // Get session instance
- HttpSession session = ((HttpServletRequest) request).getSession();
+ // Get item instance from request
+ Object object = request.getAttribute("item"); //NOI18N
// Debug message
- this.getLogger().debug(MessageFormat.format("session={0}", session));
-
- // Should not be null
- if (session == null) {
- // session is null
- throw new NullPointerException("session is null");
- }
-
- // Get item instance
- Object item = session.getAttribute("item");
-
- // 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 (null == object) {
// item is null
- throw new NullPointerException("item is null");
- } else if (!(item instanceof AddableBasketItem)) {
+ throw new NullPointerException("object is null"); //NOI18N
+ } else if (!(object instanceof AddableBasketItem)) {
// Not right instance
- throw new IllegalArgumentException("item does not implement OrderableItem");
+ throw new IllegalArgumentException("object does not implement AddableBasketItem"); //NOI18N
}
- // Get basket instance
- Basket<AddableBasketItem> basket;
+ // 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())); //NOI18N
+
try {
- basket = ItemBasket.getInstance(session);
- } catch (final UnsupportedDatabaseBackendException | SQLException ex) {
+ // Cast to servlet request
+ HttpServletRequest servletRequest = (HttpServletRequest) request;
+
+ // Get session instance
+ HttpSession session = servletRequest.getSession();
+
+ // Debug message
+ this.getLogger().debug(MessageFormat.format("session={0}", session)); //NOI18N
+
+ // Should not be null
+ if (null == session) {
+ // session is null
+ throw new NullPointerException("session is null"); //NOI18N
+ }
+
+ // Get basket instance
+ 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) {
+ // Item id is not set
+ throw new NullPointerException(MessageFormat.format("item id of item={0} is null", item)); //NOI18N
+ } else if (item.getItemType() == null) {
+ // Item type is not set
+ 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 - EXIT!", item)); //NOI18N
+
+ // Amount is not entered
+ return;
+ } else if (basket.isItemAdded(item)) {
+ // Yes, then throw exception here
+ throw new ServletException(MessageFormat.format("item id={0} has already been added.", item.getItemId())); //NOI18N
+ }
+
+ // Register item with it
+ basket.addItem(item);
+
+ // 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);
}
- // Register item with it
- basket.addItem((AddableBasketItem) item);
-
// Trace message
- this.getLogger().trace("EXIT!");
+ this.getLogger().trace("EXIT!"); //NOI18N
}
}