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.util.Collections;
20 import java.util.List;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
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.contact.Contact;
32 import org.mxchange.jcontacts.contact.gender.Gender;
33 import org.mxchange.jcontacts.exceptions.ContactAlreadyAddedException;
34 import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
35 import org.mxchange.jcustomercore.events.CustomerAdminAddedEvent;
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.contact.PizzaAdminContactWebRequestController;
40 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
43 * Administrative customer bean (controller)
45 * @author Roland Haeder<roland@mxchange.org>
47 @Named ("adminCustomerController")
49 public class PizzaAdminCustomerWebRequestBean implements PizzaAdminCustomerWebRequestController {
54 private static final long serialVersionUID = 12_487_062_487_527_913L;
57 * Administrative contact controller (for personal data)
60 private PizzaAdminContactWebRequestController adminContactController;
63 * Administrative customer EJB
65 private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
68 * An event being fired when an administrator has added a new customer
72 private Event<AdminAddedCustomerEvent> customerAddedEvent;
75 * A list of all customers
77 private List<Customer> customerList;
82 public PizzaAdminCustomerWebRequestBean () {
85 // Get initial context
86 Context context = new InitialContext();
89 this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/PizzaService-ejb/admincustomer!org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
90 } catch (final NamingException e) {
92 throw new FaceletException(e);
97 public String addCustomer () {
98 // Are at least some fields added?
99 if (!this.isCustomerDataSet()) {
100 // Not all customer data is set
101 throw new FaceletException("Please provide minimum personal data: gender, first_name, family_name"); //NOI18N
104 // Get contact instance
105 Contact contact = this.adminContactController.createContactInstance();
107 // Ask the EJB for a free customer number
108 String customerNumber = this.adminCustomerBean.createCustomerNumber();
110 // Create new customer instance
111 Customer customer = new PizzaCustomer(CustomerAccountStatus.CONFIRMED, contact, customerNumber);
114 Customer updatedCustomer;
117 // Add customer and return updated
118 updatedCustomer = this.adminCustomerBean.addCustomer(customer);
119 } catch (final CustomerAlreadyRegisteredException | ContactAlreadyAddedException ex) {
121 throw new FaceletException(ex);
124 // Add customer to list
125 this.customerList.add(updatedCustomer);
128 this.customerAddedEvent.fire(new CustomerAdminAddedEvent(updatedCustomer));
131 return "admin_list_customer"; //NOI18N
135 public List<Customer> allCustomers () {
137 return Collections.unmodifiableList(this.customerList);
141 public boolean hasCustomers () {
142 return (!this.allCustomers().isEmpty());
146 * Post-initialization of this class
149 public void init () {
150 // Initialize customer list
151 this.customerList = this.adminCustomerBean.allCustomers();
155 * Checks whether ther minimum customer data is set
157 * @return Whether minimum data is set
159 private boolean isCustomerDataSet () {
161 return ((this.adminContactController.getGender() instanceof Gender) &&
162 (this.adminContactController.getFirstName() != null) &&
163 (!this.adminContactController.getFirstName().isEmpty()) &&
164 (this.adminContactController.getFamilyName() != null) &&
165 (!this.adminContactController.getFamilyName().isEmpty()));