2 * Copyright (C) 2016 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 BasketWebSessionBean implements BasketWebSessionController {
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;
63 private AddableBasketItem currentItem;
65 /////////////////////// Properties /////////////////////
67 * Ordered orderedAmount
69 private Long orderedAmount;
74 public BasketWebSessionBean () {
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));
129 return "item_added"; //NOI18N
130 } catch (final BasketItemAlreadyAddedException ex) {
131 // Throw unchecked exception
132 throw new FacesException(ex);
137 public List<AddableBasketItem> allItems () {
139 //this.getLogger().logTrace("allItems: CALLED!");
141 // Deligate to basket instance
142 List<AddableBasketItem> list = this.basket.getAll();
145 //this.getLogger().logTrace(MessageFormat.format("allItems: list={0} - EXIT!", list));
151 public Float calculateCurrentItemPrice () {
153 //this.getLogger().logTrace("calculateCurrentItemPrice: CALLED!");
155 // Is the current item/amount set?
156 if (this.getCurrentItem() == null) {
157 // Current item is null
158 throw new NullPointerException("currentItem is null"); //NOI18N
159 } else if (this.getCurrentItem().getItemProduct() == null) {
161 throw new NullPointerException("currentItem.product is null"); //NOI18N
162 } else if (this.getCurrentItem().getOrderedAmount() == null) {
164 throw new NullPointerException("currentItem.amount is null"); //NOI18N
167 // Caculate item's price
168 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
171 //this.getLogger().logTrace(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
177 public Float calculateItemPrice (final AddableBasketItem item) {
179 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
181 // item must not be null
184 throw new NullPointerException("item is null");
188 Float totalPrice = 0.0f;
191 if (item.isProductType()) {
192 // Caculate item's price
193 totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
197 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
203 public Float calculateTotalPrice () {
205 //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
208 Float totalPrice = 0.0f;
210 // Iterate over all items
211 for (final AddableBasketItem item : this.allItems()) {
212 // Is the item a product?
213 if (item.isProductType()) {
214 // Calculate single price and add it
215 totalPrice += this.calculateItemPrice(item);
220 //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
226 public String doChangeItem (final AddableBasketItem item) {
228 //this.getLogger().logTrace(MessageFormat.format("doChangeItem: item={0} - CALLED!", item));
230 // item shall not be null
233 throw new NullPointerException("item is null");
236 // Default is not found
237 String targetPage = "item_not_changed"; //NOI18N
239 // Lookup item in basket
240 for (final AddableBasketItem basketItem : this.allItems()) {
242 if (basketItem.equals(item)) {
243 // Found it, so allow redirect to proper page
244 targetPage = "basket"; //NOI18N
250 //this.getLogger().logTrace(MessageFormat.format("doChangeItem: targetPage={0} - EXIT!", targetPage));
256 public void clear () {
257 // @TODO Also clear EJB
258 // Deligate to basket instance
263 public AddableBasketItem getCurrentItem () {
264 return this.currentItem;
268 public void setCurrentItem (final AddableBasketItem currentItem) {
269 this.currentItem = currentItem;
273 public Long getItemAmount (final Product product) {
275 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
277 // product should not be null
278 if (null == product) {
280 throw new NullPointerException("product is null");
283 // Initial value is zero
284 Long itemAmount = 0L;
287 for (final AddableBasketItem item : this.allItems()) {
289 //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
291 // Is this product instance and same?
294 throw new NullPointerException("item is null");
295 } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
297 itemAmount = item.getOrderedAmount();
303 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
309 public AddableBasketItem getLast () {
310 // Deligate to basket instance
311 return this.basket.getLast();
315 public int getLastNumRows () {
316 // Deligate to basket instance
317 return this.basket.getLastNumRows();
321 public Long getOrderedAmount () {
322 return this.orderedAmount;
326 public void setOrderedAmount (final Long orderedAmount) {
327 this.orderedAmount = orderedAmount;
331 public boolean hasItems () {
332 // Call above and invert it
333 return (!this.isEmpty());
337 public boolean isEmpty () {
338 // Deligate to basket instance
339 return this.basket.isEmpty();
343 public boolean isProductAdded (final Product product) {
345 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
348 if (null == product) {
350 throw new NullPointerException("product is null"); //NOI18N
353 // Generate fake instance
354 AddableBasketItem fake = new BasketItem(product);
357 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
359 boolean isAdded = this.basket.isAdded(fake);
362 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
366 AddableBasketItem item = this.getItemFromProduct(product);
369 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
370 // Set this as current item
371 this.setCurrentItem(item);
375 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
381 public String outputLastAddedItem () {
383 //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
386 String lastItem = ""; //NOI18N
389 AddableBasketItem item = this.getLast();
392 if (item instanceof AddableBasketItem) {
394 switch (item.getItemType()) {
395 case "product": // Sellable product //NOI18N
396 assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
399 lastItem = item.getItemProduct().getProductTitle();
402 default: // Not supported
403 throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
408 //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
414 * Getter for basket bean instance
416 * @return Basket bean instance
418 private BasketSessionBeanRemote getBasketBean () {
419 return this.basketBean;
423 * Somewhat getter for an item instance from given product instance. This
424 * method returns null if no item was found to given product. The product is
425 * found by checking it's id and itemType=product
427 * @param product Product instance
429 * @return Item instance or null if not found
431 private AddableBasketItem getItemFromProduct (final Product product) {
433 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
435 // Product must not be null
436 if (null == product) {
438 throw new NullPointerException("product is null"); //NOI18N
441 // Create item instance
442 AddableBasketItem foundItem = null;
444 // Create fake instance
445 AddableBasketItem fake = new BasketItem(product);
448 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
450 List<AddableBasketItem> list = this.basket.getAll();
453 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
455 for (final AddableBasketItem item : list) {
457 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
459 // item must not be null
462 throw new NullPointerException("item is null"); //NOI18N
466 if (item.equals(fake)) {
467 // Set found item and abort look
474 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));