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 Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (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 Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero 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;
35 import org.mxchange.pizzaapplication.beans.BasePizzaController;
38 * A bean for the basket
40 * @author Roland Haeder<roland@mxchange.org>
42 @Named ("basketController")
44 public class PizzaBasketWebSessionBean extends BasePizzaController implements PizzaBasketWebSessionController {
49 private static final long serialVersionUID = 5_476_347_320_198L;
52 * Instance of wrapped basket
54 private final Basket<AddableBasketItem> basket;
59 private final BasketSessionBeanRemote basketBean;
64 private AddableBasketItem currentItem;
66 /////////////////////// Properties /////////////////////
68 * Ordered orderedAmount
70 private Long orderedAmount;
75 public PizzaBasketWebSessionBean () {
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("java:global/jshop-ejb/basket!org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote"); //NOI18N
85 } catch (final NamingException ex) {
87 throw new FaceletException(ex);
92 public String addItem (final Product product) {
94 //* NOISY-DEBUG: */ System.out.println(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 //* NOISY-DEBUG: */ System.out.println("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 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("addItem: item {0} - has been added to basket. - EXIT!", item));
131 return "item_added"; //NOI18N
132 } catch (final BasketItemAlreadyAddedException ex) {
133 // Throw unchecked exception
134 throw new FacesException(ex);
139 public List<AddableBasketItem> allItems () {
141 //* NOISY-DEBUG: */ System.out.println("allItems: CALLED!");
143 // Deligate to basket instance
144 List<AddableBasketItem> list = this.basket.getAll();
147 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("allItems: list={0} - EXIT!", list));
154 public Float calculateCurrentItemPrice () {
156 //* NOISY-DEBUG: */ System.out.println("calculateCurrentItemPrice: CALLED!");
158 // Is the current item/amount set?
159 if (this.getCurrentItem() == null) {
160 // Current item is null
161 throw new NullPointerException("currentItem is null"); //NOI18N
162 } else if (this.getCurrentItem().getItemProduct() == null) {
164 throw new NullPointerException("currentItem.product is null"); //NOI18N
165 } else if (this.getCurrentItem().getOrderedAmount() == null) {
167 throw new NullPointerException("currentItem.amount is null"); //NOI18N
170 // Caculate item's price
171 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
174 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
181 public Float calculateItemPrice (final AddableBasketItem item) {
183 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
185 // item must not be null
188 throw new NullPointerException("item is null");
192 Float totalPrice = 0.0f;
195 if (item.isProductType()) {
196 // Caculate item's price
197 totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
201 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
208 public Float calculateTotalPrice () {
210 //* NOISY-DEBUG: */ System.out.println("calculateTotalPrice: CALLED!");
213 Float totalPrice = 0.0f;
215 // Iterate over all items
216 for (final AddableBasketItem item : this.allItems()) {
217 // Is the item a product?
218 if (item.isProductType()) {
219 // Calculate single price and add it
220 totalPrice += this.calculateItemPrice(item);
225 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
232 public void clear () {
233 // Clear bean as well
234 this.getBasketBean().clear();
236 // Deligate to basket instance
241 public String doChangeItem (final AddableBasketItem item) {
243 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doChangeItem: item={0} - CALLED!", item));
245 // item shall not be null
248 throw new NullPointerException("item is null");
251 // Default is not found
252 String targetPage = "item_not_changed"; //NOI18N
254 // Lookup item in basket
255 for (final AddableBasketItem basketItem : this.allItems()) {
257 if (basketItem.equals(item)) {
258 // Found it, so allow redirect to proper page
259 targetPage = "basket"; //NOI18N
265 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doChangeItem: targetPage={0} - EXIT!", targetPage));
272 public AddableBasketItem getCurrentItem () {
273 return this.currentItem;
277 public void setCurrentItem (final AddableBasketItem currentItem) {
278 this.currentItem = currentItem;
282 public Long getItemAmount (final Product product) {
284 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
286 // product should not be null
287 if (null == product) {
289 throw new NullPointerException("product is null");
292 // Initial value is zero
293 Long itemAmount = 0L;
296 for (final AddableBasketItem item : this.allItems()) {
298 //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
300 // Is this product instance and same?
303 throw new NullPointerException("item is null");
304 } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
306 itemAmount = item.getOrderedAmount();
312 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
319 public AddableBasketItem getLast () {
320 // Deligate to basket instance
321 return this.basket.getLast();
325 public int getLastNumRows () {
326 // Deligate to basket instance
327 return this.basket.size();
331 public Long getOrderedAmount () {
332 return this.orderedAmount;
336 public void setOrderedAmount (final Long orderedAmount) {
337 this.orderedAmount = orderedAmount;
341 public boolean hasItems () {
342 // Call above and invert it
343 return (!this.isEmpty());
347 public boolean isEmpty () {
348 // Deligate to basket instance
349 return this.basket.isEmpty();
353 public boolean isProductAdded (final Product product) {
355 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
358 if (null == product) {
360 throw new NullPointerException("product is null"); //NOI18N
363 // Generate fake instance
364 AddableBasketItem fake = new BasketItem(product);
367 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
370 boolean isAdded = this.basket.isAdded(fake);
373 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
378 AddableBasketItem item = this.getItemFromProduct(product);
381 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
382 // Set this as current item
383 this.setCurrentItem(item);
387 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
394 public String outputLastAddedItem () {
396 //* NOISY-DEBUG: */ System.out.println("outputLastAddedItem: CALLED!");
399 String lastItem = ""; //NOI18N
402 AddableBasketItem item = this.getLast();
405 if (item instanceof AddableBasketItem) {
407 switch (item.getItemType()) {
408 case "product": // Sellable product //NOI18N
409 assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
412 lastItem = item.getItemProduct().getProductTitle();
415 default: // Not supported
416 throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
421 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
428 * Getter for basket bean instance
430 * @return Basket bean instance
432 private BasketSessionBeanRemote getBasketBean () {
433 return this.basketBean;
437 * Somewhat getter for an item instance from given product instance. This
438 * method returns null if no item was found to given product. The product is
439 * found by checking it's id and itemType=product
441 * @param product Product instance
443 * @return Item instance or null if not found
445 private AddableBasketItem getItemFromProduct (final Product product) {
447 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
449 // Product must not be null
450 if (null == product) {
452 throw new NullPointerException("product is null"); //NOI18N
455 // Create item instance
456 AddableBasketItem foundItem = null;
458 // Create fake instance
459 AddableBasketItem fake = new BasketItem(product);
462 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
465 List<AddableBasketItem> list = this.basket.getAll();
468 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
471 for (final AddableBasketItem item : list) {
473 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
475 // item must not be null
478 throw new NullPointerException("item is null"); //NOI18N
482 if (item.equals(fake)) {
483 // Set found item and abort look
490 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));