]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/basket/PizzaBasketWebSessionBean.java
This "Pizza-Service" application was build on an outdated core, as it was not require...
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / basket / PizzaBasketWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.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;
35
36 /**
37  * A bean for the basket
38  * <p>
39  * @author Roland Haeder<roland@mxchange.org>
40  */
41 @Named ("basketController")
42 @SessionScoped
43 public class PizzaBasketWebSessionBean implements PizzaBasketWebSessionController {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 5_476_347_320_198L;
49
50         /**
51          * Instance of wrapped basket
52          */
53         private final Basket<AddableBasketItem> basket;
54
55         /**
56          * Basket bean
57          */
58         private final BasketSessionBeanRemote basketBean;
59
60         /**
61          * Current item
62          */
63         private AddableBasketItem currentItem;
64
65         /////////////////////// Properties /////////////////////
66         /**
67          * Ordered orderedAmount
68          */
69         private Long orderedAmount;
70
71         /**
72          * Default constructor
73          */
74         public PizzaBasketWebSessionBean () {
75                 // Get new application instance
76                 this.basket = new ShopBasket();
77
78                 try {
79                         // Get initial context
80                         Context context = new InitialContext();
81
82                         // Try to lookup
83                         this.basketBean = (BasketSessionBeanRemote) context.lookup("ejb/stateless-basket"); //NOI18N
84                 } catch (final NamingException ex) {
85                         // Continue to throw
86                         throw new FaceletException(ex);
87                 }
88         }
89
90         @Override
91         public String addItem (final Product product) {
92                 // Trace message
93                 //this.getLogger().logTrace(MessageFormat.format("addItem: product={0} - CALLED!", product));
94
95                 // product should not be null
96                 if (null == product) {
97                         // Abort here
98                         throw new NullPointerException("product is null");
99                 }
100
101                 // Generate item instance
102                 AddableBasketItem item = new BasketItem(product, this.getOrderedAmount());
103
104                 // Is orderedAmount set?
105                 if (this.getOrderedAmount() == null) {
106                         // Trace message
107                         //this.getLogger().logTrace("addItem: orderedAmount not specified, returning null ... - EXIT!");
108
109                         // No orderedAmount specified?!
110                         return null;
111                 }
112
113                 try {
114                         // item should not be null
115                         if (null == item) {
116                                 // Abort here
117                                 throw new NullPointerException("item is null"); //NOI18N
118                         }
119
120                         // Deligate to model
121                         this.basket.addItem(item);
122
123                         // Remove orderedAmount
124                         this.setOrderedAmount(null);
125
126                         // Trace message
127                         //this.getLogger().logTrace(MessageFormat.format("addItem: item {0} - has been added to basket. - EXIT!", item));
128                         // Added
129                         return "item_added"; //NOI18N
130                 } catch (final BasketItemAlreadyAddedException ex) {
131                         // Throw unchecked exception
132                         throw new FacesException(ex);
133                 }
134         }
135
136         @Override
137         public List<AddableBasketItem> allItems () {
138                 // Trace message
139                 //this.getLogger().logTrace("allItems: CALLED!");
140
141                 // Deligate to basket instance
142                 List<AddableBasketItem> list = this.basket.getAll();
143
144                 // Trace message
145                 //this.getLogger().logTrace(MessageFormat.format("allItems: list={0} - EXIT!", list));
146
147                 // Return it
148                 return list;
149         }
150
151         @Override
152         public Float calculateCurrentItemPrice () {
153                 // Trace message
154                 //this.getLogger().logTrace("calculateCurrentItemPrice: CALLED!");
155
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) {
161                         // Product is null
162                         throw new NullPointerException("currentItem.product is null"); //NOI18N
163                 } else if (this.getCurrentItem().getOrderedAmount() == null) {
164                         // Amount is null
165                         throw new NullPointerException("currentItem.amount is null"); //NOI18N
166                 }
167
168                 // Caculate item's price
169                 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
170
171                 // Trace message
172                 //this.getLogger().logTrace(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
173
174                 // Return it
175                 return totalPrice;
176         }
177
178         @Override
179         public Float calculateItemPrice (final AddableBasketItem item) {
180                 // Trace message
181                 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
182
183                 // item must not be null
184                 if (null == item) {
185                         // Abort here
186                         throw new NullPointerException("item is null");
187                 }
188
189                 // Default value
190                 Float totalPrice = 0.0f;
191
192                 // Is it a product?
193                 if (item.isProductType()) {
194                         // Caculate item's price
195                         totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
196                 }
197
198                 // Trace message
199                 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
200                 // Return it
201                 return totalPrice;
202         }
203
204         @Override
205         public Float calculateTotalPrice () {
206                 // Trace message
207                 //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
208
209                 // Init total price
210                 Float totalPrice = 0.0f;
211
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);
218                         }
219                 }
220
221                 // Trace message
222                 //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
223
224                 // Return final sum
225                 return totalPrice;
226         }
227
228         @Override
229         public void clear () {
230                 // Clear bean as well
231                 this.basketBean.clear();
232
233                 // Deligate to basket instance
234                 this.basket.clear();
235         }
236
237         @Override
238         public String doChangeItem (final AddableBasketItem item) {
239                 // Trace message
240                 //this.getLogger().logTrace(MessageFormat.format("doChangeItem: item={0} - CALLED!", item));
241
242                 // item shall not be null
243                 if (null == item) {
244                         // Abort here
245                         throw new NullPointerException("item is null");
246                 }
247
248                 // Default is not found
249                 String targetPage = "item_not_changed"; //NOI18N
250
251                 // Lookup item in basket
252                 for (final AddableBasketItem basketItem : this.allItems()) {
253                         // Is it the same?
254                         if (basketItem.equals(item)) {
255                                 // Found it, so allow redirect to proper page
256                                 targetPage = "basket"; //NOI18N
257                                 break;
258                         }
259                 }
260
261                 // Trace message
262                 //this.getLogger().logTrace(MessageFormat.format("doChangeItem: targetPage={0} - EXIT!", targetPage));
263                 // Return page
264                 return targetPage;
265         }
266
267         @Override
268         public AddableBasketItem getCurrentItem () {
269                 return this.currentItem;
270         }
271
272         @Override
273         public void setCurrentItem (final AddableBasketItem currentItem) {
274                 this.currentItem = currentItem;
275         }
276
277         @Override
278         public Long getItemAmount (final Product product) {
279                 // Trace message
280                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
281
282                 // product should not be null
283                 if (null == product) {
284                         // Abort here
285                         throw new NullPointerException("product is null");
286                 }
287
288                 // Initial value is zero
289                 Long itemAmount = 0L;
290
291                 // Iterate over all
292                 for (final AddableBasketItem item : this.allItems()) {
293                         // Debug message
294                         //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
295
296                         // Is this product instance and same?
297                         if (null == item) {
298                                 // item is null
299                                 throw new NullPointerException("item is null");
300                         } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
301                                 // Found it
302                                 itemAmount = item.getOrderedAmount();
303                                 break;
304                         }
305                 }
306
307                 // Trace message
308                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
309                 // Return it
310                 return itemAmount;
311         }
312
313         @Override
314         public AddableBasketItem getLast () {
315                 // Deligate to basket instance
316                 return this.basket.getLast();
317         }
318
319         @Override
320         public int getLastNumRows () {
321                 // Deligate to basket instance
322                 return this.basket.getLastNumRows();
323         }
324
325         @Override
326         public Long getOrderedAmount () {
327                 return this.orderedAmount;
328         }
329
330         @Override
331         public void setOrderedAmount (final Long orderedAmount) {
332                 this.orderedAmount = orderedAmount;
333         }
334
335         @Override
336         public boolean hasItems () {
337                 // Call above and invert it
338                 return (!this.isEmpty());
339         }
340
341         @Override
342         public boolean isEmpty () {
343                 // Deligate to basket instance
344                 return this.basket.isEmpty();
345         }
346
347         @Override
348         public boolean isProductAdded (final Product product) {
349                 // Trace message
350                 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
351
352                 // Must not be null
353                 if (null == product) {
354                         // Abort here
355                         throw new NullPointerException("product is null"); //NOI18N
356                 }
357
358                 // Generate fake instance
359                 AddableBasketItem fake = new BasketItem(product);
360
361                 // Debug message
362                 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
363                 // Ask bean about it
364                 boolean isAdded = this.basket.isAdded(fake);
365
366                 // Debug message
367                 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
368                 // Is it added?
369                 if (isAdded) {
370                         // Get item
371                         AddableBasketItem item = this.getItemFromProduct(product);
372
373                         // Debug message
374                         //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
375                         // Set this as current item
376                         this.setCurrentItem(item);
377                 }
378
379                 // Trace message
380                 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
381                 // Return status
382                 return isAdded;
383         }
384
385         @Override
386         public String outputLastAddedItem () {
387                 // Trace message
388                 //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
389
390                 // Default message
391                 String lastItem = ""; //NOI18N
392
393                 // Get instance
394                 AddableBasketItem item = this.getLast();
395
396                 // Is it set?
397                 if (item instanceof AddableBasketItem) {
398                         // Get type
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
402
403                                         // Get title
404                                         lastItem = item.getItemProduct().getProductTitle();
405                                         break;
406
407                                 default: // Not supported
408                                         throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
409                         }
410                 }
411
412                 // Trace message
413                 //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
414                 // Return it
415                 return lastItem;
416         }
417
418         /**
419          * Getter for basket bean instance
420          * <p>
421          * @return Basket bean instance
422          */
423         private BasketSessionBeanRemote getBasketBean () {
424                 return this.basketBean;
425         }
426
427         /**
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
431          * <p>
432          * @param product Product instance
433          * <p>
434          * @return Item instance or null if not found
435          */
436         private AddableBasketItem getItemFromProduct (final Product product) {
437                 // Trace message
438                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
439
440                 // Product must not be null
441                 if (null == product) {
442                         // Abort here
443                         throw new NullPointerException("product is null"); //NOI18N
444                 }
445
446                 // Create item instance
447                 AddableBasketItem foundItem = null;
448
449                 // Create fake instance
450                 AddableBasketItem fake = new BasketItem(product);
451
452                 // Debug message
453                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
454                 // Get all items
455                 List<AddableBasketItem> list = this.basket.getAll();
456
457                 // Debug message
458                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
459                 // Check all entries
460                 for (final AddableBasketItem item : list) {
461                         // Debug message
462                         //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
463
464                         // item must not be null
465                         if (null == item) {
466                                 // Abort here
467                                 throw new NullPointerException("item is null"); //NOI18N
468                         }
469
470                         // Is it the same?
471                         if (item.equals(fake)) {
472                                 // Set found item and abort look
473                                 foundItem = item;
474                                 break;
475                         }
476                 }
477
478                 // Trace message
479                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));
480                 // Return it
481                 return foundItem;
482         }
483
484 }