2 * Copyright (C) 2016 - 2024 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.model.basket;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import javax.ejb.Stateless;
23 import javax.persistence.EntityExistsException;
24 import org.mxchange.jcustomercore.model.customer.Customer;
25 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
26 import org.mxchange.jshopcore.model.basket.Basket;
27 import org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote;
28 import org.mxchange.jshopcore.model.basket.ShopBasket;
29 import org.mxchange.jshopcore.model.customer.ShopCustomerUtils;
30 import org.mxchange.jshopcore.model.order.Orderable;
31 import org.mxchange.jshopcore.model.order.ShopOrder;
32 import org.mxchange.pizzaaplication.enterprise.BasePizzaEnterpriseBean;
35 * A basket for orderable items
37 * @author Roland Häder<roland@mxchange.org>
39 @Stateless (name = "basket", description = "A bean handling persisting baskets of logged-in customers")
40 public class PizzaBasketSessionBean extends BasePizzaEnterpriseBean implements BasketSessionBeanRemote {
45 private static final long serialVersionUID = 4_384_123_923_163_957L;
50 public PizzaBasketSessionBean () {
51 // Call super constructor
56 public void clear () {
57 // @TODO Nothing done so far
61 public Basket<AddableBasketItem> getCurrentBasket () {
63 this.getLoggerBeanLocal().logTrace("getCurrentBasket: CALLED!!"); //NOI18N
65 // @TODO For now this method returns only an empty basked until they are persitable
66 Basket<AddableBasketItem> basket = new ShopBasket();
69 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getCurrentBasket: basket.size()={0} EXIT!!", basket.size())); //NOI18N
76 public String registerItems (final Customer customer, final List<AddableBasketItem> orderedItems) {
78 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerItems: customer={0},itemList={1} - CALLED!", customer, orderedItems)); //NOI18N
80 // Init variable for access key
83 // customer/itemList should not be null
84 if (null == customer) {
86 throw new NullPointerException("customer is null"); //NOI18N
87 } else if (null == orderedItems) {
89 throw new NullPointerException("itemList is null"); //NOI18N
90 } else if (customer.getCustomerId() == null) {
92 throw new NullPointerException(MessageFormat.format("customer {0} id is null", customer)); //NOI18N
93 } else if (customer.getCustomerId() == 0) {
95 throw new IllegalArgumentException(MessageFormat.format("customer {0} has no id set", customer)); //NOI18N
96 } else if (orderedItems.isEmpty()) {
98 throw new IllegalArgumentException("item list is empty"); //NOI18N
101 // First try to register the order (to get an id number from it)
103 // Generate access key
104 accessKey = ShopCustomerUtils.generateAccessKey(this.getEntityManager(), customer);
107 this.getLoggerBeanLocal().logDebug(MessageFormat.format("registerItems: accessKey={0}", accessKey)); //NOI18N
109 // Create order object
110 Orderable order = new ShopOrder();
111 order.setCustomer(customer);
112 order.setAccessKey(accessKey);
113 order.setOrderedItems(orderedItems);
114 order.setOrderCreated(new GregorianCalendar());
117 this.getEntityManager().persist(order);
118 } catch (final EntityExistsException ex) {
120 this.getLoggerBeanLocal().logException(ex);
123 * Don't return an access key as the exception should never be
130 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerItems: accessKey={0} - EXIT!", accessKey)); //NOI18N