]> git.mxchange.org Git - jcoreee.git/blobdiff - src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java
Continued:
[jcoreee.git] / src / org / mxchange / jcoreee / bean / faces / BaseFacesBean.java
index 5496e2909703a761784cfd8f5fc06a6425ab7649..ef7ed50f6f96da91c17203653cabce72579f4f3f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Roland Häder
+ * Copyright (C) 2017, 2018 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  */
 package org.mxchange.jcoreee.bean.faces;
 
+import java.io.Serializable;
+import java.math.BigDecimal;
 import java.security.Principal;
 import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 import javax.faces.application.FacesMessage;
 import javax.faces.context.FacesContext;
-import org.mxchange.jcoreee.bean.BaseBean;
 
 /**
  * An abstract bean for faces (web) projects.
  * <p>
  * @author Roland Häder<roland@mxchange.org>
  */
-public abstract class BaseFacesBean extends BaseBean {
+public abstract class BaseFacesBean implements Serializable {
+
+       /**
+        * Loaded resource bundles ("cached")
+        */
+       private static final List<ResourceBundle> RESOURCE_BUNDLES;
 
        /**
         * Serial number
         */
        private static final long serialVersionUID = 18_605_498_672_261L;
 
+       /**
+        * Static initializer
+        */
+       static {
+               // Init resource bundle list
+               RESOURCE_BUNDLES = new ArrayList<>(3);
+       }
+
+       /**
+        * Removes all bundles from web application. Typically you want to invoke
+        * this method in a ServletContextListener implementation on the
+        * contextDestroyed() method.
+        */
+       public static void removeBundles () {
+               // Clear bundles
+               RESOURCE_BUNDLES.clear();
+       }
+
+       /**
+        * Getter for resource bundle list
+        * <p>
+        * @return Resource bundle list
+        */
+       @SuppressWarnings ("ReturnOfCollectionOrArrayField")
+       protected static List<ResourceBundle> getBundles () {
+               return RESOURCE_BUNDLES;
+       }
+
        /**
         * Protected constructor
         */
        protected BaseFacesBean () {
-               // Call super constructor
-               super();
+       }
+
+       /**
+        * A filter for prices
+        * <p>
+        * @param value  Actual value to check
+        * @param filter Filter value to check against {@code value}
+        * @param locale Locale
+        * <p>
+        * @return Whether price ({@code value}) is at least {@code filter}
+        *
+        * @see https://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml
+        */
+       @SuppressWarnings ("unchecked")
+       public boolean filterByPrice (final Object value, final Object filter, final Locale locale) {
+               // Get trimmed string or NULL
+               final String filterText = (filter == null) ? null : filter.toString().trim();
+
+               // Is null or empty?
+               if (filterText == null || filterText.isEmpty()) {
+                       // Then return TRUE
+                       return true;
+               }
+
+               // Is value NULL or not castable?
+               if (value == null) {
+                       // Return FALSE
+                       return false;
+               } else if (!(value instanceof Comparable)) {
+                       // Not castable
+                       throw new ClassCastException("value is not instance of Comparable");
+               }
+
+               // Do the cast and compare
+               return ((Comparable<BigDecimal>) value).compareTo(BigDecimal.valueOf(Double.valueOf(filterText))) >= 0;
        }
 
        /**
@@ -53,7 +122,7 @@ public abstract class BaseFacesBean extends BaseBean {
         */
        protected String determinePrincipalName () {
                // Get principal
-               Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
+               final Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
 
                // Init with null
                String principalName = null;
@@ -74,17 +143,62 @@ public abstract class BaseFacesBean extends BaseBean {
         * @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));
+               final Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
+
                // Return it
                return contextValue;
        }
 
+       /**
+        * Returns a message based on given i18nKey or puts it into three question
+        * marks each side when not found.
+        * <p>
+        * @param i18nKey I18n key
+        * <p>
+        * @return Localized message
+        * <p>
+        * @throws NullPointerException If the parameter is null
+        * @throws IllegalArgumentException If the parameter is empty
+        */
+       protected String getMessageFromBundle (final String i18nKey) {
+               // Validate parameter
+               if (null == i18nKey) {
+                       // Throw NPE
+                       throw new NullPointerException("i18nKey is null"); //NOI18N
+               } else if (i18nKey.isEmpty()) {
+                       // Is empty
+                       throw new IllegalArgumentException("i18nKey is empty"); //NOI18N
+               }
+
+               // Get current locale
+               final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
+
+               // Get bundle bundle
+               this.loadResourceBundles(locale);
+
+               // Default is i18nKey
+               String message = MessageFormat.format("???{0}???", i18nKey); //NOI18N
+
+               // Loop through all
+               for (final ResourceBundle bundle : getBundles()) {
+                       // Found message?
+                       // Try it
+                       try {
+                               // Get message
+                               message = bundle.getString(i18nKey);
+                               break;
+                       } catch (final MissingResourceException ex) {
+                               // Did not find it, ignored
+                       }
+               }
+
+               // Return it
+               return message;
+       }
+
        /**
         * Returns given property key or throws an exception if not found.
         * <p>
@@ -96,12 +210,14 @@ public abstract class BaseFacesBean extends BaseBean {
         */
        protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
                // Get context parameter
-               String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
+               final 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;
        }
@@ -122,25 +238,26 @@ public abstract class BaseFacesBean extends BaseBean {
                        // 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
+               final String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
+
                // Is it set and true?
-               boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
+               final boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
+
                // Return it
                return isEnabled;
        }
 
        /**
-        * Loads resource bundle for given locale. This must be implemented per
+        * Loads resource bundles 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);
+       protected abstract void loadResourceBundles (final Locale locale);
 
        /**
         * Shows a faces message for given causing exception. The message from the
@@ -178,19 +295,10 @@ public abstract class BaseFacesBean extends BaseBean {
                        // 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 message from bundle
+               final String message = this.getMessageFromBundle(i18nKey);
+
                // Get context and add message
                FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
        }