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