2 * Copyright (C) 2016, 2017 Roland Häder
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.jjobs.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;
27 import javax.enterprise.context.RequestScoped;
28 import javax.enterprise.event.Observes;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jcontacts.contact.Contact;
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.jjobs.beans.BaseJobsController;
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.phonenumbers.fax.DialableFaxNumber;
45 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
46 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
47 import org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote;
48 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
51 * Regular controller (bean) for phone numbers
53 * @author Roland Häder<roland@mxchange.org>
55 @Named ("phoneController")
57 public class JobsPhoneWebRequestBean extends BaseJobsController implements JobsPhoneWebRequestController {
62 private static final long serialVersionUID = 491_058_674_675_690_105L;
68 @NamedCache (cacheName = "faxNumberCache", managementEnabled = true)
69 private transient Cache<Long, DialableFaxNumber> faxNumberCache;
72 * All land-line numbers
75 @NamedCache (cacheName = "landLineNumberCache", managementEnabled = true)
76 private transient Cache<Long, DialableLandLineNumber> landLineNumberCache;
82 @NamedCache (cacheName = "mobileNumberCache", managementEnabled = true)
83 private transient Cache<Long, DialableMobileNumber> mobileNumberCache;
86 * General EJB for phone numbers
88 @EJB (lookup = "java:global/jjobs-ejb/phone!org.mxchange.jphone.phonenumbers.phone.PhoneSessionBeanRemote")
89 private PhoneSessionBeanRemote phoneBean;
94 public JobsPhoneWebRequestBean () {
95 // Call super constructor
100 * Observes events being fired when an administrator has added a new
103 * @param event Event being fired
105 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
106 // The event must be valid
109 throw new NullPointerException("event is null"); //NOI18N
110 } else if (event.getAddedContact() == null) {
112 throw new NullPointerException("event.addedContact is null"); //NOI18N
113 } else if (event.getAddedContact().getContactId() == null) {
115 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
116 } else if (event.getAddedContact().getContactId() < 1) {
118 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
121 // Update contact's mobile, land-line and fax number
122 this.updateContactPhoneNumbers(event.getAddedContact());
129 * Event observer for newly added users by administrator
131 * @param event Event being fired
133 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
134 // event should not be null
137 throw new NullPointerException("event is null"); //NOI18N
138 } else if (event.getAddedUser() == null) {
140 throw new NullPointerException("event.addedUser is null"); //NOI18N
141 } else if (event.getAddedUser().getUserId() == null) {
143 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
144 } else if (event.getAddedUser().getUserId() < 1) {
146 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
149 // Update contact's mobile, land-line and fax number
150 this.updateContactPhoneNumbers(event.getAddedUser().getUserContact());
157 * Observes events being fired when an administrator has deleted a fax
160 * @param event Event being fired
162 public void afterAdminDeletedFaxNumberEvent (@Observes final AdminDeletedFaxNumberEvent event) {
163 // event should not be null
166 throw new NullPointerException("event is null"); //NOI18N
167 } else if (event.getDeletedFaxNumber() == null) {
169 throw new NullPointerException("event.deletedFaxNumber is null"); //NOI18N
170 } else if (event.getDeletedFaxNumber().getPhoneId() == null) {
172 throw new NullPointerException("event.deletedFaxNumber.phoneId is null"); //NOI18N
173 } else if (event.getDeletedFaxNumber().getPhoneId() < 1) {
175 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedFaxNumber(), event.getDeletedFaxNumber().getPhoneId())); //NOI18N
178 // Update contact's mobile, land-line and fax number
179 this.allFaxNumbers().remove(event.getDeletedFaxNumber());
186 * Observes events being fired when an administrator has deleted a land-line
189 * @param event Event being fired
191 public void afterAdminDeletedLandLineNumberEvent (@Observes final AdminDeletedLandLineNumberEvent event) {
192 // event should not be null
195 throw new NullPointerException("event is null"); //NOI18N
196 } else if (event.getDeletedLandLineNumber() == null) {
198 throw new NullPointerException("event.deletedLandLineNumber is null"); //NOI18N
199 } else if (event.getDeletedLandLineNumber().getPhoneId() == null) {
201 throw new NullPointerException("event.deletedLandLineNumber.phoneId is null"); //NOI18N
202 } else if (event.getDeletedLandLineNumber().getPhoneId() < 1) {
204 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedLandLineNumber(), event.getDeletedLandLineNumber().getPhoneId())); //NOI18N
207 // Update contact's mobile, land-line and fax number
208 this.allLandLineNumbers().remove(event.getDeletedLandLineNumber());
215 * Observes events being fired when an administrator has deleted a mobile
218 * @param event Event being fired
220 public void afterAdminDeletedMobileNumberEvent (@Observes final AdminDeletedMobileNumberEvent event) {
221 // event should not be null
224 throw new NullPointerException("event is null"); //NOI18N
225 } else if (event.getDeletedMobileNumber() == null) {
227 throw new NullPointerException("event.deletedMobileNumber is null"); //NOI18N
228 } else if (event.getDeletedMobileNumber().getPhoneId() == null) {
230 throw new NullPointerException("event.deletedMobileNumber.phoneId is null"); //NOI18N
231 } else if (event.getDeletedMobileNumber().getPhoneId() < 1) {
233 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getDeletedMobileNumber(), event.getDeletedMobileNumber().getPhoneId())); //NOI18N
236 // Update contact's mobile, land-line and fax number
237 this.allMobileNumbers().remove(event.getDeletedMobileNumber());
244 * Observes events being fired when an administrator has a linked a fax
247 * @param event Event being fired
249 public void afterAdminLinkedFaxNumberEvent (@Observes final ObservableAdminLinkedFaxNumberEvent event) {
250 // Is the event fine?
253 throw new NullPointerException("event is null"); //NOI18N
254 } else if (event.getContact() == null) {
256 throw new NullPointerException("event.contact is null");
257 } else if (event.getContact().getContactId() == null) {
259 throw new NullPointerException("event.contact.contactId is null");
260 } else if (event.getContact().getContactId() < 1) {
262 throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
263 } else if (event.getContact().getContactFaxNumber() == null) {
265 throw new NullPointerException("event.contact.contactFaxNumber is null");
266 } else if (event.getContact().getContactFaxNumber().getPhoneId() == null) {
268 throw new NullPointerException("event.contact.contactFaxNumber.phoneId is null");
269 } else if (event.getContact().getContactFaxNumber().getPhoneId() < 1) {
271 throw new NullPointerException("event.contact.contactFaxNumber.phoneId=" + event.getContact().getContactFaxNumber().getPhoneId() + " is invalid");
272 } else if (event.getLinkedFaxNumber() == null) {
274 throw new NullPointerException("event.linkedFaxNumer is null");
277 // Is the id number in linked number not set?
278 if (event.getLinkedFaxNumber().getPhoneId() == null) {
279 // Then it is a new number, so add it from contact as there the id number has been set
280 this.uniqueAddFaxNumber(event.getContact().getContactFaxNumber());
285 * Observes events being fired when an administrator has a linked a
288 * @param event Event being fired
290 public void afterAdminLinkedLandLineNumberEvent (@Observes final ObservableAdminLinkedLandLineNumberEvent event) {
291 // Is the event fine?
294 throw new NullPointerException("event is null"); //NOI18N
295 } else if (event.getContact() == null) {
297 throw new NullPointerException("event.contact is null");
298 } else if (event.getContact().getContactId() == null) {
300 throw new NullPointerException("event.contact.contactId is null");
301 } else if (event.getContact().getContactId() < 1) {
303 throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
304 } else if (event.getContact().getContactLandLineNumber() == null) {
306 throw new NullPointerException("event.contact.contactLandLineNumber is null");
307 } else if (event.getContact().getContactLandLineNumber().getPhoneId() == null) {
309 throw new NullPointerException("event.contact.contactLandLineNumber.phoneId is null");
310 } else if (event.getContact().getContactLandLineNumber().getPhoneId() < 1) {
312 throw new NullPointerException("event.contact.contactLandLineNumber.phoneId=" + event.getContact().getContactLandLineNumber().getPhoneId() + " is invalid");
313 } else if (event.getLinkedLandLineNumber() == null) {
315 throw new NullPointerException("event.linkedLandLineNumer is null");
318 // Is the id number in linked number not set?
319 if (event.getLinkedLandLineNumber().getPhoneId() == null) {
320 // Then it is a new number, so add it from contact as there the id number has been set
321 this.uniqueAddLandLineNumber(event.getContact().getContactLandLineNumber());
326 * Observes events being fired when an administrator has a linked a mobile
329 * @param event Event being fired
331 public void afterAdminLinkedMobileNumberEvent (@Observes final ObservableAdminLinkedMobileNumberEvent event) {
332 // Is the event fine?
335 throw new NullPointerException("event is null"); //NOI18N
336 } else if (event.getContact() == null) {
338 throw new NullPointerException("event.contact is null");
339 } else if (event.getContact().getContactId() == null) {
341 throw new NullPointerException("event.contact.contactId is null");
342 } else if (event.getContact().getContactId() < 1) {
344 throw new NullPointerException("event.contact.contactId=" + event.getContact().getContactId() + " is invalid");
345 } else if (event.getContact().getContactMobileNumber() == null) {
347 throw new NullPointerException("event.contact.contactMobileNumber is null");
348 } else if (event.getContact().getContactMobileNumber().getPhoneId() == null) {
350 throw new NullPointerException("event.contact.contactMobileNumber.phoneId is null");
351 } else if (event.getContact().getContactMobileNumber().getPhoneId() < 1) {
353 throw new NullPointerException("event.contact.contactMobileNumber.phoneId=" + event.getContact().getContactMobileNumber().getPhoneId() + " is invalid");
354 } else if (event.getLinkedMobileNumber() == null) {
356 throw new NullPointerException("event.linkedMobileNumer is null");
359 // Is the id number in linked number not set?
360 if (event.getLinkedMobileNumber().getPhoneId() == null) {
361 // Then it is a new number, so add it from contact as there the id number has been set
362 this.uniqueAddMobileNumber(event.getContact().getContactMobileNumber());
367 * Observes events being fired when an administrator has updated contact
370 * @param event Event being fired
372 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
373 // event should not be null
376 throw new NullPointerException("event is null"); //NOI18N
377 } else if (event.getUpdatedContact() == null) {
379 throw new NullPointerException("event.updatedContact is null"); //NOI18N
380 } else if (event.getUpdatedContact().getContactId() == null) {
382 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
383 } else if (event.getUpdatedContact().getContactId() < 1) {
385 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
388 // Update contact's mobile, land-line and fax number
389 this.updateContactPhoneNumbers(event.getUpdatedContact());
396 * Observes events being fired when an administrator has updated a fax
399 * @param event Event being fired
401 public void afterAdminUpdatedFaxNumberEvent (@Observes final AdminUpdatedFaxNumberEvent event) {
402 // event should not be null
405 throw new NullPointerException("event is null"); //NOI18N
406 } else if (event.getUpdatedFaxNumber() == null) {
408 throw new NullPointerException("event.updatedFaxNumber is null"); //NOI18N
409 } else if (event.getUpdatedFaxNumber().getPhoneId() == null) {
411 throw new NullPointerException("event.updatedFaxNumber.phoneId is null"); //NOI18N
412 } else if (event.getUpdatedFaxNumber().getPhoneId() < 1) {
414 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedFaxNumber(), event.getUpdatedFaxNumber().getPhoneId())); //NOI18N
418 this.uniqueAddFaxNumber(event.getUpdatedFaxNumber());
425 * Observes events being fired when an administrator has updated a land-line
428 * @param event Event being fired
430 public void afterAdminUpdatedLandLineNumberEvent (@Observes final AdminUpdatedLandLineNumberEvent event) {
431 // event should not be null
434 throw new NullPointerException("event is null"); //NOI18N
435 } else if (event.getUpdatedLandLineNumber() == null) {
437 throw new NullPointerException("event.updatedLandLineNumber is null"); //NOI18N
438 } else if (event.getUpdatedLandLineNumber().getPhoneId() == null) {
440 throw new NullPointerException("event.updatedLandLineNumber.phoneId is null"); //NOI18N
441 } else if (event.getUpdatedLandLineNumber().getPhoneId() < 1) {
443 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedLandLineNumber(), event.getUpdatedLandLineNumber().getPhoneId())); //NOI18N
447 this.uniqueAddLandLineNumber(event.getUpdatedLandLineNumber());
454 * Observes events being fired when an administrator has updated a mobile
457 * @param event Event being fired
459 public void afterAdminUpdatedMobileNumberEvent (@Observes final AdminUpdatedMobileNumberEvent event) {
460 // event should not be null
463 throw new NullPointerException("event is null"); //NOI18N
464 } else if (event.getUpdatedMobileNumber() == null) {
466 throw new NullPointerException("event.updatedMobileNumber is null"); //NOI18N
467 } else if (event.getUpdatedMobileNumber().getPhoneId() == null) {
469 throw new NullPointerException("event.updatedMobileNumber.phoneId is null"); //NOI18N
470 } else if (event.getUpdatedMobileNumber().getPhoneId() < 1) {
472 throw new IllegalArgumentException(MessageFormat.format("phoneId of contact={0} is not valid: {1}", event.getUpdatedMobileNumber(), event.getUpdatedMobileNumber().getPhoneId())); //NOI18N
476 this.uniqueAddMobileNumber(event.getUpdatedMobileNumber());
483 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
484 public List<DialableFaxNumber> allFaxNumbers () {
486 List<DialableFaxNumber> list = new LinkedList<>();
489 Iterator<Cache.Entry<Long, DialableFaxNumber>> iterator = this.faxNumberCache.iterator();
492 while (iterator.hasNext()) {
494 final Cache.Entry<Long, DialableFaxNumber> next = iterator.next();
497 list.add(next.getValue());
505 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
506 public List<DialableLandLineNumber> allLandLineNumbers () {
508 List<DialableLandLineNumber> list = new LinkedList<>();
511 Iterator<Cache.Entry<Long, DialableLandLineNumber>> iterator = this.landLineNumberCache.iterator();
514 while (iterator.hasNext()) {
516 final Cache.Entry<Long, DialableLandLineNumber> next = iterator.next();
519 list.add(next.getValue());
527 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
528 public List<DialableMobileNumber> allMobileNumbers () {
530 List<DialableMobileNumber> list = new LinkedList<>();
533 Iterator<Cache.Entry<Long, DialableMobileNumber>> iterator = this.mobileNumberCache.iterator();
536 while (iterator.hasNext()) {
538 final Cache.Entry<Long, DialableMobileNumber> next = iterator.next();
541 list.add(next.getValue());
549 * Post-construction method
552 public void init () {
554 if (!this.faxNumberCache.iterator().hasNext()) {
556 List<DialableFaxNumber> list = this.phoneBean.allFaxNumbers();
559 for (final Iterator<DialableFaxNumber> iterator = list.iterator(); iterator.hasNext();) {
561 final DialableFaxNumber next = iterator.next();
564 this.faxNumberCache.put(next.getPhoneId(), next);
569 if (!this.landLineNumberCache.iterator().hasNext()) {
571 List<DialableLandLineNumber> list = this.phoneBean.allLandLineNumbers();
574 for (final Iterator<DialableLandLineNumber> iterator = list.iterator(); iterator.hasNext();) {
576 final DialableLandLineNumber next = iterator.next();
579 this.landLineNumberCache.put(next.getPhoneId(), next);
584 if (!this.mobileNumberCache.iterator().hasNext()) {
586 List<DialableMobileNumber> list = this.phoneBean.allMobileNumbers();
589 for (final Iterator<DialableMobileNumber> iterator = list.iterator(); iterator.hasNext();) {
591 final DialableMobileNumber next = iterator.next();
594 this.mobileNumberCache.put(next.getPhoneId(), next);
602 private void clear () {
607 * Uniquely add given fax number to this bean's list. First remove the old
608 * instance (by id number), then re-add it again.
610 * @param faxNumber number to add
612 private void uniqueAddFaxNumber (final DialableFaxNumber faxNumber) {
613 // Make sure the parameter is valid
614 if (null == faxNumber) {
616 throw new NullPointerException("faxNumber is null");
617 } else if (faxNumber.getPhoneId() == null) {
619 throw new NullPointerException("faxNumber.phoneId is null");
620 } else if (faxNumber.getPhoneId() < 1) {
622 throw new IllegalArgumentException(MessageFormat.format("faxNumber.phoneId={0} is not valid.", faxNumber.getPhoneId()));
626 if (!this.allFaxNumbers().remove(faxNumber)) {
627 // Did not work, try by id number
628 for (final DialableFaxNumber fax : this.allFaxNumbers()) {
629 // Is id number the same?
630 if (Objects.equals(fax.getPhoneId(), faxNumber.getPhoneId())) {
632 this.allFaxNumbers().remove(fax);
639 this.allFaxNumbers().add(faxNumber);
643 * Uniquely add given land-line number to this bean's list. First remove the
644 * old instance (by id number), then re-add it again.
646 * @param landLineNumber Land-line number to add
648 private void uniqueAddLandLineNumber (final DialableLandLineNumber landLineNumber) {
649 // Make sure the parameter is valid
650 if (null == landLineNumber) {
652 throw new NullPointerException("landLineNumber is null");
653 } else if (landLineNumber.getPhoneId() == null) {
655 throw new NullPointerException("landLineNumber.phoneId is null");
656 } else if (landLineNumber.getPhoneId() < 1) {
658 throw new IllegalArgumentException(MessageFormat.format("landLineNumber.phoneId={0} is not valid.", landLineNumber.getPhoneId()));
662 if (!this.allLandLineNumbers().remove(landLineNumber)) {
663 // Did not work, try by id number
664 for (final DialableLandLineNumber landLine : this.allLandLineNumbers()) {
665 // Is id number the same?
666 if (Objects.equals(landLine.getPhoneId(), landLineNumber.getPhoneId())) {
668 this.allLandLineNumbers().remove(landLine);
675 this.allLandLineNumbers().add(landLineNumber);
679 * Uniquely add given mobile number to this bean's list. First remove the
680 * old instance (by id number), then re-add it again.
682 * @param mobileNumber Mobile number to add
684 private void uniqueAddMobileNumber (final DialableMobileNumber mobileNumber) {
685 // Make sure the parameter is valid
686 if (null == mobileNumber) {
688 throw new NullPointerException("mobileNumber is null");
689 } else if (mobileNumber.getPhoneId() == null) {
691 throw new NullPointerException("mobileNumber.phoneId is null");
692 } else if (mobileNumber.getPhoneId() < 1) {
694 throw new IllegalArgumentException(MessageFormat.format("mobileNumber.phoneId={0} is not valid.", mobileNumber.getPhoneId()));
697 // First remove it by object
698 if (!this.allMobileNumbers().remove(mobileNumber)) {
699 // Did not work, try by id number
700 for (final DialableMobileNumber cell : this.allMobileNumbers()) {
701 // Is id number the same?
702 if (Objects.equals(cell.getPhoneId(), mobileNumber.getPhoneId())) {
704 this.allMobileNumbers().remove(cell);
711 this.allMobileNumbers().add(mobileNumber);
715 * Updates given contact's mobile, land-line and fax number
717 * @param contact Contact instance
719 private void updateContactPhoneNumbers (final Contact contact) {
720 // Parameter must be valid
721 if (null == contact) {
723 throw new NullPointerException("contact is null");
724 } else if (contact.getContactId() == null) {
726 throw new NullPointerException("contact.contactId is null");
727 } else if (contact.getContactId() < 1) {
728 // Id number is not valid
732 if (contact.getContactMobileNumber() instanceof DialableMobileNumber) {
734 this.uniqueAddMobileNumber(contact.getContactMobileNumber());
738 if (contact.getContactLandLineNumber() instanceof DialableLandLineNumber) {
740 this.uniqueAddLandLineNumber(contact.getContactLandLineNumber());
744 if (contact.getContactFaxNumber() instanceof DialableFaxNumber) {
746 this.uniqueAddFaxNumber(contact.getContactFaxNumber());