2 * Copyright (C) 2016, 2017 Roland Häder
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.BasePizzaController;
44 import org.mxchange.pizzaapplication.beans.basket.PizzaBasketWebSessionController;
45 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
46 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
47 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
48 import org.mxchange.pizzaapplication.beans.receipt.PizzaReceiptWebSessionController;
53 * @author Roland Häder<roland@mxchange.org>
55 @Named ("checkoutController")
57 public class PizzaCheckoutWebSessionBean extends BasePizzaController implements PizzaCheckoutWebSessionController {
62 private static final long serialVersionUID = 51_987_348_347_183L;
64 ////////////////////// Bean injections ///////////////////////
69 private PizzaBasketWebSessionController basketController;
72 * Bean helper instance
75 private PizzaWebRequestHelperController beanHelper;
78 * Event fired when a checkout was completed by the user
82 private Event<ObservableCheckoutCompletedEvent> checkoutCompletedEvent;
87 private Connection connection;
93 private PizzaContactWebSessionController contactController;
99 private PizzaCustomerWebSessionController customerController;
104 private ObjectMessage message;
109 private MessageProducer messageProducer;
120 private PizzaReceiptWebSessionController receiptController;
125 private Session session;
131 public void destroy () {
134 this.messageProducer.close();
135 this.session.close();
136 this.connection.close();
137 } catch (final JMSException ex) {
138 // @TODO: Continue to throw is fine?
139 throw new FacesException(ex);
144 public String doCheckout () {
145 // Are the beans set?
146 if (null == this.basketController) {
148 throw new NullPointerException("basketController is null"); //NOI18N
149 } else if (null == this.customerController) {
151 throw new NullPointerException("customer is null"); //NOI18N
154 // Are at least the required fields set?
155 if (!this.contactController.isRequiredPersonalDataSet()) {
156 // Not set, should not happen
157 return "checkout2"; //NOI18N
158 } else if (this.basketController.isEmpty()) {
160 return "empty_basket"; //NOI18N
163 // Create customer instance
164 this.setCustomer(this.customerController.createCustomerInstance());
167 List<AddableBasketItem> list = this.basketController.allItems();
169 // Construct container
170 WrapableCheckout wrapper = new CheckoutWrapper();
171 wrapper.setCustomer(this.beanHelper.getCustomer());
172 wrapper.setList(list);
175 // Construct object message
176 this.message.setObject(wrapper);
179 this.messageProducer.send(this.message);
180 } catch (final JMSException ex) {
181 // @TODO: Log exception?
183 return "jms_failed"; //NOI18N
187 this.checkoutCompletedEvent.fire(new ShopCheckoutCompletedEvent(wrapper));
190 return "checkout_done"; //NOI18N
194 * Initialization of this bean
197 public void init () {
199 // Get initial context
200 Context context = new InitialContext();
202 // Get factory from JMS resource
203 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
206 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
209 this.connection = connectionFactory.createConnection();
211 // Init session instance
212 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
214 // And message producer
215 this.messageProducer = this.session.createProducer(this.queue);
217 // Finally the message instance itself
218 this.message = this.session.createObjectMessage();
219 } catch (final NamingException | JMSException e) {
220 // Continued to throw
221 throw new FacesException(e);