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.jjobs.beans.mobile;
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import java.util.List;
22 import java.util.Objects;
24 import javax.enterprise.context.RequestScoped;
25 import javax.enterprise.event.Event;
26 import javax.enterprise.event.Observes;
27 import javax.enterprise.inject.Any;
28 import javax.faces.application.FacesMessage;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jjobs.beans.BaseJobsBean;
32 import org.mxchange.jjobs.beans.mobile.list.JobsMobileListWebViewController;
33 import org.mxchange.jphone.events.mobile.created.ObservableCreatedMobileNumberEvent;
34 import org.mxchange.jphone.events.mobile.deleted.AdminMobileNumberDeletedEvent;
35 import org.mxchange.jphone.events.mobile.deleted.ObservableAdminMobileNumberDeletedEvent;
36 import org.mxchange.jphone.events.mobile.remove.AdminMobileNumberRemovedFromListEvent;
37 import org.mxchange.jphone.events.mobile.remove.ObservableAdminMobileNumberRemovedFromListEvent;
38 import org.mxchange.jphone.events.mobile.updated.AdminMobileNumberUpdatedEvent;
39 import org.mxchange.jphone.events.mobile.updated.ObservableAdminMobileNumberUpdatedEvent;
40 import org.mxchange.jphone.model.phonenumbers.mobile.AdminMobileSessionBeanRemote;
41 import org.mxchange.jphone.model.phonenumbers.mobile.DialableMobileNumber;
42 import org.mxchange.jphone.model.phonenumbers.mobile.MobileNumber;
43 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
46 * Administrative bean (controller) for mobile numbers
48 * @author Roland Häder<roland@mxchange.org>
50 @Named ("adminMobileController")
52 public class JobsAdminMobileWebRequestBean extends BaseJobsBean implements JobsAdminMobileWebRequestController {
57 private static final long serialVersionUID = 184_598_175_371_269_017L;
60 * Remote EJB for phone number (administrative)
62 @EJB (lookup = "java:global/jjobs-ejb/adminMobile!org.mxchange.jphone.model.phonenumbers.mobile.AdminMobileSessionBeanRemote")
63 private AdminMobileSessionBeanRemote adminMobileBean;
66 * When the phone entry has been created (persisted)
68 private Date mobileEntryCreated;
71 * When the phone entry has been updated
73 private Date mobileEntryUpdated;
76 * Mobile id (aka primary key)
78 private Long mobileId;
81 * Generic hone controller
84 private JobsMobileListWebViewController mobileListController;
89 private Long mobileNumber;
92 * Event being fired when an administrator has deleted mobile number
96 private Event<ObservableAdminMobileNumberDeletedEvent> mobileNumberDeletedEvent;
99 * Event being fired when an administrator has updated land-line number
103 private Event<ObservableAdminMobileNumberUpdatedEvent> mobileNumberUpdatedEvent;
108 private MobileProvider mobileProvider;
111 * Event being fired when a list of all unused mobile numbers is being
116 private Event<ObservableAdminMobileNumberRemovedFromListEvent> removeLinkedMobileNumbersEvent;
119 * Chosen mobile number
121 private DialableMobileNumber selectedMobileNumber;
124 * Default constructor
126 public JobsAdminMobileWebRequestBean () {
127 // Call super constructor
132 * Observes events being fired when a bean helper has successfully created a
133 * mobile number instance.
135 * @param event Event being fired
137 public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
138 // The event instance must be valid
141 throw new NullPointerException("event is null"); //NOI18N
142 } else if (event.getMobileNumber() == null) {
144 throw new NullPointerException("event.mobileNumber is null"); //NOI18N
145 } else if (event.getMobileNumber().getMobileId() == null) {
146 // Throw NPE yet again
147 throw new NullPointerException("event.mobileNumber.mobileId is null"); //NOI18N
148 } else if (event.getMobileNumber().getMobileId() < 1) {
149 // Throw NPE yet again
150 throw new NullPointerException(MessageFormat.format("event.mobileNumber.mobileId={0} is invalid", event.getMobileNumber().getMobileId())); //NOI18N
153 // Get fax number from event
154 final DialableMobileNumber number = event.getMobileNumber();
156 // Copy all data to this bean
157 this.setMobileId(number.getMobileId());
158 this.setMobileProvider(number.getMobileProvider());
159 this.setMobileNumber(number.getMobileNumber());
160 this.setMobileEntryCreated(number.getMobileEntryCreated());
161 this.setMobileEntryUpdated(number.getMobileEntryUpdated());
165 * Returns a list of all unused ("non-linked") mobile numbers
167 * @return List with all unused mobile numbers
169 public List<DialableMobileNumber> allNonLinkedMobileNumbers () {
170 // Get list of all mobile numbers
171 final List<DialableMobileNumber> numbers = this.mobileListController.getAllMobileNumbers();
173 // Visit all controllers to reduce the list
174 this.removeLinkedMobileNumbersEvent.fire(new AdminMobileNumberRemovedFromListEvent(numbers));
181 * Deletes given mobile entry data
183 public void deleteMobileData () {
184 // Get mobile number from helper
185 final DialableMobileNumber number = this.createMobileNumber();
188 if (number == null) {
189 // Not set, throw NPE
190 throw new NullPointerException("mobileNumber is null"); //NOI18N
191 } else if (number.getMobileId() == null) {
193 throw new NullPointerException("mobileNumber.mobileId is null"); //NOI18N
194 } else if (number.getMobileId() < 1) {
196 throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileId={0} is not valid", number.getMobileId())); //NOI18N
197 } else if (number.getMobileProvider() == null) {
199 throw new NullPointerException("mobileNumber.mobileProvider is null"); //NOI18N
200 } else if (number.getMobileProvider().getProviderId() == null) {
202 throw new NullPointerException("mobileNumber.mobileProvider.providerId is null"); //NOI18N
203 } else if (number.getMobileProvider().getProviderId() < 1) {
205 throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileProvider.providerId={0} is not valid.", number.getMobileProvider().getProviderId())); //NOI18N
206 } else if (number.getMobileNumber() == null) {
208 throw new NullPointerException("mobileNumber.mobileNumber is null"); //NOI18N
209 } else if (number.getMobileNumber() < 1) {
211 throw new NullPointerException(MessageFormat.format("mobileNumber.mobileNumber={0} is not valid.", number.getMobileNumber())); //NOI18N
215 this.adminMobileBean.deleteMobileData(number);
218 this.mobileNumberDeletedEvent.fire(new AdminMobileNumberDeletedEvent(number));
222 * Changes mobile entry data
224 * @return Redirect outcome
226 public String doUpdateMobileNumber () {
227 // Get mobile number from helper
228 final DialableMobileNumber number = this.createMobileNumber();
231 if (number == null) {
232 // Not set, throw NPE
233 throw new NullPointerException("mobileNumber is null"); //NOI18N
234 } else if (number.getMobileId() == null) {
236 throw new NullPointerException("mobileNumber.mobileId is null"); //NOI18N
237 } else if (number.getMobileId() < 1) {
239 throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileId={0} is not valid", number.getMobileId())); //NOI18N
240 } else if (number.getMobileProvider() == null) {
242 throw new NullPointerException("mobileNumber.mobileProvider is null"); //NOI18N
243 } else if (number.getMobileProvider().getProviderId() == null) {
245 throw new NullPointerException("mobileNumber.mobileProvider.providerId is null"); //NOI18N
246 } else if (number.getMobileProvider().getProviderId() < 1) {
248 throw new IllegalArgumentException(MessageFormat.format("mobileNumber.mobileProvider.providerId={0} is not valid.", number.getMobileProvider().getProviderId())); //NOI18N
249 } else if (number.getMobileNumber() == null) {
251 throw new NullPointerException("mobileNumber.mobileNumber is null"); //NOI18N
252 } else if (number.getMobileNumber() < 1) {
254 throw new NullPointerException(MessageFormat.format("mobileNumber.mobileNumber={0} is not valid.", number.getMobileNumber())); //NOI18N
255 } else if (this.getMobileProvider() == null) {
257 throw new NullPointerException("this.mobileProvider is null"); //NOI18N
258 } else if (this.getMobileProvider().getProviderId() == null) {
259 // Throw NPE again ...
260 throw new NullPointerException("this.mobileProvider.providerId is null"); //NOI18N
261 } else if (this.getMobileProvider().getProviderId() < 0) {
263 throw new IllegalArgumentException(MessageFormat.format("this.mobileProvider.providerId={0} is not valid.", this.getMobileProvider().getProviderId())); //NOI18N
264 } else if (this.getMobileNumber() == null) {
266 throw new NullPointerException("this.phoneNumber is null"); //NOI18N
269 // Is the mobile provider and number the same?
270 if ((Objects.equals(this.getMobileProvider(), number.getMobileProvider())) && (Objects.equals(this.getMobileNumber(), number.getMobileNumber()))) {
272 this.showFacesMessage("form_edit_mobile:mobileNumber", "ERROR_ADMIN_NO_CHANGE_ENTERED", FacesMessage.SEVERITY_WARN); //NOI18N
274 // No difference in both together, no need to edit
279 number.setMobileProvider(this.getMobileProvider());
280 number.setMobileNumber(this.getMobileNumber());
283 final DialableMobileNumber updatedNumber = this.adminMobileBean.updateMobileData(number);
286 this.mobileNumberUpdatedEvent.fire(new AdminMobileNumberUpdatedEvent(updatedNumber));
288 // All fine, redirect
289 return "admin_show_mobile"; //NOI18N
293 * Getter for phone entry created timestamp
295 * @return Mobile entry created timestamp
297 @SuppressWarnings ("ReturnOfDateField")
298 public Date getMobileEntryCreated () {
299 return this.mobileEntryCreated;
303 * Setter for phone entry created timestamp
305 * @param mobileEntryCreated Mobile entry created timestamp
307 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
308 public void setMobileEntryCreated (final Date mobileEntryCreated) {
309 this.mobileEntryCreated = mobileEntryCreated;
313 * Getter for phone entry updated timestamp
315 * @return Mobile entry updated timestamp
317 @SuppressWarnings ("ReturnOfDateField")
318 public Date getMobileEntryUpdated () {
319 return this.mobileEntryUpdated;
323 * Setter for phone entry updated timestamp
325 * @param mobileEntryUpdated Mobile entry updated timestamp
327 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
328 public void setMobileEntryUpdated (final Date mobileEntryUpdated) {
329 this.mobileEntryUpdated = mobileEntryUpdated;
333 * Getter for phone id
337 public Long getMobileId () {
338 return this.mobileId;
342 * Setter for phone id
344 * @param mobileId Mobile id
346 public void setMobileId (final Long mobileId) {
347 this.mobileId = mobileId;
351 * Getter for dial number without prefix
353 * @return Dial number without prefix
355 public Long getMobileNumber () {
356 return this.mobileNumber;
360 * Setter for dial number without prefix
362 * @param mobileNumber Dial number without prefix
364 public void setMobileNumber (final Long mobileNumber) {
365 this.mobileNumber = mobileNumber;
369 * Getter for mobile provider
371 * @return Mobile provider
373 public MobileProvider getMobileProvider () {
374 return this.mobileProvider;
378 * Setter for mobile provider
380 * @param mobileProvider Mobile provider
382 public void setMobileProvider (final MobileProvider mobileProvider) {
383 this.mobileProvider = mobileProvider;
387 * Getter for chosen mobile number
389 * @return mobile number
391 public DialableMobileNumber getSelectedMobileNumber () {
392 return this.selectedMobileNumber;
396 * Setter for chosen mobile number
398 * @param selectedMobileNumber mobile number
400 public void setSelectedMobileNumber (final DialableMobileNumber selectedMobileNumber) {
401 this.selectedMobileNumber = selectedMobileNumber;
405 * Returns an instance of a DialableMobileNumber from all fields stored in
408 * @return An instance of a DialableMobileNumber class
410 private DialableMobileNumber createMobileNumber () {
412 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
414 // Add all other data
415 number.setMobileEntryCreated(this.getMobileEntryCreated());
416 number.setMobileEntryUpdated(this.getMobileEntryUpdated());
417 number.setMobileId(this.getMobileId());