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