2 * Copyright (C) 2015 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.beans.basket;
19 import java.text.MessageFormat;
20 import java.util.List;
21 import javax.enterprise.context.SessionScoped;
22 import javax.faces.FacesException;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Named;
25 import javax.naming.Context;
26 import javax.naming.InitialContext;
27 import javax.naming.NamingException;
28 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
29 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
30 import org.mxchange.jshopcore.model.basket.Basket;
31 import org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote;
32 import org.mxchange.jshopcore.model.basket.ShopBasket;
33 import org.mxchange.jshopcore.model.basket.items.BasketItem;
34 import org.mxchange.jshopcore.model.product.Product;
37 * A bean for the basket
39 * @author Roland Haeder<roland@mxchange.org>
41 @Named ("basketController")
43 public class BasketWebBean implements BasketWebController {
48 private static final long serialVersionUID = 5_476_347_320_198L;
51 * Instance of wrapped basket
53 private final Basket<AddableBasketItem> basket;
58 private final BasketSessionBeanRemote basketBean;
60 /////////////////////// Properties /////////////////////
62 * Ordered orderedAmount
64 private Long orderedAmount;
69 private AddableBasketItem currentItem;
74 public BasketWebBean () {
75 // Get new application instance
76 this.basket = new ShopBasket();
79 // Get initial context
80 Context context = new InitialContext();
83 this.basketBean = (BasketSessionBeanRemote) context.lookup("ejb/stateless-basket"); //NOI18N
84 } catch (final NamingException ex) {
86 throw new FaceletException(ex);
91 public String addItem (final Product product) {
93 //this.getLogger().logTrace(MessageFormat.format("addItem: product={0} - CALLED!", product));
95 // product should not be null
96 if (null == product) {
98 throw new NullPointerException("product is null");
101 // Generate item instance
102 AddableBasketItem item = new BasketItem(product, this.getOrderedAmount());
104 // Is orderedAmount set?
105 if (this.getOrderedAmount() == null) {
107 //this.getLogger().logTrace("addItem: orderedAmount not specified, returning null ... - EXIT!");
109 // No orderedAmount specified?!
114 // item should not be null
117 throw new NullPointerException("item is null"); //NOI18N
121 this.basket.addItem(item);
123 // Remove orderedAmount
124 this.setOrderedAmount(null);
127 //this.getLogger().logTrace(MessageFormat.format("addItem: item {0} - has been added to basket. - EXIT!", item));
130 return "item_added"; //NOI18N
131 } catch (final BasketItemAlreadyAddedException ex) {
132 // Throw unchecked exception
133 throw new FacesException(ex);
138 public List<AddableBasketItem> allItems () {
140 //this.getLogger().logTrace("allItems: CALLED!");
142 // Deligate to basket instance
143 List<AddableBasketItem> list = this.basket.getAll();
146 //this.getLogger().logTrace(MessageFormat.format("allItems: list={0} - EXIT!", list));
153 public Float calculateCurrentItemPrice () {
155 //this.getLogger().logTrace("calculateCurrentItemPrice: CALLED!");
157 // Is the current item/amount set?
158 if (this.getCurrentItem() == null) {
159 // Current item is null
160 throw new NullPointerException("currentItem is null"); //NOI18N
161 } else if (this.getCurrentItem().getItemProduct() == null) {
163 throw new NullPointerException("currentItem.product is null"); //NOI18N
164 } else if (this.getCurrentItem().getOrderedAmount() == null) {
166 throw new NullPointerException("currentItem.amount is null"); //NOI18N
169 // Caculate item's price
170 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
173 //this.getLogger().logTrace(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
180 public Float calculateItemPrice (final AddableBasketItem item) {
182 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
184 // item must not be null
187 throw new NullPointerException("item is null");
191 Float totalPrice = 0.0f;
194 if (item.isProductType()) {
195 // Caculate item's price
196 totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
200 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
207 public Float calculateTotalPrice () {
209 //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
212 Float totalPrice = 0.0f;
214 // Iterate over all items
215 for (final AddableBasketItem item : this.allItems()) {
216 // Is the item a product?
217 if (item.isProductType()) {
218 // Calculate single price and add it
219 totalPrice += this.calculateItemPrice(item);
224 //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
231 public String changeItem (final AddableBasketItem item) {
233 //this.getLogger().logTrace(MessageFormat.format("changeItem: item={0} - CALLED!", item));
235 // item shall not be null
238 throw new NullPointerException("item is null");
241 // Default is not found
242 String targetPage = "item_not_changed"; //NOI18N
244 // Lookup item in basket
245 for (final AddableBasketItem basketItem : this.allItems()) {
247 if (basketItem.equals(item)) {
248 // Found it, so allow redirect to proper page
249 targetPage = "basket"; //NOI18N
255 //this.getLogger().logTrace(MessageFormat.format("changeItem: targetPage={0} - EXIT!", targetPage));
262 public void clear () {
263 // @TODO Also clear EJB
264 // Deligate to basket instance
269 public Long getOrderedAmount () {
270 return this.orderedAmount;
274 public void setOrderedAmount (final Long orderedAmount) {
275 this.orderedAmount = orderedAmount;
279 public AddableBasketItem getCurrentItem () {
280 return this.currentItem;
284 public void setCurrentItem (final AddableBasketItem currentItem) {
285 this.currentItem = currentItem;
289 public Long getItemAmount (final Product product) {
291 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
293 // product should not be null
294 if (null == product) {
296 throw new NullPointerException("product is null");
299 // Initial value is zero
300 Long itemAmount = 0L;
303 for (final AddableBasketItem item : this.allItems()) {
305 //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
307 // Is this product instance and same?
310 throw new NullPointerException("item is null");
311 } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
313 itemAmount = item.getOrderedAmount();
319 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
326 public AddableBasketItem getLast () {
327 // Deligate to basket instance
328 return this.basket.getLast();
332 public int getLastNumRows () {
333 // Deligate to basket instance
334 return this.basket.getLastNumRows();
338 public boolean hasItems () {
339 // Call above and invert it
340 return (!this.isEmpty());
344 public boolean isEmpty () {
345 // Deligate to basket instance
346 return this.basket.isEmpty();
350 public boolean isProductAdded (final Product product) {
352 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
355 if (null == product) {
357 throw new NullPointerException("product is null"); //NOI18N
360 // Generate fake instance
361 AddableBasketItem fake = new BasketItem(product);
364 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
367 boolean isAdded = this.basket.isAdded(fake);
370 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
375 AddableBasketItem item = this.getItemFromProduct(product);
378 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
380 // Set this as current item
381 this.setCurrentItem(item);
385 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
392 public String outputLastAddedItem () {
394 //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
397 String lastItem = ""; //NOI18N
400 AddableBasketItem item = this.getLast();
403 if (item instanceof AddableBasketItem) {
405 switch (item.getItemType()) {
406 case "product": // Sellable product //NOI18N
407 assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
410 lastItem = item.getItemProduct().getProductTitle();
413 default: // Not supported
414 throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
419 //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
426 * Getter for basket bean instance
428 * @return Basket bean instance
430 private BasketSessionBeanRemote getBasketBean () {
431 return this.basketBean;
435 * Somewhat getter for an item instance from given product instance. This
436 * method returns null if no item was found to given product. The product is
437 * found by checking it's id and itemType=product
439 * @param product Product instance
440 * @return Item instance or null if not found
442 private AddableBasketItem getItemFromProduct (final Product product) {
444 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
446 // Product must not be null
447 if (null == product) {
449 throw new NullPointerException("product is null"); //NOI18N
452 // Create item instance
453 AddableBasketItem foundItem = null;
455 // Create fake instance
456 AddableBasketItem fake = new BasketItem(product);
459 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
462 List<AddableBasketItem> list = this.basket.getAll();
465 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
468 for (final AddableBasketItem item : list) {
470 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
472 // item must not be null
475 throw new NullPointerException("item is null"); //NOI18N
479 if (item.equals(fake)) {
480 // Set found item and abort look
487 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));