2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.pizzaapplication.beans.customer;
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;
44 * Administrative customer bean (controller)
46 * @author Roland Haeder<roland@mxchange.org>
48 @Named ("adminCustomerController")
50 public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRequestController {
55 private static final long serialVersionUID = 12_487_062_487_527_913L;
58 * Administrative contact controller (for personal data)
61 private PizzaAdminContactWebRequestController adminContactController;
64 * Administrative customer EJB
66 private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
69 * Admin helper instance
72 private PizzaAdminWebRequestController adminHelper;
75 * An event being fired when an administrator has added a new customer
79 private Event<AdminAddedCustomerEvent> customerAddedEvent;
82 * General customer controller
85 private PizzaCustomerWebSessionController customerController;
90 public PizzaAdminCustomerWebRequestBean () {
93 // Get initial context
94 Context context = new InitialContext();
97 this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
98 } catch (final NamingException e) {
100 throw new FaceletException(e);
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
115 // Is the contact set in helper?
116 if (this.adminHelper.getContact() instanceof Contact) {
118 contact = this.adminHelper.getContact();
120 // Get new contact instance
121 contact = this.adminContactController.createContactInstance();
124 // Ask the EJB for a free customer number
125 String customerNumber = this.adminCustomerBean.createCustomerNumber();
127 // Create new customer instance
128 Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
131 Customer updatedCustomer;
134 // Add/link customer and return updated
135 if (this.adminHelper.getContact() instanceof Contact) {
137 updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
139 // Remove contact instance
140 this.adminHelper.setContact(null);
142 // Add new customer instance
143 updatedCustomer = this.adminCustomerBean.addCustomer(customer);
145 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
147 throw new FaceletException(ex);
151 this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
154 return "admin_list_customer"; //NOI18N
158 public void copyCustomerToController (final Customer customer) {
159 // Parameters must be valid
160 if (null == customer) {
162 throw new NullPointerException("customer is null");
163 } else if (customer.getCustomerId() == null) {
165 throw new NullPointerException("customer.customerId is null");
166 } else if (customer.getCustomerId() < 1) {
168 throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId()));
169 } else if (customer.getCustomerContact() == null) {
171 throw new NullPointerException("customer.customerContact is null");
172 } else if (customer.getCustomerContact().getContactId() == null) {
174 throw new NullPointerException("customer.customerContact.contactId is null");
175 } else if (customer.getCustomerContact().getContactId() < 1) {
177 throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId()));
180 // @TODO Set all data
184 * Post-initialization of this class
187 public void init () {
191 * Checks whether ther minimum customer data is set
193 * @return Whether minimum data is set
195 private boolean isCustomerDataSet () {
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()));