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.customer.added.AdminAddedCustomerEvent;
34 import org.mxchange.jcustomercore.events.customer.added.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.BasePizzaController;
39 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
40 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
41 import org.mxchange.pizzaapplication.model.customer.PizzaCustomer;
42 import org.mxchange.pizzaapplication.beans.helper.PizzaWebRequestController;
45 * Administrative customer bean (controller)
47 * @author Roland Haeder<roland@mxchange.org>
49 @Named ("adminCustomerController")
51 public class PizzaAdminCustomerWebRequestBean extends BasePizzaController implements PizzaAdminCustomerWebRequestController {
56 private static final long serialVersionUID = 12_487_062_487_527_913L;
59 * Administrative contact controller (for personal data)
62 private PizzaAdminContactWebRequestController adminContactController;
65 * Administrative customer EJB
67 private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
70 * Admin helper instance
73 private PizzaWebRequestController adminHelper;
76 * An event being fired when an administrator has added a new customer
80 private Event<AdminAddedCustomerEvent> customerAddedEvent;
83 * General customer controller
86 private PizzaCustomerWebSessionController customerController;
91 public PizzaAdminCustomerWebRequestBean () {
94 // Get initial context
95 Context context = new InitialContext();
98 this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
99 } catch (final NamingException e) {
101 throw new FaceletException(e);
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
116 // Is the contact set in helper?
117 if (this.adminHelper.getContact() instanceof Contact) {
119 contact = this.adminHelper.getContact();
121 // Get new contact instance
122 contact = this.adminContactController.createContactInstance();
125 // Ask the EJB for a free customer number
126 String customerNumber = this.adminCustomerBean.createCustomerNumber();
128 // Create new customer instance
129 Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
132 Customer updatedCustomer;
135 // Add/link customer and return updated
136 if (this.adminHelper.getContact() instanceof Contact) {
138 updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
140 // Remove contact instance
141 this.adminHelper.setContact(null);
143 // Add new customer instance
144 updatedCustomer = this.adminCustomerBean.addCustomer(customer);
146 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
148 throw new FaceletException(ex);
152 this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
155 return "admin_list_customer"; //NOI18N
159 public void copyCustomerToController (final Customer customer) {
160 // Parameters must be valid
161 if (null == customer) {
163 throw new NullPointerException("customer is null"); //NOI18N
164 } else if (customer.getCustomerId() == null) {
166 throw new NullPointerException("customer.customerId is null"); //NOI18N
167 } else if (customer.getCustomerId() < 1) {
169 throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId())); //NOI18N
170 } else if (customer.getCustomerContact() == null) {
172 throw new NullPointerException("customer.customerContact is null"); //NOI18N
173 } else if (customer.getCustomerContact().getContactId() == null) {
175 throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
176 } else if (customer.getCustomerContact().getContactId() < 1) {
178 throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
181 // @TODO Set all data
185 * Post-initialization of this class
188 public void init () {
192 * Checks whether ther minimum customer data is set
194 * @return Whether minimum data is set
196 private boolean isCustomerDataSet () {
198 return ((this.adminContactController.getGender() instanceof Gender) &&
199 (this.adminContactController.getFirstName() != null) &&
200 (!this.adminContactController.getFirstName().isEmpty()) &&
201 (this.adminContactController.getFamilyName() != null) &&
202 (!this.adminContactController.getFamilyName().isEmpty()));