]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / basket / PizzaBasketWebSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2020 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.beans.basket;
18
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;
37
38 /**
39  * A bean for the basket
40  * <p>
41  * @author Roland Häder<roland@mxchange.org>
42  */
43 @Named ("basketController")
44 @SessionScoped
45 public class PizzaBasketWebSessionBean extends BasePizzaBean implements PizzaBasketWebSessionController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 5_476_347_320_198L;
51
52         /**
53          * Instance of wrapped basket
54          */
55         private final Basket<AddableBasketItem> basket;
56
57         /**
58          * Basket bean
59          */
60         private final BasketSessionBeanRemote basketBean;
61
62         /**
63          * Current item
64          */
65         private AddableBasketItem currentItem;
66
67         /**
68          * Ordered orderedAmount
69          */
70         private Long orderedAmount;
71
72         /**
73          * Default constructor
74          */
75         public PizzaBasketWebSessionBean () {
76                 try {
77                         // Get initial context
78                         Context context = new InitialContext();
79
80                         // Try to lookup
81                         this.basketBean = (BasketSessionBeanRemote) context.lookup("java:global/jshop-ejb/basket!org.mxchange.jshopcore.model.basket.BasketSessionBeanRemote"); //NOI18N
82                 } catch (final NamingException ex) {
83                         // Continue to throw
84                         throw new FaceletException(ex);
85                 }
86
87                 // Get current basked from bean
88                 this.basket = this.basketBean.getCurrentBasket();
89         }
90
91         @Override
92         public String addItem (final Product product) {
93                 // product should not be null
94                 if (null == product) {
95                         // Abort here
96                         throw new NullPointerException("product is null"); //NOI18N
97                 }
98
99                 // Generate item instance
100                 AddableBasketItem item = new BasketItem(product, this.getOrderedAmount());
101
102                 // Is orderedAmount set?
103                 if (this.getOrderedAmount() == null) {
104                         // No orderedAmount specified?!
105                         return null;
106                 }
107
108                 try {
109                         // item should not be null
110                         if (null == item) {
111                                 // Abort here
112                                 throw new NullPointerException("item is null"); //NOI18N
113                         }
114
115                         // Deligate to model
116                         this.basket.addItem(item);
117
118                         // Remove orderedAmount
119                         this.setOrderedAmount(null);
120
121                         // Added
122                         return "item_added"; //NOI18N
123                 } catch (final BasketItemAlreadyAddedException ex) {
124                         // Throw unchecked exception
125                         throw new FacesException(ex);
126                 }
127         }
128
129         @Override
130         public List<AddableBasketItem> allItems () {
131                 // Deligate to basket instance
132                 List<AddableBasketItem> list = this.basket.getAll();
133
134                 // Return it
135                 return list;
136         }
137
138         @Override
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) {
145                         // Product is null
146                         throw new NullPointerException("currentItem.product is null"); //NOI18N
147                 } else if (this.getCurrentItem().getOrderedAmount() == null) {
148                         // Amount is null
149                         throw new NullPointerException("currentItem.amount is null"); //NOI18N
150                 }
151
152                 // Caculate item's price
153                 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
154
155                 // Return it
156                 return totalPrice;
157         }
158
159         @Override
160         public Float calculateItemPrice (final AddableBasketItem item) {
161                 // item must not be null
162                 if (null == item) {
163                         // Abort here
164                         throw new NullPointerException("item is null"); //NOI18N
165                 }
166
167                 // Default value
168                 Float totalPrice = 0.0f;
169
170                 // Is it a product?
171                 if (item.isProductType()) {
172                         // Caculate item's price
173                         totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
174                 }
175
176                 // Return it
177                 return totalPrice;
178         }
179
180         @Override
181         public Float calculateTotalPrice () {
182                 // Init total price
183                 Float totalPrice = 0.0f;
184
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);
191                         }
192                 }
193
194                 // Return final sum
195                 return totalPrice;
196         }
197
198         @Override
199         public String doChangeItem (final AddableBasketItem item) {
200                 // item shall not be null
201                 if (null == item) {
202                         // Abort here
203                         throw new NullPointerException("item is null"); //NOI18N
204                 }
205
206                 // Default is not found
207                 String targetPage = "item_not_changed"; //NOI18N
208
209                 // Lookup item in basket
210                 for (final AddableBasketItem basketItem : this.allItems()) {
211                         // Is it the same?
212                         if (basketItem.equals(item)) {
213                                 // Found it, so allow redirect to proper page
214                                 targetPage = "basket"; //NOI18N
215                                 break;
216                         }
217                 }
218
219                 // Return page
220                 return targetPage;
221         }
222
223         @Override
224         public AddableBasketItem getCurrentItem () {
225                 return this.currentItem;
226         }
227
228         @Override
229         public void setCurrentItem (final AddableBasketItem currentItem) {
230                 this.currentItem = currentItem;
231         }
232
233         @Override
234         public Long getItemAmount (final Product product) {
235                 // product should not be null
236                 if (null == product) {
237                         // Abort here
238                         throw new NullPointerException("product is null"); //NOI18N
239                 }
240
241                 // Initial value is zero
242                 Long itemAmount = 0L;
243
244                 // Iterate over all
245                 for (final AddableBasketItem item : this.allItems()) {
246                         // Is this product instance and same?
247                         if (null == item) {
248                                 // item is null
249                                 throw new NullPointerException("item is null"); //NOI18N
250                         } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
251                                 // Found it
252                                 itemAmount = item.getOrderedAmount();
253                                 break;
254                         }
255                 }
256
257                 // Return it
258                 return itemAmount;
259         }
260
261         @Override
262         public AddableBasketItem getLast () {
263                 // Deligate to basket instance
264                 return this.basket.getLast();
265         }
266
267         @Override
268         public int getLastNumRows () {
269                 // Deligate to basket instance
270                 return this.basket.size();
271         }
272
273         @Override
274         public Long getOrderedAmount () {
275                 return this.orderedAmount;
276         }
277
278         @Override
279         public void setOrderedAmount (final Long orderedAmount) {
280                 this.orderedAmount = orderedAmount;
281         }
282
283         @Override
284         public boolean hasItems () {
285                 // Call above and invert it
286                 return (!this.isEmpty());
287         }
288
289         @Override
290         public boolean isEmpty () {
291                 // Deligate to basket instance
292                 return this.basket.isEmpty();
293         }
294
295         @Override
296         public boolean isProductAdded (final Product product) {
297                 // Must not be null
298                 if (null == product) {
299                         // Abort here
300                         throw new NullPointerException("product is null"); //NOI18N
301                 }
302
303                 // Generate fake instance
304                 AddableBasketItem fake = new BasketItem(product);
305
306                 // Ask bean about it
307                 boolean isAdded = this.basket.isAdded(fake);
308
309                 // Is it added?
310                 if (isAdded) {
311                         // Get item
312                         AddableBasketItem item = this.getItemFromProduct(product);
313
314                         // Set this as current item
315                         this.setCurrentItem(item);
316                 }
317
318                 // Return status
319                 return isAdded;
320         }
321
322         @Override
323         public String outputLastAddedItem () {
324                 // Default message
325                 String lastItem = ""; //NOI18N
326
327                 // Get instance
328                 AddableBasketItem item = this.getLast();
329
330                 // Is it set?
331                 if (item instanceof AddableBasketItem) {
332                         // Get type
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
336
337                                         // Get title
338                                         lastItem = item.getItemProduct().getProductTitle();
339                                         break;
340
341                                 default: // Not supported
342                                         throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
343                         }
344                 }
345
346                 // Return it
347                 return lastItem;
348         }
349
350         @Override
351         public void afterCheckoutCompleted (@Observes final ObservableCheckoutCompletedEvent event) {
352                 // Is all set?
353                 if (null == event) {
354                         // Throw NPE
355                         throw new NullPointerException("event is null"); //NOI18N
356                 } else if (event.getWrapper() == null) {
357                         // Throw NPE again
358                         throw new NullPointerException("event.wrapper is null"); //NOI18N
359                 } else if (event.getWrapper().getCustomer() == null) {
360                         // Throw NPE again
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) {
366                         // Invalid id
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) {
369                         // Throw NPE again
370                         throw new NullPointerException("event.wrapper.list is null"); //NOI18N
371                 } else if (event.getWrapper().getList().isEmpty()) {
372                         // Throw NPE again
373                         throw new IllegalArgumentException("event.wrapper.list is empty"); //NOI18N
374                 }
375
376                 // Clear this method
377                 this.clear();
378         }
379
380         /**
381          * Clears this bean
382          */
383         private void clear () {
384                 // Clear bean as well
385                 this.basketBean.clear();
386
387                 // Deligate to basket instance
388                 this.basket.clear();
389         }
390
391         /**
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
395          * <p>
396          * @param product Product instance
397          * <p>
398          * @return Item instance or null if not found
399          */
400         private AddableBasketItem getItemFromProduct (final Product product) {
401                 // Product must not be null
402                 if (null == product) {
403                         // Abort here
404                         throw new NullPointerException("product is null"); //NOI18N
405                 }
406
407                 // Create item instance
408                 AddableBasketItem foundItem = null;
409
410                 // Create fake instance
411                 AddableBasketItem fake = new BasketItem(product);
412
413                 // Get all items
414                 List<AddableBasketItem> list = this.basket.getAll();
415
416                 // Check all entries
417                 for (final AddableBasketItem item : list) {
418                         // item must not be null
419                         if (null == item) {
420                                 // Abort here
421                                 throw new NullPointerException("item is null"); //NOI18N
422                         }
423
424                         // Is it the same?
425                         if (item.equals(fake)) {
426                                 // Set found item and abort look
427                                 foundItem = item;
428                                 break;
429                         }
430                 }
431
432                 // Return it
433                 return foundItem;
434         }
435
436 }