From e20ed21692d59bbbedcc48da6c5a82bd266582d2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Wed, 19 Apr 2017 19:24:14 +0200 Subject: [PATCH] Continued: - rewrote to event-observer instead of directly calling those methods - much smaller and easier code MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../basket/PizzaBasketWebSessionBean.java | 135 +++++------------- .../PizzaBasketWebSessionController.java | 13 +- .../checkout/PizzaCheckoutWebSessionBean.java | 33 ++--- .../receipt/PizzaReceiptWebSessionBean.java | 40 +++++- .../PizzaReceiptWebSessionController.java | 21 +-- 5 files changed, 97 insertions(+), 145 deletions(-) diff --git a/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionBean.java index 2faa8713..9a3afae7 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionBean.java @@ -19,6 +19,7 @@ package org.mxchange.pizzaapplication.beans.basket; import java.text.MessageFormat; import java.util.List; import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Observes; import javax.faces.FacesException; import javax.faces.view.facelets.FaceletException; import javax.inject.Named; @@ -26,11 +27,11 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jproduct.model.product.Product; +import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent; 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.basket.ShopBasket; import org.mxchange.jshopcore.model.basket.items.BasketItem; import org.mxchange.pizzaapplication.beans.BasePizzaController; @@ -63,7 +64,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi */ private AddableBasketItem currentItem; - /////////////////////// Properties ///////////////////// /** * Ordered orderedAmount */ @@ -73,9 +73,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi * Default constructor */ public PizzaBasketWebSessionBean () { - // Get new application instance - this.basket = new ShopBasket(); - try { // Get initial context Context context = new InitialContext(); @@ -86,13 +83,13 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi // Continue to throw throw new FaceletException(ex); } + + // Get current basked from bean + this.basket = this.basketBean.getCurrentBasket(); } @Override public String addItem (final Product product) { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("addItem: product={0} - CALLED!", product)); - // product should not be null if (null == product) { // Abort here @@ -104,9 +101,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi // Is orderedAmount set? if (this.getOrderedAmount() == null) { - // Trace message - //* NOISY-DEBUG: */ System.out.println("addItem: orderedAmount not specified, returning null ... - EXIT!"); - // No orderedAmount specified?! return null; } @@ -124,9 +118,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi // Remove orderedAmount this.setOrderedAmount(null); - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("addItem: item {0} - has been added to basket. - EXIT!", item)); - // Added return "item_added"; //NOI18N } catch (final BasketItemAlreadyAddedException ex) { @@ -137,24 +128,15 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi @Override public List allItems () { - // Trace message - //* NOISY-DEBUG: */ System.out.println("allItems: CALLED!"); - // Deligate to basket instance List list = this.basket.getAll(); - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("allItems: list={0} - EXIT!", list)); - // Return it return list; } @Override public Float calculateCurrentItemPrice () { - // Trace message - //* NOISY-DEBUG: */ System.out.println("calculateCurrentItemPrice: CALLED!"); - // Is the current item/amount set? if (this.getCurrentItem() == null) { // Current item is null @@ -170,18 +152,12 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi // Caculate item's price Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount()); - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice)); - // Return it return totalPrice; } @Override public Float calculateItemPrice (final AddableBasketItem item) { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item)); - // item must not be null if (null == item) { // Abort here @@ -197,18 +173,12 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount()); } - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice)); - // Return it return totalPrice; } @Override public Float calculateTotalPrice () { - // Trace message - //* NOISY-DEBUG: */ System.out.println("calculateTotalPrice: CALLED!"); - // Init total price Float totalPrice = 0.0f; @@ -221,27 +191,12 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi } } - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice)); - // Return final sum return totalPrice; } - @Override - public void clear () { - // Clear bean as well - this.getBasketBean().clear(); - - // Deligate to basket instance - this.basket.clear(); - } - @Override public String doChangeItem (final AddableBasketItem item) { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doChangeItem: item={0} - CALLED!", item)); - // item shall not be null if (null == item) { // Abort here @@ -261,9 +216,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi } } - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doChangeItem: targetPage={0} - EXIT!", targetPage)); - // Return page return targetPage; } @@ -280,9 +232,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi @Override public Long getItemAmount (final Product product) { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemAmount: product={0} - CALLED!", product)); - // product should not be null if (null == product) { // Abort here @@ -294,9 +243,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi // 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 @@ -308,9 +254,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi } } - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount)); - // Return it return itemAmount; } @@ -351,9 +294,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi @Override public boolean isProductAdded (final Product product) { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("isProductAdded: product={0} - EXIT!", product)); - // Must not be null if (null == product) { // Abort here @@ -363,38 +303,24 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi // 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 - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded)); - // Return status return isAdded; } @Override public String outputLastAddedItem () { - // Trace message - //* NOISY-DEBUG: */ System.out.println("outputLastAddedItem: CALLED!"); - // Default message String lastItem = ""; //NOI18N @@ -417,20 +343,40 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi } } - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem)); - // Return it return lastItem; } + @Override + public void afterCheckoutCompleted (@Observes final ObservableCheckoutCompletedEvent event) { + // Is all set? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getCustomer() == null) { + // Throw NPE again + throw new NullPointerException("event.customer is null"); + } else if (event.getCustomer().getCustomerId() == null) { + // Throw NPE again ... + throw new NullPointerException("event.customer.customerId is null"); + } else if (event.getCustomer().getCustomerId() < 0) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("event.customer.customerId={0} is not valid.", event.getCustomer().getCustomerId())); + } + + // Clear this method + this.clear(); + } + /** - * Getter for basket bean instance - *

