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