Skip to content

Commit

Permalink
MODTLR-141 Refactoring ConsortiumService
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrVidinieiev committed Feb 17, 2025
1 parent 7824622 commit f7f09b7
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/main/java/org/folio/service/impl/ConsortiumServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

import org.folio.domain.dto.UserTenant;
import org.folio.service.ConsortiumService;
import org.folio.service.UserTenantsService;
import org.folio.spring.FolioExecutionContext;
Expand Down Expand Up @@ -51,31 +50,30 @@ public boolean isCentralTenant(String tenantId) {
}

private TenantContext getTenantContext(String tenantId) {
TenantContext tenantContext;
if (CACHE.containsKey(tenantId)) {
tenantContext = CACHE.get(tenantId);
TenantContext tenantContext = CACHE.get(tenantId);
if (tenantContext != null) {
log.info("getTenantContext:: cache hit: {}", tenantContext);
} else {
log.info("getTenantContext:: cache miss for tenant {}", tenantId);
tenantContext = Optional.ofNullable(userTenantsService.findFirstUserTenant())
.map(userTenant -> new TenantContext(tenantId, userTenant))
.orElseThrow(() -> new IllegalStateException("Failed to fetch user tenant"));
log.info("getTenantContext:: populating cache: {}", tenantContext);
tenantContext = buildTenantContext(tenantId);
log.info("getTenantContext:: caching: {}", tenantContext);
CACHE.put(tenantId, tenantContext);
}
log.debug("getTenantContext:: cache: {}", CACHE);
return tenantContext;
}

private TenantContext buildTenantContext(String tenantId) {
return Optional.ofNullable(userTenantsService.findFirstUserTenant())
.map(ut -> new TenantContext(tenantId, ut.getConsortiumId(), ut.getCentralTenantId()))
.orElseThrow(() -> new IllegalStateException("Failed to fetch user tenant"));
}

public static void clearCache() {
log.info("clearCache:: clearing cache");
CACHE.clear();
}

private record TenantContext(String tenantId, String consortiumId, String centralTenantId) {
public TenantContext(String tenantId, UserTenant userTenant) {
this(tenantId, userTenant.getConsortiumId(), userTenant.getCentralTenantId());
}
}
private record TenantContext(String tenantId, String consortiumId, String centralTenantId) { }

}

0 comments on commit f7f09b7

Please sign in to comment.