]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java
c54df4fa147b78d8bf1b8e2babe0f94adc380ed6
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / customer / PizzaAdminCustomerWebRequestBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.inject.Any;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcontacts.contact.Contact;
31 import org.mxchange.jcontacts.contact.title.PersonalTitle;
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.exceptions.CustomerAlreadyRegisteredException;
36 import org.mxchange.jcustomercore.model.customer.Customer;
37 import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus;
38 import org.mxchange.pizzaapplication.beans.BasePizzaController;
39 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
40 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestHelperController;
41 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
42 import org.mxchange.pizzaapplication.model.customer.PizzaCustomer;
43
44 /**
45  * Administrative customer bean (controller)
46  * <p>
47  * @author Roland Häder<roland@mxchange.org>
48  */
49 @Named ("adminCustomerController")
50 @RequestScoped
51 public class PizzaAdminCustomerWebRequestBean extends BasePizzaController implements PizzaAdminCustomerWebRequestController {
52
53         /**
54          * Serial number
55          */
56         private static final long serialVersionUID = 12_487_062_487_527_913L;
57
58         /**
59          * Administrative contact controller (for personal data)
60          */
61         @Inject
62         private PizzaAdminContactWebRequestController adminContactController;
63
64         /**
65          * Administrative customer EJB
66          */
67         private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
68
69         /**
70          * Bean helper instance
71          */
72         @Inject
73         private PizzaWebRequestHelperController adminHelper;
74
75         /**
76          * An event being fired when an administrator has added a new customer
77          */
78         @Inject
79         @Any
80         private Event<ObservableAdminAddedCustomerEvent> customerAddedEvent;
81
82         /**
83          * General customer controller
84          */
85         @Inject
86         private PizzaCustomerWebSessionController customerController;
87
88         /**
89          * Default constructor
90          */
91         public PizzaAdminCustomerWebRequestBean () {
92                 // Try it
93                 try {
94                         // Get initial context
95                         Context context = new InitialContext();
96
97                         // Try to lookup
98                         this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
99                 } catch (final NamingException e) {
100                         // Throw again
101                         throw new FaceletException(e);
102                 }
103         }
104
105         @Override
106         public String addCustomer () {
107                 // Are at least some fields added?
108                 if ((this.adminHelper.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 contact;
115
116                 // Is the contact set in helper?
117                 if (this.adminHelper.getContact() instanceof Contact) {
118                         // Get from helper
119                         contact = this.adminHelper.getContact();
120                 } else {
121                         // Get new contact instance
122                         contact = 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 PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
130
131                 // Init instance
132                 Customer updatedCustomer;
133
134                 try {
135                         // Add/link customer and return updated
136                         if (this.adminHelper.getContact() instanceof Contact) {
137                                 // Link customer
138                                 updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
139
140                                 // Remove contact instance
141                                 this.adminHelper.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         @Override
159         public void copyCustomerToController (final Customer customer) {
160                 // Parameters must be valid
161                 if (null == customer) {
162                         // Throw NPE
163                         throw new NullPointerException("customer is null"); //NOI18N
164                 } else if (customer.getCustomerId() == null) {
165                         // Throw again ...
166                         throw new NullPointerException("customer.customerId is null"); //NOI18N
167                 } else if (customer.getCustomerId() < 1) {
168                         // Invalid id number
169                         throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId())); //NOI18N
170                 } else if (customer.getCustomerContact() == null) {
171                         // Throw NPE again
172                         throw new NullPointerException("customer.customerContact is null"); //NOI18N
173                 } else if (customer.getCustomerContact().getContactId() == null) {
174                         // .. and again
175                         throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
176                 } else if (customer.getCustomerContact().getContactId() < 1) {
177                         // Invalid id
178                         throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
179                 }
180
181                 // @TODO Set all data
182         }
183
184         /**
185          * Post-initialization of this class
186          */
187         @PostConstruct
188         public void init () {
189         }
190
191         /**
192          * Checks whether ther minimum customer data is set
193          * <p>
194          * @return Whether minimum data is set
195          */
196         private boolean isCustomerDataSet () {
197                 // Check all
198                 return ((this.adminContactController.getPersonalTitle() instanceof PersonalTitle) &&
199                                 (this.adminContactController.getFirstName() != null) &&
200                                 (!this.adminContactController.getFirstName().isEmpty()) &&
201                                 (this.adminContactController.getFamilyName() != null) &&
202                                 (!this.adminContactController.getFamilyName().isEmpty()));
203         }
204
205 }