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.contact.phone;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.LinkedList;
22 import java.util.List;
23 import java.util.Objects;
24 import javax.cache.Cache;
25 import javax.enterprise.context.RequestScoped;
26 import javax.enterprise.event.Observes;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
30 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
31 import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent;
32 import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent;
33 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
34 import org.mxchange.jcontacts.model.contact.Contact;
35 import org.mxchange.jphone.events.fax.created.ObservableCreatedFaxNumberEvent;
36 import org.mxchange.jphone.events.landline.created.ObservableCreatedLandLineNumberEvent;
37 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
38 import org.mxchange.jphone.model.phonenumbers.DialableNumber;
39 import org.mxchange.jphone.model.phonenumbers.fax.DialableFaxNumber;
40 import org.mxchange.jphone.model.phonenumbers.landline.DialableLandLineNumber;
41 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
42 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
43 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
44 import org.mxchange.pizzaapplication.beans.contact.PizzaContactWebRequestController;
47 * A general contact bean (controller)
49 * @author Roland Häder<roland@mxchange.org>
51 @Named ("contactPhoneController")
53 public class PizzaContactPhoneWebRequestBean extends BasePizzaBean implements PizzaContactPhoneWebRequestController {
58 private static final long serialVersionUID = 542_145_347_916L;
61 * General contact controller
64 private PizzaContactWebRequestController contactController;
67 * "Cache" for contact's mobile, land-line and fax numbers. Currently one
68 * per each type is supported. Maybe later this will change into a OneToMany
69 * relationship (one contact, many numbers).
72 @NamedCache (cacheName = "contactsPhoneCache")
73 private Cache<DialableNumber, List<Contact>> contactsPhoneCache;
78 private DialableFaxNumber faxNumber;
83 private DialableLandLineNumber landLineNumber;
86 * Chosen mobile number
88 private DialableMobileNumber mobileNumber;
93 public PizzaContactPhoneWebRequestBean () {
94 // Call super constructor
99 * Observes events being fired when an administrator has added a new
102 * @param event Event being fired
104 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
105 // The event must be valid
108 throw new NullPointerException("event is null"); //NOI18N
109 } else if (event.getAddedContact() == null) {
111 throw new NullPointerException("event.addedContact is null"); //NOI18N
112 } else if (event.getAddedContact().getContactId() == null) {
114 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
115 } else if (event.getAddedContact().getContactId() < 1) {
117 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
125 * Event observer for newly added users by administrator
127 * @param event Event being fired
129 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
130 // event should not be null
133 throw new NullPointerException("event is null"); //NOI18N
134 } else if (event.getAddedUser() == null) {
136 throw new NullPointerException("event.addedUser is null"); //NOI18N
137 } else if (event.getAddedUser().getUserId() == null) {
139 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
140 } else if (event.getAddedUser().getUserId() < 1) {
142 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
150 * Event observer for unlinked fax contact by administrators
152 * @param event Unlinked fax contact event
154 public void afterAdminUnlinkedFaxContactDataEvent (@Observes final ObservableAdminUnlinkedFaxNumberEvent event) {
155 // event should not be null
158 throw new NullPointerException("event is null"); //NOI18N
159 } else if (event.getUnlinkedFaxNumber() == null) {
161 throw new NullPointerException("event.unlinkedFaxNumber is null"); //NOI18N
162 } else if (event.getUnlinkedFaxNumber().getPhoneId() == null) {
164 throw new NullPointerException("event.unlinkedFaxNumber.contactId is null"); //NOI18N
165 } else if (event.getUnlinkedFaxNumber().getPhoneId() < 1) {
167 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedFaxNumber(), event.getUnlinkedFaxNumber().getPhoneId())); //NOI18N
170 // Remove it from list
171 this.contactsPhoneCache.remove(event.getUnlinkedFaxNumber());
178 * Event observer for unlinked land-line contact by administrators
180 * @param event Unlinked land-line contact event
182 public void afterAdminUnlinkedLandLineContactDataEvent (@Observes final ObservableAdminUnlinkedLandLineNumberEvent event) {
183 // event should not be null
186 throw new NullPointerException("event is null"); //NOI18N
187 } else if (event.getUnlinkedLandLineNumber() == null) {
189 throw new NullPointerException("event.unlinkedLandLineNumber is null"); //NOI18N
190 } else if (event.getUnlinkedLandLineNumber().getPhoneId() == null) {
192 throw new NullPointerException("event.unlinkedLandLineNumber.contactId is null"); //NOI18N
193 } else if (event.getUnlinkedLandLineNumber().getPhoneId() < 1) {
195 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedLandLineNumber(), event.getUnlinkedLandLineNumber().getPhoneId())); //NOI18N
198 // Remove it from list
199 this.contactsPhoneCache.remove(event.getUnlinkedLandLineNumber());
206 * Event observer for unlinked mobile contact by administrators
208 * @param event Unlinked mobile contact event
210 public void afterAdminUnlinkedMobileContactDataEvent (@Observes final ObservableAdminUnlinkedMobileNumberEvent event) {
211 // event should not be null
214 throw new NullPointerException("event is null"); //NOI18N
215 } else if (event.getUnlinkedMobileNumber() == null) {
217 throw new NullPointerException("event.unlinkedMobileNumber is null"); //NOI18N
218 } else if (event.getUnlinkedMobileNumber().getPhoneId() == null) {
220 throw new NullPointerException("event.unlinkedMobileNumber.contactId is null"); //NOI18N
221 } else if (event.getUnlinkedMobileNumber().getPhoneId() < 1) {
223 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUnlinkedMobileNumber(), event.getUnlinkedMobileNumber().getPhoneId())); //NOI18N
226 // Remove it from list
227 this.contactsPhoneCache.remove(event.getUnlinkedMobileNumber());
234 * Event observer for updated contact data by administrators
236 * @param event Updated contact data event
238 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
239 // event should not be null
242 throw new NullPointerException("event is null"); //NOI18N
243 } else if (event.getUpdatedContact() == null) {
245 throw new NullPointerException("event.updatedContact is null"); //NOI18N
246 } else if (event.getUpdatedContact().getContactId() == null) {
248 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
249 } else if (event.getUpdatedContact().getContactId() < 1) {
251 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
259 * Observes events being fired when a bean helper has successfully created a
260 * fax number instance.
262 * @param event Event being fired
264 public void afterCreatedFaxNumberEvent (@Observes final ObservableCreatedFaxNumberEvent event) {
265 // The event instance must be valid
268 throw new NullPointerException("event is null"); //NOI18N
269 } else if (event.getFaxNumber() == null) {
271 throw new NullPointerException("event.faxNumber is null"); //NOI18N
272 } else if (event.getFaxNumber().getPhoneId() == null) {
273 // Throw NPE yet again
274 throw new NullPointerException("event.faxNumber.phoneId is null"); //NOI18N
275 } else if (event.getFaxNumber().getPhoneId() < 1) {
276 // Throw NPE yet again
277 throw new NullPointerException(MessageFormat.format("event.faxNumber.phoneId={0} is invalid", event.getFaxNumber().getPhoneId())); //NOI18N
281 this.setFaxNumber(event.getFaxNumber());
285 * Observes events being fired when a bean helper has successfully created a
286 * land-line number instance.
288 * @param event Event being fired
290 public void afterCreatedLandLineNumberEvent (@Observes final ObservableCreatedLandLineNumberEvent event) {
291 // The event instance must be valid
294 throw new NullPointerException("event is null"); //NOI18N
295 } else if (event.getLandLineNumber() == null) {
297 throw new NullPointerException("event.landLineNumber is null"); //NOI18N
298 } else if (event.getLandLineNumber().getPhoneId() == null) {
299 // Throw NPE yet again
300 throw new NullPointerException("event.landLineNumber.phoneId is null"); //NOI18N
301 } else if (event.getLandLineNumber().getPhoneId() < 1) {
302 // Throw NPE yet again
303 throw new NullPointerException(MessageFormat.format("event.landLineNumber.phoneId={0} is invalid", event.getLandLineNumber().getPhoneId())); //NOI18N
307 this.setLandLineNumber(event.getLandLineNumber());
311 * Observes events being fired when a bean helper has successfully created a
312 * mobile number instance.
314 * @param event Event being fired
316 public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
317 // The event instance must be valid
320 throw new NullPointerException("event is null"); //NOI18N
321 } else if (event.getMobileNumber() == null) {
323 throw new NullPointerException("event.mobileNumber is null"); //NOI18N
324 } else if (event.getMobileNumber().getPhoneId() == null) {
325 // Throw NPE yet again
326 throw new NullPointerException("event.mobileNumber.phoneId is null"); //NOI18N
327 } else if (event.getMobileNumber().getPhoneId() < 1) {
328 // Throw NPE yet again
329 throw new NullPointerException(MessageFormat.format("event.mobileNumber.phoneId={0} is invalid", event.getMobileNumber().getPhoneId())); //NOI18N
333 this.setMobileNumber(event.getMobileNumber());
337 * Getter for all contacts having current fax number linked
339 * @return List of all linked contacts
341 public List<Contact> allCurrentFaxNumberContacts () {
343 final DialableFaxNumber number = this.getFaxNumber();
346 if (this.contactsPhoneCache.containsKey(number)) {
347 // Return cached version
348 return this.contactsPhoneCache.get(number);
351 final List<Contact> list = new LinkedList<>();
353 // "Walk" through all contacts
354 for (final Contact contact : this.contactController.allContacts()) {
355 // Is mobile instance the same?
356 if (Objects.equals(contact.getContactFaxNumber(), number)) {
362 // Store result in cache
363 this.contactsPhoneCache.put(number, list);
365 // Return now-cached list
371 * Getter for all contacts having current land-line number linked
373 * @return List of all linked contacts
375 public List<Contact> allCurrentLandLineNumberContacts () {
377 final DialableLandLineNumber number = this.getLandLineNumber();
380 if (this.contactsPhoneCache.containsKey(number)) {
381 // Return cached version
382 return this.contactsPhoneCache.get(number);
385 final List<Contact> list = new LinkedList<>();
387 // "Walk" through all contacts
388 for (final Contact contact : this.contactController.allContacts()) {
389 // Is mobile instance the same?
390 if (Objects.equals(contact.getContactLandLineNumber(), number)) {
396 // Store result in cache
397 this.contactsPhoneCache.put(number, list);
399 // Return now-cached list
405 * Getter for all contacts having current mobile number linked
407 * @return List of all linked contacts
409 public List<Contact> allCurrentMobileNumberContacts () {
411 final DialableMobileNumber number = this.getMobileNumber();
414 if (this.contactsPhoneCache.containsKey(number)) {
415 // Return cached version
416 return this.contactsPhoneCache.get(number);
419 final List<Contact> list = new LinkedList<>();
421 // "Walk" through all contacts
422 for (final Contact contact : this.contactController.allContacts()) {
423 // Is mobile instance the same?
424 if (Objects.equals(contact.getContactMobileNumber(), number)) {
430 // Store result in cache
431 this.contactsPhoneCache.put(number, list);
433 // Return now-cached list
439 * Getter for chosen fax number
443 public DialableFaxNumber getFaxNumber () {
444 return this.faxNumber;
448 * Setter for chosen fax number
450 * @param faxNumber fax number
452 public void setFaxNumber (final DialableFaxNumber faxNumber) {
453 this.faxNumber = faxNumber;
457 * Getter for chosen land-line number
459 * @return land-line number
461 public DialableLandLineNumber getLandLineNumber () {
462 return this.landLineNumber;
466 * Setter for chosen land-line number
468 * @param landLineNumber land-line number
470 public void setLandLineNumber (final DialableLandLineNumber landLineNumber) {
471 this.landLineNumber = landLineNumber;
475 * Getter for chosen mobile number
477 * @return mobile number
479 public DialableMobileNumber getMobileNumber () {
480 return this.mobileNumber;
484 * Setter for chosen mobile number
486 * @param mobileNumber mobile number
488 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
489 this.mobileNumber = mobileNumber;
495 private void clear () {