From 8b6a2a4c4b1b33bbd13888276d80f86f5a4eb664 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 26 Jun 2017 22:37:15 +0200 Subject: [PATCH] splitted faces-related methods to new BaseFacesBean class. BaseDatabaseBean is then for EJBs and BaseFacesBean for web controllers (aka Bean). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- src/org/mxchange/jcoreee/bean/BaseBean.java | 135 -------------- .../jcoreee/bean/faces/BaseFacesBean.java | 166 ++++++++++++++++++ 2 files changed, 166 insertions(+), 135 deletions(-) create mode 100644 src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java diff --git a/src/org/mxchange/jcoreee/bean/BaseBean.java b/src/org/mxchange/jcoreee/bean/BaseBean.java index 772ef69..a28b41e 100644 --- a/src/org/mxchange/jcoreee/bean/BaseBean.java +++ b/src/org/mxchange/jcoreee/bean/BaseBean.java @@ -152,23 +152,6 @@ public abstract class BaseBean implements Serializable { return principalName; } - /** - * 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; - } /** * Getter for loggerBeanLocal @@ -197,67 +180,6 @@ public abstract class BaseBean implements Serializable { return this.session; } - /** - * 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); /** * Sends given message to configured queue @@ -286,62 +208,5 @@ public abstract class BaseBean implements Serializable { this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.sendMessage: EXIT!", this.getClass().getSimpleName())); //NOI18N } - /** - * 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/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java new file mode 100644 index 0000000..9d5a850 --- /dev/null +++ b/src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2017 Roland Häder + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jcoreee.bean.faces; + +import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; +import org.mxchange.jcoreee.bean.BaseBean; + +/** + * An abstract bean for faces (web) projects. + *

+ * @author Roland Häder + */ +public abstract class BaseFacesBean extends BaseBean { + + /** + * Serial number + */ + private static final long serialVersionUID = 18_605_498_672_261L; + + /** + * 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