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 java.util.Collections;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.annotation.PostConstruct;
25 import javax.enterprise.context.SessionScoped;
26 import javax.enterprise.event.Event;
27 import javax.enterprise.event.Observes;
28 import javax.enterprise.inject.Any;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontacts.contact.Contact;
36 import org.mxchange.jcontacts.contact.ContactSessionBeanRemote;
37 import org.mxchange.jcustomercore.events.AdminAddedCustomerEvent;
38 import org.mxchange.jcustomercore.model.customer.Customer;
39 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
40 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
43 * A customer bean which hides the customer instance
45 * @author Roland Haeder<roland@mxchange.org>
47 @Named ("customerController")
49 public class PizzaCustomerWebSessionBean implements PizzaCustomerWebSessionController {
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;
70 private final ContactSessionBeanRemote contactBean;
73 * An event being fired when an administrator has added a new customer
77 private Event<AdminAddedCustomerEvent> customerAddedEvent;
80 * A list of all customers
82 private List<Customer> customerList;
85 * A list of all selectable contacts
87 private List<Contact> selectableContacts;
92 public PizzaCustomerWebSessionBean () {
95 // Get initial context
96 Context context = new InitialContext();
99 this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/admincustomer!de.chotime.jratecalc.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
102 this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/jratecalc-ejb/contact!org.mxchange.jcontacts.contact.ContactSessionBeanRemote"); //NOI18N
103 } catch (final NamingException e) {
105 throw new FaceletException(e);
110 public void addCustomer (final Customer customer) {
111 // The contact must be valid
112 if (null == customer) {
114 throw new NullPointerException("customer is null"); //NOI18N
115 } else if (customer.getCustomerId() == null) {
117 throw new NullPointerException("customer.customerId is null"); //NOI18N
118 } else if (customer.getCustomerId() < 1) {
120 throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId())); //NOI18N
121 } else if (customer.getCustomerContact() == null) {
123 throw new NullPointerException("customer.customerContact is null"); //NOI18N
124 } else if (customer.getCustomerContact().getContactId() == null) {
126 throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
127 } else if (customer.getCustomerContact().getContactId() < 1) {
129 throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
133 this.customerList.add(customer);
137 public void afterAdminAddedCustomer (@Observes final AdminAddedCustomerEvent event) {
138 // The event must be valid
141 throw new NullPointerException("event is null"); //NOI18N
142 } else if (event.getAddedCustomer() == null) {
144 throw new NullPointerException("event.addedCustomer is null"); //NOI18N
145 } else if (event.getAddedCustomer().getCustomerId() == null) {
147 throw new NullPointerException("event.addedCustomer.customerId is null"); //NOI18N
148 } else if (event.getAddedCustomer().getCustomerId() < 1) {
150 throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerId={0} is not valid", event.getAddedCustomer().getCustomerId())); //NOI18N //NOI18N
154 this.addCustomer(event.getAddedCustomer());
158 public List<Customer> allCustomers () {
160 return Collections.unmodifiableList(this.customerList);
164 public boolean hasCustomers () {
165 return (!this.allCustomers().isEmpty());
169 * Post-initialization of this class
172 public void init () {
173 // Initialize customer list
174 this.customerList = this.adminCustomerBean.allCustomers();
177 List<Contact> allContacts = this.contactBean.getAllContacts();
180 Iterator<Contact> iterator = allContacts.iterator();
183 while (iterator.hasNext()) {
185 Contact next = iterator.next();
188 Iterator<Customer> userIterator = this.customerList.iterator();
190 // Loop through all users
191 while (userIterator.hasNext()) {
193 Customer nextCustomer = userIterator.next();
196 if (Objects.equals(next, nextCustomer.getCustomerContact())) {
205 this.selectableContacts = allContacts;
209 public boolean isContactFound (final Contact contact) {
210 // The contact must be valid
211 if (null == contact) {
213 throw new NullPointerException("contact is null"); //NOI18N
214 } else if (contact.getContactId() == null) {
216 throw new NullPointerException("contact.contactId is null"); //NOI18N
217 } else if (contact.getContactId() < 1) {
219 throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
222 // Default is not found
223 boolean isFound = false;
226 Iterator<Customer> iterator = this.allCustomers().iterator();
228 // Loop through all entries
229 while (iterator.hasNext()) {
231 Customer next = iterator.next();
233 // Compare both objects
234 if (Objects.equals(contact, next.getCustomerContact())) {