]> git.mxchange.org Git - jjobs-war.git/blobdiff - src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / localization / JobsLocalizationSessionBean.java
index da3889b363d78c8b284a1c26522bc8a3dd411e9d..2737bc22f4ed1d780e1c14ba36ad3fb22deb07cd 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016, 2017 Roland Häder
+ * Copyright (C) 2016 - 2020 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
@@ -17,6 +17,7 @@
 package org.mxchange.jjobs.beans.localization;
 
 import java.text.MessageFormat;
+import java.text.NumberFormat;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Locale;
@@ -32,7 +33,7 @@ import javax.inject.Inject;
 import javax.inject.Named;
 import org.mxchange.jcoreee.events.locale.LocaleChangeEvent;
 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
-import org.mxchange.jjobs.beans.BaseJobsController;
+import org.mxchange.jjobs.beans.BaseJobsBean;
 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
 import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent;
 
@@ -46,7 +47,12 @@ import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent;
  */
 @Named ("localizationController")
 @SessionScoped
-public class JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
+public class JobsLocalizationSessionBean extends BaseJobsBean implements JobsLocalizationSessionController {
+
+       /**
+        * Number format
+        */
+       private static NumberFormat NUMBER_FORMAT;
 
        /**
         * Serial number
@@ -82,7 +88,7 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
                // Call super constructor
                super();
 
-               // Init list
+               // Init locale list
                this.supportedLocales = new LinkedHashMap<>(2);
        }
 
@@ -92,7 +98,7 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
         * @param event Event instance
         */
        public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
-               // event should not be null
+               // Event and contained entity instance should not be null
                if (null == event) {
                        // Throw NPE
                        throw new NullPointerException("event is null"); //NOI18N
@@ -110,7 +116,7 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
                // Is the locale set?
                if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
                        // Get user local
-                       Locale userLocale = event.getLoggedInUser().getUserLocale();
+                       final Locale userLocale = event.getLoggedInUser().getUserLocale();
 
                        // Change locale
                        this.changeLocale(userLocale, Boolean.TRUE);
@@ -123,7 +129,7 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
         * @param event Event instance
         */
        public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
-               // event should not be null
+               // Event and contained entity instance should not be null
                if (null == event) {
                        // Throw NPE
                        throw new NullPointerException("event is null"); //NOI18N
@@ -159,8 +165,8 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
                Locale newLocale = null;
 
                // Iterate over whole map
-               for (Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
-                       Locale foundLocale = entry.getValue();
+               for (final Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
+                       final Locale foundLocale = entry.getValue();
 
                        // Does the language match?
                        if (Objects.equals(foundLocale.toString(), this.getLocaleCode())) {
@@ -181,13 +187,18 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
 
                // Then change it
                this.changeLocale(newLocale, Boolean.TRUE);
+
+               // Change formatting
+               NUMBER_FORMAT = NumberFormat.getNumberInstance(newLocale);
        }
 
-       /**
-        * Getter for locale
-        * <p>
-        * @return Locale
-        */
+       @Override
+       public String formatCurrency (final Float amount) {
+               // Format amount
+               return NUMBER_FORMAT.format(amount);
+       }
+
+       @Override
        public Locale getLocale () {
                return this.locale;
        }
@@ -236,13 +247,13 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
        @PostConstruct
        public void init () {
                // Get default locale
-               Locale defaultLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
+               final Locale defaultLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
 
                // Add it to list
                this.getSupportedLocales().put(defaultLocale.toString(), defaultLocale);
 
                // Get iterator from faces context
-               Iterator<Locale> iterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
+               final Iterator<Locale> iterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
 
                // Add all locales
                while (iterator.hasNext()) {
@@ -259,13 +270,13 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
                // Is no country code found?
                if (requestLocale.getCountry().isEmpty()) {
                        // Then try to find one, get language from it
-                       String language = requestLocale.getLanguage();
+                       final String language = requestLocale.getLanguage();
                        Boolean found = Boolean.FALSE;
 
                        // Iterate over whole map
-                       for (Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
-                               String languageCode = entry.getKey();
-                               Locale foundLocale = entry.getValue();
+                       for (final Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
+                               final String languageCode = entry.getKey();
+                               final Locale foundLocale = entry.getValue();
 
                                // Does the language match?
                                if (languageCode.startsWith(language)) {
@@ -285,6 +296,9 @@ public class JobsLocalizationSessionBean extends BaseJobsController implements J
 
                // Change locale, may set same back in faces context, but triggers event
                this.changeLocale(requestLocale, Boolean.FALSE);
+
+               // Initial formatting, may change when user chooses other locale
+               NUMBER_FORMAT = NumberFormat.getNumberInstance(this.locale);
        }
 
        /**