From: Roland Häder Date: Mon, 26 Jun 2017 21:43:25 +0000 (+0200) Subject: Rewrite continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=34c9c57c7f1660b51ff53e0379b86ac0234bc051;p=pizzaservice-ejb.git 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 Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jmailee/model/delivery/PizzaEmailDeliveryMessageBean.java b/src/java/org/mxchange/jmailee/model/delivery/PizzaEmailDeliveryMessageBean.java index f432fa3..fa4c43e 100644 --- a/src/java/org/mxchange/jmailee/model/delivery/PizzaEmailDeliveryMessageBean.java +++ b/src/java/org/mxchange/jmailee/model/delivery/PizzaEmailDeliveryMessageBean.java @@ -18,18 +18,20 @@ package org.mxchange.jmailee.model.delivery; import java.io.Serializable; import java.text.MessageFormat; +import java.util.Properties; +import java.util.ResourceBundle; +import javax.annotation.PostConstruct; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import org.mxchange.jcoreeelogger.beans.local.logger.Log; -import org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal; +import javax.mail.MessagingException; import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; +import org.mxchange.pizzaaplication.database.BasePizzaDatabaseBean; +import org.mxchange.pizzaapplication.mailer.model.delivery.DeliverablePizzaEmail; +import org.mxchange.pizzaapplication.mailer.model.delivery.PizzaMailer; /** * A message queue for sending out emails @@ -44,81 +46,155 @@ import org.mxchange.jmailee.model.delivery.wrapper.WrapableEmailDelivery; @ActivationConfigProperty (propertyName = "destinationType", propertyValue = "javax.jms.Queue") } ) -public class PizzaEmailDeliveryMessageBean implements MessageListener { +public class PizzaEmailDeliveryMessageBean extends BasePizzaDatabaseBean implements MessageListener { /** - * Logger bean + * Serial number */ - @Log - private LoggerBeanLocal loggerBeanLocal; + private static final long serialVersionUID = 190_586_572_658_143L; + + /** + * Configuration file + */ + private final String configFile = "org.mxchange.jmailer.config"; //NOI18N + + /** + * Mailer instance + */ + private final DeliverablePizzaEmail mailer; /** * Default constructor */ public PizzaEmailDeliveryMessageBean () { // Call super constructor - super(); + super("jms/pizzaservice-queue-factory", "jms/pizzaservice-email-queue"); //NOI18N - try { - // Get initial context - Context context = new InitialContext(); - - // Lookup logger - this.loggerBeanLocal = (LoggerBeanLocal) context.lookup("java:global/jcore-logger-ejb/logger!org.mxchange.jcoreeelogger.beans.local.logger.LoggerBeanLocal"); //NOI18N - } catch (final NamingException ex) { - // Continue to throw - throw new RuntimeException(MessageFormat.format("context.lookup() failed: {0}", ex.getMessage()), ex); //NOI18N + // Init mailer instance + this.mailer = new PizzaMailer(); + } + + /** + * Post-construction + */ + @PostConstruct + public void init () { + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: CALLED!", this.getClass().getSimpleName())); //NOI18N + + // Try to load bundle + ResourceBundle bundle = ResourceBundle.getBundle(this.configFile); + + // Debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: bundle={1}", this.getClass().getSimpleName(), bundle)); //NOI18N + + // The bunble should be valid + if (null == bundle) { + // Throw NPE + throw new NullPointerException(MessageFormat.format("bundle is null, maybe file {0} does not exist?", this.configFile)); //NOI18N + } + + // Init Properties + Properties properties = new Properties(); + + // Is the bundle not empty? + if (!bundle.keySet().isEmpty()) { + // Loop through all + for (final String key : bundle.keySet()) { + // Log debug message + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.init: key={1}", this.getClass().getSimpleName(), key)); //NOI18N + + // Get string from bundle and set it in properties + properties.put(key, bundle.getString(key)); + } } + + // Handle it over to the mailer + this.mailer.init(properties); + + // Trace message + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.init: EXIT!", this.getClass().getSimpleName())); //NOI18N } @Override public void onMessage (final Message message) { // Trace message - this.loggerBeanLocal.logTrace(MessageFormat.format("onMessage: message={0} - CALLED!", message)); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage: message={1} - CALLED!", this.getClass().getSimpleName(), message)); //NOI18N - // Is the message castable to ObjectMessage? + // The parameter should be valid if (null == message) { - // message is null + // Throw NPE throw new NullPointerException("message is null"); //NOI18N } else if (!(message instanceof ObjectMessage)) { - // Not castable - throw new ClassCastException(MessageFormat.format("message cannot be casted to ObjectMessage: {0}", message)); //NOI18N + // Not implementing right interface + throw new IllegalArgumentException(MessageFormat.format("message={0} does not implemented ObjectMessage", message)); //NOI18N } // Securely cast it ObjectMessage objectMessage = (ObjectMessage) message; - // Init instance - Serializable object; + // Init variable + Serializable serializable; try { - // Get object from it - object = objectMessage.getObject(); + // Get object from message + serializable = objectMessage.getObject(); } catch (final JMSException ex) { - // Log exception ... - this.loggerBeanLocal.logException(ex); - - // ... and don't continue + // Log it and don't continue any further + this.getLoggerBeanLocal().logException(ex); return; } // Debug message - this.loggerBeanLocal.logDebug(MessageFormat.format("onMessage: object={0}", object)); //NOI18N - - // Does this object implement WrapableCheckout ? - if (null == object) { - // object cannot be null - throw new NullPointerException("object is null"); //NOI18N - } else if (!(object instanceof WrapableEmailDelivery)) { - // Not proper interface used - throw new ClassCastException(MessageFormat.format("object does not implement WrapableEmailDelivery: {0}", object)); //NOI18N + this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.onMessage: serializable={1}", this.getClass().getSimpleName(), serializable)); //NOI18N + + // Okay, is it the right interface? + if (null == serializable) { + // Throw NPE + throw new NullPointerException("serializable is null"); //NOI18N + } else if (!(serializable instanceof WrapableEmailDelivery)) { + // Not correct object send + throw new IllegalArgumentException(MessageFormat.format("serializable={0} does not implement WrapableEmailDelivery", serializable)); //NOI18N + } + + // Securely cast it + WrapableEmailDelivery wrapper = (WrapableEmailDelivery) serializable; + + // Is all required set? + if (wrapper.getLocale() == null) { + // Throw NPE + throw new NullPointerException("wrapper.locale is null"); //NOI18N + } else if (wrapper.getRecipient() == null) { + // Throw again ... + throw new NullPointerException("wrapper.recipient is null"); //NOI18N + } else if (wrapper.getSubjectLine() == null) { + // ... and again + throw new NullPointerException("wrapper.subjectLine is null"); //NOI18N + } else if (wrapper.getSubjectLine().isEmpty()) { + // Is empty + throw new IllegalArgumentException("wrapper.subjectLine is empty"); //NOI18N + } else if (wrapper.getTemplateName() == null) { + // Throw NPE again + throw new NullPointerException("wrapper.templateName is null"); //NOI18N + } else if (wrapper.getTemplateName().isEmpty()) { + // Is empty + throw new IllegalArgumentException("wrapper.templateName is empty"); //NOI18N + } else if (wrapper.getTemplateVariables() == null) { + // No template variables set, should not happen + throw new NullPointerException("wrapper.templateVariables is null"); //NOI18N } - // Cast the object to the wrapper interface - WrapableEmailDelivery emailDelivery = (WrapableEmailDelivery) object; + try { + // Send email out + this.mailer.sendDeliverableMail(wrapper); + } catch (final MessagingException ex) { + // Opps, something went wrong + this.getLoggerBeanLocal().logException(ex); + return; + } // Trace message - this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N + this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.onMessage - EXIT!", this.getClass().getSimpleName())); //NOI18N } } diff --git a/src/java/org/mxchange/jusercore/model/user/activity/PizzaUserActivitySessionBean.java b/src/java/org/mxchange/jusercore/model/user/activity/PizzaUserActivitySessionBean.java index f802f91..3f4aa6c 100644 --- a/src/java/org/mxchange/jusercore/model/user/activity/PizzaUserActivitySessionBean.java +++ b/src/java/org/mxchange/jusercore/model/user/activity/PizzaUserActivitySessionBean.java @@ -19,8 +19,13 @@ package org.mxchange.jusercore.model.user.activity; import java.text.MessageFormat; import java.util.Arrays; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.EJBException; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.jms.JMSException; import javax.jms.ObjectMessage; import javax.persistence.Query; @@ -35,7 +40,7 @@ import org.mxchange.jusercore.model.user.User; * @author Roland Häder */ @Stateless (name = "userActivity", description = "A bean handling the user data") -public class PizzaUserActivitySessionBean extends BaseDatabaseBean implements UserActivityLogSessionBeanRemote { +public abstract class PizzaUserActivitySessionBean extends BaseDatabaseBean implements UserActivityLogSessionBeanRemote { /** * Serial number @@ -227,4 +232,131 @@ public class PizzaUserActivitySessionBean extends BaseDatabaseBean implements Us return list; } + /** + * 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/pizzaapplication/model/basket/BasketSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/basket/BasketSessionBean.java index 8fc9dc7..53c2d12 100644 --- a/src/java/org/mxchange/pizzaapplication/model/basket/BasketSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/basket/BasketSessionBean.java @@ -19,7 +19,12 @@ package org.mxchange.pizzaapplication.model.basket; import java.text.MessageFormat; import java.util.GregorianCalendar; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.persistence.EntityExistsException; import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jcustomercore.model.customer.Customer; @@ -37,7 +42,7 @@ import org.mxchange.jshopcore.model.order.ShopOrder; * @author Roland Häder */ @Stateless (name = "basket", description = "A bean handling persisting baskets of logged-in customers") -public class BasketSessionBean extends BaseDatabaseBean implements BasketSessionBeanRemote { +public abstract class BasketSessionBean extends BaseDatabaseBean implements BasketSessionBeanRemote { /** * Serial number @@ -133,4 +138,131 @@ public class BasketSessionBean extends BaseDatabaseBean implements BasketSession return accessKey; } + /** + * 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/pizzaapplication/model/category/AdminCategorySessionBean.java b/src/java/org/mxchange/pizzaapplication/model/category/AdminCategorySessionBean.java index 7032914..9e68174 100644 --- a/src/java/org/mxchange/pizzaapplication/model/category/AdminCategorySessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/category/AdminCategorySessionBean.java @@ -17,7 +17,12 @@ package org.mxchange.pizzaapplication.model.category; import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.persistence.EntityNotFoundException; import javax.persistence.NoResultException; import javax.persistence.Query; @@ -34,7 +39,7 @@ import org.mxchange.jshopcore.model.category.AdminCategorySessionBeanRemote; * @author Roland Häder */ @Stateless (name = "admin_category", description = "An administrative bean handling product categories") -public class AdminCategorySessionBean extends BaseDatabaseBean implements AdminCategorySessionBeanRemote { +public abstract class AdminCategorySessionBean extends BaseDatabaseBean implements AdminCategorySessionBeanRemote { /** * Serial number @@ -135,4 +140,131 @@ public class AdminCategorySessionBean extends BaseDatabaseBean implements AdminC return isUsed; } + /** + * 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/pizzaapplication/model/category/CategorySessionBean.java b/src/java/org/mxchange/pizzaapplication/model/category/CategorySessionBean.java index a970d79..683246f 100644 --- a/src/java/org/mxchange/pizzaapplication/model/category/CategorySessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/category/CategorySessionBean.java @@ -18,7 +18,12 @@ package org.mxchange.pizzaapplication.model.category; import java.text.MessageFormat; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.persistence.Query; import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jproduct.model.category.Category; @@ -31,7 +36,7 @@ import org.mxchange.jshopcore.model.category.CategorySessionBeanRemote; * @author Roland Häder */ @Stateless (name = "category", description = "A bean handling categories for all others (non-admin)") -public class CategorySessionBean extends BaseDatabaseBean implements CategorySessionBeanRemote { +public abstract class CategorySessionBean extends BaseDatabaseBean implements CategorySessionBeanRemote { /** * Serial number @@ -71,4 +76,131 @@ public class CategorySessionBean extends BaseDatabaseBean implements CategorySes return list; } + /** + * 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/pizzaapplication/model/checkout/CheckoutMessageBean.java b/src/java/org/mxchange/pizzaapplication/model/checkout/CheckoutMessageBean.java index 5bdc1b8..b2bfcbb 100644 --- a/src/java/org/mxchange/pizzaapplication/model/checkout/CheckoutMessageBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/checkout/CheckoutMessageBean.java @@ -19,9 +19,14 @@ package org.mxchange.pizzaapplication.model.checkout; import java.io.Serializable; import java.text.MessageFormat; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.ActivationConfigProperty; import javax.ejb.EJB; import javax.ejb.MessageDriven; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; @@ -53,7 +58,7 @@ import org.mxchange.jshopcore.wrapper.WrapableCheckout; @ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/shopCheckoutQueue") } ) -public class CheckoutMessageBean extends BaseBean implements MessageListener { +public abstract class CheckoutMessageBean extends BaseBean implements MessageListener { /** * Serial number @@ -212,4 +217,131 @@ public class CheckoutMessageBean extends BaseBean implements MessageListener { this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N } + /** + * 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/pizzaapplication/model/customer/ShopCustomerSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/customer/ShopCustomerSessionBean.java index b62c91c..b5be173 100644 --- a/src/java/org/mxchange/pizzaapplication/model/customer/ShopCustomerSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/customer/ShopCustomerSessionBean.java @@ -18,7 +18,12 @@ package org.mxchange.pizzaapplication.model.customer; import java.text.MessageFormat; import java.util.GregorianCalendar; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.persistence.EntityNotFoundException; import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jcustomercore.exceptions.CustomerAlreadyRegisteredException; @@ -32,7 +37,7 @@ import org.mxchange.jcustomercore.model.customer.CustomerSessionBeanRemote; * @author Roland Häder */ @Stateless (name = "shop_customer", description = "A bean handling the customer data") -public class ShopCustomerSessionBean extends BaseDatabaseBean implements CustomerSessionBeanRemote { +public abstract class ShopCustomerSessionBean extends BaseDatabaseBean implements CustomerSessionBeanRemote { /** * Serial number @@ -150,4 +155,131 @@ public class ShopCustomerSessionBean extends BaseDatabaseBean implements Custome return customer; } + /** + * 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/pizzaapplication/model/product/AdminProductSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/product/AdminProductSessionBean.java index 6c14c62..d584147 100644 --- a/src/java/org/mxchange/pizzaapplication/model/product/AdminProductSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/product/AdminProductSessionBean.java @@ -18,7 +18,12 @@ package org.mxchange.pizzaapplication.model.product; import java.text.MessageFormat; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.persistence.EntityExistsException; import javax.persistence.EntityNotFoundException; import javax.persistence.NoResultException; @@ -36,7 +41,7 @@ import org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote; * @author Roland Häder */ @Stateless (name = "admin_product", description = "An administrative bean handling products") -public class AdminProductSessionBean extends BaseDatabaseBean implements AdminProductSessionBeanRemote { +public abstract class AdminProductSessionBean extends BaseDatabaseBean implements AdminProductSessionBeanRemote { /** * Serial number @@ -162,4 +167,131 @@ public class AdminProductSessionBean extends BaseDatabaseBean implements AdminPr return isUsed; } + /** + * 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/pizzaapplication/model/product/ProductSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/product/ProductSessionBean.java index ef1ce7f..96fdb09 100644 --- a/src/java/org/mxchange/pizzaapplication/model/product/ProductSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/product/ProductSessionBean.java @@ -20,8 +20,13 @@ import java.text.MessageFormat; import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.EJB; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import org.mxchange.jcoreee.database.BaseDatabaseBean; import org.mxchange.jproduct.model.product.Product; import org.mxchange.jshopcore.model.product.AdminProductSessionBeanRemote; @@ -33,7 +38,7 @@ import org.mxchange.jshopcore.model.product.ProductSessionBeanRemote; * @author Roland Häder */ @Stateless (name = "product", description = "A bean handling products for all others (non-admin)") -public class ProductSessionBean extends BaseDatabaseBean implements ProductSessionBeanRemote { +public abstract class ProductSessionBean extends BaseDatabaseBean implements ProductSessionBeanRemote { /** * Serial number @@ -102,4 +107,131 @@ public class ProductSessionBean extends BaseDatabaseBean implements ProductSessi return deque; } + /** + * 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/pizzaapplication/model/receipt/PdfReceiptSessionBean.java b/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptSessionBean.java index 2097dc8..e8ee770 100644 --- a/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/model/receipt/PdfReceiptSessionBean.java @@ -17,7 +17,12 @@ package org.mxchange.pizzaapplication.model.receipt; import java.text.MessageFormat; +import java.util.Locale; +import java.util.MissingResourceException; +import java.util.ResourceBundle; import javax.ejb.Stateless; +import javax.faces.application.FacesMessage; +import javax.faces.context.FacesContext; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; @@ -36,7 +41,7 @@ import org.mxchange.jshopreceipt.receipt.Receipt; * @author Roland Häder */ @Stateless (name = "pdf", description = "A bean creating PDF receipts") -public class PdfReceiptSessionBean extends BaseDatabaseBean implements ReceiptBeanRemote { +public abstract class PdfReceiptSessionBean extends BaseDatabaseBean implements ReceiptBeanRemote { /** * Serial number @@ -136,4 +141,131 @@ public class PdfReceiptSessionBean extends BaseDatabaseBean implements ReceiptBe this.customer = customer; } + /** + * 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)); + } + }