2 * Copyright (C) 2016 - 2024 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.addressbook.beans.contact.mobile;
19 import java.text.MessageFormat;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Objects;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Observes;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.addressbook.beans.BaseAddressbookBean;
28 import org.mxchange.addressbook.beans.contact.list.AddressbookContactListWebViewController;
29 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
30 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.ObservableUpdatedContactEvent;
32 import org.mxchange.jcontacts.model.contact.Contact;
33 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
34 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
35 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
38 * A general contact bean (controller)
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("contactMobileController")
44 public class AddressbookContactMobileWebRequestBean extends BaseAddressbookBean implements AddressbookContactMobileWebRequestController {
49 private static final long serialVersionUID = 542_145_353_001L;
52 * An instance of a contact-list controller
55 private AddressbookContactListWebViewController contactListController;
58 * Chosen mobile number
60 private DialableMobileNumber mobileNumber;
65 public AddressbookContactMobileWebRequestBean () {
66 // Call super constructor
71 * Observes events being fired when an administrator has added a new
74 * @param event Event being fired
76 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
77 // The event must be valid
80 throw new NullPointerException("event is null"); //NOI18N
81 } else if (event.getAddedContact() == null) {
83 throw new NullPointerException("event.addedContact is null"); //NOI18N
84 } else if (event.getAddedContact().getContactId() == null) {
86 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
87 } else if (event.getAddedContact().getContactId() < 1) {
89 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
97 * Event observer for newly added users by administrator
99 * @param event Event being fired
101 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
102 // Event and contained entity instance should not be null
105 throw new NullPointerException("event is null"); //NOI18N
106 } else if (event.getAddedUser() == null) {
108 throw new NullPointerException("event.addedUser is null"); //NOI18N
109 } else if (event.getAddedUser().getUserId() == null) {
111 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
112 } else if (event.getAddedUser().getUserId() < 1) {
114 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
122 * Event observer for updated contact data by administrators
124 * @param event Updated contact data event
126 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
127 // Event and contained entity instance should not be null
130 throw new NullPointerException("event is null"); //NOI18N
131 } else if (event.getUpdatedContact() == null) {
133 throw new NullPointerException("event.updatedContact is null"); //NOI18N
134 } else if (event.getUpdatedContact().getContactId() == null) {
136 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
137 } else if (event.getUpdatedContact().getContactId() < 1) {
139 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
147 * Observes events being fired when a bean helper has successfully created a
148 * mobile number instance.
150 * @param event Event being fired
152 public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
153 // The event instance must be valid
156 throw new NullPointerException("event is null"); //NOI18N
157 } else if (event.getMobileNumber() == null) {
159 throw new NullPointerException("event.mobileNumber is null"); //NOI18N
160 } else if (event.getMobileNumber().getMobileId() == null) {
161 // Throw NPE yet again
162 throw new NullPointerException("event.mobileNumber.mobileId is null"); //NOI18N
163 } else if (event.getMobileNumber().getMobileId() < 1) {
164 // Throw NPE yet again
165 throw new NullPointerException(MessageFormat.format("event.mobileNumber.mobileId={0} is invalid", event.getMobileNumber().getMobileId())); //NOI18N
169 this.setMobileNumber(event.getMobileNumber());
173 * Event observer for updated contact data by the user
175 * @param event Updated contact data event
177 public void afterUpdatedContactDataEvent (@Observes final ObservableUpdatedContactEvent event) {
178 // Event and contained entity instance should not be null
181 throw new NullPointerException("event is null"); //NOI18N
182 } else if (event.getUpdatedContact() == null) {
184 throw new NullPointerException("event.updatedContact is null"); //NOI18N
185 } else if (event.getUpdatedContact().getContactId() == null) {
187 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
188 } else if (event.getUpdatedContact().getContactId() < 1) {
190 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
198 * Getter for all contacts having current mobile number linked
200 * @return List of all linked contacts
202 public List<Contact> allCurrentMobileNumberContacts () {
204 final DialableMobileNumber dialableMobileNumber = this.getMobileNumber();
207 final List<Contact> contacts = new LinkedList<>();
209 // "Walk" through all contacts
210 for (final Contact contact : this.contactListController.getAllContacts()) {
211 // Is mobile instance the same?
212 if (Objects.equals(contact.getContactMobileNumber(), dialableMobileNumber)) {
214 contacts.add(contact);
218 // Return now-cached list
223 * Getter for chosen mobile number
225 * @return mobile number
227 public DialableMobileNumber getMobileNumber () {
228 return this.mobileNumber;
232 * Setter for chosen mobile number
234 * @param mobileNumber mobile number
236 public void setMobileNumber (final DialableMobileNumber mobileNumber) {
237 this.mobileNumber = mobileNumber;
243 private void clear () {