2 * Copyright (C) 2015 Roland Haeder
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.
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.
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/>.
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.faces.FacesException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.jms.Connection;
27 import javax.jms.JMSException;
28 import javax.jms.MessageProducer;
29 import javax.jms.ObjectMessage;
30 import javax.jms.Queue;
31 import javax.jms.QueueConnectionFactory;
32 import javax.jms.Session;
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.NamingException;
36 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
37 import org.mxchange.jshopcore.model.customer.Customer;
38 import org.mxchange.jshopcore.wrapper.CheckoutWrapper;
39 import org.mxchange.jshopcore.wrapper.WrapableCheckout;
40 import org.mxchange.pizzaapplication.beans.basket.BasketWebController;
41 import org.mxchange.pizzaapplication.beans.customer.CustomerWebController;
42 import org.mxchange.pizzaapplication.beans.receipt.ReceiptWebController;
47 * @author Roland Haeder<roland@mxchange.org>
49 @Named ("checkoutController")
51 public class CheckoutWebBean implements CheckoutWebController {
56 private static final long serialVersionUID = 51_987_348_347_183L;
61 private Connection connection;
66 private MessageProducer messageProducer;
76 private Session session;
81 private ObjectMessage message;
86 private Customer customer;
88 ////////////////////// Bean injections ///////////////////////
93 private BasketWebController basketController;
99 private CustomerWebController customerController;
105 private ReceiptWebController receiptController;
108 public String doCheckout () {
110 //this.getLogger().logTrace("doCheckout: CALLED!");
112 // Are the beans set?
113 if (null == this.basketController) {
115 throw new NullPointerException("basketController is null"); //NOI18N
116 } else if (null == this.customerController) {
118 throw new NullPointerException("customer is null"); //NOI18N
121 // Are at least the required fields set?
122 if (!this.customerController.isRequiredPersonalDataSet()) {
124 //this.getLogger().logTrace("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!");
126 // Not set, should not happen
127 return "checkout2"; //NOI18N
128 } else if (this.basketController.isEmpty()) {
130 //this.getLogger().logTrace("doCheckout: basket is empty, returning empty_basket ... - EXIT!");
133 return "empty_basket"; //NOI18N
136 // Create customer instance
137 this.setCustomer(this.customerController.createCustomerInstance());
140 //this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer()));
143 List<AddableBasketItem> list = this.basketController.allItems();
146 //this.getLogger().logTrace(MessageFormat.format("doCheckout: list={0}", list));
148 // Construct container
149 WrapableCheckout wrapper = new CheckoutWrapper();
150 wrapper.setCustomer(this.getCustomer());
151 wrapper.setList(list);
154 // Construct object message
155 this.message.setObject(wrapper);
158 this.messageProducer.send(this.message);
159 } catch (final JMSException ex) {
160 // TODO: Log exception?
162 return "jms_failed"; //NOI18N
166 this.basketController.clear();
168 // Set customer in receipt controller for verification
169 this.receiptController.setCustomer(this.getCustomer());
172 return "checkout_done"; //NOI18N
176 public Customer getCustomer () {
177 return this.customer;
181 public void setCustomer (final Customer customer) {
182 this.customer = customer;
186 public void init () {
188 // Get initial context
189 Context context = new InitialContext();
191 // Get factory from JMS resource
192 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
195 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
198 this.connection = connectionFactory.createConnection();
200 // Init session instance
201 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
203 // And message producer
204 this.messageProducer = session.createProducer(queue);
206 // Finally the message instance itself
207 this.message = this.session.createObjectMessage();
208 } catch (final NamingException | JMSException e) {
209 // Continued to throw
210 throw new FacesException(e);
218 public void destroy () {
221 this.messageProducer.close();
222 this.session.close();
223 this.connection.close();
224 } catch (final JMSException ex) {
225 // TODO: Continue to throw is fine?
226 throw new FacesException(ex);