]> git.mxchange.org Git - jfinancials-war.git/blob
08fc1511046bcb764931a141edb4b405afe3690f
[jfinancials-war.git] /
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jfinancials.beans.contact.mobile;
18
19 import java.text.MessageFormat;
20 import java.util.Date;
21 import javax.ejb.EJB;
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.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
30 import org.mxchange.jcontacts.events.contact.created.ObservableCreatedContactEvent;
31 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
32 import org.mxchange.jcontacts.events.contact.update.ObservableUpdatedContactEvent;
33 import org.mxchange.jcontacts.events.mobile.linked.AdminLinkedMobileNumberEvent;
34 import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent;
35 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
36 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
37 import org.mxchange.jcontacts.exceptions.ContactNotFoundException;
38 import org.mxchange.jcontacts.model.contact.Contact;
39 import org.mxchange.jcontacts.model.mobile.AdminContactsMobileSessionBeanRemote;
40 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
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;
48
49 /**
50  * An administrative contact mobile controller (bean)
51  * <p>
52  * @author Roland Häder<roland@mxchange.org>
53  */
54 @Named ("adminContactMobileController")
55 @RequestScoped
56 public class FinancialsAdminContactMobileWebRequestBean extends BaseFinancialsBean implements FinancialsAdminContactMobileWebRequestController {
57
58         /**
59          * Serial number
60          */
61         private static final long serialVersionUID = 542_145_347_919L;
62
63         /**
64          * Administrative EJB for phone number
65          */
66         @EJB (lookup = "java:global/jfinancials-ejb/adminContactMobile!org.mxchange.jcontacts.model.mobile.AdminContactsMobileSessionBeanRemote")
67         private AdminContactsMobileSessionBeanRemote adminContactMobileBean;
68
69         /**
70          * Event being fired when a mobile number has been linked
71          */
72         @Inject
73         @Any
74         private Event<ObservableAdminLinkedMobileNumberEvent> adminLinkedMobileNumberEvent;
75
76         /**
77          * Contact instance
78          */
79         private Contact contact;
80
81         /**
82          * When mobile number has been created
83          */
84         private Date mobileEntryCreated;
85
86         /**
87          * When mobile number has been updated
88          */
89         private Date mobileEntryUpdated;
90
91         /**
92          * Phone id (primary key)
93          */
94         private Long mobileId;
95
96         /**
97          * Mobile number
98          */
99         private Long mobileNumber;
100
101         /**
102          * Event being fired when administrator unlinks mobile from contact
103          */
104         @Inject
105         @Any
106         private Event<ObservableAdminUnlinkedMobileNumberEvent> mobileNumberUnlinkedEvent;
107
108         /**
109          * Mobile provider
110          */
111         private MobileProvider mobileProvider;
112
113         /**
114          * Default constructor
115          */
116         public FinancialsAdminContactMobileWebRequestBean () {
117                 // Call super constructor
118                 super();
119
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));
122         }
123
124         /**
125          * Observes events being fired when an administrator has added a new
126          * contact.
127          * <p>
128          * @param event Event being fired
129          */
130         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
131                 // The event must be valid
132                 if (null == event) {
133                         // Throw NPE
134                         throw new NullPointerException("event is null"); //NOI18N
135                 } else if (event.getAddedContact() == null) {
136                         // Throw again ...
137                         throw new NullPointerException("event.addedContact is null"); //NOI18N
138                 } else if (event.getAddedContact().getContactId() == null) {
139                         // ... and again
140                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
141                 } else if (event.getAddedContact().getContactId() < 1) {
142                         // Not valid
143                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
144                 }
145
146                 // Clear this bean
147                 this.clear();
148         }
149
150         /**
151          * Event observer for newly added users by administrator
152          * <p>
153          * @param event Event being fired
154          */
155         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
156                 // Event and contained entity instance should not be null
157                 if (null == event) {
158                         // Throw NPE
159                         throw new NullPointerException("event is null"); //NOI18N
160                 } else if (event.getAddedUser() == null) {
161                         // Throw NPE again
162                         throw new NullPointerException("event.addedUser is null"); //NOI18N
163                 } else if (event.getAddedUser().getUserId() == null) {
164                         // userId is null
165                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
166                 } else if (event.getAddedUser().getUserId() < 1) {
167                         // Not avalid id
168                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
169                 }
170
171                 // Clear all data
172                 this.clear();
173         }
174
175         /**
176          * Event observer for updated contact data by administrators
177          * <p>
178          * @param event Updated contact data event
179          */
180         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
181                 // Event and contained entity instance should not be null
182                 if (null == event) {
183                         // Throw NPE
184                         throw new NullPointerException("event is null"); //NOI18N
185                 } else if (event.getUpdatedContact() == null) {
186                         // Throw NPE again
187                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
188                 } else if (event.getUpdatedContact().getContactId() == null) {
189                         // userId is null
190                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
191                 } else if (event.getUpdatedContact().getContactId() < 1) {
192                         // Not avalid id
193                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
194                 }
195
196                 // Clear all data
197                 this.clear();
198         }
199
200         /**
201          * Observer for events being fired when a bean helper has successfully
202          * created a contact instance.
203          * <p>
204          * @param event Event being fired
205          */
206         public void afterCreatedContactEvent (@Observes final ObservableCreatedContactEvent event) {
207                 // Log message
208                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AdminContactController::afterCreatedContactEvent(): contact={0} - CALLED!", contact)); //NOI18N
209
210                 // The event instance must be valid
211                 if (null == event) {
212                         // Throw NPE again
213                         throw new NullPointerException("event is null"); //NOI18N
214                 } else if (event.getCreatedContact() == null) {
215                         // Throw NPE again
216                         throw new NullPointerException("event.createdContact is null"); //NOI18N
217                 } else if (event.getCreatedContact().getContactId() == null) {
218                         // Throw NPE again
219                         throw new NullPointerException("event.createdContact.contactId is null"); //NOI18N
220                 } else if (event.getCreatedContact().getContactId() < 1) {
221                         // Not valid
222                         throw new IllegalStateException(MessageFormat.format("event.createdContact.contactId={0} is not valid.", event.getCreatedContact().getContactId())); //NOI18N
223                 }
224
225                 // Set it here
226                 this.setContact(event.getCreatedContact());
227         }
228
229         /**
230          * Observes events being fired when a bean helper has successfully created a
231          * mobile number instance.
232          * <p>
233          * @param event Event being fired
234          */
235         public void afterCreatedMobileNumberEvent (@Observes final ObservableCreatedMobileNumberEvent event) {
236                 // The event instance must be valid
237                 if (null == event) {
238                         // Throw NPE
239                         throw new NullPointerException("event is null"); //NOI18N
240                 } else if (event.getMobileNumber() == null) {
241                         // Throw NPE again
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
249                 }
250
251                 // Get fax number from event
252                 final DialableMobileNumber number = event.getMobileNumber();
253
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());
260         }
261
262         /**
263          * Event observer for updated contact data by the user
264          * <p>
265          * @param event Updated contact data event
266          */
267         public void afterUpdatedContactDataEvent (@Observes final ObservableUpdatedContactEvent event) {
268                 // Event and contained entity instance should not be null
269                 if (null == event) {
270                         // Throw NPE
271                         throw new NullPointerException("event is null"); //NOI18N
272                 } else if (event.getUpdatedContact() == null) {
273                         // Throw NPE again
274                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
275                 } else if (event.getUpdatedContact().getContactId() == null) {
276                         // userId is null
277                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
278                 } else if (event.getUpdatedContact().getContactId() < 1) {
279                         // Not avalid id
280                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
281                 }
282
283                 // Clear all data
284                 this.clear();
285         }
286
287         /**
288          * Links mobile number to contact from bean helper as "main mobile number".
289          * <p>
290          * @return Redirect outcome
291          */
292         public String doLinkMainMobileNumber () {
293                 // Get contact from helper
294                 final Contact targetContact = this.getContact();
295
296                 // Is all data properly set?
297                 if (null == targetContact) {
298                         // Throw NPE
299                         throw new NullPointerException("targetContact is null"); //NOI18N
300                 } else if (targetContact.getContactId() == null) {
301                         // Throw it again
302                         throw new NullPointerException("targetContact.contactId is null"); //NOI18N
303                 } else if (targetContact.getContactId() < 1) {
304                         // Is not valid
305                         throw new IllegalArgumentException(MessageFormat.format("targetContact.contactId={0} is not valid", targetContact.getContactId())); //NOI18N
306                 } else if (this.getMobileProvider() == null) {
307                         // Throw NPE
308                         throw new NullPointerException("this.mobileProvider is null"); //NOI18N
309                 } else if (this.getMobileProvider().getProviderId() == null) {
310                         // Throw NPE
311                         throw new NullPointerException("this.mobileProvider.providerId is null"); //NOI18N
312                 } else if (this.getMobileProvider().getProviderId() < 1) {
313                         // Throw NPE
314                         throw new NullPointerException(MessageFormat.format("this.mobileProvider.providerId={0} is invalid", this.getMobileProvider().getProviderId())); //NOI18N
315                 } else if (this.getMobileNumber() == null) {
316                         // Throw NPE again
317                         throw new NullPointerException("this.mobileNumber is null"); //NOI18N
318                 } else if (this.getMobileNumber() < 1) {
319                         // Invalid id number
320                         throw new IllegalArgumentException(MessageFormat.format("this.mobileNumber={0} is invalid", this.getMobileNumber())); //NOI18N
321                 }
322
323                 // Init instance
324                 final Contact updatedContact;
325                 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
326
327                 // Try it again
328                 try {
329                         // Link it, too
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
334                         return ""; //NOI18N
335                 }
336
337                 // Fire event
338                 this.adminLinkedMobileNumberEvent.fire(new AdminLinkedMobileNumberEvent(updatedContact, number));
339
340                 // Return to contact profile
341                 return "admin_show_contact"; //NOI18N
342         }
343
344         /**
345          * Getter for contact instance
346          * <p>
347          * @return Contact instance
348          */
349         public Contact getContact () {
350                 return this.contact;
351         }
352
353         /**
354          * Setter for contact instance
355          * <p>
356          * @param contact Contact instance
357          */
358         public void setContact (final Contact contact) {
359                 this.contact = contact;
360         }
361
362         /**
363          * Setter for phone id
364          * <p>
365          * @return Phone id
366          */
367         public Long getMobileId () {
368                 return this.mobileId;
369         }
370
371         /**
372          * Getter for phone id
373          * <p>
374          * @param mobileId Phone id
375          */
376         public void setMobileId (final Long mobileId) {
377                 this.mobileId = mobileId;
378         }
379
380         /**
381          * Getter for mobile number
382          * <p>
383          * @return Mobile number
384          */
385         public Long getMobileNumber () {
386                 return this.mobileNumber;
387         }
388
389         /**
390          * Setter for mobile number
391          * <p>
392          * @param mobileNumber Mobile number
393          */
394         public void setMobileNumber (final Long mobileNumber) {
395                 this.mobileNumber = mobileNumber;
396         }
397
398         /**
399          * Getter for mobile provider
400          * <p>
401          * @return Mobile provider
402          */
403         public MobileProvider getMobileProvider () {
404                 return this.mobileProvider;
405         }
406
407         /**
408          * Setter for mobile provider
409          * <p>
410          * @param mobileProvider Mobile provider
411          */
412         public void setMobileProvider (final MobileProvider mobileProvider) {
413                 this.mobileProvider = mobileProvider;
414         }
415
416         /**
417          * Unlinks mobile data with current contact
418          * <p>
419          * @return Redirect outcome
420          */
421         public String unlinkMobileContactData () {
422                 // Create fax number instance
423                 final DialableMobileNumber number = this.createMobileNumber();
424
425                 // Is all data set
426                 if (number == null) {
427                         // Not set, throw NPE
428                         throw new NullPointerException("number is null"); //NOI18N
429                 } else if (number.getMobileId() == null) {
430                         // Throw NPE again
431                         throw new NullPointerException("number.phoneId is null"); //NOI18N
432                 } else if (number.getMobileId() < 1) {
433                         // Invalid number
434                         throw new IllegalArgumentException(MessageFormat.format("number.phoneId={0} is not valid", number.getMobileId())); //NOI18N
435                 } else if (number.getMobileProvider() == null) {
436                         // Throw NPE
437                         throw new NullPointerException("number.mobileProvider is null"); //NOI18N
438                 } else if (number.getMobileProvider().getProviderId() == null) {
439                         // ... throw again
440                         throw new NullPointerException("number.mobileProvider.providerId is null"); //NOI18N
441                 } else if (number.getMobileProvider().getProviderId() < 1) {
442                         // Id not valid
443                         throw new IllegalArgumentException(MessageFormat.format("number.mobileProvider.providerId={0} is not valid.", number.getMobileProvider().getProviderId())); //NOI18N
444                 } else if (number.getMobileNumber() == null) {
445                         // Throw NPE again
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) {
454                         // ... and again ...
455                         throw new NullPointerException("this.contact.contactId is null"); //NOI18N
456                 } else if (this.getContact().getContactId() < 1) {
457                         // Invalid id number
458                         throw new IllegalArgumentException(MessageFormat.format("this.contact.contactId={0} is invalid.", this.getContact().getContactId())); //NOI18N
459                 }
460
461                 // Init contact instance
462                 final Contact updatedContact;
463
464                 try {
465                         // Unlink it and return contact without mobile instance
466                         updatedContact = this.adminContactMobileBean.unlinkMobileDataFromContact(this.getContact(), number);
467                 } catch (final MobileNumberNotLinkedException | ContactNotFoundException ex) {
468                         // Did not work
469                         this.showFacesMessage("form_unlink_contact_mobile:mobileNumberId", ex, FacesMessage.SEVERITY_ERROR); //NOI18N
470                         return ""; //NOI18N
471                 }
472
473                 // Fire event
474                 this.mobileNumberUnlinkedEvent.fire(new AdminUnlinkedMobileNumberEvent(updatedContact, number));
475
476                 // All fine here
477                 return "admin_show_contact"; //NOI18N
478         }
479
480         /**
481          * Clears this bean
482          */
483         private void clear () {
484                 // Clear all data
485         }
486
487         /**
488          * Returns an instance of a DialableMobileNumber from all fields stored in
489          * this bean.
490          * <p>
491          * @return An instance of a DialableMobileNumber class
492          */
493         private DialableMobileNumber createMobileNumber () {
494                 // Initialize it
495                 final DialableMobileNumber number = new MobileNumber(this.getMobileProvider(), this.getMobileNumber());
496
497                 // Add all other data
498                 number.setMobileEntryCreated(this.getMobileEntryCreated());
499                 number.setMobileEntryUpdated(this.getMobileEntryUpdated());
500
501                 // Is id number set?
502                 if (this.getMobileId() instanceof Long) {
503                         // Set it
504                         number.setMobileId(this.getMobileId());
505                 }
506
507                 // Return it
508                 return number;
509         }
510
511         /**
512          * Getter for mobile entry created
513          * <p>
514          * @return Mobile entry created
515          */
516         @SuppressWarnings ("ReturnOfDateField")
517         private Date getMobileEntryCreated () {
518                 return this.mobileEntryCreated;
519         }
520
521         /**
522          * Setter for mobile entry created
523          * <p>
524          * @param mobileEntryCreated Mobile entry created
525          */
526         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
527         private void setMobileEntryCreated (final Date mobileEntryCreated) {
528                 this.mobileEntryCreated = mobileEntryCreated;
529         }
530
531         /**
532          * Getter for mobile entry updated
533          * <p>
534          * @return Mobile entry updated
535          */
536         @SuppressWarnings ("ReturnOfDateField")
537         private Date getMobileEntryUpdated () {
538                 return this.mobileEntryUpdated;
539         }
540
541         /**
542          * Setter for mobile entry updated
543          * <p>
544          * @param mobileEntryUpdated Mobile entry updated
545          */
546         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
547         private void setMobileEntryUpdated (final Date mobileEntryUpdated) {
548                 this.mobileEntryUpdated = mobileEntryUpdated;
549         }
550
551 }