]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java
Continued a bit:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / checkout / PizzaCheckoutWebSessionBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.beans.checkout;
18
19 import java.util.List;
20 import javax.annotation.PostConstruct;
21 import javax.annotation.PreDestroy;
22 import javax.enterprise.context.SessionScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.FacesException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.jms.Connection;
29 import javax.jms.JMSException;
30 import javax.jms.MessageProducer;
31 import javax.jms.ObjectMessage;
32 import javax.jms.Queue;
33 import javax.jms.QueueConnectionFactory;
34 import javax.jms.Session;
35 import javax.naming.Context;
36 import javax.naming.InitialContext;
37 import javax.naming.NamingException;
38 import org.mxchange.jshopcore.events.ObservableCheckoutCompletedEvent;
39 import org.mxchange.jshopcore.events.ShopCheckoutCompletedEvent;
40 import org.mxchange.jshopcore.model.basket.AddableBasketItem;
41 import org.mxchange.jshopcore.wrapper.CheckoutWrapper;
42 import org.mxchange.jshopcore.wrapper.WrapableCheckout;
43 import org.mxchange.pizzaapplication.beans.BasePizzaController;
44 import org.mxchange.pizzaapplication.beans.basket.PizzaBasketWebSessionController;
45 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebSessionController;
46 import org.mxchange.pizzaapplication.beans.customer.PizzaCustomerWebSessionController;
47 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
48 import org.mxchange.pizzaapplication.beans.receipt.PizzaReceiptWebSessionController;
49
50 /**
51  * Checkout controller
52  * <p>
53  * @author Roland Häder<roland@mxchange.org>
54  */
55 @Named ("checkoutController")
56 @SessionScoped
57 public class PizzaCheckoutWebSessionBean extends BasePizzaController implements PizzaCheckoutWebSessionController {
58
59         /**
60          * Serial number
61          */
62         private static final long serialVersionUID = 51_987_348_347_183L;
63
64         ////////////////////// Bean injections ///////////////////////
65         /**
66          * Basket bean
67          */
68         @Inject
69         private PizzaBasketWebSessionController basketController;
70
71         /**
72          * Bean helper instance
73          */
74         @Inject
75         private PizzaWebRequestHelperController beanHelper;
76
77         /**
78          * Event fired when a checkout was completed by the user
79          */
80         @Inject
81         @Any
82         private Event<ObservableCheckoutCompletedEvent> checkoutCompletedEvent;
83
84         /**
85          * Connection
86          */
87         private Connection connection;
88
89         /**
90          * Contact controller
91          */
92         @Inject
93         private PizzaContactWebSessionController contactController;
94
95         /**
96          * Customer bean
97          */
98         @Inject
99         private PizzaCustomerWebSessionController customerController;
100
101         /**
102          * Object message
103          */
104         private ObjectMessage message;
105
106         /**
107          * Message producer
108          */
109         private MessageProducer messageProducer;
110
111         /**
112          * Queue instance
113          */
114         private Queue queue;
115
116         /**
117          * Receipt bean
118          */
119         @Inject
120         private PizzaReceiptWebSessionController receiptController;
121
122         /**
123          * Session instance
124          */
125         private Session session;
126
127         /**
128          * Destructor
129          */
130         @PreDestroy
131         public void destroy () {
132                 try {
133                         // Try to close all
134                         this.messageProducer.close();
135                         this.session.close();
136                         this.connection.close();
137                 } catch (final JMSException ex) {
138                         // @TODO: Continue to throw is fine?
139                         throw new FacesException(ex);
140                 }
141         }
142
143         @Override
144         public String doCheckout () {
145                 // Are the beans set?
146                 if (null == this.basketController) {
147                         // Abort here
148                         throw new NullPointerException("basketController is null"); //NOI18N
149                 } else if (null == this.customerController) {
150                         // Abort here
151                         throw new NullPointerException("customer is null"); //NOI18N
152                 }
153
154                 // Are at least the required fields set?
155                 if (!this.contactController.isRequiredPersonalDataSet()) {
156                         // Not set, should not happen
157                         return "checkout2"; //NOI18N
158                 } else if (this.basketController.isEmpty()) {
159                         // Nothing to order
160                         return "empty_basket"; //NOI18N
161                 }
162
163                 // Create customer instance
164                 this.setCustomer(this.customerController.createCustomerInstance());
165
166                 // Get ordered list
167                 List<AddableBasketItem> list = this.basketController.allItems();
168
169                 // Construct container
170                 WrapableCheckout wrapper = new CheckoutWrapper();
171                 wrapper.setCustomer(this.beanHelper.getCustomer());
172                 wrapper.setList(list);
173
174                 try {
175                         // Construct object message
176                         this.message.setObject(wrapper);
177
178                         // Send message
179                         this.messageProducer.send(this.message);
180                 } catch (final JMSException ex) {
181                         // @TODO: Log exception?
182                         // Not working
183                         return "jms_failed"; //NOI18N
184                 }
185
186                 // Fire event
187                 this.checkoutCompletedEvent.fire(new ShopCheckoutCompletedEvent(wrapper));
188
189                 // All fine
190                 return "checkout_done"; //NOI18N
191         }
192
193         /**
194          * Initialization of this bean
195          */
196         @PostConstruct
197         public void init () {
198                 try {
199                         // Get initial context
200                         Context context = new InitialContext();
201
202                         // Get factory from JMS resource
203                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
204
205                         // Lookup queue
206                         this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
207
208                         // Create connection
209                         this.connection = connectionFactory.createConnection();
210
211                         // Init session instance
212                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
213
214                         // And message producer
215                         this.messageProducer = this.session.createProducer(this.queue);
216
217                         // Finally the message instance itself
218                         this.message = this.session.createObjectMessage();
219                 } catch (final NamingException | JMSException e) {
220                         // Continued to throw
221                         throw new FacesException(e);
222                 }
223         }
224
225 }