]> git.mxchange.org Git - pizzaservice-war.git/blobdiff - src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebBean.java
Auto-formatted whole project
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / basket / BasketWebBean.java
index c0cb2ed5b789b520fd931cb3066551657594e729..467033c469104cc872f0f6e6df3d56a100768cec 100644 (file)
  */
 package org.mxchange.pizzaapplication.beans.basket;
 
-import javax.annotation.PostConstruct;
+import java.text.MessageFormat;
+import java.util.List;
 import javax.enterprise.context.SessionScoped;
 import javax.faces.FacesException;
+import javax.faces.view.facelets.FaceletException;
 import javax.inject.Named;
+import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
-import org.mxchange.jcoreee.beans.BaseFrameworkBean;
 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
+import org.mxchange.jshopcore.model.basket.Basket;
 import org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote;
-import org.mxchange.jshopcore.model.item.BasketItem;
+import org.mxchange.jshopcore.model.basket.ShopBasket;
+import org.mxchange.jshopcore.model.basket.items.BasketItem;
 import org.mxchange.jshopcore.model.product.Product;
 
 /**
  * A bean for the basket
- *
+ * <p>
  * @author Roland Haeder<roland@mxchange.org>
  */
-@Named ("basket")
+@Named ("basketController")
 @SessionScoped
