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.BasePizzaController;
41 import org.mxchange.pizzaapplication.beans.basket.PizzaBasketWebSessionController;
42 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
43 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
44 import org.mxchange.pizzaapplication.beans.receipt.PizzaReceiptWebSessionController;
49 * @author Roland Haeder<roland@mxchange.org>
51 @Named ("checkoutController")
53 public class PizzaCheckoutWebSessionBean extends BasePizzaController implements PizzaCheckoutWebSessionController {
58 private static final long serialVersionUID = 51_987_348_347_183L;
60 ////////////////////// Bean injections ///////////////////////
65 private PizzaBasketWebSessionController basketController;
70 private Connection connection;
76 private PizzaContactWebSessionController contactController;
81 private Customer customer;
87 private PizzaCustomerWebSessionController customerController;
92 private ObjectMessage message;
97 private MessageProducer messageProducer;
108 private PizzaReceiptWebSessionController receiptController;
113 private Session session;
119 public void destroy () {
122 this.messageProducer.close();
123 this.session.close();
124 this.connection.close();
125 } catch (final JMSException ex) {
126 // TODO: Continue to throw is fine?
127 throw new FacesException(ex);
132 public String doCheckout () {
134 //* NOISY-DEBUG: */ System.out.println("doCheckout: CALLED!");
136 // Are the beans set?
137 if (null == this.basketController) {
139 throw new NullPointerException("basketController is null"); //NOI18N
140 } else if (null == this.customerController) {
142 throw new NullPointerException("customer is null"); //NOI18N
145 // Are at least the required fields set?
146 if (!this.contactController.isRequiredPersonalDataSet()) {
148 //* NOISY-DEBUG: */ System.out.println("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!");
150 // Not set, should not happen
151 return "checkout2"; //NOI18N
152 } else if (this.basketController.isEmpty()) {
154 //* NOISY-DEBUG: */ System.out.println("doCheckout: basket is empty, returning empty_basket ... - EXIT!");
157 return "empty_basket"; //NOI18N
160 // Create customer instance
161 this.setCustomer(this.customerController.createCustomerInstance());
164 //this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer()));
167 List<AddableBasketItem> list = this.basketController.allItems();
170 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doCheckout: list={0}", list));
172 // Construct container
173 WrapableCheckout wrapper = new CheckoutWrapper();
174 wrapper.setCustomer(this.getCustomer());
175 wrapper.setList(list);
178 // Construct object message
179 this.message.setObject(wrapper);
182 this.messageProducer.send(this.message);
183 } catch (final JMSException ex) {
184 // TODO: Log exception?
186 return "jms_failed"; //NOI18N
190 this.basketController.clear();
192 // Set customer in receipt controller for verification
193 this.receiptController.setCustomer(this.getCustomer());
196 return "checkout_done"; //NOI18N
200 public Customer getCustomer () {
201 return this.customer;
205 public void setCustomer (final Customer customer) {
206 this.customer = customer;
210 * Initialization of this bean
213 public void init () {
215 // Get initial context
216 Context context = new InitialContext();
218 // Get factory from JMS resource
219 QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
222 this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
225 this.connection = connectionFactory.createConnection();
227 // Init session instance
228 this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
230 // And message producer
231 this.messageProducer = this.session.createProducer(this.queue);
233 // Finally the message instance itself
234 this.message = this.session.createObjectMessage();
235 } catch (final NamingException | JMSException e) {
236 // Continued to throw
237 throw new FacesException(e);