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