]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/customer/PizzaCustomerWebSessionBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / customer / PizzaCustomerWebSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2024 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.beans.customer;
18
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.Observes;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import javax.naming.Context;
31 import javax.naming.InitialContext;
32 import javax.naming.NamingException;
33 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
34 import org.mxchange.jcontacts.model.contact.Contact;
35 import org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote;
36 import org.mxchange.jcustomercore.events.customer.added.ObservableAdminAddedCustomerEvent;
37 import org.mxchange.jcustomercore.model.customer.Customer;
38 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
39 import org.mxchange.pizzaapplication.beans.contact.PizzaAdminContactWebRequestController;
40 import org.mxchange.pizzaapplication.model.customer.PizzaAdminCustomerSessionBeanRemote;
41
42 /**
43  * A customer bean which hides the customer instance
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Named ("customerController")
48 @SessionScoped
49 public class PizzaCustomerWebSessionBean extends BasePizzaBean implements PizzaCustomerWebSessionController {
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 12_487_062_487_527_913L;
55
56         /**
57          * Administrative contact controller (for personal data)
58          */
59         @Inject
60         private PizzaAdminContactWebRequestController adminContactController;
61
62         /**
63          * Administrative customer EJB
64          */
65         private PizzaAdminCustomerSessionBeanRemote adminCustomerBean;
66
67         /**
68          * Remote user bean
69          */
70         private final ContactSessionBeanRemote contactBean;
71
72         /**
73          * A list of all customers
74          */
75         private List<Customer> customerList;
76
77         /**
78          * A list of all selectable contacts
79          */
80         private List<Contact> selectableContacts;
81
82         /**
83          * Default constructor
84          */
85         public PizzaCustomerWebSessionBean () {
86                 // Try it
87                 try {
88                         // Get initial context
89                         Context context = new InitialContext();
90
91                         // Try to lookup
92                         this.adminCustomerBean = (PizzaAdminCustomerSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/admincustomer!org.mxchange.pizzaservice.model.customer.PizzaAdminCustomerSessionBeanRemote"); //NOI18N
93
94                         // Try to lookup
95                         this.contactBean = (ContactSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/contact!org.mxchange.jcontacts.model.contact.ContactSessionBeanRemote"); //NOI18N
96                 } catch (final NamingException e) {
97                         // Throw again
98                         throw new FaceletException(e);
99                 }
100         }
101
102         @Override
103         public void afterAdminAddedContact (@Observes final ObservableAdminAddedContactEvent event) {
104                 // The event must be valid
105                 if (null == event) {
106                         // Throw NPE
107                         throw new NullPointerException("event is null"); //NOI18N
108                 } else if (event.getAddedContact() == null) {
109                         // Throw again ...
110                         throw new NullPointerException("event.addedContact is null"); //NOI18N
111                 } else if (event.getAddedContact().getContactId() == null) {
112                         // ... and again
113                         throw new NullPointerException("event.addedContact.customerId is null"); //NOI18N
114                 } else if (event.getAddedContact().getContactId() < 1) {
115                         // Not valid
116                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.customerId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N //NOI18N
117                 }
118
119                 // Call other method
120                 this.selectableContacts.add(event.getAddedContact());
121         }
122
123         @Override
124         public void afterAdminAddedCustomer (@Observes final ObservableAdminAddedCustomerEvent event) {
125                 // The event must be valid
126                 if (null == event) {
127                         // Throw NPE
128                         throw new NullPointerException("event is null"); //NOI18N
129                 } else if (event.getAddedCustomer() == null) {
130                         // Throw again ...
131                         throw new NullPointerException("event.addedCustomer is null"); //NOI18N
132                 } else if (event.getAddedCustomer().getCustomerId() == null) {
133                         // ... and again
134                         throw new NullPointerException("event.addedCustomer.customerId is null"); //NOI18N
135                 } else if (event.getAddedCustomer().getCustomerId() < 1) {
136                         // Not valid
137                         throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerId={0} is not valid", event.getAddedCustomer().getCustomerId())); //NOI18N //NOI18N
138                 } else if (event.getAddedCustomer().getCustomerContact() == null) {
139                         // Throw NPE
140                         throw new NullPointerException("event.addedCustomer.customerContact is null"); //NOI18N
141                 } else if (event.getAddedCustomer().getCustomerContact().getContactId() == null) {
142                         // Throw again ...
143                         throw new NullPointerException("event.addedCustomer.customerContact.contactId is null"); //NOI18N
144                 } else if (event.getAddedCustomer().getCustomerContact().getContactId() < 1) {
145                         // Not valid
146                         throw new IllegalArgumentException(MessageFormat.format("event.addedCustomer.customerContact.contactId={0} is not valid", event.getAddedCustomer().getCustomerContact().getContactId())); //NOI18N
147                 } else if (!this.selectableContacts.contains(event.getAddedCustomer().getCustomerContact())) {
148                         // Contact not in selectable list found
149                         throw new IllegalStateException(MessageFormat.format("event.addedCustomer.customerId={0} is not in selectableContacts list.", event.getAddedCustomer().getCustomerContact().getContactId()));
150                 }
151
152                 // Remove this contact from selectable list
153                 assert (this.selectableContacts.remove(event.getAddedCustomer().getCustomerContact())) : "contact was not removed"; //NOI18N
154
155                 // Call other method
156                 this.addCustomer(event.getAddedCustomer());
157         }
158
159         @Override
160         public List<Customer> allCustomers () {
161                 // Return it
162                 return Collections.unmodifiableList(this.customerList);
163         }
164
165         @Override
166         public boolean hasCustomers () {
167                 return (!this.allCustomers().isEmpty());
168         }
169
170         /**
171          * Post-initialization of this class
172          */
173         @PostConstruct
174         public void init () {
175                 // Initialize customer list
176                 this.customerList = this.adminCustomerBean.allCustomers();
177
178                 // Get all contacts
179                 List<Contact> allContacts = this.contactBean.getAllContacts();
180
181                 // Get iterator
182                 Iterator<Contact> iterator = allContacts.iterator();
183
184                 // Loop through it
185                 while (iterator.hasNext()) {
186                         // Get next element
187                         Contact next = iterator.next();
188
189                         // Get iterator
190                         Iterator<Customer> userIterator = this.customerList.iterator();
191
192                         // Loop through all users
193                         while (userIterator.hasNext()) {
194                                 // Get user instance
195                                 Customer nextCustomer = userIterator.next();
196
197                                 // Is contact same?
198                                 if (Objects.equals(next, nextCustomer.getCustomerContact())) {
199                                         // Found same
200                                         iterator.remove();
201                                         break;
202                                 }
203                         }
204                 }
205
206                 // Set contact list
207                 this.selectableContacts = allContacts;
208         }
209
210         @Override
211         public boolean isContactFound (final Contact contact) {
212                 // The contact must be valid
213                 if (null == contact) {
214                         // Throw NPE
215                         throw new NullPointerException("contact is null"); //NOI18N
216                 } else if (contact.getContactId() == null) {
217                         // Throw again ...
218                         throw new NullPointerException("contact.contactId is null"); //NOI18N
219                 } else if (contact.getContactId() < 1) {
220                         // Not valid
221                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
222                 }
223
224                 // Default is not found
225                 boolean isFound = false;
226
227                 // Get iterator
228                 Iterator<Customer> iterator = this.allCustomers().iterator();
229
230                 // Loop through all entries
231                 while (iterator.hasNext()) {
232                         // Get customer
233                         Customer next = iterator.next();
234
235                         // Compare both objects
236                         if (Objects.equals(contact, next.getCustomerContact())) {
237                                 // Found it
238                                 isFound = true;
239                                 break;
240                         }
241                 }
242
243                 // Return status
244                 return isFound;
245         }
246
247         @Override
248         public List<Contact> selectableContacts () {
249                 return Collections.unmodifiableList(this.selectableContacts);
250         }
251
252         /**
253          * Adds given customer instance to this controller. The customer should be
254          * an updated instance (with id number).
255          * <p>
256          * @param customer Customer instance
257          */
258         private void addCustomer (final Customer customer) {
259                 // The contact must be valid
260                 if (null == customer) {
261                         // Throw NPE
262                         throw new NullPointerException("customer is null"); //NOI18N
263                 } else if (customer.getCustomerId() == null) {
264                         // Throw again ...
265                         throw new NullPointerException("customer.customerId is null"); //NOI18N
266                 } else if (customer.getCustomerId() < 1) {
267                         // Not valid
268                         throw new IllegalArgumentException(MessageFormat.format("customer.customerId={0} is not valid", customer.getCustomerId())); //NOI18N
269                 } else if (customer.getCustomerContact() == null) {
270                         // Throw NPE
271                         throw new NullPointerException("customer.customerContact is null"); //NOI18N
272                 } else if (customer.getCustomerContact().getContactId() == null) {
273                         // Throw again ...
274                         throw new NullPointerException("customer.customerContact.contactId is null"); //NOI18N
275                 } else if (customer.getCustomerContact().getContactId() < 1) {
276                         // Not valid
277                         throw new IllegalArgumentException(MessageFormat.format("customer.customerContact.contactId={0} is not valid", customer.getCustomerContact().getContactId())); //NOI18N
278                 }
279
280                 // Add to list
281                 this.customerList.add(customer);
282         }
283
284 }