]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/checkout/PizzaCheckoutWebSessionBean.java
these are noisy debug lines ...
[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                 //* NOISY-DEBUG: */ System.out.println("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                         //* NOISY-DEBUG: */ System.out.println("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                         //* NOISY-DEBUG: */ System.out.println("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
165                 // Get ordered list
166                 List<AddableBasketItem> list = this.basketController.allItems();
167
168                 // Debug message
169                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("doCheckout: list={0}", list));
170
171                 // Construct container
172                 WrapableCheckout wrapper = new CheckoutWrapper();
173                 wrapper.setCustomer(this.getCustomer());
174                 wrapper.setList(list);
175
176                 try {
177                         // Construct object message
178                         this.message.setObject(wrapper);
179
180                         // Send message
181                         this.messageProducer.send(this.message);
182                 } catch (final JMSException ex) {
183                         // TODO: Log exception?
184                         // Not working
185                         return "jms_failed"; //NOI18N
186                 }
187
188                 // Clear basket
189                 this.basketController.clear();
190
191                 // Set customer in receipt controller for verification
192                 this.receiptController.setCustomer(this.getCustomer());
193
194                 // All fine
195                 return "checkout_done"; //NOI18N
196         }
197
198         @Override
199         public Customer getCustomer () {
200                 return this.customer;
201         }
202
203         @Override
204         public void setCustomer (final Customer customer) {
205                 this.customer = customer;
206         }
207
208         /**
209          * Initialization of this bean
210          */
211         @PostConstruct
212         public void init () {
213                 try {
214                         // Get initial context
215                         Context context = new InitialContext();
216
217                         // Get factory from JMS resource
218                         QueueConnectionFactory connectionFactory = (QueueConnectionFactory) context.lookup("jms/shopConnectionFactory"); //NOI18N
219
220                         // Lookup queue
221                         this.queue = (Queue) context.lookup("jms/shopCheckoutQueue"); //NOI18N
222
223                         // Create connection
224                         this.connection = connectionFactory.createConnection();
225
226                         // Init session instance
227                         this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
228
229                         // And message producer
230                         this.messageProducer = this.session.createProducer(this.queue);
231
232                         // Finally the message instance itself
233                         this.message = this.session.createObjectMessage();
234                 } catch (final NamingException | JMSException e) {
235                         // Continued to throw
236                         throw new FacesException(e);
237                 }
238         }
239
240 }