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