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