]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / customer / PizzaAdminCustomerWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
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.customer;
18
19 import java.text.MessageFormat;
20 import javax.annotation.PostConstruct;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.event.Observes;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcontacts.model.contact.Contact;
32 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
33 import org.mxchange.jcustomercore.events.customer.added.AdminAddedCustomerEvent;
34 import org.mxchange.jcustomercore.events.customer.added.ObservableAdminAddedCustomerEvent;
35 import org.mxchange.jcustomercore.events.customer.created.ObservableCreatedCustomerEvent;
36 import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
37 import org.mxchange.jcustomercore.model.customer.Customer;
38 import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus;
39 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
40 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
41 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
42 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
43 import org.mxchange.jcustomercore.model.customer.ContactCustomer;
44
45 /**
46  * Administrative customer bean (controller)
47  * <p>
48  * @author Roland Häder<roland@mxchange.org>
49  */
50 @Named ("adminCustomerController")
51 @RequestScoped
52 public class PizzaAdminCustomerWebRequestBean extends BasePizzaBean implements PizzaAdminCustomerWebRequestController {
53
54         /**
55          * Serial number
56          */
57         private static final long serialVersionUID = 12_487_062_487_527_913L;
58
59         /**
60          * Administrative contact controller (for personal data)
61          */
62         @Inject
63         private PizzaAdminContactWebRequestController adminContactController;
64
65         /**
66          * Administrative customer EJB
67          */
68         private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
69
70         /**
71          * Bean helper instance
72          */
73         @Inject
74         private PizzaWebRequestHelperController beanHelper;
75
76         /**
77          * Contact instance
78          */
79         private Contact contact;
80
81         /**
82          * An event being fired when an administrator has added a new customer
83          */
84         @Inject
85         @Any
86         private Event<ObservableAdminAddedCustomerEvent> customerAddedEvent;
87
88         /**
89          * General customer controller
90          */
91         @Inject
92         private PizzaCustomerWebSessionController customerController;
93
94         /**
95          * Default constructor
96          */
97         public PizzaAdminCustomerWebRequestBean () {
98         }
99
100         /**
101          * Adds customer to database if not already added. This method should return
102          * a redirect outcome on success.
103          * <p>
104          * @return Redirect outcome
105          */
106         public String addCustomer () {
107                 // Are at least some fields added?
108                 if ((this.getContact() == null) && (!this.isCustomerDataSet())) {
109                         // Not all customer data is set
110                         throw new FaceletException("Please provide minimum personal data: gender, first_name, family_name"); //NOI18N
111                 }
112
113                 // Init contact
114                 Contact customerContact;
115
116                 // Is the contact set in helper?
117                 if (this.getContact() instanceof Contact) {
118                         // Get from helper
119                         customerContact = this.getContact();
120                 } else {
121                         // Get new contact instance
122                         customerContact = this.adminContactController.createContactInstance();
123                 }
124
125                 // Ask the EJB for a free customer number
126                 String customerNumber = this.adminCustomerBean.createCustomerNumber();
127
128                 // Create new customer instance
129                 Customer customer = new ContactCustomer(CustomerAccountStatus.CONFIRMED, customerContact, customerNumber);
130
131                 // Init instance
132                 Customer updatedCustomer;
133
134                 try {
135                         // Add/link customer and return updated
136                         if (this.getContact() instanceof Contact) {
137                                 // Link customer
138                                 updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
139
140                                 // Remove contact instance
141                                 this.setContact(null);
142                         } else {
143                                 // Add new customer instance
144                                 updatedCustomer = this.adminCustomerBean.addCustomer(customer);
145                         }
146                 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
147                         // Throw again
148                         throw new FaceletException(ex);
149                 }
150
151                 // Fire event
152                 this.customerAddedEvent.fire(new AdminAddedCustomerEvent(updatedCustomer));
153
154                 // Redirect
155                 return "admin_list_customer"; //NOI18N
156         }
157
158         /**
159          * Observes an even when a customer has been created
160          * <p>
161          * @param event Event being fired
162          */
163         public void afterCustomerCreatedEvent (final @Observes ObservableCreatedCustomerEvent event) {
164                 // The event instance must be valid
165                 if (null == event) {
166                         // Throw NPE again
167                         throw new NullPointerException("event is null"); //NOI18N
168                 } else if (event.getCreatedCustomer() == null) {
169                         // Throw NPE
170                         throw new NullPointerException("event.createdCustomer is null"); //NOI18N
171                 } else if (event.getCreatedCustomer().getCustomerId() == null) {
172                         // Throw again ...
173                         throw new NullPointerException("event.createdCustomer.customerId is null"); //NOI18N
174                 } else if (event.getCreatedCustomer().getCustomerId() < 1) {
175                         // Invalid id number
176                         throw new IllegalArgumentException(MessageFormat.format("event.createdCustomer.customerId={0} is not valid", event.getCreatedCustomer().getCustomerId())); //NOI18N
177                 } else if (event.getCreatedCustomer().getCustomerContact() == null) {
178                         // Throw NPE again
179                         throw new NullPointerException("event.createdCustomer.customerContact is null"); //NOI18N
180                 } else if (event.getCreatedCustomer().getCustomerContact().getContactId() == null) {
181                         // .. and again
182                         throw new NullPointerException("event.createdCustomer.customerContact.contactId is null"); //NOI18N
183                 } else if (event.getCreatedCustomer().getCustomerContact().getContactId() < 1) {
184                         // Invalid id
185                         throw new IllegalArgumentException(MessageFormat.format("event.createdCustomer.customerContact.contactId={0} is not valid", event.getCreatedCustomer().getCustomerContact().getContactId())); //NOI18N
186                 }
187
188                 // @TODO Set all data
189         }
190
191         /**
192          * Getter for contact instance
193          * <p>
194          * @return Contact instance
195          */
196         public Contact getContact () {
197                 return this.contact;
198         }
199
200         /**
201          * Setter for contact instance
202          * <p>
203          * @param contact Contact instance
204          */
205         public void setContact (final Contact contact) {
206                 this.contact = contact;
207         }
208
209         /**
210          * Post-initialization of this class
211          */
212         @PostConstruct
213         public void init () {
214                 // Try it
215                 try {
216                         // Get initial context
217                         Context context = new InitialContext();
218
219                         // Try to lookup
220                         this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
221                 } catch (final NamingException e) {
222                         // Throw again
223                         throw new FaceletException(e);
224                 }
225         }
226
227         /**
228          * Checks whether the minimum customer data is set
229          * <p>
230          * @return Whether minimum data is set
231          */
232         private boolean isCustomerDataSet () {
233                 // Check all
234                 return this.adminContactController.isRequiredPersonalDataSet();
235         }
236
237 }