-public class BasketWebBean extends BaseFrameworkBean implements BasketWebController {
+public class BasketWebBean implements BasketWebController {
 
        /**
         * Serial number
         */
        private static final long serialVersionUID = 5_476_347_320_198L;
 
+       /**
+        * Instance of wrapped basket
+        */
+       private final Basket<AddableBasketItem> basket;
+
        /**
         * Basket bean
         */
        private final BasketSessionBeanRemote basketBean;
 
+       /////////////////////// Properties /////////////////////
        /**
-        * Ordered amount
+        * Ordered orderedAmount
         */
-       private Long amount;
+       private Long orderedAmount;
 
        /**
         * Current item
@@ -60,46 +70,261 @@ public class BasketWebBean extends BaseFrameworkBean implements BasketWebControl
 
        /**
         * Default constructor
-        *
-        * @throws javax.naming.NamingException If something happened?
         */
-       public BasketWebBean () throws NamingException {
-               // Get initial context
-               InitialContext context = new InitialContext();
+       public BasketWebBean () {
+               // Get new application instance
+               this.basket = new ShopBasket();
+
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
 
-               // Try to lookup
-               this.basketBean = (BasketSessionBeanRemote) context.lookup("ejb/stateless-basket"); //NOI18N
+                       // Try to lookup
+                       this.basketBean = (BasketSessionBeanRemote) context.lookup("ejb/stateless-basket"); //NOI18N
+               } catch (final NamingException ex) {
+                       // Continue to throw
+                       throw new FaceletException(ex);
+               }
        }
 
        @Override
-       public Long getAmount () {
-               return this.amount;
+       public String addItem (final Product product) {
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("addItem: product={0} - CALLED!", product));
+
+               // product should not be null
+               if (null == product) {
+                       // Abort here
+                       throw new NullPointerException("product is null");
+               }
+
+               // Generate item instance
+               AddableBasketItem item = new BasketItem(product, this.getOrderedAmount());
+
+               // Is orderedAmount set?
+               if (this.getOrderedAmount() == null) {
+                       // Trace message
+                       //this.getLogger().logTrace("addItem: orderedAmount not specified, returning null ... - EXIT!");
+
+                       // No orderedAmount specified?!
+                       return null;
+               }
+
+               try {
+                       // item should not be null
+                       if (null == item) {
+                               // Abort here
+                               throw new NullPointerException("item is null"); //NOI18N
+                       }
+
+                       // Deligate to model
+                       this.basket.addItem(item);
+
+                       // Remove orderedAmount
+                       this.setOrderedAmount(null);
+
+                       // Trace message
+                       //this.getLogger().logTrace(MessageFormat.format("addItem: item {0} - has been added to basket. - EXIT!", item));
+                       // Added
+                       return "item_added"; //NOI18N
+               } catch (final BasketItemAlreadyAddedException ex) {
+                       // Throw unchecked exception
+                       throw new FacesException(ex);
+               }
        }
-       
+
        @Override
-       public void setAmount (final Long amount) {
-               this.amount = amount;
+       public List<AddableBasketItem> allItems () {
+               // Trace message
+               //this.getLogger().logTrace("allItems: CALLED!");
+
+               // Deligate to basket instance
+               List<AddableBasketItem> list = this.basket.getAll();
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("allItems: list={0} - EXIT!", list));
+               // Return it
+               return list;
        }
 
        @Override
-       public boolean isProductAdded (final Product product) {
-               // Must not be null
+       public Float calculateCurrentItemPrice () {
+               // Trace message
+               //this.getLogger().logTrace("calculateCurrentItemPrice: CALLED!");
+
+               // Is the current item/amount set?
+               if (this.getCurrentItem() == null) {
+                       // Current item is null
+                       throw new NullPointerException("currentItem is null"); //NOI18N
+               } else if (this.getCurrentItem().getItemProduct() == null) {
+                       // Product is null
+                       throw new NullPointerException("currentItem.product is null"); //NOI18N
+               } else if (this.getCurrentItem().getOrderedAmount() == null) {
+                       // Amount is null
+                       throw new NullPointerException("currentItem.amount is null"); //NOI18N
+               }
+
+               // Caculate item's price
+               Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
+               // Return it
+               return totalPrice;
+       }
+
+       @Override
+       public Float calculateItemPrice (final AddableBasketItem item) {
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
+
+               // item must not be null
+               if (null == item) {
+                       // Abort here
+                       throw new NullPointerException("item is null");
+               }
+
+               // Default value
+               Float totalPrice = 0.0f;
+
+               // Is it a product?
+               if (item.isProductType()) {
+                       // Caculate item's price
+                       totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
+               }
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
+               // Return it
+               return totalPrice;
+       }
+
+       @Override
+       public Float calculateTotalPrice () {
+               // Trace message
+               //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
+
+               // Init total price
+               Float totalPrice = 0.0f;
+
+               // Iterate over all items
+               for (final AddableBasketItem item : this.allItems()) {
+                       // Is the item a product?
+                       if (item.isProductType()) {
+                               // Calculate single price and add it
+                               totalPrice += this.calculateItemPrice(item);
+                       }
+               }
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
+               // Return final sum
+               return totalPrice;
+       }
+
+       @Override
+       public String changeItem (final AddableBasketItem item) {
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("changeItem: item={0} - CALLED!", item));
+
+               // item shall not be null
+               if (null == item) {
+                       // Abort here
+                       throw new NullPointerException("item is null");
+               }
+
+               // Default is not found
+               String targetPage = "item_not_changed"; //NOI18N
+
+               // Lookup item in basket
+               for (final AddableBasketItem basketItem : this.allItems()) {
+                       // Is it the same?
+                       if (basketItem.equals(item)) {
+                               // Found it, so allow redirect to proper page
+                               targetPage = "basket"; //NOI18N
+                               break;
+                       }
+               }
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("changeItem: targetPage={0} - EXIT!", targetPage));
+               // Return page
+               return targetPage;
+       }
+
+       @Override
+       public void clear () {
+               // @TODO Also clear EJB
+               // Deligate to basket instance
+               this.basket.clear();
+       }
+
+       @Override
+       public Long getOrderedAmount () {
+               return this.orderedAmount;
+       }
+
+       @Override
+       public void setOrderedAmount (final Long orderedAmount) {
+               this.orderedAmount = orderedAmount;
+       }
+
+       @Override
+       public AddableBasketItem getCurrentItem () {
+               return this.currentItem;
+       }
+
+       @Override
+       public void setCurrentItem (final AddableBasketItem currentItem) {
+               this.currentItem = currentItem;
+       }
+
+       @Override
+       public Long getItemAmount (final Product product) {
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
+
+               // product should not be null
                if (null == product) {
                        // Abort here
-                       throw new NullPointerException("product is null"); //NOI18N
+                       throw new NullPointerException("product is null");
                }
 
-               // Generate fake instance
-               AddableBasketItem item = new BasketItem(product);
+               // Initial value is zero
+               Long itemAmount = 0L;
 
-               // Ask bean about it
-               return this.getBasketBean().isAdded(item);
+               // Iterate over all
+               for (final AddableBasketItem item : this.allItems()) {
+                       // Debug message
+                       //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
+
+                       // Is this product instance and same?
+                       if (null == item) {
+                               // item is null
+                               throw new NullPointerException("item is null");
+                       } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
+                               // Found it
+                               itemAmount = item.getOrderedAmount();
+                               break;
+                       }
+               }
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
+               // Return it
+               return itemAmount;
        }
 
        @Override
