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.contact.PizzaContactWebSessionController;
42 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
43 import org.mxchange.pizzaapplication.beans.receipt.PizzaReceiptWebSessionController;
48 * @author Roland Haeder<roland@mxchange.org>
50 @Named ("checkoutController")
52 public class PizzaCheckoutWebSessionBean implements PizzaCheckoutWebSessionController {
57 private static final long serialVersionUID = 51_987_348_347_183L;
59 ////////////////////// Bean injections ///////////////////////
64 private PizzaBasketWebSessionController basketController;
69 private Connection connection;
75 private PizzaContactWebSessionController contactController;
80 private Customer customer;
86 private PizzaCustomerWebSessionController customerController;
91 private ObjectMessage message;
96 private MessageProducer messageProducer;
107 private PizzaReceiptWebSessionController receiptController;
112 private Session session;
118 public void destroy () {
121 this.messageProducer.close();
122 this.session.close();
123 this.connection.close();
124 } catch (final JMSException ex) {
125 // TODO: Continue to throw is fine?
126 throw new FacesException(ex);
131 public String doCheckout () {
133 //* NOISY-DEBUG: */ System.out.println("doCheckout: CALLED!");
135 // Are the beans set?
136 if (null == this.basketController) {
138 throw new NullPointerException("basketController is null"); //NOI18N
139 } else if (null == this.customerController) {
141 throw new NullPointerException("customer is null"); //NOI18N
144 // Are at least the required fields set?
145 if (!this.contactController.isRequiredPersonalDataSet()) {
147 //* NOISY-DEBUG: */ System.out.println("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!");
149 // Not set, should not happen
150 return "checkout2"; //NOI18N
151 } else if (this.basketController.isEmpty()) {
153 //* NOISY-DEBUG: */ System.out.println("doCheckout: basket is empty, returning empty_basket ... - EXIT!");
156 return "empty_basket"; //NOI18N
159 // Create customer instance
160 this.setCustomer(this.customerController.createCustomerInstance());
163 //this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer()));
166 List<AddableBasketItem> list = this.basketController.allItems();
169 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doCheckout: list={0}", list));
171 // Construct container
172 WrapableCheckout wrapper = new CheckoutWrapper();
173 wrapper.setCustomer(this.getCustomer());
174 wrapper.setList(list);
177 // Construct object message
178 this.message.setObject(wrapper);
181 this.messageProducer.send(this.message);
182 } catch (final JMSException ex) {
183 // TODO: Log exception?
185 return "jms_failed"; //NOI18N
189 this.basketController.clear();
191 // Set customer in receipt controller for verification
192 this.receiptController.setCustomer(this.getCustomer());
195 return "checkout_done"; //NOI18N
199 public Customer getCustomer () {
200 return this.customer;
204 public void setCustomer (final Customer customer) {
205 this.customer = customer;
209 * Initialization of this bean
212 public void init () {
214 // Get initial context
215 Context context = new InitialContext();
217 // Get factory from JMS resource
218 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
221 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
224 this.connection = connectionFactory.createConnection();
226 // Init session instance
227 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
229 // And message producer
230 this.messageProducer = this.session.createProducer(this.queue);
232 // Finally the message instance itself
233 this.message = this.session.createObjectMessage();
234 } catch (final NamingException | JMSException e) {
235 // Continued to throw
236 throw new FacesException(e);