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