]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebBean.java
Continued:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / basket / BasketWebBean.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  *
39  * @author Roland Haeder<roland@mxchange.org>
40  */
41 @Named ("basketController")
42 @SessionScoped
43 public class BasketWebBean  implements BasketWebController {
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         /////////////////////// Properties /////////////////////
61         /**
62          * Ordered orderedAmount
63          */
64         private Long orderedAmount;
65
66         /**
67          * Current item
68          */
69         private AddableBasketItem currentItem;
70
71         /**
72          * Default constructor
73          */
74         public BasketWebBean () {
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
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
148                 // Return it
149                 return list;
150         }
151
152         @Override
153         public Float calculateCurrentItemPrice () {
154                 // Trace message
155                 //this.getLogger().logTrace("calculateCurrentItemPrice: CALLED!");
156
157                 // Is the current item/amount set?
158                 if (this.getCurrentItem() == null) {
159                         // Current item is null
160                         throw new NullPointerException("currentItem is null"); //NOI18N
161                 } else if (this.getCurrentItem().getItemProduct() == null) {
162                         // Product is null
163                         throw new NullPointerException("currentItem.product is null"); //NOI18N
164                 } else if (this.getCurrentItem().getOrderedAmount() == null) {
165                         // Amount is null
166                         throw new NullPointerException("currentItem.amount is null"); //NOI18N
167                 }
168
169                 // Caculate item's price
170                 Float totalPrice = (this.getCurrentItem().getItemProduct().getProductPrice() * this.getCurrentItem().getOrderedAmount());
171
172                 // Trace message
173                 //this.getLogger().logTrace(MessageFormat.format("calculateCurrentItemPrice: totalPrice={0} - EXIT!", totalPrice));
174
175                 // Return it
176                 return totalPrice;
177         }
178
179         @Override
180         public Float calculateItemPrice (final AddableBasketItem item) {
181                 // Trace message
182                 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: item={0} - CALLED!", item));
183
184                 // item must not be null
185                 if (null == item) {
186                         // Abort here
187                         throw new NullPointerException("item is null");
188                 }
189
190                 // Default value
191                 Float totalPrice = 0.0f;
192
193                 // Is it a product?
194                 if (item.isProductType()) {
195                         // Caculate item's price
196                         totalPrice = (item.getItemProduct().getProductPrice() * item.getOrderedAmount());
197                 }
198
199                 // Trace message
200                 //this.getLogger().logTrace(MessageFormat.format("calculateItemPrice: totalPrice={0} - EXIT!", totalPrice));
201
202                 // Return it
203                 return totalPrice;
204         }
205
206         @Override
207         public Float calculateTotalPrice () {
208                 // Trace message
209                 //this.getLogger().logTrace("calculateTotalPrice: CALLED!");
210
211                 // Init total price
212                 Float totalPrice = 0.0f;
213
214                 // Iterate over all items
215                 for (final AddableBasketItem item : this.allItems()) {
216                         // Is the item a product?
217                         if (item.isProductType()) {
218                                 // Calculate single price and add it
219                                 totalPrice += this.calculateItemPrice(item);
220                         }
221                 }
222
223                 // Trace message
224                 //this.getLogger().logTrace(MessageFormat.format("calculateTotalPrice: totalPrice={0} - EXIT!", totalPrice));
225
226                 // Return final sum
227                 return totalPrice;
228         }
229
230         @Override
231         public String changeItem (final AddableBasketItem item) {
232                 // Trace message
233                 //this.getLogger().logTrace(MessageFormat.format("changeItem: item={0} - CALLED!", item));
234
235                 // item shall not be null
236                 if (null == item) {
237                         // Abort here
238                         throw new NullPointerException("item is null");
239                 }
240
241                 // Default is not found
242                 String targetPage = "item_not_changed"; //NOI18N
243
244                 // Lookup item in basket
245                 for (final AddableBasketItem basketItem : this.allItems()) {
246                         // Is it the same?
247                         if (basketItem.equals(item)) {
248                                 // Found it, so allow redirect to proper page
249                                 targetPage = "basket"; //NOI18N
250                                 break;
251                         }
252                 }
253
254                 // Trace message
255                 //this.getLogger().logTrace(MessageFormat.format("changeItem: targetPage={0} - EXIT!", targetPage));
256
257                 // Return page
258                 return targetPage;
259         }
260
261         @Override
262         public void clear () {
263                 // @TODO Also clear EJB
264                 // Deligate to basket instance
265                 this.basket.clear();
266         }
267
268         @Override
269         public Long getOrderedAmount () {
270                 return this.orderedAmount;
271         }
272
273         @Override
274         public void setOrderedAmount (final Long orderedAmount) {
275                 this.orderedAmount = orderedAmount;
276         }
277
278         @Override
279         public AddableBasketItem getCurrentItem () {
280                 return this.currentItem;
281         }
282
283         @Override
284         public void setCurrentItem (final AddableBasketItem currentItem) {
285                 this.currentItem = currentItem;
286         }
287
288         @Override
289         public Long getItemAmount (final Product product) {
290                 // Trace message
291                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
292
293                 // product should not be null
294                 if (null == product) {
295                         // Abort here
296                         throw new NullPointerException("product is null");
297                 }
298
299                 // Initial value is zero
300                 Long itemAmount = 0L;
301
302                 // Iterate over all
303                 for (final AddableBasketItem item : this.allItems()) {
304                         // Debug message
305                         //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
306
307                         // Is this product instance and same?
308                         if (null == item) {
309                                 // item is null
310                                 throw new NullPointerException("item is null");
311                         } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
312                                 // Found it
313                                 itemAmount = item.getOrderedAmount();
314                                 break;
315                         }
316                 }
317
318                 // Trace message
319                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
320
321                 // Return it
322                 return itemAmount;
323         }
324
325         @Override
326         public AddableBasketItem getLast () {
327                 // Deligate to basket instance
328                 return this.basket.getLast();
329         }
330
331         @Override
332         public int getLastNumRows () {
333                 // Deligate to basket instance
334                 return this.basket.getLastNumRows();
335         }
336
337         @Override
338         public boolean hasItems () {
339                 // Call above and invert it
340                 return (!this.isEmpty());
341         }
342
343         @Override
344         public boolean isEmpty () {
345                 // Deligate to basket instance
346                 return this.basket.isEmpty();
347         }
348
349         @Override
350         public boolean isProductAdded (final Product product) {
351                 // Trace message
352                 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: product={0} - EXIT!", product));
353
354                 // Must not be null
355                 if (null == product) {
356                         // Abort here
357                         throw new NullPointerException("product is null"); //NOI18N
358                 }
359
360                 // Generate fake instance
361                 AddableBasketItem fake = new BasketItem(product);
362
363                 // Debug message
364                 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: fake={0}", fake));
365
366                 // Ask bean about it
367                 boolean isAdded = this.basket.isAdded(fake);
368
369                 // Debug message
370                 //this.getLogger().logDebug(MessageFormat.format("isProductAdded: isAdded={0}", isAdded));
371
372                 // Is it added?
373                 if (isAdded) {
374                         // Get item
375                         AddableBasketItem item = this.getItemFromProduct(product);
376
377                         // Debug message
378                         //this.getLogger().logDebug(MessageFormat.format("isProductAdded: item={0} - setting as current item.", item));
379
380                         // Set this as current item
381                         this.setCurrentItem(item);
382                 }
383
384                 // Trace message
385                 //this.getLogger().logTrace(MessageFormat.format("isProductAdded: isAdded={0} - EXIT!", isAdded));
386
387                 // Return status
388                 return isAdded;
389         }
390
391         @Override
392         public String outputLastAddedItem () {
393                 // Trace message
394                 //this.getLogger().logTrace("outputLastAddedItem: CALLED!");
395
396                 // Default message
397                 String lastItem = ""; //NOI18N
398
399                 // Get instance
400                 AddableBasketItem item = this.getLast();
401
402                 // Is it set?
403                 if (item instanceof AddableBasketItem) {
404                         // Get type
405                         switch (item.getItemType()) {
406                                 case "product": // Sellable product //NOI18N
407                                         assert (item.getItemProduct() instanceof Product) : MessageFormat.format("item {0} has no product instance set.", item); //NOI18N
408
409                                         // Get title
410                                         lastItem = item.getItemProduct().getProductTitle();
411                                         break;
412
413                                 default: // Not supported
414                                         throw new FacesException(MessageFormat.format("item type {0} is not supported.", item.getItemType())); //NOI18N
415                         }
416                 }
417
418                 // Trace message
419                 //this.getLogger().logTrace(MessageFormat.format("outputLastAddedItem: lastItem={0} - EXIT!", lastItem));
420
421                 // Return it
422                 return lastItem;
423         }
424
425         /**
426          * Getter for basket bean instance
427          *
428          * @return Basket bean instance
429          */
430         private BasketSessionBeanRemote getBasketBean () {
431                 return this.basketBean;
432         }
433
434         /**
435          * Somewhat getter for an item instance from given product instance. This
436          * method returns null if no item was found to given product. The product is
437          * found by checking it's id and itemType=product
438          *
439          * @param product Product instance
440          * @return Item instance or null if not found
441          */
442         private AddableBasketItem getItemFromProduct (final Product product) {
443                 // Trace message
444                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
445
446                 // Product must not be null
447                 if (null == product) {
448                         // Abort here
449                         throw new NullPointerException("product is null"); //NOI18N
450                 }
451
452                 // Create item instance
453                 AddableBasketItem foundItem = null;
454
455                 // Create fake instance
456                 AddableBasketItem fake = new BasketItem(product);
457
458                 // Debug message
459                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
460
461                 // Get all items
462                 List<AddableBasketItem> list = this.basket.getAll();
463
464                 // Debug message
465                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
466
467                 // Check all entries
468                 for (final AddableBasketItem item : list) {
469                         // Debug message
470                         //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
471
472                         // item must not be null
473                         if (null == item) {
474                                 // Abort here
475                                 throw new NullPointerException("item is null"); //NOI18N
476                         }
477
478                         // Is it the same?
479                         if (item.equals(fake)) {
480                                 // Set found item and abort look
481                                 foundItem = item;
482                                 break;
483                         }
484                 }
485
486                 // Trace message
487                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));
488
489                 // Return it
490                 return foundItem;
491         }
492 }