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;
48 * @author Roland Haeder<roland@mxchange.org>
50 @Named ("checkoutController")
52 public class CheckoutWebBean extends BaseFrameworkBean implements CheckoutWebController {
57 private static final long serialVersionUID = 51_987_348_347_183L;
62 private Connection connection;
67 private MessageProducer messageProducer;
77 private Session session;
82 private ObjectMessage message;
84 ////////////////////// Bean injections ///////////////////////
89 private BasketWebController basketController;
95 private CustomerWebController customerController;
98 public String doCheckout () {
100 this.getLogger().logTrace("doCheckout: CALLED!");
102 // Are the beans set?
103 if (null == this.basketController) {
105 throw new NullPointerException("basketController is null"); //NOI18N
106 } else if (null == this.customerController) {
108 throw new NullPointerException("customer is null"); //NOI18N
111 // Are at least the required fields set?
112 if (!this.customerController.isRequiredPersonalDataSet()) {
114 this.getLogger().logTrace("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!");
116 // Not set, should not happen
117 return "checkout2"; //NOI18N
118 } else if (this.basketController.isEmpty()) {
120 this.getLogger().logTrace("doCheckout: basket is empty, returning empty_basket ... - EXIT!");
123 return "empty_basket"; //NOI18N
126 // Create customer instance
127 Customer customer = this.customerController.createInstance();
130 this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", customer));
133 List<AddableBasketItem> list = this.basketController.allItems();
136 this.getLogger().logTrace(MessageFormat.format("doCheckout: list={0}", list));
138 // Construct container
139 WrapableCheckout wrapper = new CheckoutWrapper();
140 wrapper.setCustomer(customer);
141 wrapper.setList(list);
144 // Construct object message
145 this.message.setObject(wrapper);
148 this.messageProducer.send(this.message);
149 } catch (final JMSException ex) {
150 // TODO: Log exception?
152 return "jms_failed"; //NOI18N
156 this.basketController.clear();
159 return "checkout_done"; //NOI18N
163 public void init () {
164 // Call super init for getting resource bundle
168 // Get initial context
169 Context context = new InitialContext();
171 // Get factory from JMS resource
172 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
175 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
178 this.connection = connectionFactory.createConnection();
180 // Init session instance
181 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
183 // And message producer
184 this.messageProducer = session.createProducer(queue);
186 // Finally the message instance itself
187 this.message = this.session.createObjectMessage();
188 } catch (final NamingException | JMSException e) {
189 // Continued to throw
190 throw new FacesException(e);
198 public void destroy () {
201 this.messageProducer.close();
202 this.session.close();
203 this.connection.close();
204 } catch (final JMSException ex) {
205 // TODO: Continue to throw is fine?
206 throw new FacesException(ex);