Skip to content

Commit fb1bedc

Browse files
feat: add AccountTimeZoneProvider and enhance AccountSessionHolder for account-level time zone management
1 parent b6808e5 commit fb1bedc

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

sources/core/src/main/java/tools/dynamia/modules/saas/AccountSessionHolder.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import tools.dynamia.modules.saas.services.AccountService;
3232

3333
import java.io.Serializable;
34+
import java.time.ZoneId;
3435
import java.util.Locale;
3536

3637
/**
@@ -45,6 +46,7 @@ public class AccountSessionHolder implements Serializable {
4546
private transient AccountService service;
4647

4748
private Locale accountLocale;
49+
private ZoneId accountTimeZone;
4850
private Long currentId;
4951
private AccountDTO currentDTO;
5052

@@ -82,7 +84,8 @@ public void setCurrent(final Account account) {
8284
try {
8385
DomainUtils.lookupCrudService().executeWithinTransaction(() -> {
8486
var current = getService().getAccountById(account.getId());
85-
accountLocale = current.getLocale() != null ? Locale.forLanguageTag(current.getLocale()) : null;
87+
loadAccountLocale(current);
88+
loadAccountTimeZone(current);
8689
currentId = current.getId();
8790
currentDTO = current.toDTO();
8891
});
@@ -92,6 +95,22 @@ public void setCurrent(final Account account) {
9295
}
9396
}
9497

98+
private void loadAccountTimeZone(Account current) {
99+
try {
100+
accountTimeZone = current.getTimeZone() != null ? ZoneId.of(current.getTimeZone()) : null;
101+
} catch (Exception e) {
102+
accountTimeZone = null;
103+
}
104+
}
105+
106+
private void loadAccountLocale(Account current) {
107+
try {
108+
accountLocale = current.getLocale() != null ? Locale.forLanguageTag(current.getLocale()) : null;
109+
} catch (Exception e) {
110+
accountLocale = null;
111+
}
112+
}
113+
95114

96115
public AccountDTO toDTO() {
97116
return currentDTO;
@@ -114,4 +133,11 @@ private AccountService getService() {
114133
}
115134
return service;
116135
}
136+
137+
public ZoneId getAccountTimeZone() {
138+
if (accountTimeZone == null) {
139+
accountTimeZone = ZoneId.systemDefault();
140+
}
141+
return accountTimeZone;
142+
}
117143
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2023 Dynamia Soluciones IT S.A.S - NIT 900302344-1
3+
* Colombia / South America
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package tools.dynamia.modules.saas;
19+
20+
import tools.dynamia.commons.TimeZoneProvider;
21+
import tools.dynamia.commons.logger.LoggingService;
22+
import tools.dynamia.commons.logger.SLF4JLoggingService;
23+
import tools.dynamia.integration.sterotypes.Provider;
24+
25+
import java.time.ZoneId;
26+
27+
/**
28+
* AccountTimeZoneProvider is an implementation of {@link TimeZoneProvider} for SaaS environments.
29+
* <p>
30+
* Provides the default {@link ZoneId} based on the current account session context.
31+
* The priority for this provider is set to 10, making it suitable for account-level time zone resolution.
32+
* <p>
33+
* If the account session or time zone cannot be resolved, this provider returns null.
34+
*
35+
* @author Mario
36+
*/
37+
@Provider
38+
public class AccountTimeZoneProvider implements TimeZoneProvider {
39+
/**
40+
* Logger for this provider, using SLF4J.
41+
*/
42+
private final LoggingService logger = new SLF4JLoggingService(AccountTimeZoneProvider.class);
43+
44+
/**
45+
* Returns the priority of this provider. Lower values indicate higher priority.
46+
*
47+
* @return the priority value (10)
48+
*/
49+
@Override
50+
public int getPriority() {
51+
return 10;
52+
}
53+
54+
/**
55+
* Returns the default {@link ZoneId} for the current account session.
56+
* <p>
57+
* Attempts to retrieve the account time zone from the current session. If unavailable, returns null.
58+
*
59+
* @return the account's default ZoneId, or null if not available
60+
*/
61+
@Override
62+
public ZoneId getDefaultTimeZone() {
63+
try {
64+
return AccountSessionHolder.get().getAccountTimeZone();
65+
} catch (Exception e) {
66+
return null;
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)