2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.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;
46 * Administrative customer bean (controller)
48 * @author Roland Häder<roland@mxchange.org>
50 @Named ("adminCustomerController")
52 public class PizzaAdminCustomerWebRequestBean extends BasePizzaBean implements PizzaAdminCustomerWebRequestController {
57 private static final long serialVersionUID = 12_487_062_487_527_913L;
60 * Administrative contact controller (for personal data)
63 private PizzaAdminContactWebRequestController adminContactController;
66 * Administrative customer EJB
68 private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
71 * Bean helper instance
74 private PizzaWebRequestHelperController beanHelper;
79 private Contact contact;
82 * An event being fired when an administrator has added a new customer
86 private Event<ObservableAdminAddedCustomerEvent> customerAddedEvent;
89 * General customer controller
92 private PizzaCustomerWebSessionController customerController;
97 public PizzaAdminCustomerWebRequestBean () {
101 * Adds customer to database if not already added. This method should return
102 * a redirect outcome on success.
104 * @return Redirect outcome
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
114 Contact customerContact;
116 // Is the contact set in helper?
117 if (this.getContact() instanceof Contact) {
119 customerContact = this.getContact();
121 // Get new contact instance
122 customerContact = 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 ContactCustomer(CustomerAccountStatus.CONFIRMED, customerContact, customerNumber);
132 Customer updatedCustomer;
135 // Add/link customer and return updated
136 if (this.getContact() instanceof Contact) {
138 updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
140 // Remove contact instance
141 this.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 AdminAddedCustomerEvent(updatedCustomer));
155 return "admin_list_customer"; //NOI18N
159 * Observes an even when a customer has been created
161 * @param event Event being fired
163 public void afterCustomerCreatedEvent (final @Observes ObservableCreatedCustomerEvent event) {
164 // The event instance must be valid
167 throw new NullPointerException("event is null"); //NOI18N
168 } else if (event.getCreatedCustomer() == null) {
170 throw new NullPointerException("event.createdCustomer is null"); //NOI18N
171 } else if (event.getCreatedCustomer().getCustomerId() == null) {
173 throw new NullPointerException("event.createdCustomer.customerId is null"); //NOI18N
174 } else if (event.getCreatedCustomer().getCustomerId() < 1) {
176 throw new IllegalArgumentException(MessageFormat.format("event.createdCustomer.customerId={0} is not valid", event.getCreatedCustomer().getCustomerId())); //NOI18N
177 } else if (event.getCreatedCustomer().getCustomerContact() == null) {
179 throw new NullPointerException("event.createdCustomer.customerContact is null"); //NOI18N
180 } else if (event.getCreatedCustomer().getCustomerContact().getContactId() == null) {
182 throw new NullPointerException("event.createdCustomer.customerContact.contactId is null"); //NOI18N
183 } else if (event.getCreatedCustomer().getCustomerContact().getContactId() < 1) {
185 throw new IllegalArgumentException(MessageFormat.format("event.createdCustomer.customerContact.contactId={0} is not valid", event.getCreatedCustomer().getCustomerContact().getContactId())); //NOI18N
188 // @TODO Set all data
192 * Getter for contact instance
194 * @return Contact instance
196 public Contact getContact () {
201 * Setter for contact instance
203 * @param contact Contact instance
205 public void setContact (final Contact contact) {
206 this.contact = contact;
210 * Post-initialization of this class
213 public void init () {
216 // Get initial context
217 Context context = new InitialContext();
220 this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
221 } catch (final NamingException e) {
223 throw new FaceletException(e);
228 * Checks whether the minimum customer data is set
230 * @return Whether minimum data is set
232 private boolean isCustomerDataSet () {
234 return this.adminContactController.isRequiredPersonalDataSet();