]> git.mxchange.org Git - pizzaservice-ejb.git/blob - src/java/org/mxchange/pizzaapplication/model/basket/PizzaBasketSessionBean.java
Updated copyright year
[pizzaservice-ejb.git] / src / java / org / mxchange / pizzaapplication / model / basket / PizzaBasketSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.model.basket;
18
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;
33
34 /**
35  * A basket for orderable items
36  * <p>
37  * @author Roland Häder<roland@mxchange.org>
38  */
39 @Stateless (name = "basket", description = "A bean handling persisting baskets of logged-in customers")
40 public class PizzaBasketSessionBean extends BasePizzaEnterpriseBean implements BasketSessionBeanRemote {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 4_384_123_923_163_957L;
46
47         /**
48          * Default constructor
49          */
50         public PizzaBasketSessionBean () {
51                 // Call super constructor
52                 super();
53         }
54
55         @Override
56         public void clear () {
57                 // @TODO Nothing done so far
58         }
59
60         @Override
61         public Basket<AddableBasketItem> getCurrentBasket () {
62                 // Trace message
63                 this.getLoggerBeanLocal().logTrace("getCurrentBasket: CALLED!!"); //NOI18N
64
65                 // @TODO For now this method returns only an empty basked until they are persitable
66                 Basket<AddableBasketItem> basket = new ShopBasket();
67
68                 // Trace message
69                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("getCurrentBasket: basket.size()={0} EXIT!!", basket.size())); //NOI18N
70
71                 // Return it
72                 return basket;
73         }
74
75         @Override
76         public String registerItems (final Customer customer, final List<AddableBasketItem> orderedItems) {
77                 // Trace message
78                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerItems: customer={0},itemList={1} - CALLED!", customer, orderedItems)); //NOI18N
79
80                 // Init variable for access key
81                 String accessKey;
82
83                 // customer/itemList should not be null
84                 if (null == customer) {
85                         // Abort here
86                         throw new NullPointerException("customer is null"); //NOI18N
87                 } else if (null == orderedItems) {
88                         // Abort here
89                         throw new NullPointerException("itemList is null"); //NOI18N
90                 } else if (customer.getCustomerId() == null) {
91                         // null pointer found
92                         throw new NullPointerException(MessageFormat.format("customer {0} id is null", customer)); //NOI18N
93                 } else if (customer.getCustomerId() == 0) {
94                         // id not set
95                         throw new IllegalArgumentException(MessageFormat.format("customer {0} has no id set", customer)); //NOI18N
96                 } else if (orderedItems.isEmpty()) {
97                         // Empty list
98                         throw new IllegalArgumentException("item list is empty"); //NOI18N
99                 }
100
101                 // First try to register the order (to get an id number from it)
102                 try {
103                         // Generate access key
104                         accessKey = ShopCustomerUtils.generateAccessKey(this.getEntityManager(), customer);
105
106                         // Debug message
107                         this.getLoggerBeanLocal().logDebug(MessageFormat.format("registerItems: accessKey={0}", accessKey)); //NOI18N
108
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());
115
116                         // Persist it
117                         this.getEntityManager().persist(order);
118                 } catch (final EntityExistsException ex) {
119                         // Log exception
120                         this.getLoggerBeanLocal().logException(ex);
121
122                         /*
123                          * Don't return an access key as the exception should never be
124                          * thrown.
125                          */
126                         accessKey = null;
127                 }
128
129                 // Trace message
130                 this.getLoggerBeanLocal().logTrace(MessageFormat.format("registerItems: accessKey={0} - EXIT!", accessKey)); //NOI18N
131
132                 // Return it
133                 return accessKey;
134         }
135
136 }