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