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