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.annotation.PostConstruct;
22 import javax.enterprise.context.SessionScoped;
23 import javax.faces.FacesException;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcoreee.beans.BaseFrameworkBean;
30 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
31 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
32 import org.mxchange.jshopcore.model.basket.Basket;
33 import org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote;
34 import org.mxchange.jshopcore.model.basket.ShopBasket;
35 import org.mxchange.jshopcore.model.basket.items.BasketItem;
36 import org.mxchange.jshopcore.model.product.Product;
39 * A bean for the basket
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("basketController")
45 public class BasketWebBean extends BaseFrameworkBean implements BasketWebController {
50 private static final long serialVersionUID = 5_476_347_320_198L;
53 * Instance of wrapped basket
55 private final Basket<AddableBasketItem> basket;
60 private final BasketSessionBeanRemote basketBean;
62 /////////////////////// Properties /////////////////////
71 private AddableBasketItem currentItem;
76 public BasketWebBean () {
77 // Get new application instance
78 this.basket = new ShopBasket();
81 // Get initial context
82 Context context = new InitialContext();
85 this.basketBean = (BasketSessionBeanRemote) context.lookup("ejb/stateless-basket"); //NOI18N
86 } catch (final NamingException ex) {
88 throw new FaceletException(ex);
93 public String addItem (final Product product) {
95 this.getLogger().logTrace(MessageFormat.format("addItem: product={0} - CALLED!", product));
97 // product should not be null
98 if (null == product) {
100 throw new NullPointerException("product is null");
103 // Generate item instance
104 AddableBasketItem item = new BasketItem(product, this.getAmount());
107 if (this.getAmount() == null) {
109 this.getLogger().logTrace("addItem: amount not specified, returning null ... - EXIT!");
111 // No amount specified?!
116 // item should not be null
119 throw new NullPointerException("item is null"); //NOI18N
123 this.basket.addItem(item);
126 this.setAmount(null);
129 this.getLogger().logTrace(MessageFormat.format("addItem: item {0} - has been added to basket. - EXIT!", item));
132 return "item_added"; //NOI18N
133 } catch (final BasketItemAlreadyAddedException ex) {
134 // Throw unchecked exception
135 throw new FacesException(ex);
140 public List<AddableBasketItem> allItems () {
142 this.getLogger().logTrace("allItems: CALLED!");
144 // Deligate to basket instance
145 List<AddableBasketItem> list = this.basket.getAll();
148 this.getLogger().logTrace(MessageFormat.format("allItems: list={0} - EXIT!", list));
155 public Float calculateCurrentItemPrice () {
157 this.getLogger().logTrace("calculateCurrentItemPrice: CALLED!");
159 // Is the current item/amount set?
160 if (this.getCurrentItem() == null) {
161 // Current item is null
162 throw new NullPointerException("currentItem is null"); //NOI18N
163 } else if (this.getCurrentItem().getProduct() == null) {
165 throw new NullPointerException("currentItem.product is null"); //NOI18N
166 } else if (this.getCurrentItem().getAmount() == null) {
168 throw new NullPointerException("currentItem.amount is null"); //NOI18N
171 // Caculate item's price
172 Float totalPrice = (this.getCurrentItem().getProduct().getPrice() * this.getCurrentItem().getAmount());
175 this.getLogger().logTrace(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
182 public Float calculateItemPrice (final AddableBasketItem item) {
184 this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
186 // item must not be null
189 throw new NullPointerException("item is null");
193 Float totalPrice = 0.0f;
196 if (item.isProductType()) {
197 // Caculate item's price
198 totalPrice = (item.getProduct().getPrice() * item.getAmount());
202 this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
209 public Float calculateTotalPrice () {
211 this.getLogger().logTrace("calculateTotalPrice: CALLED!");
214 Float totalPrice = 0.0f;
216 // Iterate over all items
217 for (final AddableBasketItem item : this.allItems()) {
218 // Is the item a product?
219 if (item.isProductType()) {
220 // Calculate single price and add it
221 totalPrice += this.calculateItemPrice(item);
226 this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
233 public String changeItem (final AddableBasketItem item) {
235 this.getLogger().logTrace(MessageFormat.format("changeItem: item={0} - CALLED!", item));
237 // item shall not be null
240 throw new NullPointerException("item is null");
243 // Default is not found
244 String targetPage = "item_not_changed"; //NOI18N
246 // Lookup item in basket
247 for (final AddableBasketItem basketItem : this.allItems()) {
249 if (basketItem.equals(item)) {
250 // Found it, so allow redirect to proper page
251 targetPage = "basket"; //NOI18N
257 this.getLogger().logTrace(MessageFormat.format("changeItem: targetPage={0} - EXIT!", targetPage));
264 public void clear () {
265 // @TODO Also clear EJB
266 // Deligate to basket instance
271 public Long getAmount () {
276 public void setAmount (final Long amount) {
277 this.amount = amount;
281 public AddableBasketItem getCurrentItem () {
282 return this.currentItem;
286 public void setCurrentItem (final AddableBasketItem currentItem) {
287 this.currentItem = currentItem;
291 public Long getItemAmount (final Product product) {
293 this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
295 // product should not be null
296 if (null == product) {
298 throw new NullPointerException("product is null");
301 // Initial value is zero
302 Long itemAmount = 0L;
305 for (final AddableBasketItem item : this.allItems()) {
307 this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
309 // Is this product instance and same?
312 throw new NullPointerException("item is null");
313 } else if ((item.isProductType()) && (item.getProduct().equals(product))) {
315 itemAmount = item.getAmount();
321 this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
328 public AddableBasketItem getLast () {
329 // Deligate to basket instance
330 return this.basket.getLast();
334 public int getLastNumRows () {
335 // Deligate to basket instance
336 return this.basket.getLastNumRows();
340 public boolean hasItems () {
341 // Call above and invert it
342 return (!this.isEmpty());
346 public void init () {
347 // Call generic init first
352 public boolean isEmpty () {
353 // Deligate to basket instance
354 return this.basket.isEmpty();
358 public boolean isProductAdded (final Product product) {
360 this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
363 if (null == product) {
365 throw new NullPointerException("product is null"); //NOI18N
368 // Generate fake instance
369 AddableBasketItem fake = new BasketItem(product);
372 this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
375 boolean isAdded = this.basket.isAdded(fake);
378 this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
383 AddableBasketItem item = this.getItemFromProduct(product);
386 this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
388 // Set this as current item
389 this.setCurrentItem(item);
393 this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
400 public String outputLastAddedItem () {
402 this.getLogger().logTrace("outputLastAddedItem: CALLED!");
405 String lastItem = ""; //NOI18N
408 AddableBasketItem item = this.getLast();
411 if (item instanceof AddableBasketItem) {
413 switch (item.getItemType()) {
414 case "product": // Sellable product //NOI18N
415 assert (item.getProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
418 lastItem = item.getProduct().getTitle();
421 default: // Not supported
422 throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
427 this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
434 * Getter for basket bean instance
436 * @return Basket bean instance
438 private BasketSessionBeanRemote getBasketBean () {
439 return this.basketBean;
443 * Somewhat getter for an item instance from given product instance. This
444 * method returns null if no item was found to given product. The product is
445 * found by checking it's id and itemType=product
447 * @param product Product instance
448 * @return Item instance or null if not found
450 private AddableBasketItem getItemFromProduct (final Product product) {
452 this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
454 // Product must not be null
455 if (null == product) {
457 throw new NullPointerException("product is null"); //NOI18N
460 // Create item instance
461 AddableBasketItem foundItem = null;
463 // Create fake instance
464 AddableBasketItem fake = new BasketItem(product);
467 this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
470 List<AddableBasketItem> list = this.basket.getAll();
473 this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
476 for (final AddableBasketItem item : list) {
478 this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
480 // item must not be null
483 throw new NullPointerException("item is null"); //NOI18N
487 if (item.equals(fake)) {
488 // Set found item and abort look
495 this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));