From: Roland Häder Date: Fri, 18 Aug 2017 22:45:43 +0000 (+0200) Subject: Please cherry-pick: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=f01ec7c1d30f1fcb6c41da709962ab7deff1e261;p=jfinancials-war.git Please cherry-pick: - removed setter/getter and any other action methods from interface as this is no longer accepted - rewrote broken getter calls to event-based notification of other beans - removed obsolete language backing-bean property from localizationController because the already stored Locale instance does already have a 2-letter language code - introduced changeLocale() for encapsulation of setting locale in bean, view root and triggering event for bean notification - added ValueChangeEvent listener method for locale selection box Signed-off-by: Roland Häder --- diff --git a/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java b/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java index 2b6b3a21..a290f39c 100644 --- a/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionBean.java @@ -17,12 +17,21 @@ package org.mxchange.jfinancials.beans.localization; import java.text.MessageFormat; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; import java.util.Locale; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; +import javax.enterprise.event.Event; import javax.enterprise.event.Observes; +import javax.enterprise.inject.Any; import javax.faces.context.FacesContext; +import javax.faces.event.ValueChangeEvent; +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.jfinancials.beans.BaseFinancialsController; import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent; import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent; @@ -49,12 +58,32 @@ public class FinancialsLocalizationSessionBean extends BaseFinancialsController */ private Locale locale; + /** + * Event being fired when a locale has changed successfully + */ + @Inject + @Any + private Event localeChangeEvent; + + /** + * Locale code (example: de for Germany) + */ + private String localeCode; + + /** + * A list of all supported locales + */ + private final List supportedLocales; + /** * Default constructor */ public FinancialsLocalizationSessionBean () { // Call super constructor super(); + + // Init list + this.supportedLocales = new LinkedList<>(); } /** @@ -80,8 +109,11 @@ public class FinancialsLocalizationSessionBean extends BaseFinancialsController // Is the locale set? if (event.getLoggedInUser().getUserLocale() instanceof Locale) { - // Set locale here - this.setLocale(event.getLoggedInUser().getUserLocale()); + // Get user local + Locale userLocale = event.getLoggedInUser().getUserLocale(); + + // Change locale + this.changeLocale(userLocale); } } @@ -110,50 +142,51 @@ public class FinancialsLocalizationSessionBean extends BaseFinancialsController this.clear(); } - @Override - public String getLanguage () { - return this.getLocale().getLanguage().toLowerCase(); - } - - @Override - public void setLanguage (final String language) { - // Is the language null? - if (null == language) { - // This may sometimes happen, so abort here - return; - } - - // Language splits - String[] splits = language.split("_"); //NOI18N - if (null == splits[1]) { - splits[1] = ""; //NOI18N - } - - // Get new locale with upper-case country code - Locale loc = new Locale(splits[0], splits[1]); - - // Set it here and in the JSF context - this.setLocale(loc); - FacesContext.getCurrentInstance().getViewRoot().setLocale(loc); - } - - @Override + /** + * Getter for locale + *

+ * @return Locale + */ public Locale getLocale () { return this.locale; } - @Override + /** + * Setter for locale + *

+ * @param locale Locale + */ public void setLocale (final Locale locale) { this.locale = locale; } - @Override - public Locale[] getSelectableLocalizations () { - Locale[] locales = { - Locale.GERMANY, - Locale.US - }; - return locales; + /** + * Getter for localeCode code + *

+ * @return Language code + */ + public String getLocaleCode () { + return this.localeCode; + } + + /** + * Setter for localeCode code + *

+ * @param localeCode Language code + */ + public void setLocaleCode (final String localeCode) { + this.localeCode = localeCode; + } + + /** + * Getter for selectable localizations + *

+ * @return Selectable localizations + */ + @SuppressWarnings ("ReturnOfCollectionOrArrayField") + public List getSupportedLocales () { + // Return full list + return this.supportedLocales; } /** @@ -161,11 +194,96 @@ public class FinancialsLocalizationSessionBean extends BaseFinancialsController */ @PostConstruct public void init () { - // Create locale instance from context - Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale(); + // Get locale instance from context + Locale currentLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale(); + + // Change locale, may set same back in faces context, but triggers event + this.changeLocale(currentLocale); + + // Get default locale + Locale defaultLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale(); + + // Add it to list + this.getSupportedLocales().add(defaultLocale); + + // Get iterator from faces context + Iterator iterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales(); + + // Add all locales + while (iterator.hasNext()) { + // Get next locale + Locale supportedLocale = iterator.next(); + + // Add it + this.getSupportedLocales().add(supportedLocale); + } + } + + /** + * Listens on value-change event and changes locale accordingly, if found in + * list. + *

+ * This method has been taken from + * https://www.mkyong.com/jsf2/jsf-2-internationalization-example/ and has + * been customized, e.g. checked parameter deeply. If the selected locale + * does not exist (someone changed the value in HTML form) then a proper + * exception is thrown. + *

+ * @param event Event being triggered by JSF view/template + * + * @throws NullPointerException If event or newValue field is empty + * @throws IllegalArgumentException If newValue is empty or the locale does + * not exist + */ + public void localeCodeChanged (final ValueChangeEvent event) { + // Validate event + if (null == event) { + // Abort here + throw new NullPointerException("event is null"); //NOI18N + } else if (event.getNewValue() == null) { + // Abort again + throw new NullPointerException("event.newValue is null"); //NOI18N + } + + // First, get new value from event + String newCountryCode = event.getNewValue().toString(); + + // Is it valid? + if (null == newCountryCode) { + // Abort here + throw new NullPointerException("newCountryCode is null"); //NOI18N + } else if (newCountryCode.isEmpty()) { + // Is empty + throw new IllegalArgumentException("newCountryCode is empty"); //NOI18N + } + + // Generate locale from new value (which is the 2-letter country code) + Locale newLocale = new Locale(newCountryCode); + + // Lookup locale in list, very easy + if (!this.getSupportedLocales().contains(newLocale)) { + // Locale is not in supportedLocales list + throw new IllegalArgumentException(MessageFormat.format("Country code {0} is not supported.", newCountryCode)); //NOI18N + } + + // All fine, then change locale + this.changeLocale(newLocale); + } + + /** + * Changes current locale in this controller and faces context to given + *

+ * @param locale New locale to set + */ + private void changeLocale (final Locale locale) { + // Set locale here + this.setLocale(locale); + + // Inform faces context + FacesContext.getCurrentInstance().getViewRoot().setLocale(locale); - // Set it here - this.setLocale(loc); + // Fire event + this.localeChangeEvent.fire(new LocaleChangeEvent(locale)); } /** @@ -173,7 +291,7 @@ public class FinancialsLocalizationSessionBean extends BaseFinancialsController */ private void clear () { // Clear all fields - this.setLanguage(null); + this.setLocaleCode(null); this.setLocale(null); } diff --git a/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionController.java b/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionController.java index f8bac04e..9b84d0d6 100644 --- a/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionController.java +++ b/src/java/org/mxchange/jfinancials/beans/localization/FinancialsLocalizationSessionController.java @@ -17,7 +17,6 @@ package org.mxchange.jfinancials.beans.localization; import java.io.Serializable; -import java.util.Locale; /** * An interface for localization change beans @@ -26,39 +25,4 @@ import java.util.Locale; */ public interface FinancialsLocalizationSessionController extends Serializable { - /** - * Getter for locale - *

- * @return Locale - */ - Locale getLocale (); - - /** - * Setter for locale - *

- * @param locale Locale - */ - void setLocale (final Locale locale); - - /** - * Getter for language code - *

- * @return Language code - */ - String getLanguage (); - - /** - * Setter for language code - *

- * @param language Language code - */ - void setLanguage (final String language); - - /** - * Getter for selectable localizations - *

- * @return Selectable localizations - */ - Locale[] getSelectableLocalizations (); - } diff --git a/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java b/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java index 1104a35b..0b590192 100644 --- a/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java +++ b/src/java/org/mxchange/jfinancials/beans/user/FinancialsAdminUserWebRequestBean.java @@ -17,6 +17,7 @@ package org.mxchange.jfinancials.beans.user; import java.text.MessageFormat; +import java.util.Locale; import java.util.Objects; import javax.annotation.PostConstruct; import javax.enterprise.context.RequestScoped; @@ -31,6 +32,7 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent; import org.mxchange.jcoreee.utils.FacesUtils; import org.mxchange.jfinancials.beans.BaseFinancialsController; import org.mxchange.jfinancials.beans.contact.FinancialsAdminContactWebRequestController; @@ -123,6 +125,11 @@ public class FinancialsAdminUserWebRequestBean extends BaseFinancialsController @Any private Event deleteUserEvent; + /** + * Locale instance + */ + private Locale locale; + /** * Localization controller */ @@ -242,7 +249,7 @@ public class FinancialsAdminUserWebRequestBean extends BaseFinancialsController newUser.setUserProfileMode(ProfileMode.INVISIBLE); // Copy user locale - newUser.setUserLocale(this.localizationController.getLocale()); + newUser.setUserLocale(this.getLocale()); // Init instance Contact userContact; @@ -352,6 +359,26 @@ public class FinancialsAdminUserWebRequestBean extends BaseFinancialsController this.setUser(event.getCreatedUser()); } + /** + * Observer method for events being fired when the application's locale has + * been changed. + *

+ * @param event Event being fired + */ + public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) { + // Is the parameter valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getLocale() == null) { + // Throw NPE again + throw new NullPointerException("event.locale is null"); + } + + // Set it here + this.setLocale(event.getLocale()); + } + /** * Event observer for new user registrations *

@@ -768,6 +795,24 @@ public class FinancialsAdminUserWebRequestBean extends BaseFinancialsController this.setUserName(null); } + /** + * Getter for locale instance + *

+ * @return Locale instance + */ + private Locale getLocale () { + return this.locale; + } + + /** + * Setter for locale instance + *

+ * @param locale Locale instance + */ + private void setLocale (final Locale locale) { + this.locale = locale; + } + /** * Checks if same password is entered and that they are not empty. *

diff --git a/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java index 8afe8e57..8e26fa65 100644 --- a/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/user/FinancialsUserWebSessionBean.java @@ -19,6 +19,7 @@ package org.mxchange.jfinancials.beans.user; import java.text.MessageFormat; import java.util.Iterator; import java.util.List; +import java.util.Locale; import java.util.Objects; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; @@ -33,6 +34,7 @@ import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.mxchange.jcontacts.contact.Contact; +import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent; import org.mxchange.jfinancials.beans.BaseFinancialsController; import org.mxchange.jfinancials.beans.contact.FinancialsContactWebSessionController; import org.mxchange.jfinancials.beans.features.FinancialsFeaturesWebApplicationController; @@ -41,6 +43,7 @@ import org.mxchange.jfinancials.beans.user.login.FinancialsUserLoginWebSessionCo import org.mxchange.jusercore.events.user.add.ObservableAdminAddedUserEvent; import org.mxchange.jusercore.events.user.clear.password.ObservableClearUserPasswordEvent; import org.mxchange.jusercore.events.user.clear.username.ObservableClearUserNameEvent; +import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; import org.mxchange.jusercore.events.user.delete.ObservableAdminDeletedUserEvent; import org.mxchange.jusercore.events.user.linked.ObservableAdminLinkedUserEvent; import org.mxchange.jusercore.events.user.locked.ObservableAdminLockedUserEvent; @@ -60,7 +63,6 @@ import org.mxchange.juserlogincore.events.registration.ObservableUserRegisteredE import org.mxchange.juserlogincore.events.user.password_change.ObservableUpdatedUserPasswordEvent; import org.mxchange.juserlogincore.exceptions.UserPasswordMismatchException; import org.mxchange.juserlogincore.login.UserLoginUtils; -import org.mxchange.jusercore.events.user.created.ObservableCreatedUserEvent; /** * A user bean (controller) @@ -88,6 +90,11 @@ public class FinancialsUserWebSessionBean extends BaseFinancialsController imple @Inject private FinancialsFeaturesWebApplicationController featureController; + /** + * Locale instance + */ + private Locale locale; + /** * Localization controller */ @@ -361,6 +368,26 @@ public class FinancialsUserWebSessionBean extends BaseFinancialsController imple this.copyUser(user); } + /** + * Observer method for events being fired when the application's locale has + * been changed. + *

+ * @param event Event being fired + */ + public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) { + // Is the parameter valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getLocale() == null) { + // Throw NPE again + throw new NullPointerException("event.locale is null"); + } + + // Set it here + this.setLocale(event.getLocale()); + } + /** * Event observer when user confirmed account. *

@@ -617,7 +644,7 @@ public class FinancialsUserWebSessionBean extends BaseFinancialsController imple // Set user name profile mode and locale user.setUserName(this.getUserName()); user.setUserProfileMode(this.getUserProfileMode()); - user.setUserLocale(this.localizationController.getLocale()); + user.setUserLocale(this.getLocale()); // Is multiple registration page if ((createContactData) || (!this.featureController.isFeatureEnabled("user_register_multiple_page"))) { //NOI18N @@ -1094,6 +1121,24 @@ public class FinancialsUserWebSessionBean extends BaseFinancialsController imple this.setUserProfileMode(user.getUserProfileMode()); } + /** + * Getter for locale instance + *

+ * @return Locale instance + */ + private Locale getLocale () { + return this.locale; + } + + /** + * Setter for locale instance + *

+ * @param locale Locale instance + */ + private void setLocale (final Locale locale) { + this.locale = locale; + } + /** * Removes user from all lists *

diff --git a/src/java/org/mxchange/jfinancials/beans/user/resendlink/FinancialsResendLinkWebSessionBean.java b/src/java/org/mxchange/jfinancials/beans/user/resendlink/FinancialsResendLinkWebSessionBean.java index 407780c4..d8a39971 100644 --- a/src/java/org/mxchange/jfinancials/beans/user/resendlink/FinancialsResendLinkWebSessionBean.java +++ b/src/java/org/mxchange/jfinancials/beans/user/resendlink/FinancialsResendLinkWebSessionBean.java @@ -16,9 +16,11 @@ */ package org.mxchange.jfinancials.beans.user.resendlink; +import java.util.Locale; import javax.annotation.PostConstruct; import javax.enterprise.context.SessionScoped; import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; import javax.enterprise.inject.Any; import javax.faces.view.facelets.FaceletException; import javax.inject.Inject; @@ -26,6 +28,7 @@ import javax.inject.Named; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; +import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent; import org.mxchange.jcoreee.utils.FacesUtils; import org.mxchange.jfinancials.beans.BaseFinancialsController; import org.mxchange.jfinancials.beans.localization.FinancialsLocalizationSessionController; @@ -59,6 +62,11 @@ public class FinancialsResendLinkWebSessionBean extends BaseFinancialsController */ private String emailAddress; + /** + * Locale instance + */ + private Locale locale; + /** * Localization controller */ @@ -91,6 +99,26 @@ public class FinancialsResendLinkWebSessionBean extends BaseFinancialsController super(); } + /** + * Observer method for events being fired when the application's locale has + * been changed. + *

+ * @param event Event being fired + */ + public void afterLocaleChangeEvent (@Observes final ObservableLocaleChangeEvent event) { + // Is the parameter valid? + if (null == event) { + // Throw NPE + throw new NullPointerException("event is null"); + } else if (event.getLocale() == null) { + // Throw NPE again + throw new NullPointerException("event.locale is null"); + } + + // Set it here + this.setLocale(event.getLocale()); + } + /** * Resends (new) confirmation link to given email address, if found. * Otherwise an exception is thrown. On success a redirect takes place. @@ -147,7 +175,7 @@ public class FinancialsResendLinkWebSessionBean extends BaseFinancialsController String baseUrl = FacesUtils.generateBaseUrl(); // Call EJB and return redirect target - managedUser = this.resendLinkBean.resendConfirmationLink(user, this.localizationController.getLocale(), baseUrl); + managedUser = this.resendLinkBean.resendConfirmationLink(user, this.getLocale(), baseUrl); } catch (final UserNotFoundException ex) { // User not found this.showFacesMessage("form_resend_link:resendEmailAddress", "ERROR_USER_NOT_FOUND"); //NOI18N @@ -212,4 +240,22 @@ public class FinancialsResendLinkWebSessionBean extends BaseFinancialsController this.setEmailAddress(null); } + /** + * Getter for locale instance + *

+ * @return Locale instance + */ + private Locale getLocale () { + return this.locale; + } + + /** + * Setter for locale instance + *

+ * @param locale Locale instance + */ + private void setLocale (final Locale locale) { + this.locale = locale; + } + } diff --git a/web/WEB-INF/templates/base.tpl b/web/WEB-INF/templates/base.tpl index 5bc1930a..4f96022d 100644 --- a/web/WEB-INF/templates/base.tpl +++ b/web/WEB-INF/templates/base.tpl @@ -5,7 +5,7 @@ xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> - + diff --git a/web/WEB-INF/templates/generic/locale_selection_box.tpl b/web/WEB-INF/templates/generic/locale_selection_box.tpl index 303a5260..b956a276 100644 --- a/web/WEB-INF/templates/generic/locale_selection_box.tpl +++ b/web/WEB-INF/templates/generic/locale_selection_box.tpl @@ -6,9 +6,9 @@ xmlns:ui="http://xmlns.jcp.org/jsf/facelets"> - + - +