-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MODTLR-141: Create shadow instance in primary request tenant (#104)
* MODTLR-141 Create shadow instance in primary request tenant * MODTLR-141 Replace InventoryItem with Item * MODTLR-141 Share instance through central tenant * MODTLR-141 Fix TenantContext constructor * MODTLR-141 Call instance sharing API in central tenant without system user * MODTLR-141 Call instance sharing API in central tenant without system user * MODTLR-141 Log and throw sharing error * MODTLR-141 Call instance sharing API in target tenant * MODTLR-141 Call instance sharing API in target tenant * MODTLR-141 Refactoring * MODTLR-141 Fix build * MODTLR-141 Extend test * MODTLR-141 Remove redundant import * MODTLR-141 Fix ecs-tlr.yaml * MODTLR-141 Tests for ConsortiumService * MODTLR-141 Refactoring ConsortiumService * MODTLR-141 Refactoring * MODTLR-141 Unit test for instance sharing failure * MODTLR-141 Remove instance sharing permission from module permissions (cherry picked from commit fd6a42a)
- Loading branch information
1 parent
a7e597c
commit 376a00e
Showing
22 changed files
with
619 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.folio.service; | ||
|
||
public interface ConsortiumService { | ||
String getCurrentTenantId(); | ||
String getCurrentConsortiumId(); | ||
String getCentralTenantId(); | ||
boolean isCurrentTenantCentral(); | ||
boolean isCentralTenant(String tenantId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
src/main/java/org/folio/service/impl/ConsortiumServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package org.folio.service.impl; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.folio.service.ConsortiumService; | ||
import org.folio.service.UserTenantsService; | ||
import org.folio.spring.FolioExecutionContext; | ||
import org.springframework.stereotype.Service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Log4j2 | ||
public class ConsortiumServiceImpl implements ConsortiumService { | ||
|
||
private static final Map<String, TenantContext> CACHE = new ConcurrentHashMap<>(); | ||
|
||
private final UserTenantsService userTenantsService; | ||
private final FolioExecutionContext folioContext; | ||
|
||
@Override | ||
public String getCurrentTenantId() { | ||
return folioContext.getTenantId(); | ||
} | ||
|
||
@Override | ||
public String getCurrentConsortiumId() { | ||
return getTenantContext(getCurrentTenantId()) | ||
.consortiumId(); | ||
} | ||
|
||
@Override | ||
public String getCentralTenantId() { | ||
return getTenantContext(getCurrentTenantId()) | ||
.centralTenantId(); | ||
} | ||
|
||
@Override | ||
public boolean isCurrentTenantCentral() { | ||
return isCentralTenant(getCurrentTenantId()); | ||
} | ||
|
||
@Override | ||
public boolean isCentralTenant(String tenantId) { | ||
return getCentralTenantId().equals(tenantId); | ||
} | ||
|
||
private TenantContext getTenantContext(String 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 = 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) { } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.