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;
37 * A bean for the basket
39 * @author Roland Haeder<roland@mxchange.org>
41 @Named ("basketController")
43 public class PizzaBasketWebSessionBean implements PizzaBasketWebSessionController {
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 PizzaBasketWebSessionBean () {
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("java:global/jshop-ejb/basket!org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote"); //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));
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));
179 public Float calculateItemPrice (final AddableBasketItem item) {
181 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
183 // item must not be null
186 throw new NullPointerException("item is null");
190 Float totalPrice = 0.0f;
193 if (item.isProductType()) {
194 // Caculate item's price
195 totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
199 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
205 public Float calculateTotalPrice () {
207 //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
210 Float totalPrice = 0.0f;
212 // Iterate over all items
213 for (final AddableBasketItem item : this.allItems()) {
214 // Is the item a product?
215 if (item.isProductType()) {
216 // Calculate single price and add it
217 totalPrice += this.calculateItemPrice(item);
222 //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
229 public void clear () {
230 // Clear bean as well
231 this.basketBean.clear();
233 // Deligate to basket instance
238 public String doChangeItem (final AddableBasketItem item) {
240 //this.getLogger().logTrace(MessageFormat.format("doChangeItem: item={0} - CALLED!", item));
242 // item shall not be null
245 throw new NullPointerException("item is null");
248 // Default is not found
249 String targetPage = "item_not_changed"; //NOI18N
251 // Lookup item in basket
252 for (final AddableBasketItem basketItem : this.allItems()) {
254 if (basketItem.equals(item)) {
255 // Found it, so allow redirect to proper page
256 targetPage = "basket"; //NOI18N
262 //this.getLogger().logTrace(MessageFormat.format("doChangeItem: targetPage={0} - EXIT!", targetPage));
268 public AddableBasketItem getCurrentItem () {
269 return this.currentItem;
273 public void setCurrentItem (final AddableBasketItem currentItem) {
274 this.currentItem = currentItem;
278 public Long getItemAmount (final Product product) {
280 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
282 // product should not be null
283 if (null == product) {
285 throw new NullPointerException("product is null");
288 // Initial value is zero
289 Long itemAmount = 0L;
292 for (final AddableBasketItem item : this.allItems()) {
294 //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
296 // Is this product instance and same?
299 throw new NullPointerException("item is null");
300 } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
302 itemAmount = item.getOrderedAmount();
308 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
314 public AddableBasketItem getLast () {
315 // Deligate to basket instance
316 return this.basket.getLast();
320 public int getLastNumRows () {
321 // Deligate to basket instance
322 return this.basket.getLastNumRows();
326 public Long getOrderedAmount () {
327 return this.orderedAmount;
331 public void setOrderedAmount (final Long orderedAmount) {
332 this.orderedAmount = orderedAmount;
336 public boolean hasItems () {
337 // Call above and invert it
338 return (!this.isEmpty());
342 public boolean isEmpty () {
343 // Deligate to basket instance
344 return this.basket.isEmpty();
348 public boolean isProductAdded (final Product product) {
350 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
353 if (null == product) {
355 throw new NullPointerException("product is null"); //NOI18N
358 // Generate fake instance
359 AddableBasketItem fake = new BasketItem(product);
362 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
364 boolean isAdded = this.basket.isAdded(fake);
367 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
371 AddableBasketItem item = this.getItemFromProduct(product);
374 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
375 // Set this as current item
376 this.setCurrentItem(item);
380 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
386 public String outputLastAddedItem () {
388 //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
391 String lastItem = ""; //NOI18N
394 AddableBasketItem item = this.getLast();
397 if (item instanceof AddableBasketItem) {
399 switch (item.getItemType()) {
400 case "product": // Sellable product //NOI18N
401 assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
404 lastItem = item.getItemProduct().getProductTitle();
407 default: // Not supported
408 throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
413 //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
419 * Getter for basket bean instance
421 * @return Basket bean instance
423 private BasketSessionBeanRemote getBasketBean () {
424 return this.basketBean;
428 * Somewhat getter for an item instance from given product instance. This
429 * method returns null if no item was found to given product. The product is
430 * found by checking it's id and itemType=product
432 * @param product Product instance
434 * @return Item instance or null if not found
436 private AddableBasketItem getItemFromProduct (final Product product) {
438 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
440 // Product must not be null
441 if (null == product) {
443 throw new NullPointerException("product is null"); //NOI18N
446 // Create item instance
447 AddableBasketItem foundItem = null;
449 // Create fake instance
450 AddableBasketItem fake = new BasketItem(product);
453 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
455 List<AddableBasketItem> list = this.basket.getAll();
458 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
460 for (final AddableBasketItem item : list) {
462 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
464 // item must not be null
467 throw new NullPointerException("item is null"); //NOI18N
471 if (item.equals(fake)) {
472 // Set found item and abort look
479 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));