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.beans.helper.PizzaAdminWebRequestController;
39 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
42 * Administrative customer bean (controller)
44 * @author Roland Haeder<roland@mxchange.org>
46 @Named ("adminCustomerController")
48 public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRequestController {
53 private static final long serialVersionUID = 12_487_062_487_527_913L;
56 * Administrative contact controller (for personal data)
59 private PizzaAdminContactWebRequestController adminContactController;
62 * Administrative customer EJB
64 private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
67 * Admin helper instance
70 private PizzaAdminWebRequestController adminHelper;
73 * An event being fired when an administrator has added a new customer
77 private Event<AdminAddedCustomerEvent> customerAddedEvent;
80 * General customer controller
83 private PizzaCustomerWebSessionController customerController;
88 public PizzaAdminCustomerWebRequestBean () {
91 // Get initial context
92 Context context = new InitialContext();
95 this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
96 } catch (final NamingException e) {
98 throw new FaceletException(e);
103 public String addCustomer () {
104 // Are at least some fields added?
105 if ((this.adminHelper.getContact() == null) && (!this.isCustomerDataSet())) {
106 // Not all customer data is set
107 throw new FaceletException("Please provide minimum personal data: gender, first_name, family_name"); //NOI18N
113 // Is the contact set in helper?
114 if (this.adminHelper.getContact() instanceof Contact) {
116 contact = this.adminHelper.getContact();
118 // Get new contact instance
119 contact = this.adminContactController.createContactInstance();
122 // Ask the EJB for a free customer number
123 String customerNumber = this.adminCustomerBean.createCustomerNumber();
125 // Create new customer instance
126 Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
129 Customer updatedCustomer;
132 // Add/link customer and return updated
133 if (this.adminHelper.getContact() instanceof Contact) {
135 updatedCustomer = this.adminCustomerBean.linkCustomer(customer);
137 // Add new customer instance
138 updatedCustomer = this.adminCustomerBean.addCustomer(customer);
140 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
142 throw new FaceletException(ex);
145 // Add customer to list
146 this.customerController.addCustomer(updatedCustomer);
149 this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
152 return "admin_list_customer"; //NOI18N
156 * Post-initialization of this class
159 public void init () {
163 * Checks whether ther minimum customer data is set
165 * @return Whether minimum data is set
167 private boolean isCustomerDataSet () {
169 return ((this.adminContactController.getGender() instanceof Gender) &&
170 (this.adminContactController.getFirstName() != null) &&
171 (!this.adminContactController.getFirstName().isEmpty()) &&
172 (this.adminContactController.getFamilyName() != null) &&
173 (!this.adminContactController.getFamilyName().isEmpty()));