-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MODORDERS-929] - Local shadow instance creation for linked instances (…
- Loading branch information
1 parent
7650d82
commit d2cfce1
Showing
19 changed files
with
452 additions
and
32 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
4 changes: 4 additions & 0 deletions
4
src/main/java/org/folio/models/consortium/ConsortiumConfiguration.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,4 @@ | ||
package org.folio.models.consortium; | ||
|
||
public record ConsortiumConfiguration(String centralTenantId, String consortiumId) { | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/org/folio/models/consortium/SharingInstance.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,18 @@ | ||
package org.folio.models.consortium; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
import java.util.UUID; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record SharingInstance(UUID id, | ||
UUID instanceIdentifier, | ||
String sourceTenantId, | ||
String targetTenantId, | ||
SharingStatus status, | ||
String error) { | ||
public SharingInstance(UUID instanceIdentifier, String sourceTenantId, String targetTenantId) { | ||
this(null, instanceIdentifier, sourceTenantId, targetTenantId, null, null); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/org/folio/models/consortium/SharingStatus.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,38 @@ | ||
package org.folio.models.consortium; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
|
||
public enum SharingStatus { | ||
COMPLETE("COMPLETE"), | ||
|
||
ERROR("ERROR"), | ||
|
||
IN_PROGRESS("IN_PROGRESS"); | ||
|
||
private final String value; | ||
|
||
SharingStatus(String value) { | ||
this.value = value; | ||
} | ||
|
||
@JsonValue | ||
public String getValue() { | ||
return value; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(value); | ||
} | ||
|
||
@JsonCreator | ||
public static SharingStatus fromValue(String value) { | ||
for (SharingStatus b : SharingStatus.values()) { | ||
if (b.value.equals(value)) { | ||
return b; | ||
} | ||
} | ||
throw new IllegalArgumentException("Unexpected value '" + value + "'"); | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/org/folio/rest/core/exceptions/ConsortiumException.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,10 @@ | ||
package org.folio.rest.core.exceptions; | ||
|
||
/** | ||
* Exception that used for consortium process | ||
*/ | ||
public class ConsortiumException extends RuntimeException { | ||
public ConsortiumException(String message) { | ||
super(message); | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/main/java/org/folio/service/consortium/ConsortiumConfigurationService.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,70 @@ | ||
package org.folio.service.consortium; | ||
|
||
import com.github.benmanes.caffeine.cache.AsyncCache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Vertx; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.folio.models.consortium.ConsortiumConfiguration; | ||
import org.folio.rest.core.RestClient; | ||
import org.folio.rest.core.models.RequestContext; | ||
import org.folio.rest.core.models.RequestEntry; | ||
import org.folio.rest.tools.utils.TenantTool; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public class ConsortiumConfigurationService { | ||
private static final Logger logger = LogManager.getLogger(ConsortiumConfigurationService.class); | ||
|
||
private static final String CONSORTIUM_ID_FIELD = "consortiumId"; | ||
private static final String CENTRAL_TENANT_ID_FIELD = "centralTenantId"; | ||
private static final String USER_TENANTS_ARRAY_IDENTIFIER = "userTenants"; | ||
private static final String USER_TENANTS_ENDPOINT = "/user-tenants"; | ||
|
||
@Value("${orders.cache.consortium-data.expiration.time.seconds:300}") | ||
private long cacheExpirationTime; | ||
|
||
private final RestClient restClient; | ||
private final AsyncCache<String, Optional<ConsortiumConfiguration>> asyncCache; | ||
|
||
public ConsortiumConfigurationService(RestClient restClient) { | ||
this.restClient = restClient; | ||
|
||
asyncCache = Caffeine.newBuilder() | ||
.expireAfterWrite(cacheExpirationTime, TimeUnit.SECONDS) | ||
.executor(task -> Vertx.currentContext().runOnContext(v -> task.run())) | ||
.buildAsync(); | ||
} | ||
|
||
public Future<Optional<ConsortiumConfiguration>> getConsortiumConfiguration(RequestContext requestContext) { | ||
try { | ||
var cacheKey = TenantTool.tenantId(requestContext.getHeaders()); | ||
return Future.fromCompletionStage(asyncCache.get(cacheKey, (key, executor) -> | ||
getConsortiumConfigurationFromRemote(requestContext))); | ||
} catch (Exception e) { | ||
logger.error("Error when retrieving consortium configuration", e); | ||
return Future.failedFuture(e); | ||
} | ||
} | ||
|
||
private CompletableFuture<Optional<ConsortiumConfiguration>> getConsortiumConfigurationFromRemote(RequestContext requestContext) { | ||
RequestEntry requestEntry = new RequestEntry(USER_TENANTS_ENDPOINT).withLimit(1); | ||
return restClient.getAsJsonObject(requestEntry, requestContext) | ||
.map(jsonObject -> jsonObject.getJsonArray(USER_TENANTS_ARRAY_IDENTIFIER)) | ||
.map(userTenants -> { | ||
if (userTenants.isEmpty()) { | ||
logger.debug("Central tenant and consortium id not found"); | ||
return Optional.<ConsortiumConfiguration>empty(); | ||
} | ||
String consortiumId = userTenants.getJsonObject(0).getString(CONSORTIUM_ID_FIELD); | ||
String centralTenantId = userTenants.getJsonObject(0).getString(CENTRAL_TENANT_ID_FIELD); | ||
logger.debug("Found centralTenantId: {} and consortiumId: {}", centralTenantId, consortiumId); | ||
return Optional.of(new ConsortiumConfiguration(centralTenantId, consortiumId)); | ||
}).toCompletionStage().toCompletableFuture(); | ||
} | ||
|
||
} |
Oops, something went wrong.