2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.enterprise.event.Observes;
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.jproduct.model.product.Product;
30 import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent;
31 import org.mxchange.jshopcore.exceptions.BasketItemAlreadyAddedException;
32 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
33 import org.mxchange.jshopcore.model.basket.Basket;
34 import org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote;
35 import org.mxchange.jshopcore.model.basket.items.BasketItem;
36 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
39 * A bean for the basket
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("basketController")
45 public class PizzaBasketWebSessionBean extends BasePizzaBean implements PizzaBasketWebSessionController {
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;
65 private AddableBasketItem currentItem;
68 * Ordered orderedAmount
70 private Long orderedAmount;
75 public PizzaBasketWebSessionBean () {
77 // Get initial context
78 Context context = new InitialContext();
81 this.basketBean = (BasketSessionBeanRemote) context.lookup("java:global/jshop-ejb/basket!org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote"); //NOI18N
82 } catch (final NamingException ex) {
84 throw new FaceletException(ex);
87 // Get current basked from bean
88 this.basket = this.basketBean.getCurrentBasket();
92 public String addItem (final Product product) {
93 // product should not be null
94 if (null == product) {
96 throw new NullPointerException("product is null"); //NOI18N
99 // Generate item instance
100 AddableBasketItem item = new BasketItem(product, this.getOrderedAmount());
102 // Is orderedAmount set?
103 if (this.getOrderedAmount() == null) {
104 // No orderedAmount specified?!
109 // item should not be null
112 throw new NullPointerException("item is null"); //NOI18N
116 this.basket.addItem(item);
118 // Remove orderedAmount
119 this.setOrderedAmount(null);
122 return "item_added"; //NOI18N
123 } catch (final BasketItemAlreadyAddedException ex) {
124 // Throw unchecked exception
125 throw new FacesException(ex);
130 public List<AddableBasketItem> allItems () {
131 // Deligate to basket instance
132 List<AddableBasketItem> list = this.basket.getAll();
139 public Float calculateCurrentItemPrice () {
140 // Is the current item/amount set?
141 if (this.getCurrentItem() == null) {
142 // Current item is null
143 throw new NullPointerException("currentItem is null"); //NOI18N
144 } else if (this.getCurrentItem().getItemProduct() == null) {
146 throw new NullPointerException("currentItem.product is null"); //NOI18N
147 } else if (this.getCurrentItem().getOrderedAmount() == null) {
149 throw new NullPointerException("currentItem.amount is null"); //NOI18N
152 // Caculate item's price
153 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
160 public Float calculateItemPrice (final AddableBasketItem item) {
161 // item must not be null
164 throw new NullPointerException("item is null"); //NOI18N
168 Float totalPrice = 0.0f;
171 if (item.isProductType()) {
172 // Caculate item's price
173 totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
181 public Float calculateTotalPrice () {
183 Float totalPrice = 0.0f;
185 // Iterate over all items
186 for (final AddableBasketItem item : this.allItems()) {
187 // Is the item a product?
188 if (item.isProductType()) {
189 // Calculate single price and add it
190 totalPrice += this.calculateItemPrice(item);
199 public String doChangeItem (final AddableBasketItem item) {
200 // item shall not be null
203 throw new NullPointerException("item is null"); //NOI18N
206 // Default is not found
207 String targetPage = "item_not_changed"; //NOI18N
209 // Lookup item in basket
210 for (final AddableBasketItem basketItem : this.allItems()) {
212 if (basketItem.equals(item)) {
213 // Found it, so allow redirect to proper page
214 targetPage = "basket"; //NOI18N
224 public AddableBasketItem getCurrentItem () {
225 return this.currentItem;
229 public void setCurrentItem (final AddableBasketItem currentItem) {
230 this.currentItem = currentItem;
234 public Long getItemAmount (final Product product) {
235 // product should not be null
236 if (null == product) {
238 throw new NullPointerException("product is null"); //NOI18N
241 // Initial value is zero
242 Long itemAmount = 0L;
245 for (final AddableBasketItem item : this.allItems()) {
246 // Is this product instance and same?
249 throw new NullPointerException("item is null"); //NOI18N
250 } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
252 itemAmount = item.getOrderedAmount();
262 public AddableBasketItem getLast () {
263 // Deligate to basket instance
264 return this.basket.getLast();
268 public int getLastNumRows () {
269 // Deligate to basket instance
270 return this.basket.size();
274 public Long getOrderedAmount () {
275 return this.orderedAmount;
279 public void setOrderedAmount (final Long orderedAmount) {
280 this.orderedAmount = orderedAmount;
284 public boolean hasItems () {
285 // Call above and invert it
286 return (!this.isEmpty());
290 public boolean isEmpty () {
291 // Deligate to basket instance
292 return this.basket.isEmpty();
296 public boolean isProductAdded (final Product product) {
298 if (null == product) {
300 throw new NullPointerException("product is null"); //NOI18N
303 // Generate fake instance
304 AddableBasketItem fake = new BasketItem(product);
307 boolean isAdded = this.basket.isAdded(fake);
312 AddableBasketItem item = this.getItemFromProduct(product);
314 // Set this as current item
315 this.setCurrentItem(item);
323 public String outputLastAddedItem () {
325 String lastItem = ""; //NOI18N
328 AddableBasketItem item = this.getLast();
331 if (item instanceof AddableBasketItem) {
333 switch (item.getItemType()) {
334 case "product": // Sellable product //NOI18N
335 assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
338 lastItem = item.getItemProduct().getProductTitle();
341 default: // Not supported
342 throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
351 public void afterCheckoutCompleted (@Observes final ObservableCheckoutCompletedEvent event) {
355 throw new NullPointerException("event is null"); //NOI18N
356 } else if (event.getWrapper() == null) {
358 throw new NullPointerException("event.wrapper is null"); //NOI18N
359 } else if (event.getWrapper().getCustomer() == null) {
361 throw new NullPointerException("event.wrapper.customer is null"); //NOI18N
362 } else if (event.getWrapper().getCustomer().getCustomerId() == null) {
363 // Throw NPE again ...
364 throw new NullPointerException("event.wrapper.customer.customerId is null"); //NOI18N
365 } else if (event.getWrapper().getCustomer().getCustomerId() < 0) {
367 throw new IllegalArgumentException(MessageFormat.format("event.wrapper.customer.customerId={0} is not valid.", event.getWrapper().getCustomer().getCustomerId())); //NOI18N
368 } else if (event.getWrapper().getList() == null) {
370 throw new NullPointerException("event.wrapper.list is null"); //NOI18N
371 } else if (event.getWrapper().getList().isEmpty()) {
373 throw new IllegalArgumentException("event.wrapper.list is empty"); //NOI18N
383 private void clear () {
384 // Clear bean as well
385 this.basketBean.clear();
387 // Deligate to basket instance
392 * Somewhat getter for an item instance from given product instance. This
393 * method returns null if no item was found to given product. The product is
394 * found by checking it's id and itemType=product
396 * @param product Product instance
398 * @return Item instance or null if not found
400 private AddableBasketItem getItemFromProduct (final Product product) {
401 // Product must not be null
402 if (null == product) {
404 throw new NullPointerException("product is null"); //NOI18N
407 // Create item instance
408 AddableBasketItem foundItem = null;
410 // Create fake instance
411 AddableBasketItem fake = new BasketItem(product);
414 List<AddableBasketItem> list = this.basket.getAll();
417 for (final AddableBasketItem item : list) {
418 // item must not be null
421 throw new NullPointerException("item is null"); //NOI18N
425 if (item.equals(fake)) {
426 // Set found item and abort look