]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebSessionBean.java
Renamed beans to their proper scope + renamed bean mappedName to always end with...
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / basket / BasketWebSessionBean.java
1 /*
2  * Copyright (C) 2015 Roland Haeder
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU 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 BasketWebSessionBean implements BasketWebSessionController {
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         /**
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 BasketWebSessionBean () {
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("ejb/stateless-basket"); //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                 //this.getLogger().logTrace(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                         //this.getLogger().logTrace("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                         //this.getLogger().logTrace(MessageFormat.format("addItem: item {0} - has been added to basket. - EXIT!", item));
129                         // Added
130                         return "item_added"; //NOI18N
131                 } catch (final BasketItemAlreadyAddedException ex) {
132                         // Throw unchecked exception
133                         throw new FacesException(ex);
134                 }
135         }
136
137         @Override
138         public List<AddableBasketItem> allItems () {
139                 // Trace message
140                 //this.getLogger().logTrace("allItems: CALLED!");
141
142                 // Deligate to basket instance
143                 List<AddableBasketItem> list = this.basket.getAll();
144
145                 // Trace message
146                 //this.getLogger().logTrace(MessageFormat.format("allItems: list={0} - EXIT!", list));
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                 // Return it
174                 return totalPrice;
175         }
176
177         @Override
178         public Float calculateItemPrice (final AddableBasketItem item) {
179                 // Trace message
180                 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
181
182                 // item must not be null
183                 if (null == item) {
184                         // Abort here
185                         throw new NullPointerException("item is null");
186                 }
187
188                 // Default value
189                 Float totalPrice = 0.0f;
190
191                 // Is it a product?
192                 if (item.isProductType()) {
193                         // Caculate item's price
194                         totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
195                 }
196
197                 // Trace message
198                 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
199                 // Return it
200                 return totalPrice;
201         }
202
203         @Override
204         public Float calculateTotalPrice () {
205                 // Trace message
206                 //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
207
208                 // Init total price
209                 Float totalPrice = 0.0f;
210
211                 // Iterate over all items
212                 for (final AddableBasketItem item : this.allItems()) {
213                         // Is the item a product?
214                         if (item.isProductType()) {
215                                 // Calculate single price and add it
216                                 totalPrice += this.calculateItemPrice(item);
217                         }
218                 }
219
220                 // Trace message
221                 //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
222                 // Return final sum
223                 return totalPrice;
224         }
225
226         @Override
227         public String changeItem (final AddableBasketItem item) {
228                 // Trace message
229                 //this.getLogger().logTrace(MessageFormat.format("changeItem: item={0} - CALLED!", item));
230
231                 // item shall not be null
232                 if (null == item) {
233                         // Abort here
234                         throw new NullPointerException("item is null");
235                 }
236
237                 // Default is not found
238                 String targetPage = "item_not_changed"; //NOI18N
239
240                 // Lookup item in basket
241                 for (final AddableBasketItem basketItem : this.allItems()) {
242                         // Is it the same?
243                         if (basketItem.equals(item)) {
244                                 // Found it, so allow redirect to proper page
245                                 targetPage = "basket"; //NOI18N
246                                 break;
247                         }
248                 }
249
250                 // Trace message
251                 //this.getLogger().logTrace(MessageFormat.format("changeItem: targetPage={0} - EXIT!", targetPage));
252                 // Return page
253                 return targetPage;
254         }
255
256         @Override
257         public void clear () {
258                 // @TODO Also clear EJB
259                 // Deligate to basket instance
260                 this.basket.clear();
261         }
262
263         @Override
264         public AddableBasketItem getCurrentItem () {
265                 return this.currentItem;
266         }
267
268         @Override
269         public void setCurrentItem (final AddableBasketItem currentItem) {
270                 this.currentItem = currentItem;
271         }
272
273         @Override
274         public Long getItemAmount (final Product product) {
275                 // Trace message
276                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
277
278                 // product should not be null
279                 if (null == product) {
280                         // Abort here
281                         throw new NullPointerException("product is null");
282                 }
283
284                 // Initial value is zero
285                 Long itemAmount = 0L;
286
287                 // Iterate over all
288                 for (final AddableBasketItem item : this.allItems()) {
289                         // Debug message
290                         //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
291
292                         // Is this product instance and same?
293                         if (null == item) {
294                                 // item is null
295                                 throw new NullPointerException("item is null");
296                         } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
297                                 // Found it
298                                 itemAmount = item.getOrderedAmount();
299                                 break;
300                         }
301                 }
302
303                 // Trace message
304                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
305                 // Return it
306                 return itemAmount;
307         }
308
309         @Override
310         public AddableBasketItem getLast () {
311                 // Deligate to basket instance
312                 return this.basket.getLast();
313         }
314
315         @Override
316         public int getLastNumRows () {
317                 // Deligate to basket instance
318                 return this.basket.getLastNumRows();
319         }
320
321         @Override
322         public Long getOrderedAmount () {
323                 return this.orderedAmount;
324         }
325
326         @Override
327         public void setOrderedAmount (final Long orderedAmount) {
328                 this.orderedAmount = orderedAmount;
329         }
330
331         @Override
332         public boolean hasItems () {
333                 // Call above and invert it
334                 return (!this.isEmpty());
335         }
336
337         @Override
338         public boolean isEmpty () {
339                 // Deligate to basket instance
340                 return this.basket.isEmpty();
341         }
342
343         @Override
344         public boolean isProductAdded (final Product product) {
345                 // Trace message
346                 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
347
348                 // Must not be null
349                 if (null == product) {
350                         // Abort here
351                         throw new NullPointerException("product is null"); //NOI18N
352                 }
353
354                 // Generate fake instance
355                 AddableBasketItem fake = new BasketItem(product);
356
357                 // Debug message
358                 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
359                 // Ask bean about it
360                 boolean isAdded = this.basket.isAdded(fake);
361
362                 // Debug message
363                 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
364                 // Is it added?
365                 if (isAdded) {
366                         // Get item
367                         AddableBasketItem item = this.getItemFromProduct(product);
368
369                         // Debug message
370                         //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
371                         // Set this as current item
372                         this.setCurrentItem(item);
373                 }
374
375                 // Trace message
376                 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
377                 // Return status
378                 return isAdded;
379         }
380
381         @Override
382         public String outputLastAddedItem () {
383                 // Trace message
384                 //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
385
386                 // Default message
387                 String lastItem = ""; //NOI18N
388
389                 // Get instance
390                 AddableBasketItem item = this.getLast();
391
392                 // Is it set?
393                 if (item instanceof AddableBasketItem) {
394                         // Get type
395                         switch (item.getItemType()) {
396                                 case "product": // Sellable product //NOI18N
397                                         assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
398
399                                         // Get title
400                                         lastItem = item.getItemProduct().getProductTitle();
401                                         break;
402
403                                 default: // Not supported
404                                         throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
405                         }
406                 }
407
408                 // Trace message
409                 //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
410                 // Return it
411                 return lastItem;
412         }
413
414         /**
415          * Getter for basket bean instance
416          * <p>
417          * @return Basket bean instance
418          */
419         private BasketSessionBeanRemote getBasketBean () {
420                 return this.basketBean;
421         }
422
423         /**
424          * Somewhat getter for an item instance from given product instance. This
425          * method returns null if no item was found to given product. The product is
426          * found by checking it's id and itemType=product
427          * <p>
428          * @param product Product instance
429          * <p>
430          * @return Item instance or null if not found
431          */
432         private AddableBasketItem getItemFromProduct (final Product product) {
433                 // Trace message
434                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
435
436                 // Product must not be null
437                 if (null == product) {
438                         // Abort here
439                         throw new NullPointerException("product is null"); //NOI18N
440                 }
441
442                 // Create item instance
443                 AddableBasketItem foundItem = null;
444
445                 // Create fake instance
446                 AddableBasketItem fake = new BasketItem(product);
447
448                 // Debug message
449                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
450                 // Get all items
451                 List<AddableBasketItem> list = this.basket.getAll();
452
453                 // Debug message
454                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
455                 // Check all entries
456                 for (final AddableBasketItem item : list) {
457                         // Debug message
458                         //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
459
460                         // item must not be null
461                         if (null == item) {
462                                 // Abort here
463                                 throw new NullPointerException("item is null"); //NOI18N
464                         }
465
466                         // Is it the same?
467                         if (item.equals(fake)) {
468                                 // Set found item and abort look
469                                 foundItem = item;
470                                 break;
471                         }
472                 }
473
474                 // Trace message
475                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));
476                 // Return it
477                 return foundItem;
478         }
479 }