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;
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;
*/
private AddableBasketItem currentItem;
- /////////////////////// Properties /////////////////////
/**
* Ordered orderedAmount
*/
* Default constructor
*/
public PizzaBasketWebSessionBean () {
- // Get new application instance
- this.basket = new ShopBasket();
-
try {
// Get initial context
Context context = new InitialContext();
// 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
// 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;
}
// 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) {
@Override
public List<AddableBasketItem> allItems () {
- // Trace message
- //* NOISY-DEBUG: */ System.out.println("allItems: CALLED!");
-
// Deligate to basket instance
List<AddableBasketItem> 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
// 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
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;
}
}
- // 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
}
}
- // Trace message
- //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doChangeItem: targetPage={0} - EXIT!", targetPage));
-
// Return page
return targetPage;
}
@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
// 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
}
}
- // Trace message
- //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
-
// Return it
return itemAmount;
}
@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
// 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
}
}
- // 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
- * <p>
- * @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();
}
/**
* @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
// Create fake instance
AddableBasketItem fake = new BasketItem(product);
- // Debug message
- //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
-
// Get all items
List<AddableBasketItem> 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
}
}
- // Trace message
- //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));
-
// Return it
return foundItem;
}
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;
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;
@Inject
private PizzaBasketWebSessionController basketController;
+ /**
+ * Event fired when a checkout was completed by the user
+ */
+ @Inject
+ @Any
+ private Event<ObservableCheckoutCompletedEvent> checkoutCompletedEvent;
+
/**
* Connection
*/
@Override
public String doCheckout () {
- // Trace message
- //* NOISY-DEBUG: */ System.out.println("doCheckout: CALLED!");
-
// Are the beans set?
if (null == this.basketController) {
// Abort here
// 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
}
// Create customer instance
this.setCustomer(this.customerController.createCustomerInstance());
- // Debug message
- //this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer()));
-
// Get ordered list
List<AddableBasketItem> 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());
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
*/
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;
}
}
+ @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
+ * <p>
+ * @return Customer instance
+ */
+ private Customer getCustomer () {
return this.customer;
}
- @Override
- public void setCustomer (final Customer customer) {
+ /**
+ * Setter for customer instance
+ * <p>
+ * @param customer Customer instance
+ */
+ private void setCustomer (final Customer customer) {
this.customer = customer;
}