- * @return Basket bean instance + * Clears this bean */ - private BasketSessionBeanRemote getBasketBean () { - return this.basketBean; + private void clear () { + // Clear bean as well + this.basketBean.clear(); + + // Deligate to basket instance + this.basket.clear(); } /** @@ -443,9 +389,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi * @return Item instance or null if not found */ private AddableBasketItem getItemFromProduct (final Product product) { - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product)); - // Product must not be null if (null == product) { // Abort here @@ -458,20 +401,11 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi // Create fake instance AddableBasketItem fake = new BasketItem(product); - // Debug message - //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake)); - // Get all items List list = this.basket.getAll(); - // 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)); - // item must not be null if (null == item) { // Abort here @@ -486,9 +420,6 @@ public class PizzaBasketWebSessionBean extends BasePizzaController implements Pi } } - // Trace message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem)); - // Return it return foundItem; } diff --git a/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionController.java index 473577bc..382558e1 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionController.java @@ -20,6 +20,7 @@ import java.io.Serializable; import java.util.List; import javax.ejb.Local; import org.mxchange.jproduct.model.product.Product; +import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent; import org.mxchange.jshopcore.model.basket.AddableBasketItem; /** @@ -30,6 +31,13 @@ import org.mxchange.jshopcore.model.basket.AddableBasketItem; @Local public interface PizzaBasketWebSessionController extends Serializable { + /** + * Observes events being fired when a checkout is completed by user + *

+ * @param event Event being fired + */ + void afterCheckoutCompleted (final ObservableCheckoutCompletedEvent event); + /** * Adds given product instance to basket by adding amount from form data to * it. @@ -81,11 +89,6 @@ public interface PizzaBasketWebSessionController extends Serializable { */ String doChangeItem (final AddableBasketItem item); - /** - * Clears this basket instance - */ - void clear (); - /** * Getter for item amount property *

diff --git a/src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java index c0fb6a1a..91680b02 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java @@ -20,6 +20,8 @@ import java.util.List; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Event; +import javax.enterprise.inject.Any; import javax.faces.FacesException; import javax.inject.Inject; import javax.inject.Named; @@ -34,6 +36,8 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jcustomercore.model.customer.Customer; +import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent; +import org.mxchange.jshopcore.events.ShopCheckoutCompletedEvent; import org.mxchange.jshopcore.model.basket.AddableBasketItem; import org.mxchange.jshopcore.wrapper.CheckoutWrapper; import org.mxchange.jshopcore.wrapper.WrapableCheckout; @@ -64,6 +68,13 @@ public class PizzaCheckoutWebSessionBean extends BasePizzaController implements @Inject private PizzaBasketWebSessionController basketController; + /** + * Event fired when a checkout was completed by the user + */ + @Inject + @Any + private Event checkoutCompletedEvent; + /** * Connection */ @@ -130,9 +141,6 @@ public class PizzaCheckoutWebSessionBean extends BasePizzaController implements @Override public String doCheckout () { - // Trace message - //* NOISY-DEBUG: */ System.out.println("doCheckout: CALLED!"); - // Are the beans set? if (null == this.basketController) { // Abort here @@ -144,15 +152,9 @@ public class PizzaCheckoutWebSessionBean extends BasePizzaController implements // Are at least the required fields set? if (!this.contactController.isRequiredPersonalDataSet()) { - // Trace message - //* NOISY-DEBUG: */ System.out.println("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!"); - // Not set, should not happen return "checkout2"; //NOI18N } else if (this.basketController.isEmpty()) { - // Trace message - //* NOISY-DEBUG: */ System.out.println("doCheckout: basket is empty, returning empty_basket ... - EXIT!"); - // Nothing to order return "empty_basket"; //NOI18N } @@ -160,15 +162,9 @@ public class PizzaCheckoutWebSessionBean extends BasePizzaController implements // Create customer instance this.setCustomer(this.customerController.createCustomerInstance()); - // Debug message - //this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer())); - // Get ordered list List list = this.basketController.allItems(); - // Debug message - //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doCheckout: list={0}", list)); - // Construct container WrapableCheckout wrapper = new CheckoutWrapper(); wrapper.setCustomer(this.getCustomer()); @@ -186,11 +182,8 @@ public class PizzaCheckoutWebSessionBean extends BasePizzaController implements return "jms_failed"; //NOI18N } - // Clear basket - this.basketController.clear(); - - // Set customer in receipt controller for verification - this.receiptController.setCustomer(this.getCustomer()); + // Fire event + this.checkoutCompletedEvent.fire(new ShopCheckoutCompletedEvent(this.getCustomer())); // All fine return "checkout_done"; //NOI18N diff --git a/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionBean.java index 8fbf106e..f7103359 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionBean.java @@ -16,13 +16,16 @@ */ package org.mxchange.pizzaapplication.beans.receipt; +import java.text.MessageFormat; import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Observes; import javax.faces.FacesException; import javax.inject.Named; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jcustomercore.model.customer.Customer; +import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent; import org.mxchange.jshopcore.model.receipt.ReceiptBeanRemote; import org.mxchange.pizzaapplication.beans.BasePizzaController; @@ -66,18 +69,47 @@ public class PizzaReceiptWebSessionBean extends BasePizzaController implements P } } + @Override + public void afterCheckoutCompleted (@Observes final ObservableCheckoutCompletedEvent event) { + // Is all set? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getCustomer() == null) { + // Throw NPE again + throw new NullPointerException("event.customer is null"); + } else if (event.getCustomer().getCustomerId() == null) { + // Throw NPE again ... + throw new NullPointerException("event.customer.customerId is null"); + } else if (event.getCustomer().getCustomerId() < 0) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("event.customer.customerId={0} is not valid.", event.getCustomer().getCustomerId())); + } + + // Set customer + this.setCustomer(event.getCustomer()); + } + @Override public String fetchAccessKey () { return this.receiptBean.fetchAccessKey(this.getCustomer()); } - @Override - public Customer getCustomer () { + /** + * Getter for customer instance + *

+ * @return Customer instance + */ + private Customer getCustomer () { return this.customer; } - @Override - public void setCustomer (final Customer customer) { + /** + * Setter for customer instance + *

+ * @param customer Customer instance + */ + private void setCustomer (final Customer customer) { this.customer = customer; } diff --git a/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionController.java index e0acee27..cf1b7dc8 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/receipt/PizzaReceiptWebSessionController.java @@ -17,7 +17,7 @@ package org.mxchange.pizzaapplication.beans.receipt; import java.io.Serializable; -import org.mxchange.jcustomercore.model.customer.Customer; +import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent; /** * An interface for the shop @@ -27,24 +27,17 @@ import org.mxchange.jcustomercore.model.customer.Customer; public interface PizzaReceiptWebSessionController extends Serializable { /** - * Fetches last access key for given customer instance - *

- * @return Access key to receipt - */ - String fetchAccessKey (); - - /** - * Getter for customer instamce + * Observes events being fired when a checkout is completed by user *

- * @return Customer instance + * @param event Event being fired */ - Customer getCustomer (); + void afterCheckoutCompleted (final ObservableCheckoutCompletedEvent event); /** - * Setter for customer instamce + * Fetches last access key for given customer instance *

- * @param customer Customer instance + * @return Access key to receipt */ - void setCustomer (final Customer customer); + String fetchAccessKey (); } -- 2.39.5