2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.checkout;
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;
53 * @author Roland Häder<roland@mxchange.org>
55 @Named ("checkoutController")
57 public class PizzaCheckoutWebSessionBean extends BasePizzaBean implements PizzaCheckoutWebSessionController {
62 private static final long serialVersionUID = 51_987_348_347_183L;
68 private PizzaBasketWebSessionController basketController;
71 * Bean helper instance
74 private PizzaWebRequestHelperController beanHelper;
77 * Event fired when a checkout was completed by the user
81 private Event<ObservableCheckoutCompletedEvent> checkoutCompletedEvent;
86 private Connection connection;
92 private PizzaContactWebRequestController contactController;
98 private PizzaCustomerWebSessionController customerController;
103 private ObjectMessage message;
108 private MessageProducer messageProducer;
119 private PizzaReceiptWebRequestController receiptController;
124 private Session session;
130 public void destroy () {
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);
143 public String doCheckout () {
144 // Are the beans set?
145 if (null == this.basketController) {
147 throw new NullPointerException("basketController is null"); //NOI18N
148 } else if (null == this.customerController) {
150 throw new NullPointerException("customer is null"); //NOI18N
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()) {
159 return "empty_basket"; //NOI18N
162 // Create customer instance
163 this.setCustomer(this.customerController.createCustomerInstance());
166 List<AddableBasketItem> list = this.basketController.allItems();
168 // Construct container
169 WrapableCheckout wrapper = new CheckoutWrapper();
170 wrapper.setCustomer(this.beanHelper.getCustomer());
171 wrapper.setList(list);
174 // Construct object message
175 this.message.setObject(wrapper);
178 this.messageProducer.send(this.message);
179 } catch (final JMSException ex) {
180 // @TODO: Log exception?
182 return "jms_failed"; //NOI18N
186 this.checkoutCompletedEvent.fire(new ShopCheckoutCompletedEvent(wrapper));
189 return "checkout_done"; //NOI18N
193 * Initialization of this bean
196 public void init () {
198 // Get initial context
199 Context context = new InitialContext();
201 // Get factory from JMS resource
202 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
205 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
208 this.connection = connectionFactory.createConnection();
210 // Init session instance
211 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
213 // And message producer
214 this.messageProducer = this.session.createProducer(this.queue);
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);