]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/contact/phone/JobsAdminContactPhoneWebRequestBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / contact / phone / JobsAdminContactPhoneWebRequestBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.jjobs.beans.contact.phone;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import java.util.MissingResourceException;
22 import java.util.ResourceBundle;
23 import javax.annotation.PostConstruct;
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.faces.context.FacesContext;
30 import javax.faces.view.facelets.FaceletException;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import javax.naming.Context;
34 import javax.naming.InitialContext;
35 import javax.naming.NamingException;
36 import org.mxchange.jcontacts.contact.Contact;
37 import org.mxchange.jcontacts.events.contact.add.ObservableAdminAddedContactEvent;
38 import org.mxchange.jcontacts.events.contact.update.ObservableAdminUpdatedContactEvent;
39 import org.mxchange.jcontacts.events.fax.linked.AdminLinkedFaxNumberEvent;
40 import org.mxchange.jcontacts.events.fax.linked.ObservableAdminLinkedFaxNumberEvent;
41 import org.mxchange.jcontacts.events.fax.unlinked.AdminUnlinkedFaxNumberEvent;
42 import org.mxchange.jcontacts.events.fax.unlinked.ObservableAdminUnlinkedFaxNumberEvent;
43 import org.mxchange.jcontacts.events.landline.linked.AdminLinkedLandLineNumberEvent;
44 import org.mxchange.jcontacts.events.landline.linked.ObservableAdminLinkedLandLineNumberEvent;
45 import org.mxchange.jcontacts.events.landline.unlinked.AdminUnlinkedLandLineNumberEvent;
46 import org.mxchange.jcontacts.events.landline.unlinked.ObservableAdminUnlinkedLandLineNumberEvent;
47 import org.mxchange.jcontacts.events.mobile.linked.AdminLinkedMobileNumberEvent;
48 import org.mxchange.jcontacts.events.mobile.linked.ObservableAdminLinkedMobileNumberEvent;
49 import org.mxchange.jcontacts.events.mobile.unlinked.AdminUnlinkedMobileNumberEvent;
50 import org.mxchange.jcontacts.events.mobile.unlinked.ObservableAdminUnlinkedMobileNumberEvent;
51 import org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote;
52 import org.mxchange.jcountry.data.Country;
53 import org.mxchange.jjobs.beans.BaseJobsController;
54 import org.mxchange.jjobs.beans.helper.JobsWebRequestHelperController;
55 import org.mxchange.jjobs.beans.phone.JobsAdminPhoneWebRequestController;
56 import org.mxchange.jphone.exceptions.PhoneNumberAlreadyLinkedException;
57 import org.mxchange.jphone.exceptions.PhoneNumberNotLinkedException;
58 import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
59 import org.mxchange.jphone.phonenumbers.fax.FaxNumber;
60 import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
61 import org.mxchange.jphone.phonenumbers.landline.LandLineNumber;
62 import org.mxchange.jphone.phonenumbers.mobile.DialableMobileNumber;
63 import org.mxchange.jphone.phonenumbers.mobile.MobileNumber;
64 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
65 import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent;
66
67 /**
68  * An administrative contact phone controller (bean)
69  * <p>
70  * @author Roland Häder<roland@mxchange.org>
71  */
72 @Named ("adminContactPhoneController")
73 @RequestScoped
74 public abstract class JobsAdminContactPhoneWebRequestBean extends BaseJobsController implements JobsAdminContactPhoneWebRequestController {
75
76         /**
77          * Call-stack instance (5 may show BeanELResolver.getValue as caller)
78          */
79         private static final short THREAD_STACK = 5;
80
81         /**
82          * Serial number
83          */
84         private static final long serialVersionUID = 542_145_347_916L;
85
86         /**
87          * Event being fired when a fax number has been linked
88          */
89         @Inject
90         @Any
91         private Event<ObservableAdminLinkedFaxNumberEvent> adminLinkedFaxNumberEvent;
92
93         /**
94          * Event being fired when a land-line number has been linked
95          */
96         @Inject
97         @Any
98         private Event<ObservableAdminLinkedLandLineNumberEvent> adminLinkedLandLineNumberEvent;
99
100         /**
101          * Event being fired when a mobile number has been linked
102          */
103         @Inject
104         @Any
105         private Event<ObservableAdminLinkedMobileNumberEvent> adminLinkedMobileNumberEvent;
106
107         /**
108          * Administrative EJB for phone number
109          */
110         private AdminContactsPhoneSessionBeanRemote adminPhoneBean;
111
112         /**
113          * Administrative phone controller
114          */
115         @Inject
116         private JobsAdminPhoneWebRequestController adminPhoneController;
117
118         /**
119          * Bean helper
120          */
121         @Inject
122         private JobsWebRequestHelperController beanHelper;
123
124         /**
125          * Event being fired when a fax number has been unlinked
126          */
127         @Inject
128         @Any
129         private Event<ObservableAdminUnlinkedFaxNumberEvent> faxNumberUnlinkedEvent;
130
131         /**
132          * Event being fired when a land-line number has been unlinked
133          */
134         @Inject
135         @Any
136         private Event<ObservableAdminUnlinkedLandLineNumberEvent> landLineNumberUnlinkedEvent;
137
138         /**
139          * Event being fired when admin unlinks mobile from contact
140          */
141         @Inject
142         @Any
143         private Event<ObservableAdminUnlinkedMobileNumberEvent> mobileNumberUnlinkedEvent;
144
145         /**
146          * Default constructor
147          */
148         public JobsAdminContactPhoneWebRequestBean () {
149                 // Call super constructor
150                 super();
151
152                 // String caller = MessageFormat.format("{0}.{1}", Thread.currentThread().getStackTrace()[3].getClassName(), Thread.currentThread().getStackTrace()[3].getMethodName());
153                 // System.out.println(MessageFormat.format("{0}: Constructed, caller: {1}", this.getClass().getSimpleName(), caller));
154         }
155
156         /**
157          * Observes events being fired when an administrator has added a new
158          * contact.
159          * <p>
160          * @param event Event being fired
161          */
162         public void afterAdminAddedContactEvent (@Observes final ObservableAdminAddedContactEvent event) {
163                 // The event must be valid
164                 if (null == event) {
165                         // Throw NPE
166                         throw new NullPointerException("event is null"); //NOI18N
167                 } else if (event.getAddedContact() == null) {
168                         // Throw again ...
169                         throw new NullPointerException("event.addedContact is null"); //NOI18N
170                 } else if (event.getAddedContact().getContactId() == null) {
171                         // ... and again
172                         throw new NullPointerException("event.addedContact.contactId is null"); //NOI18N
173                 } else if (event.getAddedContact().getContactId() < 1) {
174                         // Not valid
175                         throw new IllegalArgumentException(MessageFormat.format("event.addedContact.contactId={0} is not valid", event.getAddedContact().getContactId())); //NOI18N
176                 }
177
178                 // Clear this bean
179                 this.clear();
180         }
181
182         /**
183          * Event observer for newly added users by administrator
184          * <p>
185          * @param event Event being fired
186          */
187         public void afterAdminAddedUserEvent (@Observes final ObservableAdminAddedUserEvent event) {
188                 // event should not be null
189                 if (null == event) {
190                         // Throw NPE
191                         throw new NullPointerException("event is null"); //NOI18N
192                 } else if (event.getAddedUser() == null) {
193                         // Throw NPE again
194                         throw new NullPointerException("event.addedUser is null"); //NOI18N
195                 } else if (event.getAddedUser().getUserId() == null) {
196                         // userId is null
197                         throw new NullPointerException("event.addedUser.userId is null"); //NOI18N
198                 } else if (event.getAddedUser().getUserId() < 1) {
199                         // Not avalid id
200                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getAddedUser(), event.getAddedUser().getUserId())); //NOI18N
201                 }
202
203                 // Clear all data
204                 this.clear();
205         }
206
207         /**
208          * Event observer for updated contact data by administrators
209          * <p>
210          * @param event Updated contact data event
211          */
212         public void afterAdminUpdatedContactDataEvent (@Observes final ObservableAdminUpdatedContactEvent event) {
213                 // event should not be null
214                 if (null == event) {
215                         // Throw NPE
216                         throw new NullPointerException("event is null"); //NOI18N
217                 } else if (event.getUpdatedContact() == null) {
218                         // Throw NPE again
219                         throw new NullPointerException("event.updatedContact is null"); //NOI18N
220                 } else if (event.getUpdatedContact().getContactId() == null) {
221                         // userId is null
222                         throw new NullPointerException("event.updatedContact.contactId is null"); //NOI18N
223                 } else if (event.getUpdatedContact().getContactId() < 1) {
224                         // Not avalid id
225                         throw new IllegalArgumentException(MessageFormat.format("contactId of contact={0} is not valid: {1}", event.getUpdatedContact(), event.getUpdatedContact().getContactId())); //NOI18N
226                 }
227
228                 // Clear all data
229                 this.clear();
230         }
231
232         @Override
233         public String doLinkAddFaxNumber (final Contact contact) {
234                 // Is all data properly set?
235                 if (null == contact) {
236                         // Throw NPE
237                         throw new NullPointerException("contact is null"); //NOI18N
238                 } else if (contact.getContactId() == null) {
239                         // Throw it again
240                         throw new NullPointerException("contact.contactId is null"); //NOI18N
241                 } else if (contact.getContactId() < 1) {
242                         // Is not valid
243                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
244                 } else if ((this.adminPhoneController.getFaxNumber() == null) && (this.adminPhoneController.getPhoneCountry() == null) && ((this.adminPhoneController.getPhoneAreaCode() == null) || (this.adminPhoneController.getPhoneAreaCode() < 1)) && ((this.adminPhoneController.getPhoneNumber() == null) || (this.adminPhoneController.getPhoneNumber() < 1))) {
245                         // All fields are empty
246                         throw new NullPointerException("faxNumber, mobileProvider and phoneNumer are null"); //NOI18N
247                 } else if ((this.adminPhoneController.getFaxNumber() instanceof DialableFaxNumber) && (this.adminPhoneController.getPhoneCountry() instanceof Country) && (this.adminPhoneController.getPhoneAreaCode() instanceof Integer) && (this.adminPhoneController.getPhoneNumber() instanceof Long)) {
248                         // All fields are set
249                         throw new IllegalStateException("faxNumber, mobileProvider and phoneNumer are all set"); //NOI18N
250                 } else if ((this.adminPhoneController.getPhoneCountry() == null) && ((this.adminPhoneController.getPhoneAreaCode() instanceof Integer) || (this.adminPhoneController.getPhoneNumber() instanceof Long))) {
251                         // No country selected
252                         this.showFacesMessage("form_add_contact_fax:phoneCountry", "ERROR_ADMIN_NO_PHONE_COUNTRY_SELECTED"); //NOI18N
253                         return ""; //NOI18N
254                 } else if (((this.adminPhoneController.getPhoneAreaCode() == null) || (this.adminPhoneController.getPhoneAreaCode() < 1)) && ((this.adminPhoneController.getPhoneCountry() instanceof Country) || (this.adminPhoneController.getPhoneNumber() instanceof Long))) {
255                         // No area code entered
256                         this.showFacesMessage("form_add_contact_fax:phoneAreaCode", "ERROR_ADMIN_NO_PHONE_AREA_CODE_ENTERED"); //NOI18N
257                         return ""; //NOI18N
258                 } else if (((this.adminPhoneController.getPhoneNumber() == null) || (this.adminPhoneController.getPhoneNumber() < 1)) && ((this.adminPhoneController.getPhoneCountry() instanceof Country) || (this.adminPhoneController.getPhoneAreaCode() instanceof Integer))) {
259                         // No phone number entered
260                         this.showFacesMessage("form_add_contact_fax:phoneNumber", "ERROR_ADMIN_NO_PHONE_NUMBER_ENTERED"); //NOI18N
261                         return ""; //NOI18N
262                 }
263
264                 // Init instances
265                 Contact updatedContact;
266                 DialableFaxNumber faxNumber = this.adminPhoneController.getFaxNumber();
267
268                 // Try it again
269                 try {
270                         // All fine so far, then which was choosen?
271                         if (faxNumber instanceof DialableFaxNumber) {
272                                 // User has choosen number
273                                 updatedContact = this.adminPhoneBean.linkExistingFaxNumberWithContact(contact, faxNumber);
274                         } else {
275                                 // Create new instance
276                                 faxNumber = new FaxNumber(this.adminPhoneController.getPhoneCountry(), this.adminPhoneController.getPhoneAreaCode(), this.adminPhoneController.getPhoneNumber());
277
278                                 // Link it, too
279                                 updatedContact = this.adminPhoneBean.linkNewFaxNumberWithContact(contact, faxNumber);
280                         }
281                 } catch (final PhoneNumberAlreadyLinkedException ex) {
282                         // Throw again as cause
283                         this.showFacesMessage("form_add_contact_fax:phoneNumber", ex); //NOI18N
284                         return ""; //NOI18N
285                 }
286
287                 // Fire event
288                 this.adminLinkedFaxNumberEvent.fire(new AdminLinkedFaxNumberEvent(updatedContact, faxNumber));
289
290                 // Return to contact profile
291                 return "admin_show_contact"; //NOI18N
292         }
293
294         @Override
295         public String doLinkAddLandLineNumber (final Contact contact) {
296                 // Is all data properly set?
297                 if (null == contact) {
298                         // Throw NPE
299                         throw new NullPointerException("contact is null"); //NOI18N
300                 } else if (contact.getContactId() == null) {
301                         // Throw it again
302                         throw new NullPointerException("contact.contactId is null"); //NOI18N
303                 } else if (contact.getContactId() < 1) {
304                         // Is not valid
305                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
306                 } else if ((this.adminPhoneController.getLandLineNumber() == null) && (this.adminPhoneController.getPhoneCountry() == null) && ((this.adminPhoneController.getPhoneAreaCode() == null) || (this.adminPhoneController.getPhoneAreaCode() < 1)) && ((this.adminPhoneController.getPhoneNumber() == null) || (this.adminPhoneController.getPhoneNumber() < 1))) {
307                         // All fields are empty
308                         throw new NullPointerException("landLineNumber, mobileProvider and phoneNumer are null"); //NOI18N
309                 } else if ((this.adminPhoneController.getLandLineNumber() instanceof DialableLandLineNumber) && (this.adminPhoneController.getPhoneCountry() instanceof Country) && (this.adminPhoneController.getPhoneAreaCode() instanceof Integer) && (this.adminPhoneController.getPhoneNumber() instanceof Long)) {
310                         // All fields are set
311                         throw new IllegalStateException("landLineNumber, mobileProvider and phoneNumer are all set"); //NOI18N
312                 } else if ((this.adminPhoneController.getPhoneCountry() == null) && ((this.adminPhoneController.getPhoneAreaCode() instanceof Integer) || (this.adminPhoneController.getPhoneNumber() instanceof Long))) {
313                         // No country selected
314                         this.showFacesMessage("form_add_contact_landLine:phoneCountry", "ERROR_ADMIN_NO_PHONE_COUNTRY_SELECTED"); //NOI18N
315                         return ""; //NOI18N
316                 } else if (((this.adminPhoneController.getPhoneAreaCode() == null) || (this.adminPhoneController.getPhoneAreaCode() < 1)) && ((this.adminPhoneController.getPhoneCountry() instanceof Country) || (this.adminPhoneController.getPhoneNumber() instanceof Long))) {
317                         // No area code entered
318                         this.showFacesMessage("form_add_contact_landLine:phoneAreaCode", "ERROR_ADMIN_NO_PHONE_AREA_CODE_ENTERED"); //NOI18N
319                         return ""; //NOI18N
320                 } else if (((this.adminPhoneController.getPhoneNumber() == null) || (this.adminPhoneController.getPhoneNumber() < 1)) && ((this.adminPhoneController.getPhoneCountry() instanceof Country) || (this.adminPhoneController.getPhoneAreaCode() instanceof Integer))) {
321                         // No phone number entered
322                         this.showFacesMessage("form_add_contact_landLine:phoneNumber", "ERROR_ADMIN_NO_PHONE_NUMBER_ENTERED"); //NOI18N
323                         return ""; //NOI18N
324                 }
325
326                 // Init instance
327                 Contact updatedContact;
328                 DialableLandLineNumber landLineNumber = this.adminPhoneController.getLandLineNumber();
329
330                 // Try it again
331                 try {
332                         // All fine so far, then which was choosen?
333                         if (landLineNumber instanceof DialableLandLineNumber) {
334                                 // User has choosen number
335                                 updatedContact = this.adminPhoneBean.linkExistingLandLineNumberWithContact(contact, landLineNumber);
336                         } else {
337                                 // Create new instance
338                                 landLineNumber = new LandLineNumber(this.adminPhoneController.getPhoneCountry(), this.adminPhoneController.getPhoneAreaCode(), this.adminPhoneController.getPhoneNumber());
339
340                                 // Link it, too
341                                 updatedContact = this.adminPhoneBean.linkNewLandLineNumberWithContact(contact, landLineNumber);
342                         }
343                 } catch (final PhoneNumberAlreadyLinkedException ex) {
344                         // Throw again as cause
345                         this.showFacesMessage("form_add_contact_landLine:phoneNumber", ex); //NOI18N
346                         return ""; //NOI18N
347                 }
348
349                 // Fire event
350                 this.adminLinkedLandLineNumberEvent.fire(new AdminLinkedLandLineNumberEvent(updatedContact, landLineNumber));
351
352                 // Return to contact profile
353                 return "admin_show_contact"; //NOI18N
354         }
355
356         @Override
357         public String doLinkAddMobileNumber (final Contact contact) {
358                 // Is all data properly set?
359                 if (null == contact) {
360                         // Throw NPE
361                         throw new NullPointerException("contact is null"); //NOI18N
362                 } else if (contact.getContactId() == null) {
363                         // Throw it again
364                         throw new NullPointerException("contact.contactId is null"); //NOI18N
365                 } else if (contact.getContactId() < 1) {
366                         // Is not valid
367                         throw new IllegalArgumentException(MessageFormat.format("contact.contactId={0} is not valid", contact.getContactId())); //NOI18N
368                 } else if ((this.adminPhoneController.getMobileNumber() == null) && (this.adminPhoneController.getMobileProvider() == null) && ((this.adminPhoneController.getPhoneNumber() == null) || (this.adminPhoneController.getPhoneNumber() < 1))) {
369                         // All fields are empty
370                         throw new NullPointerException("mobileNumber, mobileProvider and phoneNumer are null"); //NOI18N
371                 } else if ((this.adminPhoneController.getMobileNumber() instanceof DialableMobileNumber) && (this.adminPhoneController.getMobileProvider() instanceof MobileProvider) && (this.adminPhoneController.getPhoneNumber() instanceof Long)) {
372                         // All fields are set
373                         throw new IllegalStateException("mobileNumber, mobileProvider and phoneNumer are all set"); //NOI18N
374                 } else if ((this.adminPhoneController.getMobileProvider() == null) && (this.adminPhoneController.getPhoneNumber() instanceof Long)) {
375                         // No mobile provider selected
376                         this.showFacesMessage("form_add_contact_mobile:phoneCountry", "ERROR_ADMIN_NO_PHONE_MOBILE_PROVIDER_SELECTED"); //NOI18N
377                         return ""; //NOI18N
378                 } else if (((this.adminPhoneController.getPhoneNumber() == null) || (this.adminPhoneController.getPhoneNumber() < 1)) && (this.adminPhoneController.getMobileProvider() instanceof MobileProvider)) {
379                         // No phone number entered
380                         this.showFacesMessage("form_add_contact_mobile:phoneNumber", "ERROR_ADMIN_NO_PHONE_NUMBER_ENTERED"); //NOI18N
381                         return ""; //NOI18N
382                 }
383
384                 // Init instance
385                 Contact updatedContact;
386                 DialableMobileNumber mobileNumber = this.adminPhoneController.getMobileNumber();
387
388                 // Try it again
389                 try {
390                         // All fine so far, then which was choosen?
391                         if (mobileNumber instanceof DialableMobileNumber) {
392                                 // User has choosen number
393                                 updatedContact = this.adminPhoneBean.linkExistingMobileNumberWithContact(contact, mobileNumber);
394                         } else {
395                                 // Create new instance
396                                 mobileNumber = new MobileNumber(this.adminPhoneController.getMobileProvider(), this.adminPhoneController.getPhoneNumber());
397
398                                 // Link it, too
399                                 updatedContact = this.adminPhoneBean.linkNewMobileNumberWithContact(contact, mobileNumber);
400                         }
401                 } catch (final PhoneNumberAlreadyLinkedException ex) {
402                         // Throw again as cause
403                         this.showFacesMessage("form_add_contact_mobile:phoneNumber", ex); //NOI18N
404                         return ""; //NOI18N
405                 }
406
407                 // Fire event
408                 this.adminLinkedMobileNumberEvent.fire(new AdminLinkedMobileNumberEvent(updatedContact, mobileNumber));
409
410                 // Return to contact profile
411                 return "admin_show_contact"; //NOI18N
412         }
413
414         /**
415          * Post-construction method
416          */
417         @PostConstruct
418         public void init () {
419                 // Try it
420                 try {
421                         // Get initial context
422                         Context context = new InitialContext();
423
424                         // Try to lookup the beans
425                         this.adminPhoneBean = (AdminContactsPhoneSessionBeanRemote) context.lookup("java:global/jjobs-ejb/adminContactPhone!org.mxchange.jcontacts.phone.AdminContactsPhoneSessionBeanRemote"); //NOI18N
426                 } catch (final NamingException e) {
427                         // Throw again
428                         throw new FaceletException(e);
429                 }
430         }
431
432         @Override
433         public String unlinkFaxContactData () {
434                 // Is all data set
435                 if (this.beanHelper.getFaxNumber() == null) {
436                         // Not set, throw NPE
437                         throw new NullPointerException("this.beanHelper.faxNumber is null"); //NOI18N
438                 } else if (this.beanHelper.getFaxNumber().getPhoneId() == null) {
439                         // Throw NPE again
440                         throw new NullPointerException("this.beanHelper.faxNumber.phoneId is null"); //NOI18N
441                 } else if (this.beanHelper.getFaxNumber().getPhoneId() < 1) {
442                         // Invalid number
443                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.faxNumber.phoneId={0} is not valid", this.beanHelper.getFaxNumber().getPhoneId())); //NOI18N
444                 } else if (this.beanHelper.getFaxNumber().getPhoneNumber() == null) {
445                         // Throw NPE again
446                         throw new NullPointerException("this.beanHelper.faxNumber.phoneNumber is null"); //NOI18N
447                 } else if (this.beanHelper.getFaxNumber().getPhoneNumber() < 1) {
448                         // Throw it again ...
449                         throw new NullPointerException(MessageFormat.format("this.beanHelper.faxNumber.phoneNumber={0} is not valid.", this.beanHelper.getFaxNumber().getPhoneNumber())); //NOI18N
450                 } else if (this.beanHelper.getContact() == null) {
451                         // ... and throw again
452                         throw new NullPointerException("this.beanHelper.contact is null"); //NOI18N
453                 } else if (this.beanHelper.getContact().getContactId() == null) {
454                         // ... and again ...
455                         throw new NullPointerException("this.beanHelper.contact.contactId is null"); //NOI18N
456                 } else if (this.beanHelper.getContact().getContactId() < 1) {
457                         // Invalid id number
458                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.contact.contactId={0} is invalid.", this.beanHelper.getContact().getContactId())); //NOI18N
459                 }
460
461                 // Init contact instance
462                 Contact updatedContact;
463
464                 try {
465                         // Unlink it and return contact without fax instance
466                         updatedContact = this.adminPhoneBean.unlinkFaxDataFromContact(this.beanHelper.getContact(), this.beanHelper.getFaxNumber());
467                 } catch (final PhoneNumberNotLinkedException ex) {
468                         // Did not work
469                         this.showFacesMessage("form_unlink_contact_fax:faxNumberId", ex); //NOI18N
470                         return ""; //NOI18N
471                 }
472
473                 // Fire event
474                 this.faxNumberUnlinkedEvent.fire(new AdminUnlinkedFaxNumberEvent(updatedContact, this.beanHelper.getFaxNumber()));
475
476                 // All fine here
477                 return "admin_show_contact"; //NOI18N
478         }
479
480         @Override
481         public String unlinkLandLineContactData () {
482                 // Is all data set
483                 if (this.beanHelper.getLandLineNumber() == null) {
484                         // Not set, throw NPE
485                         throw new NullPointerException("this.beanHelper.landLineNumber is null"); //NOI18N
486                 } else if (this.beanHelper.getLandLineNumber().getPhoneId() == null) {
487                         // Throw NPE again
488                         throw new NullPointerException("this.beanHelper.landLineNumber.phoneId is null"); //NOI18N
489                 } else if (this.beanHelper.getLandLineNumber().getPhoneId() < 1) {
490                         // Invalid number
491                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.landLineNumber.phoneId={0} is not valid", this.beanHelper.getLandLineNumber().getPhoneId())); //NOI18N
492                 } else if (this.beanHelper.getLandLineNumber().getPhoneNumber() == null) {
493                         // Throw NPE again
494                         throw new NullPointerException("this.beanHelper.landLineNumber.phoneNumber is null"); //NOI18N
495                 } else if (this.beanHelper.getLandLineNumber().getPhoneNumber() < 1) {
496                         // Throw it again ...
497                         throw new NullPointerException(MessageFormat.format("this.beanHelper.landLineNumber.phoneNumber={0} is not valid.", this.beanHelper.getLandLineNumber().getPhoneNumber())); //NOI18N
498                 } else if (this.beanHelper.getContact() == null) {
499                         // ... and throw again
500                         throw new NullPointerException("this.beanHelper.contact is null"); //NOI18N
501                 } else if (this.beanHelper.getContact().getContactId() == null) {
502                         // ... and again ...
503                         throw new NullPointerException("this.beanHelper.contact.contactId is null"); //NOI18N
504                 } else if (this.beanHelper.getContact().getContactId() < 1) {
505                         // Invalid id number
506                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.contact.contactId={0} is invalid.", this.beanHelper.getContact().getContactId())); //NOI18N
507                 }
508
509                 // Init contact instance
510                 Contact updatedContact;
511
512                 try {
513                         // Unlink it and return contact without landLine instance
514                         updatedContact = this.adminPhoneBean.unlinkLandLineDataFromContact(this.beanHelper.getContact(), this.beanHelper.getLandLineNumber());
515                 } catch (final PhoneNumberNotLinkedException ex) {
516                         // Did not work
517                         this.showFacesMessage("form_unlink_contact_landLine:landLineNumberId", ex); //NOI18N
518                         return ""; //NOI18N
519                 }
520
521                 // Fire event
522                 this.landLineNumberUnlinkedEvent.fire(new AdminUnlinkedLandLineNumberEvent(updatedContact, this.beanHelper.getLandLineNumber()));
523
524                 // All fine here
525                 return "admin_show_contact"; //NOI18N
526         }
527
528         @Override
529         public String unlinkMobileContactData () {
530                 // Is all data set
531                 if (this.beanHelper.getMobileNumber() == null) {
532                         // Not set, throw NPE
533                         throw new NullPointerException("this.beanHelper.mobileNumber is null"); //NOI18N
534                 } else if (this.beanHelper.getMobileNumber().getPhoneId() == null) {
535                         // Throw NPE again
536                         throw new NullPointerException("this.beanHelper.mobileNumber.phoneId is null"); //NOI18N
537                 } else if (this.beanHelper.getMobileNumber().getPhoneId() < 1) {
538                         // Invalid number
539                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.mobileNumber.phoneId={0} is not valid", this.beanHelper.getMobileNumber().getPhoneId())); //NOI18N
540                 } else if (this.beanHelper.getMobileNumber().getMobileProvider() == null) {
541                         // Throw NPE
542                         throw new NullPointerException("this.beanHelper.mobileNumber.mobileProvider is null"); //NOI18N
543                 } else if (this.beanHelper.getMobileNumber().getMobileProvider().getProviderId() == null) {
544                         // ... throw again
545                         throw new NullPointerException("this.beanHelper.mobileNumber.mobileProvider.providerId is null"); //NOI18N
546                 } else if (this.beanHelper.getMobileNumber().getMobileProvider().getProviderId() < 1) {
547                         // Id not valid
548                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.mobileNumber.mobileProvider.providerId={0} is not valid.", this.beanHelper.getMobileNumber().getMobileProvider().getProviderId())); //NOI18N
549                 } else if (this.beanHelper.getMobileNumber().getPhoneNumber() == null) {
550                         // Throw NPE again
551                         throw new NullPointerException("this.beanHelper.mobileNumber.phoneNumber is null"); //NOI18N
552                 } else if (this.beanHelper.getMobileNumber().getPhoneNumber() < 1) {
553                         // Throw it again ...
554                         throw new NullPointerException(MessageFormat.format("this.beanHelper.mobileNumber.phoneNumber={0} is not valid.", this.beanHelper.getMobileNumber().getPhoneNumber())); //NOI18N
555                 } else if (this.beanHelper.getContact() == null) {
556                         // ... and throw again
557                         throw new NullPointerException("this.beanHelper.contact is null"); //NOI18N
558                 } else if (this.beanHelper.getContact().getContactId() == null) {
559                         // ... and again ...
560                         throw new NullPointerException("this.beanHelper.contact.contactId is null"); //NOI18N
561                 } else if (this.beanHelper.getContact().getContactId() < 1) {
562                         // Invalid id number
563                         throw new IllegalArgumentException(MessageFormat.format("this.beanHelper.contact.contactId={0} is invalid.", this.beanHelper.getContact().getContactId())); //NOI18N
564                 }
565
566                 // Init contact instance
567                 Contact updatedContact;
568
569                 try {
570                         // Unlink it and return contact without mobile instance
571                         updatedContact = this.adminPhoneBean.unlinkMobileDataFromContact(this.beanHelper.getContact(), this.beanHelper.getMobileNumber());
572                 } catch (final PhoneNumberNotLinkedException ex) {
573                         // Did not work
574                         this.showFacesMessage("form_unlink_contact_mobile:mobileNumberId", ex); //NOI18N
575                         return ""; //NOI18N
576                 }
577
578                 // Fire event
579                 this.mobileNumberUnlinkedEvent.fire(new AdminUnlinkedMobileNumberEvent(updatedContact, this.beanHelper.getMobileNumber()));
580
581                 // All fine here
582                 return "admin_show_contact"; //NOI18N
583         }
584
585         /**
586          * Clears this bean
587          */
588         private void clear () {
589                 // Clear all data
590         }
591
592         /**
593          * Returns given property key or throws an exception if not found.
594          * <p>
595          * @param parameterKey Property key
596          * <p>
597          * @return Property value
598          * <p>
599          * @throws NullPointerException If given key is not found
600          * @throws NumberFormatException If no number is given in context parameter
601          */
602         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
603                 // Get context parameter
604                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
605                 // Return it
606                 return contextValue;
607         }
608
609         /**
610          * Returns given property key or throws an exception if not found.
611          * <p>
612          * @param parameterKey Property key
613          * <p>
614          * @return Property value
615          * <p>
616          * @throws NullPointerException If given key is not found
617          */
618         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
619                 // Get context parameter
620                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
621                 // Is it null?
622                 if (null == contextValue) {
623                         // Throw NPE
624                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
625                 }
626                 // Return it
627                 return contextValue;
628         }
629
630         /**
631          * Checks whether debug mode is enabled for given controller
632          * <p>
633          * @param controllerName Name of controller
634          * <p>
635          * @return Whether debug mode is enabled
636          */
637         protected boolean isDebugModeEnabled (final String controllerName) {
638                 // Parameters should be valid
639                 if (null == controllerName) {
640                         // Throw NPE
641                         throw new NullPointerException("controllerName is null"); //NOI18N
642                 } else if (controllerName.isEmpty()) {
643                         // Is empty
644                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
645                 }
646                 // Try to get context parameter
647                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
648                 // Is it set and true?
649                 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
650                 // Return it
651                 return isEnabled;
652         }
653
654         /**
655          * Loads resource bundle for given locale. This must be implemented per
656          * project so all projects can still customize their methods. Calling
657          * ResourceBundleloadBundle() in this class means that also the bundle files
658          * must be present here.
659          * <p>
660          * @param locale Locale from e.g. FacesContext
661          * <p>
662          * @return Initialized and loaded resource bundle
663          */
664         protected abstract ResourceBundle loadResourceBundle (final Locale locale);
665
666         /**
667          * Shows a faces message for given causing exception. The message from the
668          * exception is being inserted into the message.
669          * <p>
670          * @param clientId Client id to send message to
671          * @param cause    Causing exception
672          */
673         protected void showFacesMessage (final String clientId, final Throwable cause) {
674                 // Get context and add message
675                 this.showFacesMessage(clientId, cause.getMessage());
676         }
677
678         /**
679          * Shows a faces message with given message (i18n) key.
680          * <p>
681          * @param clientId Client id to send message to
682          * @param i18nKey  Message key
683          * <p>
684          * @throws NullPointerException If clientId or i18nKey is null
685          * @throws IllegalArgumentException If clientId or i18nKey is empty
686          */
687         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
688                 // Both parameter must be valid
689                 if (null == clientId) {
690                         // Throw NPE
691                         throw new NullPointerException("clientId is null"); //NOI18N
692                 } else if (clientId.isEmpty()) {
693                         // Is empty
694                         throw new IllegalArgumentException("clientId is null"); //NOI18N
695                 } else if (null == i18nKey) {
696                         // Throw NPE
697                         throw new NullPointerException("i18nKey is null"); //NOI18N
698                 } else if (i18nKey.isEmpty()) {
699                         // Is empty
700                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
701                 }
702                 // Get current locale
703                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
704                 // Get bundle bundle
705                 ResourceBundle bundle = this.loadResourceBundle(locale);
706                 // Default is i18nKey
707                 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
708                 // Try it
709                 try {
710                         // Get message
711                         message = bundle.getString(i18nKey);
712                 } catch (final MissingResourceException ex) {
713                         // Did not find it, ignored
714                 }
715                 // Get context and add message
716                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
717         }
718
719 }