2 * Copyright (C) 2016 - 2020 Free Software Foundation
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.phone;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.cache.Cache;
28 import javax.enterprise.context.RequestScoped;
29 import javax.enterprise.event.Observes;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
33 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
34 import org.mxchange.jcontacts.events.fax.linked.ObservableAdminLinkedFaxNumberEvent;
35 import org.mxchange.jcontacts.events.landline.linked.ObservableAdminLinkedLandLineNumberEvent;
36 import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent;
37 import org.mxchange.jcontacts.model.contact.Contact;
38 import org.mxchange.jphone.events.fax.deleted.AdminDeletedFaxNumberEvent;
39 import org.mxchange.jphone.events.fax.updated.AdminUpdatedFaxNumberEvent;
40 import org.mxchange.jphone.events.landline.deleted.AdminDeletedLandLineNumberEvent;
41 import org.mxchange.jphone.events.landline.updated.AdminUpdatedLandLineNumberEvent;
42 import org.mxchange.jphone.events.mobile.deleted.AdminDeletedMobileNumberEvent;
43 import org.mxchange.jphone.events.mobile.updated.AdminUpdatedMobileNumberEvent;
44 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
46 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
47 import org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote;
48 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
49 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
52 * Regular controller (bean) for phone numbers
54 * @author Roland Häder<roland@mxchange.org>
56 @Named ("phoneController")
58 public class PizzaPhoneWebRequestBean extends BasePizzaBean implements PizzaPhoneWebRequestController {
63 private static final long serialVersionUID = 491_058_674_675_690_105L;
69 @NamedCache (cacheName = "faxNumberCache")
70 private Cache<Long, DialableFaxNumber> faxNumberCache;
73 * All land-line numbers
76 @NamedCache (cacheName = "landLineNumberCache")
77 private Cache<Long, DialableLandLineNumber> landLineNumberCache;
83 @NamedCache (cacheName = "mobileNumberCache")
84 private Cache<Long, DialableMobileNumber> mobileNumberCache;
87 * General EJB for phone numbers
89 @EJB (lookup = "java:global/pizzaservice-ejb/phone!org.mxchange.jphone.model.phonenumbers.phone.PhoneSessionBeanRemote")
90 private PhoneSessionBeanRemote phoneBean;
95 public PizzaPhoneWebRequestBean () {
96 // Call super constructor
101 * Observes events being fired when an administrator has added a new
104 * @param event Event being fired
106 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
107 // The event must be valid
110 throw new NullPointerException("event is null"); //NOI18N
111 } else if (event.getAddedContact() == null) {
113 throw new NullPointerException("event.addedContact is null"); //NOI18N
114 } else if (event.getAddedContact().getContactId() == null) {
116 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
117 } else if (event.getAddedContact().getContactId() < 1) {
119 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
122 // Update contact's mobile, land-line and fax number
123 this.updateContactPhoneNumbers(event.getAddedContact());
130 * Event observer for newly added users by administrator
132 * @param event Event being fired
134 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
135 // event should not be null
138 throw new NullPointerException("event is null"); //NOI18N
139 } else if (event.getAddedUser() == null) {
141 throw new NullPointerException("event.addedUser is null"); //NOI18N
142 } else if (event.getAddedUser().getUserId() == null) {
144 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
145 } else if (event.getAddedUser().getUserId() < 1) {
147 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
150 // Update contact's mobile, land-line and fax number
151 this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
158 * Observes events being fired when an administrator has deleted a fax
161 * @param event Event being fired
163 public void afterAdminDeletedFaxNumberEvent (@Observes final AdminDeletedFaxNumberEvent event) {
164 // event should not be null
167 throw new NullPointerException("event is null"); //NOI18N
168 } else if (event.getDeletedFaxNumber() == null) {
170 throw new NullPointerException("event.deletedFaxNumber is null"); //NOI18N
171 } else if (event.getDeletedFaxNumber().getPhoneId() == null) {
173 throw new NullPointerException("event.deletedFaxNumber.phoneId is null"); //NOI18N
174 } else if (event.getDeletedFaxNumber().getPhoneId() < 1) {
176 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedFaxNumber(), event.getDeletedFaxNumber().getPhoneId())); //NOI18N
179 // Update contact's mobile, land-line and fax number
180 this.allFaxNumbers().remove(event.getDeletedFaxNumber());
187 * Observes events being fired when an administrator has deleted a land-line
190 * @param event Event being fired
192 public void afterAdminDeletedLandLineNumberEvent (@Observes final AdminDeletedLandLineNumberEvent event) {
193 // event should not be null
196 throw new NullPointerException("event is null"); //NOI18N
197 } else if (event.getDeletedLandLineNumber() == null) {
199 throw new NullPointerException("event.deletedLandLineNumber is null"); //NOI18N
200 } else if (event.getDeletedLandLineNumber().getPhoneId() == null) {
202 throw new NullPointerException("event.deletedLandLineNumber.phoneId is null"); //NOI18N
203 } else if (event.getDeletedLandLineNumber().getPhoneId() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedLandLineNumber(), event.getDeletedLandLineNumber().getPhoneId())); //NOI18N
208 // Update contact's mobile, land-line and fax number
209 this.allLandLineNumbers().remove(event.getDeletedLandLineNumber());
216 * Observes events being fired when an administrator has deleted a mobile
219 * @param event Event being fired
221 public void afterAdminDeletedMobileNumberEvent (@Observes final AdminDeletedMobileNumberEvent event) {
222 // event should not be null
225 throw new NullPointerException("event is null"); //NOI18N
226 } else if (event.getDeletedMobileNumber() == null) {
228 throw new NullPointerException("event.deletedMobileNumber is null"); //NOI18N
229 } else if (event.getDeletedMobileNumber().getPhoneId() == null) {
231 throw new NullPointerException("event.deletedMobileNumber.phoneId is null"); //NOI18N
232 } else if (event.getDeletedMobileNumber().getPhoneId() < 1) {
234 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedMobileNumber(), event.getDeletedMobileNumber().getPhoneId())); //NOI18N
237 // Update contact's mobile, land-line and fax number
238 this.allMobileNumbers().remove(event.getDeletedMobileNumber());
245 * Observes events being fired when an administrator has a linked a fax
248 * @param event Event being fired
250 public void afterAdminLinkedFaxNumberEvent (@Observes final ObservableAdminLinkedFaxNumberEvent event) {
251 // Is the event fine?
254 throw new NullPointerException("event is null"); //NOI18N
255 } else if (event.getContact() == null) {
257 throw new NullPointerException("event.contact is null");
258 } else if (event.getContact().getContactId() == null) {
260 throw new NullPointerException("event.contact.contactId is null");
261 } else if (event.getContact().getContactId() < 1) {
263 throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
264 } else if (event.getContact().getContactFaxNumber() == null) {
266 throw new NullPointerException("event.contact.contactFaxNumber is null");
267 } else if (event.getContact().getContactFaxNumber().getPhoneId() == null) {
269 throw new NullPointerException("event.contact.contactFaxNumber.phoneId is null");
270 } else if (event.getContact().getContactFaxNumber().getPhoneId() < 1) {
272 throw new NullPointerException("event.contact.contactFaxNumber.phoneId=" + event.getContact().getContactFaxNumber().getPhoneId() + " is invalid");
273 } else if (event.getLinkedFaxNumber() == null) {
275 throw new NullPointerException("event.linkedFaxNumer is null");
278 // Is the id number in linked number not set?
279 if (event.getLinkedFaxNumber().getPhoneId() == null) {
280 // Then it is a new number, so add it from contact as there the id number has been set
281 this.uniqueAddFaxNumber(event.getContact().getContactFaxNumber());
286 * Observes events being fired when an administrator has a linked a
289 * @param event Event being fired
291 public void afterAdminLinkedLandLineNumberEvent (@Observes final ObservableAdminLinkedLandLineNumberEvent event) {
292 // Is the event fine?
295 throw new NullPointerException("event is null"); //NOI18N
296 } else if (event.getContact() == null) {
298 throw new NullPointerException("event.contact is null");
299 } else if (event.getContact().getContactId() == null) {
301 throw new NullPointerException("event.contact.contactId is null");
302 } else if (event.getContact().getContactId() < 1) {
304 throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
305 } else if (event.getContact().getContactLandLineNumber() == null) {
307 throw new NullPointerException("event.contact.contactLandLineNumber is null");
308 } else if (event.getContact().getContactLandLineNumber().getPhoneId() == null) {
310 throw new NullPointerException("event.contact.contactLandLineNumber.phoneId is null");
311 } else if (event.getContact().getContactLandLineNumber().getPhoneId() < 1) {
313 throw new NullPointerException("event.contact.contactLandLineNumber.phoneId=" + event.getContact().getContactLandLineNumber().getPhoneId() + " is invalid");
314 } else if (event.getLinkedLandLineNumber() == null) {
316 throw new NullPointerException("event.linkedLandLineNumer is null");
319 // Is the id number in linked number not set?
320 if (event.getLinkedLandLineNumber().getPhoneId() == null) {
321 // Then it is a new number, so add it from contact as there the id number has been set
322 this.uniqueAddLandLineNumber(event.getContact().getContactLandLineNumber());
327 * Observes events being fired when an administrator has a linked a mobile
330 * @param event Event being fired
332 public void afterAdminLinkedMobileNumberEvent (@Observes final ObservableAdminLinkedMobileNumberEvent event) {
333 // Is the event fine?
336 throw new NullPointerException("event is null"); //NOI18N
337 } else if (event.getContact() == null) {
339 throw new NullPointerException("event.contact is null");
340 } else if (event.getContact().getContactId() == null) {
342 throw new NullPointerException("event.contact.contactId is null");
343 } else if (event.getContact().getContactId() < 1) {
345 throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
346 } else if (event.getContact().getContactMobileNumber() == null) {
348 throw new NullPointerException("event.contact.contactMobileNumber is null");
349 } else if (event.getContact().getContactMobileNumber().getPhoneId() == null) {
351 throw new NullPointerException("event.contact.contactMobileNumber.phoneId is null");
352 } else if (event.getContact().getContactMobileNumber().getPhoneId() < 1) {
354 throw new NullPointerException("event.contact.contactMobileNumber.phoneId=" + event.getContact().getContactMobileNumber().getPhoneId() + " is invalid");
355 } else if (event.getLinkedMobileNumber() == null) {
357 throw new NullPointerException("event.linkedMobileNumer is null");
360 // Is the id number in linked number not set?
361 if (event.getLinkedMobileNumber().getPhoneId() == null) {
362 // Then it is a new number, so add it from contact as there the id number has been set
363 this.uniqueAddMobileNumber(event.getContact().getContactMobileNumber());
368 * Observes events being fired when an administrator has updated contact
371 * @param event Event being fired
373 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
374 // event should not be null
377 throw new NullPointerException("event is null"); //NOI18N
378 } else if (event.getUpdatedContact() == null) {
380 throw new NullPointerException("event.updatedContact is null"); //NOI18N
381 } else if (event.getUpdatedContact().getContactId() == null) {
383 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
384 } else if (event.getUpdatedContact().getContactId() < 1) {
386 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
389 // Update contact's mobile, land-line and fax number
390 this.updateContactPhoneNumbers(event.getUpdatedContact());
397 * Observes events being fired when an administrator has updated a fax
400 * @param event Event being fired
402 public void afterAdminUpdatedFaxNumberEvent (@Observes final AdminUpdatedFaxNumberEvent event) {
403 // event should not be null
406 throw new NullPointerException("event is null"); //NOI18N
407 } else if (event.getUpdatedFaxNumber() == null) {
409 throw new NullPointerException("event.updatedFaxNumber is null"); //NOI18N
410 } else if (event.getUpdatedFaxNumber().getPhoneId() == null) {
412 throw new NullPointerException("event.updatedFaxNumber.phoneId is null"); //NOI18N
413 } else if (event.getUpdatedFaxNumber().getPhoneId() < 1) {
415 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedFaxNumber(), event.getUpdatedFaxNumber().getPhoneId())); //NOI18N
419 this.uniqueAddFaxNumber(event.getUpdatedFaxNumber());
426 * Observes events being fired when an administrator has updated a land-line
429 * @param event Event being fired
431 public void afterAdminUpdatedLandLineNumberEvent (@Observes final AdminUpdatedLandLineNumberEvent event) {
432 // event should not be null
435 throw new NullPointerException("event is null"); //NOI18N
436 } else if (event.getUpdatedLandLineNumber() == null) {
438 throw new NullPointerException("event.updatedLandLineNumber is null"); //NOI18N
439 } else if (event.getUpdatedLandLineNumber().getPhoneId() == null) {
441 throw new NullPointerException("event.updatedLandLineNumber.phoneId is null"); //NOI18N
442 } else if (event.getUpdatedLandLineNumber().getPhoneId() < 1) {
444 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedLandLineNumber(), event.getUpdatedLandLineNumber().getPhoneId())); //NOI18N
448 this.uniqueAddLandLineNumber(event.getUpdatedLandLineNumber());
455 * Observes events being fired when an administrator has updated a mobile
458 * @param event Event being fired
460 public void afterAdminUpdatedMobileNumberEvent (@Observes final AdminUpdatedMobileNumberEvent event) {
461 // event should not be null
464 throw new NullPointerException("event is null"); //NOI18N
465 } else if (event.getUpdatedMobileNumber() == null) {
467 throw new NullPointerException("event.updatedMobileNumber is null"); //NOI18N
468 } else if (event.getUpdatedMobileNumber().getPhoneId() == null) {
470 throw new NullPointerException("event.updatedMobileNumber.phoneId is null"); //NOI18N
471 } else if (event.getUpdatedMobileNumber().getPhoneId() < 1) {
473 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedMobileNumber(), event.getUpdatedMobileNumber().getPhoneId())); //NOI18N
477 this.uniqueAddMobileNumber(event.getUpdatedMobileNumber());
484 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
485 public List<DialableFaxNumber> allFaxNumbers () {
487 final List<DialableFaxNumber> list = new LinkedList<>();
490 final Iterator<Cache.Entry<Long, DialableFaxNumber>> iterator = this.faxNumberCache.iterator();
493 while (iterator.hasNext()) {
495 final Cache.Entry<Long, DialableFaxNumber> next = iterator.next();
498 list.add(next.getValue());
506 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
507 public List<DialableLandLineNumber> allLandLineNumbers () {
509 final List<DialableLandLineNumber> list = new LinkedList<>();
512 final Iterator<Cache.Entry<Long, DialableLandLineNumber>> iterator = this.landLineNumberCache.iterator();
515 while (iterator.hasNext()) {
517 final Cache.Entry<Long, DialableLandLineNumber> next = iterator.next();
520 list.add(next.getValue());
528 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
529 public List<DialableMobileNumber> allMobileNumbers () {
531 final List<DialableMobileNumber> list = new LinkedList<>();
534 final Iterator<Cache.Entry<Long, DialableMobileNumber>> iterator = this.mobileNumberCache.iterator();
537 while (iterator.hasNext()) {
539 final Cache.Entry<Long, DialableMobileNumber> next = iterator.next();
542 list.add(next.getValue());
550 * Post-construction method
553 public void init () {
555 if (!this.faxNumberCache.iterator().hasNext()) {
557 final List<DialableFaxNumber> list = this.phoneBean.allFaxNumbers();
560 for (final Iterator<DialableFaxNumber> iterator = list.iterator(); iterator.hasNext();) {
562 final DialableFaxNumber next = iterator.next();
565 this.faxNumberCache.put(next.getPhoneId(), next);
570 if (!this.landLineNumberCache.iterator().hasNext()) {
572 final List<DialableLandLineNumber> list = this.phoneBean.allLandLineNumbers();
575 for (final Iterator<DialableLandLineNumber> iterator = list.iterator(); iterator.hasNext();) {
577 final DialableLandLineNumber next = iterator.next();
580 this.landLineNumberCache.put(next.getPhoneId(), next);
585 if (!this.mobileNumberCache.iterator().hasNext()) {
587 final List<DialableMobileNumber> list = this.phoneBean.allMobileNumbers();
590 for (final Iterator<DialableMobileNumber> iterator = list.iterator(); iterator.hasNext();) {
592 final DialableMobileNumber next = iterator.next();
595 this.mobileNumberCache.put(next.getPhoneId(), next);
603 private void clear () {
608 * Uniquely add given fax number to this bean's list. First remove the old
609 * instance (by id number), then re-add it again.
611 * @param faxNumber number to add
613 private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
614 // Make sure the parameter is valid
615 if (null == faxNumber) {
617 throw new NullPointerException("faxNumber is null");
618 } else if (faxNumber.getPhoneId() == null) {
620 throw new NullPointerException("faxNumber.phoneId is null");
621 } else if (faxNumber.getPhoneId() < 1) {
623 throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
627 if (!this.allFaxNumbers().remove(faxNumber)) {
628 // Did not work, try by id number
629 for (final DialableFaxNumber fax : this.allFaxNumbers()) {
630 // Is id number the same?
631 if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) {
633 this.allFaxNumbers().remove(fax);
640 this.allFaxNumbers().add(faxNumber);
644 * Uniquely add given land-line number to this bean's list. First remove the
645 * old instance (by id number), then re-add it again.
647 * @param landLineNumber Land-line number to add
649 private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
650 // Make sure the parameter is valid
651 if (null == landLineNumber) {
653 throw new NullPointerException("landLineNumber is null");
654 } else if (landLineNumber.getPhoneId() == null) {
656 throw new NullPointerException("landLineNumber.phoneId is null");
657 } else if (landLineNumber.getPhoneId() < 1) {
659 throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
663 if (!this.allLandLineNumbers().remove(landLineNumber)) {
664 // Did not work, try by id number
665 for (final DialableLandLineNumber landLine : this.allLandLineNumbers()) {
666 // Is id number the same?
667 if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) {
669 this.allLandLineNumbers().remove(landLine);
676 this.allLandLineNumbers().add(landLineNumber);
680 * Uniquely add given mobile number to this bean's list. First remove the
681 * old instance (by id number), then re-add it again.
683 * @param mobileNumber Mobile number to add
685 private void uniqueAddMobileNumber (final DialableMobileNumber mobileNumber) {
686 // Make sure the parameter is valid
687 if (null == mobileNumber) {
689 throw new NullPointerException("mobileNumber is null");
690 } else if (mobileNumber.getPhoneId() == null) {
692 throw new NullPointerException("mobileNumber.phoneId is null");
693 } else if (mobileNumber.getPhoneId() < 1) {
695 throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid.", mobileNumber.getPhoneId()));
698 // First remove it by object
699 if (!this.allMobileNumbers().remove(mobileNumber)) {
700 // Did not work, try by id number
701 for (final DialableMobileNumber cell : this.allMobileNumbers()) {
702 // Is id number the same?
703 if (Objects.equals(cell.getPhoneId(), mobileNumber.getPhoneId())) {
705 this.allMobileNumbers().remove(cell);
712 this.allMobileNumbers().add(mobileNumber);
716 * Updates given contact's mobile, land-line and fax number
718 * @param contact Contact instance
720 private void updateContactPhoneNumbers (final Contact contact) {
721 // Parameter must be valid
722 if (null == contact) {
724 throw new NullPointerException("contact is null");
725 } else if (contact.getContactId() == null) {
727 throw new NullPointerException("contact.contactId is null");
728 } else if (contact.getContactId() < 1) {
729 // Id number is not valid
733 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
735 this.uniqueAddMobileNumber(contact.getContactMobileNumber());
739 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
741 this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
745 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
747 this.uniqueAddFaxNumber(contact.getContactFaxNumber());