2 * Copyright (C) 2016 - 2022 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.Date;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.event.Observes;
25 import javax.enterprise.inject.Any;
26 import javax.faces.application.FacesMessage;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import org.mxchange.addressbook.beans.BaseAddressbookBean;
30 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
31 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
32 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
33 import org.mxchange.jcontacts.events.contact.update.ObservableUpdatedContactEvent;
34 import org.mxchange.jcontacts.events.mobile.linked.AdminLinkedMobileNumberEvent;
35 import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent;
36 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
37 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
38 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
39 import org.mxchange.jcontacts.model.contact.Contact;
40 import org.mxchange.jcontacts.model.mobile.AdminContactsMobileSessionBeanRemote;
41 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
42 import org.mxchange.jphone.exceptions.mobile.MobileNumberAlreadyLinkedException;
43 import org.mxchange.jphone.exceptions.mobile.MobileNumberNotLinkedException;
44 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
45 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
46 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
47 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
50 * An administrative contact mobile controller (bean)
52 * @author Roland Häder<roland@mxchange.org>
54 @Named ("adminContactMobileController")
56 public class AddressbookAdminContactMobileWebRequestBean extends BaseAddressbookBean implements AddressbookAdminContactMobileWebRequestController {
61 private static final long serialVersionUID = 542_145_347_919L;
64 * Administrative EJB for phone number
66 @EJB (lookup = "java:global/addressbook-ejb/adminContactMobile!org.mxchange.jcontacts.model.mobile.AdminContactsMobileSessionBeanRemote")
67 private AdminContactsMobileSessionBeanRemote adminContactMobileBean;
70 * Event being fired when a mobile number has been linked
74 private Event<ObservableAdminLinkedMobileNumberEvent> adminLinkedMobileNumberEvent;
79 private Contact contact;
82 * When mobile number has been created
84 private Date mobileEntryCreated;
87 * When mobile number has been updated
89 private Date mobileEntryUpdated;
92 * Phone id (primary key)
94 private Long mobileId;
99 private Long mobileNumber;
102 * Event being fired when administrator unlinks mobile from contact
106 private Event<ObservableAdminUnlinkedMobileNumberEvent> mobileNumberUnlinkedEvent;
111 private MobileProvider mobileProvider;
114 * Default constructor
116 public AddressbookAdminContactMobileWebRequestBean () {
117 // Call super constructor
120 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
121 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
125 * Observes events being fired when an administrator has added a new
128 * @param event Event being fired
130 public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
131 // The event must be valid
134 throw new NullPointerException("event is null"); //NOI18N
135 } else if (event.getAddedContact() == null) {
137 throw new NullPointerException("event.addedContact is null"); //NOI18N
138 } else if (event.getAddedContact().getContactId() == null) {
140 throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
141 } else if (event.getAddedContact().getContactId() < 1) {
143 throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
151 * Event observer for newly added users by administrator
153 * @param event Event being fired
155 public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
156 // Event and contained entity instance should not be null
159 throw new NullPointerException("event is null"); //NOI18N
160 } else if (event.getAddedUser() == null) {
162 throw new NullPointerException("event.addedUser is null"); //NOI18N
163 } else if (event.getAddedUser().getUserId() == null) {
165 throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
166 } else if (event.getAddedUser().getUserId() < 1) {
168 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
176 * Event observer for updated contact data by administrators
178 * @param event Updated contact data event
180 public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
181 // Event and contained entity instance should not be null
184 throw new NullPointerException("event is null"); //NOI18N
185 } else if (event.getUpdatedContact() == null) {
187 throw new NullPointerException("event.updatedContact is null"); //NOI18N
188 } else if (event.getUpdatedContact().getContactId() == null) {
190 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
191 } else if (event.getUpdatedContact().getContactId() < 1) {
193 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
201 * Observer for events being fired when a bean helper has successfully
202 * created a contact instance.
204 * @param event Event being fired
206 public void afterCreatedContactEvent (@Observes final ObservableCreatedContactEvent event) {
208 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::afterCreatedContactEvent(): contact={0} - CALLED!", contact)); //NOI18N
210 // The event instance must be valid
213 throw new NullPointerException("event is null"); //NOI18N
214 } else if (event.getCreatedContact() == null) {
216 throw new NullPointerException("event.createdContact is null"); //NOI18N
217 } else if (event.getCreatedContact().getContactId() == null) {
219 throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N
220 } else if (event.getCreatedContact().getContactId() < 1) {
222 throw new IllegalStateException(MessageFormat.format("event.createdContact.contactId={0} is not valid.", event.getCreatedContact().getContactId())); //NOI18N
226 this.setContact(event.getCreatedContact());
230 * Observes events being fired when a bean helper has successfully created a
231 * mobile number instance.
233 * @param event Event being fired
235 public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
236 // The event instance must be valid
239 throw new NullPointerException("event is null"); //NOI18N
240 } else if (event.getMobileNumber() == null) {
242 throw new NullPointerException("event.mobileNumber is null"); //NOI18N
243 } else if (event.getMobileNumber().getMobileId() == null) {
244 // Throw NPE yet again
245 throw new NullPointerException("event.mobileNumber.mobileId is null"); //NOI18N
246 } else if (event.getMobileNumber().getMobileId() < 1) {
247 // Throw NPE yet again
248 throw new NullPointerException(MessageFormat.format("event.mobileNumber.mobileId={0} is invalid", event.getMobileNumber().getMobileId())); //NOI18N
251 // Get fax number from event
252 final DialableMobileNumber number = event.getMobileNumber();
254 // Copy all data to this bean
255 this.setMobileId(number.getMobileId());
256 this.setMobileProvider(number.getMobileProvider());
257 this.setMobileNumber(number.getMobileNumber());
258 this.setMobileEntryCreated(number.getMobileEntryCreated());
259 this.setMobileEntryUpdated(number.getMobileEntryUpdated());
263 * Event observer for updated contact data by the user
265 * @param event Updated contact data event
267 public void afterUpdatedContactDataEvent (@Observes final ObservableUpdatedContactEvent event) {
268 // Event and contained entity instance should not be null
271 throw new NullPointerException("event is null"); //NOI18N
272 } else if (event.getUpdatedContact() == null) {
274 throw new NullPointerException("event.updatedContact is null"); //NOI18N
275 } else if (event.getUpdatedContact().getContactId() == null) {
277 throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
278 } else if (event.getUpdatedContact().getContactId() < 1) {
280 throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
288 * Links mobile number to contact from bean helper as "main mobile number".
290 * @return Redirect outcome
292 public String doLinkMainMobileNumber () {
293 // Get contact from helper
294 final Contact targetContact = this.getContact();
296 // Is all data properly set?
297 if (null == targetContact) {
299 throw new NullPointerException("targetContact is null"); //NOI18N
300 } else if (targetContact.getContactId() == null) {
302 throw new NullPointerException("targetContact.contactId is null"); //NOI18N
303 } else if (targetContact.getContactId() < 1) {
305 throw new IllegalArgumentException(MessageFormat.format("targetContact.contactId={0} is not valid", targetContact.getContactId())); //NOI18N
306 } else if (this.getMobileProvider() == null) {
308 throw new NullPointerException("this.mobileProvider is null"); //NOI18N
309 } else if (this.getMobileProvider().getProviderId() == null) {
311 throw new NullPointerException("this.mobileProvider.providerId is null"); //NOI18N
312 } else if (this.getMobileProvider().getProviderId() < 1) {
314 throw new NullPointerException(MessageFormat.format("this.mobileProvider.providerId={0} is invalid", this.getMobileProvider().getProviderId())); //NOI18N
315 } else if (this.getMobileNumber() == null) {
317 throw new NullPointerException("this.mobileNumber is null"); //NOI18N
318 } else if (this.getMobileNumber() < 1) {
320 throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber={0} is invalid", this.getMobileNumber())); //NOI18N
324 final Contact updatedContact;
325 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
330 updatedContact = this.adminContactMobileBean.linkNewMobileNumberWithContact(targetContact, number);
331 } catch (final MobileNumberAlreadyLinkedException | ContactNotFoundException ex) {
332 // Throw again as cause
333 this.showFacesMessage("form_add_contact_mobile:mobileNumber", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
338 this.adminLinkedMobileNumberEvent.fire(new AdminLinkedMobileNumberEvent(updatedContact, number));
340 // Return to contact profile
341 return "admin_show_contact"; //NOI18N
345 * Getter for contact instance
347 * @return Contact instance
349 public Contact getContact () {
354 * Setter for contact instance
356 * @param contact Contact instance
358 public void setContact (final Contact contact) {
359 this.contact = contact;
363 * Setter for phone id
367 public Long getMobileId () {
368 return this.mobileId;
372 * Getter for phone id
374 * @param mobileId Phone id
376 public void setMobileId (final Long mobileId) {
377 this.mobileId = mobileId;
381 * Getter for mobile number
383 * @return Mobile number
385 public Long getMobileNumber () {
386 return this.mobileNumber;
390 * Setter for mobile number
392 * @param mobileNumber Mobile number
394 public void setMobileNumber (final Long mobileNumber) {
395 this.mobileNumber = mobileNumber;
399 * Getter for mobile provider
401 * @return Mobile provider
403 public MobileProvider getMobileProvider () {
404 return this.mobileProvider;
408 * Setter for mobile provider
410 * @param mobileProvider Mobile provider
412 public void setMobileProvider (final MobileProvider mobileProvider) {
413 this.mobileProvider = mobileProvider;
417 * Unlinks mobile data with current contact
419 * @return Redirect outcome
421 public String unlinkMobileContactData () {
422 // Create fax number instance
423 final DialableMobileNumber number = this.createMobileNumber();
426 if (number == null) {
427 // Not set, throw NPE
428 throw new NullPointerException("number is null"); //NOI18N
429 } else if (number.getMobileId() == null) {
431 throw new NullPointerException("number.phoneId is null"); //NOI18N
432 } else if (number.getMobileId() < 1) {
434 throw new IllegalArgumentException(MessageFormat.format("number.phoneId={0} is not valid", number.getMobileId())); //NOI18N
435 } else if (number.getMobileProvider() == null) {
437 throw new NullPointerException("number.mobileProvider is null"); //NOI18N
438 } else if (number.getMobileProvider().getProviderId() == null) {
440 throw new NullPointerException("number.mobileProvider.providerId is null"); //NOI18N
441 } else if (number.getMobileProvider().getProviderId() < 1) {
443 throw new IllegalArgumentException(MessageFormat.format("number.mobileProvider.providerId={0} is not valid.", number.getMobileProvider().getProviderId())); //NOI18N
444 } else if (number.getMobileNumber() == null) {
446 throw new NullPointerException("number.phoneNumber is null"); //NOI18N
447 } else if (number.getMobileNumber() < 1) {
448 // Throw it again ...
449 throw new NullPointerException(MessageFormat.format("number.phoneNumber={0} is not valid.", number.getMobileNumber())); //NOI18N
450 } else if (this.getContact() == null) {
451 // ... and throw again
452 throw new NullPointerException("this.contact is null"); //NOI18N
453 } else if (this.getContact().getContactId() == null) {
455 throw new NullPointerException("this.contact.contactId is null"); //NOI18N
456 } else if (this.getContact().getContactId() < 1) {
458 throw new IllegalArgumentException(MessageFormat.format("this.contact.contactId={0} is invalid.", this.getContact().getContactId())); //NOI18N
461 // Init contact instance
462 final Contact updatedContact;
465 // Unlink it and return contact without mobile instance
466 updatedContact = this.adminContactMobileBean.unlinkMobileDataFromContact(this.getContact(), number);
467 } catch (final MobileNumberNotLinkedException | ContactNotFoundException ex) {
469 this.showFacesMessage("form_unlink_contact_mobile:mobileNumberId", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
474 this.mobileNumberUnlinkedEvent.fire(new AdminUnlinkedMobileNumberEvent(updatedContact, number));
477 return "admin_show_contact"; //NOI18N
483 private void clear () {
488 * Returns an instance of a DialableMobileNumber from all fields stored in
491 * @return An instance of a DialableMobileNumber class
493 private DialableMobileNumber createMobileNumber () {
495 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
497 // Add all other data
498 number.setMobileEntryCreated(this.getMobileEntryCreated());
499 number.setMobileEntryUpdated(this.getMobileEntryUpdated());
502 if (this.getMobileId() instanceof Long) {
504 number.setMobileId(this.getMobileId());
512 * Getter for mobile entry created
514 * @return Mobile entry created
516 @SuppressWarnings ("ReturnOfDateField")
517 private Date getMobileEntryCreated () {
518 return this.mobileEntryCreated;
522 * Setter for mobile entry created
524 * @param mobileEntryCreated Mobile entry created
526 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
527 private void setMobileEntryCreated (final Date mobileEntryCreated) {
528 this.mobileEntryCreated = mobileEntryCreated;
532 * Getter for mobile entry updated
534 * @return Mobile entry updated
536 @SuppressWarnings ("ReturnOfDateField")
537 private Date getMobileEntryUpdated () {
538 return this.mobileEntryUpdated;
542 * Setter for mobile entry updated
544 * @param mobileEntryUpdated Mobile entry updated
546 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
547 private void setMobileEntryUpdated (final Date mobileEntryUpdated) {
548 this.mobileEntryUpdated = mobileEntryUpdated;