2 * Copyright (C) 2016 Roland Haeder
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.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.jcustomercore.model.customer.Customer;
37 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
38 import org.mxchange.jshopcore.wrapper.CheckoutWrapper;
39 import org.mxchange.jshopcore.wrapper.WrapableCheckout;
40 import org.mxchange.pizzaapplication.beans.basket.PizzaBasketWebSessionController;
41 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
42 import org.mxchange.pizzaapplication.beans.receipt.PizzaReceiptWebSessionController;
47 * @author Roland Haeder<roland@mxchange.org>
49 @Named ("checkoutController")
51 public class PizzaCheckoutWebSessionBean implements PizzaCheckoutWebSessionController {
56 private static final long serialVersionUID = 51_987_348_347_183L;
58 ////////////////////// Bean injections ///////////////////////
63 private PizzaBasketWebSessionController basketController;
68 private Connection connection;
73 private Customer customer;
79 private PizzaCustomerWebSessionController customerController;
84 private ObjectMessage message;
89 private MessageProducer messageProducer;
100 private PizzaReceiptWebSessionController receiptController;
105 private Session session;
111 public void destroy () {
114 this.messageProducer.close();
115 this.session.close();
116 this.connection.close();
117 } catch (final JMSException ex) {
118 // TODO: Continue to throw is fine?
119 throw new FacesException(ex);
124 public String doCheckout () {
126 //this.getLogger().logTrace("doCheckout: CALLED!");
128 // Are the beans set?
129 if (null == this.basketController) {
131 throw new NullPointerException("basketController is null"); //NOI18N
132 } else if (null == this.customerController) {
134 throw new NullPointerException("customer is null"); //NOI18N
137 // Are at least the required fields set?
138 if (!this.customerController.isRequiredPersonalDataSet()) {
140 //this.getLogger().logTrace("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!");
142 // Not set, should not happen
143 return "checkout2"; //NOI18N
144 } else if (this.basketController.isEmpty()) {
146 //this.getLogger().logTrace("doCheckout: basket is empty, returning empty_basket ... - EXIT!");
149 return "empty_basket"; //NOI18N
152 // Create customer instance
153 this.setCustomer(this.customerController.createCustomerInstance());
156 //this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer()));
158 List<AddableBasketItem> list = this.basketController.allItems();
161 //this.getLogger().logTrace(MessageFormat.format("doCheckout: list={0}", list));
162 // Construct container
163 WrapableCheckout wrapper = new CheckoutWrapper();
164 wrapper.setCustomer(this.getCustomer());
165 wrapper.setList(list);
168 // Construct object message
169 this.message.setObject(wrapper);
172 this.messageProducer.send(this.message);
173 } catch (final JMSException ex) {
174 // TODO: Log exception?
176 return "jms_failed"; //NOI18N
180 this.basketController.clear();
182 // Set customer in receipt controller for verification
183 this.receiptController.setCustomer(this.getCustomer());
186 return "checkout_done"; //NOI18N
190 public Customer getCustomer () {
191 return this.customer;
195 public void setCustomer (final Customer customer) {
196 this.customer = customer;
200 * Initialization of this bean
203 public void init () {
205 // Get initial context
206 Context context = new InitialContext();
208 // Get factory from JMS resource
209 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
212 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
215 this.connection = connectionFactory.createConnection();
217 // Init session instance
218 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
220 // And message producer
221 this.messageProducer = this.session.createProducer(this.queue);
223 // Finally the message instance itself
224 this.message = this.session.createObjectMessage();
225 } catch (final NamingException | JMSException e) {
226 // Continued to throw
227 throw new FacesException(e);