]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java
Some rewrites:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / checkout / PizzaCheckoutWebSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.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;
43
44 /**
45  * Checkout controller
46  * <p>
47  * @author Roland Haeder<roland@mxchange.org>
48  */
49 @Named ("checkoutController")
50 @SessionScoped
51 public class PizzaCheckoutWebSessionBean implements PizzaCheckoutWebSessionController {
52
53         /**
54          * Serial number
55          */
56         private static final long serialVersionUID = 51_987_348_347_183L;
57
58         ////////////////////// Bean injections ///////////////////////
59         /**
60          * Basket bean
61          */
62         @Inject
63         private PizzaBasketWebSessionController basketController;
64
65         /**
66          * Connection
67          */
68         private Connection connection;
69
70         /**
71          * Customer instance
72          */
73         private Customer customer;
74
75         /**
76          * Customer bean
77          */
78         @Inject
79         private PizzaCustomerWebSessionController customerController;
80
81         /**
82          * Object message
83          */
84         private ObjectMessage message;
85
86         /**
87          * Message producer
88          */
89         private MessageProducer messageProducer;
90
91         /**
92          * Queue instance
93          */
94         private Queue queue;
95
96         /**
97          * Receipt bean
98          */
99         @Inject
100         private PizzaReceiptWebSessionController receiptController;
101
102         /**
103          * Session instance
104          */
105         private Session session;
106
107         /**
108          * Destructor
109          */
110         @PreDestroy
111         public void destroy () {
112                 try {
113                         // Try to close all
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);
120                 }
121         }
122
123         @Override
124         public String doCheckout () {
125                 // Trace message
126                 //this.getLogger().logTrace("doCheckout: CALLED!");
127
128                 // Are the beans set?
129                 if (null == this.basketController) {
130                         // Abort here
131                         throw new NullPointerException("basketController is null"); //NOI18N
132                 } else if (null == this.customerController) {
133                         // Abort here
134                         throw new NullPointerException("customer is null"); //NOI18N
135                 }
136
137                 // Are at least the required fields set?
138                 if (!this.customerController.isRequiredPersonalDataSet()) {
139                         // Trace message
140                         //this.getLogger().logTrace("doCheckout: Not all required fields are set, returning checkout2 ... - EXIT!");
141
142                         // Not set, should not happen
143                         return "checkout2"; //NOI18N
144                 } else if (this.basketController.isEmpty()) {
145                         // Trace message
146                         //this.getLogger().logTrace("doCheckout: basket is empty, returning empty_basket ... - EXIT!");
147
148                         // Nothing to order
149                         return "empty_basket"; //NOI18N
150                 }
151
152                 // Create customer instance
153                 this.setCustomer(this.customerController.createCustomerInstance());
154
155                 // Debug message
156                 //this.getLogger().logDebug(MessageFormat.format("doCheckout: customer={0}", this.getCustomer()));
157                 // Get ordered list
158                 List<AddableBasketItem> list = this.basketController.allItems();
159
160                 // Debug message
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);
166
167                 try {
168                         // Construct object message
169                         this.message.setObject(wrapper);
170
171                         // Send message
172                         this.messageProducer.send(this.message);
173                 } catch (final JMSException ex) {
174                         // TODO: Log exception?
175                         // Not working
176                         return "jms_failed"; //NOI18N
177                 }
178
179                 // Clear basket
180                 this.basketController.clear();
181
182                 // Set customer in receipt controller for verification
183                 this.receiptController.setCustomer(this.getCustomer());
184
185                 // All fine
186                 return "checkout_done"; //NOI18N
187         }
188
189         @Override
190         public Customer getCustomer () {
191                 return this.customer;
192         }
193
194         @Override
195         public void setCustomer (final Customer customer) {
196                 this.customer = customer;
197         }
198
199         /**
200          * Initialization of this bean
201          */
202         @PostConstruct
203         public void init () {
204                 try {
205                         // Get initial context
206                         Context context = new InitialContext();
207
208                         // Get factory from JMS resource
209                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
210
211                         // Lookup queue
212                         this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
213
214                         // Create connection
215                         this.connection = connectionFactory.createConnection();
216
217                         // Init session instance
218                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
219
220                         // And message producer
221                         this.messageProducer = this.session.createProducer(this.queue);
222
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);
228                 }
229         }
230 }