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