- 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 <roland@mxchange.org>
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
@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
}
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
return list;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
return accessKey;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
return isUsed;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
return list;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
@ActivationConfigProperty (propertyName = "destinationLookup", propertyValue = "jms/shopCheckoutQueue")
}
)
-public class CheckoutMessageBean extends BaseBean implements MessageListener {
+public abstract class CheckoutMessageBean extends BaseBean implements MessageListener {
/**
* Serial number
this.loggerBeanLocal.logTrace("onMessage: EXIT!"); //NOI18N
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
return customer;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
return isUsed;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
return deque;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}
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;
* @author Roland Häder<roland@mxchange.org>
*/
@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
this.customer = customer;
}
+ /**
+ * Returns given property key or throws an exception if not found.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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.
+ * <p>
+ * @param parameterKey Property key
+ * <p>
+ * @return Property value
+ * <p>
+ * @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
+ * <p>
+ * @param controllerName Name of controller
+ * <p>
+ * @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.
+ * <p>
+ * @param locale Locale from e.g. FacesContext
+ * <p>
+ * @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.
+ * <p>
+ * @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.
+ * <p>
+ * @param clientId Client id to send message to
+ * @param i18nKey Message key
+ * <p>
+ * @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));
+ }
+
}