From 2b2d8099d01e04940c5a4eabf41c6cd682a5f4bd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 26 Jun 2017 23:43:27 +0200 Subject: [PATCH] Rewrite continued: - Now all project-specific abstract web beans (controllers) inherit from BaseFacesBean to have these nice showFacesMessage() methods. - Also all project-specific abstract EJBs inherit now only BaseDataBean (one was missing in an old project) - So, if you have a WAR project, inherit from BaseFacesBean, if you have an EJB project, inherit from BaseDatabaseBean MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../jjobs/beans/BaseJobsController.java | 33 ++--- .../JobsBusinessContactWebSessionBean.java | 135 +++++++++++++++++- .../contact/JobsContactWebSessionBean.java | 134 ++++++++++++++++- .../JobsAdminContactPhoneWebRequestBean.java | 134 ++++++++++++++++- .../JobsAdminCountryWebRequestBean.java | 135 +++++++++++++++++- .../JobsCountryWebApplicationBean.java | 134 ++++++++++++++++- .../JobsFeatureWebApplicationBean.java | 135 +++++++++++++++++- ...JobsAdminMobileProviderWebRequestBean.java | 135 +++++++++++++++++- .../phone/JobsAdminPhoneWebRequestBean.java | 134 ++++++++++++++++- .../phone/JobsPhoneWebApplicationBean.java | 134 ++++++++++++++++- .../JobsUserProfileWebRequestBean.java | 134 ++++++++++++++++- .../JobsProfileModeWebApplicationBean.java | 135 +++++++++++++++++- .../JobsUserActivityWebApplicationBean.java | 134 ++++++++++++++++- 13 files changed, 1610 insertions(+), 36 deletions(-) diff --git a/src/java/org/mxchange/jjobs/beans/BaseJobsController.java b/src/java/org/mxchange/jjobs/beans/BaseJobsController.java index 16ba02af..74842006 100644 --- a/src/java/org/mxchange/jjobs/beans/BaseJobsController.java +++ b/src/java/org/mxchange/jjobs/beans/BaseJobsController.java @@ -16,16 +16,16 @@ */ package org.mxchange.jjobs.beans; -import java.io.Serializable; -import org.mxchange.jcoreee.bean.BaseBean; -import org.mxchange.jusercore.model.user.UserUtils; +import java.util.Locale; +import java.util.ResourceBundle; +import org.mxchange.jcoreee.bean.faces.BaseFacesBean; /** * A general controller *

* @author Roland Häder */ -public abstract class BaseJobsController extends BaseBean implements Serializable { +public abstract class BaseJobsController extends BaseFacesBean { /** * Serial number @@ -38,28 +38,13 @@ public abstract class BaseJobsController extends BaseBean implements Serializabl protected BaseJobsController () { } - /** - * Checks if given password is to weak to be used - *

- * @param password Clear-text password - *

- * @return Whether the entered password is to weak - */ - protected boolean isWeakPassword (final String password) { - // Is parameter set? - if (null == password) { - // Throw NPE - throw new NullPointerException("password is null"); //NOI18N - } - - // Get score value - double passwordScore = UserUtils.calculatePasswordScore(password); - - // Is the score within range? - boolean isWeak = (passwordScore <= this.getIntegerContextParameter("min_user_password_score")); //NOI18N + @Override + protected ResourceBundle loadResourceBundle (final Locale locale) { + // Load resource bundle + ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale); // Return it - return isWeak; + return bundle; } } diff --git a/src/java/org/mxchange/jjobs/beans/businesscontact/JobsBusinessContactWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/businesscontact/JobsBusinessContactWebSessionBean.java index 12a1c7cc..2073bf3a 100644 --- a/src/java/org/mxchange/jjobs/beans/businesscontact/JobsBusinessContactWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/businesscontact/JobsBusinessContactWebSessionBean.java @@ -16,10 +16,16 @@ */ package org.mxchange.jjobs.beans.businesscontact; +import java.text.MessageFormat; import java.util.Collections; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -38,7 +44,7 @@ import org.mxchange.jjobs.beans.login.user.JobsUserLoginWebSessionController; */ @Named ("businessContactController") @SessionScoped -public class JobsBusinessContactWebSessionBean extends BaseJobsController implements JobsBusinessContactWebSessionController { +public abstract class JobsBusinessContactWebSessionBean extends BaseJobsController implements JobsBusinessContactWebSessionController { /** * Serial number @@ -95,4 +101,131 @@ public class JobsBusinessContactWebSessionBean extends BaseJobsController implem } } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java index 25102c5d..db1ebb23 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/JobsContactWebSessionBean.java @@ -22,10 +22,15 @@ import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Objects; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.enterprise.event.Observes; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -66,7 +71,7 @@ import org.mxchange.jusercore.model.user.User; */ @Named ("contactController") @SessionScoped -public class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController { +public abstract class JobsContactWebSessionBean extends BaseJobsController implements JobsContactWebSessionController { /** * Serial number @@ -1168,4 +1173,131 @@ public class JobsContactWebSessionBean extends BaseJobsController implements Job this.contactList.add(contact); } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/contact/phone/JobsAdminContactPhoneWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/contact/phone/JobsAdminContactPhoneWebRequestBean.java index 2a356829..89e86a07 100644 --- a/src/java/org/mxchange/jjobs/beans/contact/phone/JobsAdminContactPhoneWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/contact/phone/JobsAdminContactPhoneWebRequestBean.java @@ -17,11 +17,16 @@ package org.mxchange.jjobs.beans.contact.phone; import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; import javax.enterprise.event.Observes; import javax.enterprise.inject.Any; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -66,7 +71,7 @@ import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; */ @Named ("adminContactPhoneController") @RequestScoped -public class JobsAdminContactPhoneWebRequestBean extends BaseJobsController implements JobsAdminContactPhoneWebRequestController { +public abstract class JobsAdminContactPhoneWebRequestBean extends BaseJobsController implements JobsAdminContactPhoneWebRequestController { /** * Call-stack instance (5 may show BeanELResolver.getValue as caller) @@ -584,4 +589,131 @@ public class JobsAdminContactPhoneWebRequestBean extends BaseJobsController impl // Clear all data } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java index a66fe95b..b8db871a 100644 --- a/src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java @@ -16,13 +16,19 @@ */ package org.mxchange.jjobs.beans.country; +import java.text.MessageFormat; import java.util.Iterator; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Objects; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; import javax.enterprise.inject.Any; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -44,7 +50,7 @@ import org.mxchange.jjobs.beans.BaseJobsController; */ @Named ("adminCountryController") @RequestScoped -public class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController { +public abstract class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController { /** * Serial number @@ -285,4 +291,131 @@ public class JobsAdminCountryWebRequestBean extends BaseJobsController implement return isAdded; } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java b/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java index ceffa6bb..8a376d3e 100644 --- a/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java +++ b/src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java @@ -18,9 +18,14 @@ package org.mxchange.jjobs.beans.country; import java.text.MessageFormat; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Observes; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Named; import javax.naming.Context; @@ -38,7 +43,7 @@ import org.mxchange.jjobs.beans.BaseJobsController; */ @Named ("countryController") @ApplicationScoped -public class JobsCountryWebApplicationBean extends BaseJobsController implements JobsCountryWebApplicationController { +public abstract class JobsCountryWebApplicationBean extends BaseJobsController implements JobsCountryWebApplicationController { /** * Serial number @@ -117,4 +122,131 @@ public class JobsCountryWebApplicationBean extends BaseJobsController implements this.countryList = this.countryBean.allCountries(); } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/features/JobsFeatureWebApplicationBean.java b/src/java/org/mxchange/jjobs/beans/features/JobsFeatureWebApplicationBean.java index 5a1edcee..7baaa96e 100644 --- a/src/java/org/mxchange/jjobs/beans/features/JobsFeatureWebApplicationBean.java +++ b/src/java/org/mxchange/jjobs/beans/features/JobsFeatureWebApplicationBean.java @@ -16,8 +16,14 @@ */ package org.mxchange.jjobs.beans.features; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.inject.Named; import org.mxchange.jjobs.beans.BaseJobsController; @@ -28,7 +34,7 @@ import org.mxchange.jjobs.beans.BaseJobsController; */ @Named ("featureController") @ApplicationScoped -public class JobsFeatureWebApplicationBean extends BaseJobsController implements JobsFeaturesWebApplicationController { +public abstract class JobsFeatureWebApplicationBean extends BaseJobsController implements JobsFeaturesWebApplicationController { /** * Serial number @@ -78,4 +84,131 @@ public class JobsFeatureWebApplicationBean extends BaseJobsController implements return isEnabled; } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/mobileprovider/JobsAdminMobileProviderWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/mobileprovider/JobsAdminMobileProviderWebRequestBean.java index 4672ce8c..77c9c8b6 100644 --- a/src/java/org/mxchange/jjobs/beans/mobileprovider/JobsAdminMobileProviderWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/mobileprovider/JobsAdminMobileProviderWebRequestBean.java @@ -16,13 +16,19 @@ */ package org.mxchange.jjobs.beans.mobileprovider; +import java.text.MessageFormat; import java.util.Iterator; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Objects; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; import javax.enterprise.inject.Any; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -45,7 +51,7 @@ import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider; */ @Named ("adminMobileProviderController") @RequestScoped -public class JobsAdminMobileProviderWebRequestBean extends BaseJobsController implements JobsAdminMobileProviderWebRequestController { +public abstract class JobsAdminMobileProviderWebRequestBean extends BaseJobsController implements JobsAdminMobileProviderWebRequestController { /** * Serial number @@ -221,4 +227,131 @@ public class JobsAdminMobileProviderWebRequestBean extends BaseJobsController im return isFound; } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/phone/JobsAdminPhoneWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/phone/JobsAdminPhoneWebRequestBean.java index 72929676..bf7295f1 100644 --- a/src/java/org/mxchange/jjobs/beans/phone/JobsAdminPhoneWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/phone/JobsAdminPhoneWebRequestBean.java @@ -18,11 +18,16 @@ package org.mxchange.jjobs.beans.phone; import java.text.MessageFormat; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Objects; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; import javax.enterprise.event.Event; import javax.enterprise.inject.Any; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -63,7 +68,7 @@ import org.mxchange.jphone.phonenumbers.phone.AdminPhoneSessionBeanRemote; */ @Named ("adminPhoneController") @RequestScoped -public class JobsAdminPhoneWebRequestBean extends BaseJobsController implements JobsAdminPhoneWebRequestController { +public abstract class JobsAdminPhoneWebRequestBean extends BaseJobsController implements JobsAdminPhoneWebRequestController { /** * Call-stack position @@ -644,4 +649,131 @@ public class JobsAdminPhoneWebRequestBean extends BaseJobsController implements } } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/phone/JobsPhoneWebApplicationBean.java b/src/java/org/mxchange/jjobs/beans/phone/JobsPhoneWebApplicationBean.java index 357d9932..fe68e3fa 100644 --- a/src/java/org/mxchange/jjobs/beans/phone/JobsPhoneWebApplicationBean.java +++ b/src/java/org/mxchange/jjobs/beans/phone/JobsPhoneWebApplicationBean.java @@ -19,10 +19,15 @@ package org.mxchange.jjobs.beans.phone; import java.text.MessageFormat; import java.util.LinkedList; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; import java.util.Objects; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Observes; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Named; import javax.naming.Context; @@ -54,7 +59,7 @@ import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; */ @Named ("phoneController") @ApplicationScoped -public class JobsPhoneWebApplicationBean extends BaseJobsController implements JobsPhoneWebApplicationController { +public abstract class JobsPhoneWebApplicationBean extends BaseJobsController implements JobsPhoneWebApplicationController { /** * Serial number @@ -610,4 +615,131 @@ public class JobsPhoneWebApplicationBean extends BaseJobsController implements J } } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java b/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java index 25fe991e..c06aeaec 100644 --- a/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java +++ b/src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java @@ -17,8 +17,13 @@ package org.mxchange.jjobs.beans.profile; import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -36,7 +41,7 @@ import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; */ @Named (value = "profileController") @RequestScoped -public class JobsUserProfileWebRequestBean extends BaseJobsController implements JobsUserProfileWebRequestController { +public abstract class JobsUserProfileWebRequestBean extends BaseJobsController implements JobsUserProfileWebRequestController { /** * Serial number @@ -130,4 +135,131 @@ public class JobsUserProfileWebRequestBean extends BaseJobsController implements (this.userLoginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS))); } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/profilemode/JobsProfileModeWebApplicationBean.java b/src/java/org/mxchange/jjobs/beans/profilemode/JobsProfileModeWebApplicationBean.java index e6b54844..befcc5a1 100644 --- a/src/java/org/mxchange/jjobs/beans/profilemode/JobsProfileModeWebApplicationBean.java +++ b/src/java/org/mxchange/jjobs/beans/profilemode/JobsProfileModeWebApplicationBean.java @@ -16,8 +16,14 @@ */ package org.mxchange.jjobs.beans.profilemode; +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.inject.Named; import org.mxchange.jjobs.beans.BaseJobsController; import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; @@ -29,7 +35,7 @@ import org.mxchange.jusercore.model.user.profilemodes.ProfileMode; */ @Named ("profileModeController") @ApplicationScoped -public class JobsProfileModeWebApplicationBean extends BaseJobsController implements JobsProfileModeWebApplicationController { +public abstract class JobsProfileModeWebApplicationBean extends BaseJobsController implements JobsProfileModeWebApplicationController { /** * Serial number @@ -57,4 +63,131 @@ public class JobsProfileModeWebApplicationBean extends BaseJobsController implem public void init () { } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } diff --git a/src/java/org/mxchange/jjobs/beans/user/activity/JobsUserActivityWebApplicationBean.java b/src/java/org/mxchange/jjobs/beans/user/activity/JobsUserActivityWebApplicationBean.java index cbdfde30..5317d968 100644 --- a/src/java/org/mxchange/jjobs/beans/user/activity/JobsUserActivityWebApplicationBean.java +++ b/src/java/org/mxchange/jjobs/beans/user/activity/JobsUserActivityWebApplicationBean.java @@ -22,10 +22,15 @@ import java.util.GregorianCalendar; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; +import java.util.Locale; import java.util.Map; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.enterprise.event.Observes; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; import javax.inject.Named; @@ -60,7 +65,7 @@ import org.mxchange.jjobs.beans.helper.JobsWebRequestHelperController; */ @Named ("userActivityController") @ApplicationScoped -public class JobsUserActivityWebApplicationBean extends BaseJobsController implements JobsUserActivityWebApplicationController { +public abstract class JobsUserActivityWebApplicationBean extends BaseJobsController implements JobsUserActivityWebApplicationController { /** * Serial number @@ -565,4 +570,131 @@ public class JobsUserActivityWebApplicationBean extends BaseJobsController imple this.usersActivity.get(userActivity.getActivityUser()).add(userActivity); } + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + * @throws NumberFormatException If no number is given in context parameter + */ + protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException { + // Get context parameter + Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey)); + // Return it + return contextValue; + } + + /** + * Returns given property key or throws an exception if not found. + *

+ * @param parameterKey Property key + *

+ * @return Property value + *

+ * @throws NullPointerException If given key is not found + */ + protected String getStringContextParameter (final String parameterKey) throws NullPointerException { + // Get context parameter + String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey); + // Is it null? + if (null == contextValue) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N + } + // Return it + return contextValue; + } + + /** + * Checks whether debug mode is enabled for given controller + *

+ * @param controllerName Name of controller + *

+ * @return Whether debug mode is enabled + */ + protected boolean isDebugModeEnabled (final String controllerName) { + // Parameters should be valid + if (null == controllerName) { + // Throw NPE + throw new NullPointerException("controllerName is null"); //NOI18N + } else if (controllerName.isEmpty()) { + // Is empty + throw new IllegalArgumentException("controllerName is empty"); //NOI18N + } + // Try to get context parameter + String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N + // Is it set and true? + boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE; + // Return it + return isEnabled; + } + + /** + * Loads resource bundle for given locale. This must be implemented per + * project so all projects can still customize their methods. Calling + * ResourceBundleloadBundle() in this class means that also the bundle files + * must be present here. + *

+ * @param locale Locale from e.g. FacesContext + *

+ * @return Initialized and loaded resource bundle + */ + protected abstract ResourceBundle loadResourceBundle (final Locale locale); + + /** + * Shows a faces message for given causing exception. The message from the + * exception is being inserted into the message. + *

+ * @param clientId Client id to send message to + * @param cause Causing exception + */ + protected void showFacesMessage (final String clientId, final Throwable cause) { + // Get context and add message + this.showFacesMessage(clientId, cause.getMessage()); + } + + /** + * Shows a faces message with given message (i18n) key. + *

+ * @param clientId Client id to send message to + * @param i18nKey Message key + *

+ * @throws NullPointerException If clientId or i18nKey is null + * @throws IllegalArgumentException If clientId or i18nKey is empty + */ + protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException { + // Both parameter must be valid + if (null == clientId) { + // Throw NPE + throw new NullPointerException("clientId is null"); //NOI18N + } else if (clientId.isEmpty()) { + // Is empty + throw new IllegalArgumentException("clientId is null"); //NOI18N + } else if (null == i18nKey) { + // Throw NPE + throw new NullPointerException("i18nKey is null"); //NOI18N + } else if (i18nKey.isEmpty()) { + // Is empty + throw new IllegalArgumentException("i18nKey is null"); //NOI18N + } + // Get current locale + Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale(); + // Get bundle bundle + ResourceBundle bundle = this.loadResourceBundle(locale); + // Default is i18nKey + String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N + // Try it + try { + // Get message + message = bundle.getString(i18nKey); + } catch (final MissingResourceException ex) { + // Did not find it, ignored + } + // Get context and add message + FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message)); + } + } -- 2.39.5