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.text.MessageFormat;
20 import java.util.List;
21 import javax.annotation.PostConstruct;
22 import javax.annotation.PreDestroy;
23 import javax.enterprise.context.SessionScoped;
24 import javax.faces.FacesException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.jms.Connection;
28 import javax.jms.JMSException;
29 import javax.jms.MessageProducer;
30 import javax.jms.ObjectMessage;
31 import javax.jms.Queue;
32 import javax.jms.QueueConnectionFactory;
33 import javax.jms.Session;
34 import javax.naming.Context;
35 import javax.naming.InitialContext;
36 import javax.naming.NamingException;
37 import org.mxchange.jcoreee.beans.BaseFrameworkBean;
38 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
39 import org.mxchange.jshopcore.model.customer.Customer;
40 import org.mxchange.jshopcore.wrapper.CheckoutWrapper;
41 import org.mxchange.jshopcore.wrapper.WrapableCheckout;
42 import org.mxchange.pizzaapplication.beans.basket.BasketWebController;
43 import org.mxchange.pizzaapplication.beans.customer.CustomerWebController;
44 import org.mxchange.pizzaapplication.beans.receipt.ReceiptWebController;
49 * @author Roland Haeder<roland@mxchange.org>
51 @Named ("checkoutController")
53 public class CheckoutWebBean extends BaseFrameworkBean implements CheckoutWebController {
58 private static final long serialVersionUID = 51_987_348_347_183L;
63 private Connection connection;
68 private MessageProducer messageProducer;
78 private Session session;
83 private ObjectMessage message;
88 private Customer customer;
90 ////////////////////// Bean injections ///////////////////////
95 private BasketWebController basketController;
101 private CustomerWebController customerController;
107 private ReceiptWebController receiptController;
110 public String doCheckout () {
112 this.getLogger().logTrace("doCheckout: CALLED!");
114 // Are the beans set?
115 if (null == this.basketController) {
117 throw new NullPointerException("basketController is null"); //NOI18N
118 } else if (null == this.customerController) {
120 throw new NullPointerException("customer is null"); //NOI18N
123 // Are at least the required fields set?
124 if (!this.customerController.isRequiredPersonalDataSet()) {
126 this.getLogger().logTrace("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!");
128 // Not set, should not happen
129 return "checkout2"; //NOI18N
130 } else if (this.basketController.isEmpty()) {
132 this.getLogger().logTrace("doCheckout: basket is empty, returning empty_basket ... - EXIT!");
135 return "empty_basket"; //NOI18N
138 // Create customer instance
139 this.setCustomer(this.customerController.createCustomerInstance());
142 this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer()));
145 List<AddableBasketItem> list = this.basketController.allItems();
148 this.getLogger().logTrace(MessageFormat.format("doCheckout: list={0}", list));
150 // Construct container
151 WrapableCheckout wrapper = new CheckoutWrapper();
152 wrapper.setCustomer(this.getCustomer());
153 wrapper.setList(list);
156 // Construct object message
157 this.message.setObject(wrapper);
160 this.messageProducer.send(this.message);
161 } catch (final JMSException ex) {
162 // TODO: Log exception?
164 return "jms_failed"; //NOI18N
168 this.basketController.clear();
170 // Set customer in receipt controller for verification
171 this.receiptController.setCustomer(this.getCustomer());
174 return "checkout_done"; //NOI18N
178 public Customer getCustomer () {
179 return this.customer;
183 public void setCustomer (final Customer customer) {
184 this.customer = customer;
188 public void init () {
189 // Call super init for getting resource bundle
193 // Get initial context
194 Context context = new InitialContext();
196 // Get factory from JMS resource
197 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
200 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
203 this.connection = connectionFactory.createConnection();
205 // Init session instance
206 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
208 // And message producer
209 this.messageProducer = session.createProducer(queue);
211 // Finally the message instance itself
212 this.message = this.session.createObjectMessage();
213 } catch (final NamingException | JMSException e) {
214 // Continued to throw
215 throw new FacesException(e);
223 public void destroy () {
226 this.messageProducer.close();
227 this.session.close();
228 this.connection.close();
229 } catch (final JMSException ex) {
230 // TODO: Continue to throw is fine?
231 throw new FacesException(ex);