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 javax.annotation.PostConstruct;
20 import javax.enterprise.context.RequestScoped;
21 import javax.enterprise.event.Event;
22 import javax.enterprise.inject.Any;
23 import javax.faces.view.facelets.FaceletException;
24 import javax.inject.Inject;
25 import javax.inject.Named;
26 import javax.naming.Context;
27 import javax.naming.InitialContext;
28 import javax.naming.NamingException;
29 import org.mxchange.jcontacts.contact.Contact;
30 import org.mxchange.jcontacts.contact.gender.Gender;
31 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
32 import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
33 import org.mxchange.jcustomercore.events.CustomerAdminAddedEvent;
34 import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException;
35 import org.mxchange.jcustomercore.model.customer.Customer;
36 import org.mxchange.jcustomercore.model.customer.status.CustomerAccountStatus;
37 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
38 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
41 * Administrative customer bean (controller)
43 * @author Roland Haeder<roland@mxchange.org>
45 @Named ("adminCustomerController")
47 public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRequestController {
52 private static final long serialVersionUID = 12_487_062_487_527_913L;
55 * Administrative contact controller (for personal data)
58 private PizzaAdminContactWebRequestController adminContactController;
61 * Administrative customer EJB
63 private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
66 * An event being fired when an administrator has added a new customer
70 private Event<AdminAddedCustomerEvent> customerAddedEvent;
73 * General customer controller
76 private PizzaCustomerWebSessionController customerController;
81 public PizzaAdminCustomerWebRequestBean () {
84 // Get initial context
85 Context context = new InitialContext();
88 this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
89 } catch (final NamingException e) {
91 throw new FaceletException(e);
96 public String addCustomer () {
97 // Are at least some fields added?
98 if (!this.isCustomerDataSet()) {
99 // Not all customer data is set
100 throw new FaceletException("Please provide minimum personal data: gender, first_name, family_name"); //NOI18N
103 // Get contact instance
104 Contact contact = this.adminContactController.createContactInstance();
106 // Ask the EJB for a free customer number
107 String customerNumber = this.adminCustomerBean.createCustomerNumber();
109 // Create new customer instance
110 Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
113 Customer updatedCustomer;
116 // Add customer and return updated
117 updatedCustomer = this.adminCustomerBean.addCustomer(customer);
118 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
120 throw new FaceletException(ex);
123 // Add customer to list
124 this.customerController.addCustomer(updatedCustomer);
127 this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
130 return "admin_list_customer"; //NOI18N
134 * Post-initialization of this class
137 public void init () {
141 * Checks whether ther minimum customer data is set
143 * @return Whether minimum data is set
145 private boolean isCustomerDataSet () {
147 return ((this.adminContactController.getGender() instanceof Gender) &&
148 (this.adminContactController.getFirstName() != null) &&
149 (!this.adminContactController.getFirstName().isEmpty()) &&
150 (this.adminContactController.getFamilyName() != null) &&
151 (!this.adminContactController.getFamilyName().isEmpty()));