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