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 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;
64 private AddableBasketItem currentItem;
66 /////////////////////// Properties /////////////////////
68 * Ordered orderedAmount
70 private Long orderedAmount;
75 public BasketWebSessionBean () {
76 // Get new application instance
77 this.basket = new ShopBasket();
80 // Get initial context
81 Context context = new InitialContext();
84 this.basketBean = (BasketSessionBeanRemote) context.lookup("ejb/stateless-basket"); //NOI18N
85 } catch (final NamingException ex) {
87 throw new FaceletException(ex);
92 public String addItem (final Product product) {
94 //this.getLogger().logTrace(MessageFormat.format("addItem: product={0} - CALLED!", product));
96 // product should not be null
97 if (null == product) {
99 throw new NullPointerException("product is null");
102 // Generate item instance
103 AddableBasketItem item = new BasketItem(product, this.getOrderedAmount());
105 // Is orderedAmount set?
106 if (this.getOrderedAmount() == null) {
108 //this.getLogger().logTrace("addItem: orderedAmount not specified, returning null ... - EXIT!");
110 // No orderedAmount specified?!
115 // item should not be null
118 throw new NullPointerException("item is null"); //NOI18N
122 this.basket.addItem(item);
124 // Remove orderedAmount
125 this.setOrderedAmount(null);
128 //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));
152 public Float calculateCurrentItemPrice () {
154 //this.getLogger().logTrace("calculateCurrentItemPrice: CALLED!");
156 // Is the current item/amount set?
157 if (this.getCurrentItem() == null) {
158 // Current item is null
159 throw new NullPointerException("currentItem is null"); //NOI18N
160 } else if (this.getCurrentItem().getItemProduct() == null) {
162 throw new NullPointerException("currentItem.product is null"); //NOI18N
163 } else if (this.getCurrentItem().getOrderedAmount() == null) {
165 throw new NullPointerException("currentItem.amount is null"); //NOI18N
168 // Caculate item's price
169 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
172 //this.getLogger().logTrace(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
178 public Float calculateItemPrice (final AddableBasketItem item) {
180 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
182 // item must not be null
185 throw new NullPointerException("item is null");
189 Float totalPrice = 0.0f;
192 if (item.isProductType()) {
193 // Caculate item's price
194 totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
198 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
204 public Float calculateTotalPrice () {
206 //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
209 Float totalPrice = 0.0f;
211 // Iterate over all items
212 for (final AddableBasketItem item : this.allItems()) {
213 // Is the item a product?
214 if (item.isProductType()) {
215 // Calculate single price and add it
216 totalPrice += this.calculateItemPrice(item);
221 //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
227 public String changeItem (final AddableBasketItem item) {
229 //this.getLogger().logTrace(MessageFormat.format("changeItem: item={0} - CALLED!", item));
231 // item shall not be null
234 throw new NullPointerException("item is null");
237 // Default is not found
238 String targetPage = "item_not_changed"; //NOI18N
240 // Lookup item in basket
241 for (final AddableBasketItem basketItem : this.allItems()) {
243 if (basketItem.equals(item)) {
244 // Found it, so allow redirect to proper page
245 targetPage = "basket"; //NOI18N
251 //this.getLogger().logTrace(MessageFormat.format("changeItem: targetPage={0} - EXIT!", targetPage));
257 public void clear () {
258 // @TODO Also clear EJB
259 // Deligate to basket instance
264 public AddableBasketItem getCurrentItem () {
265 return this.currentItem;
269 public void setCurrentItem (final AddableBasketItem currentItem) {
270 this.currentItem = currentItem;
274 public Long getItemAmount (final Product product) {
276 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
278 // product should not be null
279 if (null == product) {
281 throw new NullPointerException("product is null");
284 // Initial value is zero
285 Long itemAmount = 0L;
288 for (final AddableBasketItem item : this.allItems()) {
290 //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
292 // Is this product instance and same?
295 throw new NullPointerException("item is null");
296 } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
298 itemAmount = item.getOrderedAmount();
304 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
310 public AddableBasketItem getLast () {
311 // Deligate to basket instance
312 return this.basket.getLast();
316 public int getLastNumRows () {
317 // Deligate to basket instance
318 return this.basket.getLastNumRows();
322 public Long getOrderedAmount () {
323 return this.orderedAmount;
327 public void setOrderedAmount (final Long orderedAmount) {
328 this.orderedAmount = orderedAmount;
332 public boolean hasItems () {
333 // Call above and invert it
334 return (!this.isEmpty());
338 public boolean isEmpty () {
339 // Deligate to basket instance
340 return this.basket.isEmpty();
344 public boolean isProductAdded (final Product product) {
346 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
349 if (null == product) {
351 throw new NullPointerException("product is null"); //NOI18N
354 // Generate fake instance
355 AddableBasketItem fake = new BasketItem(product);
358 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
360 boolean isAdded = this.basket.isAdded(fake);
363 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
367 AddableBasketItem item = this.getItemFromProduct(product);
370 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
371 // Set this as current item
372 this.setCurrentItem(item);
376 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
382 public String outputLastAddedItem () {
384 //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
387 String lastItem = ""; //NOI18N
390 AddableBasketItem item = this.getLast();
393 if (item instanceof AddableBasketItem) {
395 switch (item.getItemType()) {
396 case "product": // Sellable product //NOI18N
397 assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
400 lastItem = item.getItemProduct().getProductTitle();
403 default: // Not supported
404 throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
409 //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
415 * Getter for basket bean instance
417 * @return Basket bean instance
419 private BasketSessionBeanRemote getBasketBean () {
420 return this.basketBean;
424 * Somewhat getter for an item instance from given product instance. This
425 * method returns null if no item was found to given product. The product is
426 * found by checking it's id and itemType=product
428 * @param product Product instance
430 * @return Item instance or null if not found
432 private AddableBasketItem getItemFromProduct (final Product product) {
434 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
436 // Product must not be null
437 if (null == product) {
439 throw new NullPointerException("product is null"); //NOI18N
442 // Create item instance
443 AddableBasketItem foundItem = null;
445 // Create fake instance
446 AddableBasketItem fake = new BasketItem(product);
449 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
451 List<AddableBasketItem> list = this.basket.getAll();
454 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
456 for (final AddableBasketItem item : list) {
458 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
460 // item must not be null
463 throw new NullPointerException("item is null"); //NOI18N
467 if (item.equals(fake)) {
468 // Set found item and abort look
475 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));