-       public boolean isEmpty () {
-               // Check local "cache"
-               return this.getBasketBean().isEmpty();
+       public AddableBasketItem getLast () {
+               // Deligate to basket instance
+               return this.basket.getLast();
+       }
+
+       @Override
+       public int getLastNumRows () {
+               // Deligate to basket instance
+               return this.basket.getLastNumRows();
        }
 
        @Override
@@ -108,60 +333,146 @@ public class BasketWebBean extends BaseFrameworkBean implements BasketWebControl
                return (!this.isEmpty());
        }
 
-       @PostConstruct
-       public void init () throws RuntimeException {
-               // Call super init first
-               super.genericInit();
+       @Override
+       public boolean isEmpty () {
+               // Deligate to basket instance
+               return this.basket.isEmpty();
+       }
+
+       @Override
+       public boolean isProductAdded (final Product product) {
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
+
+               // Must not be null
+               if (null == product) {
+                       // Abort here
+                       throw new NullPointerException("product is null"); //NOI18N
+               }
+
+               // Generate fake instance
+               AddableBasketItem fake = new BasketItem(product);
+
+               // Debug message
+               //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
+               // Ask bean about it
+               boolean isAdded = this.basket.isAdded(fake);
+
+               // Debug message
+               //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
+               // Is it added?
+               if (isAdded) {
+                       // Get item
+                       AddableBasketItem item = this.getItemFromProduct(product);
+
+                       // Debug message
+                       //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
+                       // Set this as current item
+                       this.setCurrentItem(item);
+               }
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
+               // Return status
+               return isAdded;
+       }
+
+       @Override
+       public String outputLastAddedItem () {
+               // Trace message
+               //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
+
+               // Default message
+               String lastItem = ""; //NOI18N
+
+               // Get instance
+               AddableBasketItem item = this.getLast();
+
+               // Is it set?
+               if (item instanceof AddableBasketItem) {
+                       // Get type
+                       switch (item.getItemType()) {
+                               case "product": // Sellable product //NOI18N
+                                       assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
+
+                                       // Get title
+                                       lastItem = item.getItemProduct().getProductTitle();
+                                       break;
+
+                               default: // Not supported
+                                       throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
+                       }
+               }
+
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
+               // Return it
+               return lastItem;
        }
 
        /**
         * Getter for basket bean instance
-        *
+        * <p>
         * @return Basket bean instance
         */
        private BasketSessionBeanRemote getBasketBean () {
                return this.basketBean;
        }
 
-       @Override
-       public String addItem (final Product product) {
-               // Generate item instance
-               AddableBasketItem item = new BasketItem(product);
+       /**
+        * Somewhat getter for an item instance from given product instance. This
+        * method returns null if no item was found to given product. The product is
+        * found by checking it's id and itemType=product
+        * <p>
+        * @param product Product instance
+        * <p>
+        * @return Item instance or null if not found
+        */
+       private AddableBasketItem getItemFromProduct (final Product product) {
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
 
-               // Is amount set?
-               if (this.getAmount() == null) {
-                       // No amount specified?!
-                       return null;
+               // Product must not be null
+               if (null == product) {
+                       // Abort here
+                       throw new NullPointerException("product is null"); //NOI18N
                }
 
-               // Set amount
-               item.setAmount(this.getAmount());
+               // Create item instance
+               AddableBasketItem foundItem = null;
 
-               try {
-                       // Handle it to bean
-                       this.getBasketBean().addItem(item);
+               // Create fake instance
+               AddableBasketItem fake = new BasketItem(product);
 
-                       // Add item to local basket, too
-                       //this.basket.addItem(item);
+               // Debug message
+               //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
+               // Get all items
+               List<AddableBasketItem> list = this.basket.getAll();
 
-                       // Remove amount
-                       this.setAmount(null);
+               // Debug message
+               //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
+               // Check all entries
+               for (final AddableBasketItem item : list) {
+                       // Debug message
+                       //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
 
-                       // Added
-                       return "item_added"; //NOI18N
-               } catch (final BasketItemAlreadyAddedException ex) {
-                       // Throw unchecked exception
-                       throw new FacesException(ex);
-               }
-       }
+                       // item must not be null
+                       if (null == item) {
+                               // Abort here
+                               throw new NullPointerException("item is null"); //NOI18N
+                       }
 
-       @Override
-       public AddableBasketItem getCurrentItem () {
-               return this.currentItem;
-       }
+                       // Is it the same?
+                       if (item.equals(fake)) {
+                               // Set found item and abort look
+                               foundItem = item;
+                               break;
+                       }
+               }
 
-       @Override
-       public void setCurrentItem (final AddableBasketItem currentItem) {
-               this.currentItem = currentItem;
+               // Trace message
+               //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));
+               // Return it
+               return foundItem;
        }
 }