+ * Provides the default {@link Locale} based on the current account session context. + * The priority for this provider is set to 10, making it suitable for account-level locale resolution. + *
+ * If the account session or locale cannot be resolved, this provider returns null. + * + * @author Mario + */ @Provider public class AccountLocaleProvider implements LocaleProvider { - + /** + * Logger for this provider, using SLF4J. + */ private final LoggingService logger = new SLF4JLoggingService(AccountLocaleProvider.class); + /** + * Returns the priority of this provider. Lower values indicate higher priority. + * + * @return the priority value (10) + */ @Override public int getPriority() { return 10; } + /** + * Returns the default {@link Locale} for the current account session. + *
+ * Attempts to retrieve the account locale from the current session. If unavailable, returns null. + * + * @return the account's default Locale, or null if not available + */ @Override public Locale getDefaultLocale() { try { diff --git a/sources/core/src/main/java/tools/dynamia/modules/saas/AccountSessionHolder.java b/sources/core/src/main/java/tools/dynamia/modules/saas/AccountSessionHolder.java index 7152e98..ef4f089 100644 --- a/sources/core/src/main/java/tools/dynamia/modules/saas/AccountSessionHolder.java +++ b/sources/core/src/main/java/tools/dynamia/modules/saas/AccountSessionHolder.java @@ -31,6 +31,7 @@ import tools.dynamia.modules.saas.services.AccountService; import java.io.Serializable; +import java.time.ZoneId; import java.util.Locale; /** @@ -45,6 +46,7 @@ public class AccountSessionHolder implements Serializable { private transient AccountService service; private Locale accountLocale; + private ZoneId accountTimeZone; private Long currentId; private AccountDTO currentDTO; @@ -82,7 +84,8 @@ public void setCurrent(final Account account) { try { DomainUtils.lookupCrudService().executeWithinTransaction(() -> { var current = getService().getAccountById(account.getId()); - accountLocale = current.getLocale() != null ? Locale.forLanguageTag(current.getLocale()) : null; + loadAccountLocale(current); + loadAccountTimeZone(current); currentId = current.getId(); currentDTO = current.toDTO(); }); @@ -92,6 +95,22 @@ public void setCurrent(final Account account) { } } + private void loadAccountTimeZone(Account current) { + try { + accountTimeZone = current.getTimeZone() != null ? ZoneId.of(current.getTimeZone()) : null; + } catch (Exception e) { + accountTimeZone = null; + } + } + + private void loadAccountLocale(Account current) { + try { + accountLocale = current.getLocale() != null ? Locale.forLanguageTag(current.getLocale()) : null; + } catch (Exception e) { + accountLocale = null; + } + } + public AccountDTO toDTO() { return currentDTO; @@ -114,4 +133,11 @@ private AccountService getService() { } return service; } + + public ZoneId getAccountTimeZone() { + if (accountTimeZone == null) { + accountTimeZone = ZoneId.systemDefault(); + } + return accountTimeZone; + } } diff --git a/sources/core/src/main/java/tools/dynamia/modules/saas/AccountTimeZoneProvider.java b/sources/core/src/main/java/tools/dynamia/modules/saas/AccountTimeZoneProvider.java new file mode 100644 index 0000000..2be0f1d --- /dev/null +++ b/sources/core/src/main/java/tools/dynamia/modules/saas/AccountTimeZoneProvider.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1 + * Colombia / South America + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package tools.dynamia.modules.saas; + +import tools.dynamia.commons.TimeZoneProvider; +import tools.dynamia.commons.logger.LoggingService; +import tools.dynamia.commons.logger.SLF4JLoggingService; +import tools.dynamia.integration.sterotypes.Provider; + +import java.time.ZoneId; + +/** + * AccountTimeZoneProvider is an implementation of {@link TimeZoneProvider} for SaaS environments. + *
+ * Provides the default {@link ZoneId} based on the current account session context. + * The priority for this provider is set to 10, making it suitable for account-level time zone resolution. + *
+ * If the account session or time zone cannot be resolved, this provider returns null. + * + * @author Mario + */ +@Provider +public class AccountTimeZoneProvider implements TimeZoneProvider { + /** + * Logger for this provider, using SLF4J. + */ + private final LoggingService logger = new SLF4JLoggingService(AccountTimeZoneProvider.class); + + /** + * Returns the priority of this provider. Lower values indicate higher priority. + * + * @return the priority value (10) + */ + @Override + public int getPriority() { + return 10; + } + + /** + * Returns the default {@link ZoneId} for the current account session. + *
+ * Attempts to retrieve the account time zone from the current session. If unavailable, returns null.
+ *
+ * @return the account's default ZoneId, or null if not available
+ */
+ @Override
+ public ZoneId getDefaultTimeZone() {
+ try {
+ return AccountSessionHolder.get().getAccountTimeZone();
+ } catch (Exception e) {
+ return null;
+ }
+ }
+}
diff --git a/sources/jpa/pom.xml b/sources/jpa/pom.xml
index c3fa1a8..026622b 100644
--- a/sources/jpa/pom.xml
+++ b/sources/jpa/pom.xml
@@ -22,7 +22,7 @@