]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/customer/PizzaAdminCustomerWebRequestBean.java
Continued with customer:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / customer / PizzaAdminCustomerWebRequestBean.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.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.gender.Gender;
32 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
33 import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
34 import org.mxchange.jcustomercore.events.CustomerAdminAddedEvent;
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.contact.PizzaAdminContactWebRequestController;
39 import org.mxchange.pizzaapplication.beans.helper.PizzaAdminWebRequestController;
40 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
41 import org.mxchange.pizzaapplication.model.customer.PizzaCustomer;
42
43 /**
44  * Administrative customer bean (controller)
45  * <p>
46  * @author Roland Haeder<roland@mxchange.org>
47  */
48 @Named ("adminCustomerController")
49 @RequestScoped
50 public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRequestController {
51
52         /**
53          * Serial number
54          */
55         private static final long serialVersionUID = 12_487_062_487_527_913L;
56
57         /**
58          * Administrative contact controller (for personal data)
59          */
60         @Inject
61         private PizzaAdminContactWebRequestController adminContactController;
62
63         /**
64          * Administrative customer EJB
65          */
66         private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
67
68         /**
69          * Admin helper instance
70          */
71         @Inject
72         private PizzaAdminWebRequestController adminHelper;
73
74         /**
75          * An event being fired when an administrator has added a new customer
76          */
77         @Inject
78         @Any
79         private Event<AdminAddedCustomerEvent> customerAddedEvent;
80
81         /**
82          * General customer controller
83          */
84         @Inject
85         private PizzaCustomerWebSessionController customerController;
86
87         /**
88          * Default constructor
89          */
90         public PizzaAdminCustomerWebRequestBean () {
91                 // Try it
92                 try {
93                         // Get initial context
94                         Context context = new InitialContext();
95
96                         // Try to lookup
97                         this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
98                 } catch (final NamingException e) {
99                         // Throw again
100                         throw new FaceletException(e);
101                 }
102         }
103
104         @Override
105         public String addCustomer () {
106                 // Are at least some fields added?
107                 if ((this.adminHelper.getContact() == null) && (!this.isCustomerDataSet())) {
108                         // Not all customer data is set
109                         throw new FaceletException("Please provide minimum personal data: gender, first_name, family_name"); //NOI18N
110                 }
111
112                 // Init contact
113                 Contact contact;
114
115                 // Is the contact set in helper?
116                 if (this.adminHelper.getContact() instanceof Contact) {
117                         // Get from helper
118                         contact = this.adminHelper.getContact();
119                 } else {
120                         // Get new contact instance
121                         contact = this.adminContactController.createContactInstance();
122                 }
123
124                 // Ask the EJB for a free customer number
125                 String customerNumber = this.adminCustomerBean.createCustomerNumber();
126
127                 // Create new customer instance
128                 Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
129
130                 // Init instance
131                 Customer updatedCustomer;
132
133                 try {
134                         // Add/link customer and return updated
135                         if (this.adminHelper.getContact() instanceof Contact) {
136                                 // Link customer
137                                 updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
138
139                                 // Remove contact instance
140                                 this.adminHelper.setContact(null);
141                         } else {
142                                 // Add new customer instance
143                                 updatedCustomer = this.adminCustomerBean.addCustomer(customer);
144                         }
145                 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
146                         // Throw again
147                         throw new FaceletException(ex);
148                 }
149
150                 // Fire event
151                 this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
152
153                 // Redirect
154                 return "admin_list_customer"; //NOI18N
155         }
156
157         @Override
158         public void copyCustomerToController (final Customer customer) {
159                 // Parameters must be valid
160                 if (null == customer) {
161                         // Throw NPE
162                         throw new NullPointerException("customer is null");
163                 } else if (customer.getCustomerId() == null) {
164                         // Throw again ...
165                         throw new NullPointerException("customer.customerId is null");
166                 } else if (customer.getCustomerId() < 1) {
167                         // Invalid id number
168                         throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId()));
169                 } else if (customer.getCustomerContact() == null) {
170                         // Throw NPE again
171                         throw new NullPointerException("customer.customerContact is null");
172                 } else if (customer.getCustomerContact().getContactId() == null) {
173                         // .. and again
174                         throw new NullPointerException("customer.customerContact.contactId is null");
175                 } else if (customer.getCustomerContact().getContactId() < 1) {
176                         // Invalid id
177                         throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId()));
178                 }
179
180                 // @TODO Set all data
181         }
182
183         /**
184          * Post-initialization of this class
185          */
186         @PostConstruct
187         public void init () {
188         }
189
190         /**
191          * Checks whether ther minimum customer data is set
192          * <p>
193          * @return Whether minimum data is set
194          */
195         private boolean isCustomerDataSet () {
196                 // Check all
197                 return ((this.adminContactController.getGender() instanceof Gender) &&
198                                 (this.adminContactController.getFirstName() != null) &&
199                                 (!this.adminContactController.getFirstName().isEmpty()) &&
200                                 (this.adminContactController.getFamilyName() != null) &&
201                                 (!this.adminContactController.getFamilyName().isEmpty()));
202         }
203
204 }