]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / checkout / PizzaCheckoutWebSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
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.checkout;
18
19 import java.util.List;
20 import javax.annotation.PostConstruct;
21 import javax.annotation.PreDestroy;
22 import javax.enterprise.context.SessionScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.jms.Connection;
29 import javax.jms.JMSException;
30 import javax.jms.MessageProducer;
31 import javax.jms.ObjectMessage;
32 import javax.jms.Queue;
33 import javax.jms.QueueConnectionFactory;
34 import javax.jms.Session;
35 import javax.naming.Context;
36 import javax.naming.InitialContext;
37 import javax.naming.NamingException;
38 import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent;
39 import org.mxchange.jshopcore.events.ShopCheckoutCompletedEvent;
40 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
41 import org.mxchange.jshopcore.wrapper.CheckoutWrapper;
42 import org.mxchange.jshopcore.wrapper.WrapableCheckout;
43 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
44 import org.mxchange.pizzaapplication.beans.basket.PizzaBasketWebSessionController;
45 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebRequestController;
46 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
47 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
48 import org.mxchange.pizzaapplication.beans.receipt.PizzaReceiptWebRequestController;
49
50 /**
51  * Checkout controller
52  * <p>
53  * @author Roland Häder<roland@mxchange.org>
54  */
55 @Named ("checkoutController")
56 @SessionScoped
57 public class PizzaCheckoutWebSessionBean extends BasePizzaBean implements PizzaCheckoutWebSessionController {
58
59         /**
60          * Serial number
61          */
62         private static final long serialVersionUID = 51_987_348_347_183L;
63
64         /**
65          * Basket bean
66          */
67         @Inject
68         private PizzaBasketWebSessionController basketController;
69
70         /**
71          * Bean helper instance
72          */
73         @Inject
74         private PizzaWebRequestHelperController beanHelper;
75
76         /**
77          * Event fired when a checkout was completed by the user
78          */
79         @Inject
80         @Any
81         private Event<ObservableCheckoutCompletedEvent> checkoutCompletedEvent;
82
83         /**
84          * Connection
85          */
86         private Connection connection;
87
88         /**
89          * Contact controller
90          */
91         @Inject
92         private PizzaContactWebRequestController contactController;
93
94         /**
95          * Customer bean
96          */
97         @Inject
98         private PizzaCustomerWebSessionController customerController;
99
100         /**
101          * Object message
102          */
103         private ObjectMessage message;
104
105         /**
106          * Message producer
107          */
108         private MessageProducer messageProducer;
109
110         /**
111          * Queue instance
112          */
113         private Queue queue;
114
115         /**
116          * Receipt bean
117          */
118         @Inject
119         private PizzaReceiptWebRequestController receiptController;
120
121         /**
122          * Session instance
123          */
124         private Session session;
125
126         /**
127          * Destructor
128          */
129         @PreDestroy
130         public void destroy () {
131                 try {
132                         // Try to close all
133                         this.messageProducer.close();
134                         this.session.close();
135                         this.connection.close();
136                 } catch (final JMSException ex) {
137                         // @TODO: Continue to throw is fine?
138                         throw new FacesException(ex);
139                 }
140         }
141
142         @Override
143         public String doCheckout () {
144                 // Are the beans set?
145                 if (null == this.basketController) {
146                         // Abort here
147                         throw new NullPointerException("basketController is null"); //NOI18N
148                 } else if (null == this.customerController) {
149                         // Abort here
150                         throw new NullPointerException("customer is null"); //NOI18N
151                 }
152
153                 // Are at least the required fields set?
154                 if (!this.contactController.isRequiredPersonalDataSet()) {
155                         // Not set, should not happen
156                         return "checkout2"; //NOI18N
157                 } else if (this.basketController.isEmpty()) {
158                         // Nothing to order
159                         return "empty_basket"; //NOI18N
160                 }
161
162                 // Create customer instance
163                 this.setCustomer(this.customerController.createCustomerInstance());
164
165                 // Get ordered list
166                 List<AddableBasketItem> list = this.basketController.allItems();
167
168                 // Construct container
169                 WrapableCheckout wrapper = new CheckoutWrapper();
170                 wrapper.setCustomer(this.beanHelper.getCustomer());
171                 wrapper.setList(list);
172
173                 try {
174                         // Construct object message
175                         this.message.setObject(wrapper);
176
177                         // Send message
178                         this.messageProducer.send(this.message);
179                 } catch (final JMSException ex) {
180                         // @TODO: Log exception?
181                         // Not working
182                         return "jms_failed"; //NOI18N
183                 }
184
185                 // Fire event
186                 this.checkoutCompletedEvent.fire(new ShopCheckoutCompletedEvent(wrapper));
187
188                 // All fine
189                 return "checkout_done"; //NOI18N
190         }
191
192         /**
193          * Initialization of this bean
194          */
195         @PostConstruct
196         public void init () {
197                 try {
198                         // Get initial context
199                         Context context = new InitialContext();
200
201                         // Get factory from JMS resource
202                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
203
204                         // Lookup queue
205                         this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
206
207                         // Create connection
208                         this.connection = connectionFactory.createConnection();
209
210                         // Init session instance
211                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
212
213                         // And message producer
214                         this.messageProducer = this.session.createProducer(this.queue);
215
216                         // Finally the message instance itself
217                         this.message = this.session.createObjectMessage();
218                 } catch (final NamingException | JMSException e) {
219                         // Continued to throw
220                         throw new FacesException(e);
221                 }
222         }
223
224 }