]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/basket/BasketWebSessionBean.java
Renamed controller method to doChangeItem() (naming convention)
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / basket / BasketWebSessionBean.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 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          * 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 BasketWebSessionBean () {
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 doChangeItem (final AddableBasketItem item) {
227                 // Trace message
228                 //this.getLogger().logTrace(MessageFormat.format("doChangeItem: 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("doChangeItem: 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 AddableBasketItem getCurrentItem () {
264                 return this.currentItem;
265         }
266
267         @Override
268         public void setCurrentItem (final AddableBasketItem currentItem) {
269                 this.currentItem = currentItem;
270         }
271
272         @Override
273         public Long getItemAmount (final Product product) {
274                 // Trace message
275                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: product={0} - CALLED!", product));
276
277                 // product should not be null
278                 if (null == product) {
279                         // Abort here
280                         throw new NullPointerException("product is null");
281                 }
282
283                 // Initial value is zero
284                 Long itemAmount = 0L;
285
286                 // Iterate over all
287                 for (final AddableBasketItem item : this.allItems()) {
288                         // Debug message
289                         //this.getLogger().logDebug(MessageFormat.format("getItemAmount: item={0}", item));
290
291                         // Is this product instance and same?
292                         if (null == item) {
293                                 // item is null
294                                 throw new NullPointerException("item is null");
295                         } else if ((item.isProductType()) && (item.getItemProduct().equals(product))) {
296                                 // Found it
297                                 itemAmount = item.getOrderedAmount();
298                                 break;
299                         }
300                 }
301
302                 // Trace message
303                 //this.getLogger().logTrace(MessageFormat.format("getItemAmount: itemAmount={0} - EXIT!", itemAmount));
304                 // Return it
305                 return itemAmount;
306         }
307
308         @Override
309         public AddableBasketItem getLast () {
310                 // Deligate to basket instance
311                 return this.basket.getLast();
312         }
313
314         @Override
315         public int getLastNumRows () {
316                 // Deligate to basket instance
317                 return this.basket.getLastNumRows();
318         }
319
320         @Override
321         public Long getOrderedAmount () {
322                 return this.orderedAmount;
323         }
324
325         @Override
326         public void setOrderedAmount (final Long orderedAmount) {
327                 this.orderedAmount = orderedAmount;
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          * <p>
429          * @return Item instance or null if not found
430          */
431         private AddableBasketItem getItemFromProduct (final Product product) {
432                 // Trace message
433                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: product={0} - CALLED!", product));
434
435                 // Product must not be null
436                 if (null == product) {
437                         // Abort here
438                         throw new NullPointerException("product is null"); //NOI18N
439                 }
440
441                 // Create item instance
442                 AddableBasketItem foundItem = null;
443
444                 // Create fake instance
445                 AddableBasketItem fake = new BasketItem(product);
446
447                 // Debug message
448                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: fake={0}", fake));
449                 // Get all items
450                 List<AddableBasketItem> list = this.basket.getAll();
451
452                 // Debug message
453                 //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: list={0}", list));
454                 // Check all entries
455                 for (final AddableBasketItem item : list) {
456                         // Debug message
457                         //this.getLogger().logDebug(MessageFormat.format("getItemFromProduct: item={0}", item));
458
459                         // item must not be null
460                         if (null == item) {
461                                 // Abort here
462                                 throw new NullPointerException("item is null"); //NOI18N
463                         }
464
465                         // Is it the same?
466                         if (item.equals(fake)) {
467                                 // Set found item and abort look
468                                 foundItem = item;
469                                 break;
470                         }
471                 }
472
473                 // Trace message
474                 //this.getLogger().logTrace(MessageFormat.format("getItemFromProduct: foundItem={0} - EXIT!", foundItem));
475                 // Return it
476                 return foundItem;
477         }
478 }