Skip to content

Commit

Permalink
CIRC-2100 Changes made as per the review comments and refactoring code
Browse files Browse the repository at this point in the history
  • Loading branch information
Vignesh-kalyanasundaram committed Jul 31, 2024
1 parent 140a354 commit ba38f05
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static org.folio.circulation.resources.PrintEventsResource.PRINT_EVENT_FLAG_PROPERTY_NAME;
import static org.folio.circulation.resources.PrintEventsResource.PRINT_EVENT_FLAG_QUERY;
import static org.folio.circulation.support.http.ResponseMapping.forwardOnFailure;
import static org.folio.circulation.support.json.JsonPropertyFetcher.getProperty;
import static org.folio.circulation.support.results.Result.of;
Expand All @@ -28,7 +31,8 @@

public class PrintEventsRepository {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());

private static final String RECORDS_PROPERTY_NAME = "printEventsStatusResponses";
private static final String REQUEST_IDS = "requestIds";
private final CollectionResourceClient printEventsStorageClient;
private final CollectionResourceClient printEventsStorageStatusClient;
private final CirculationSettingsRepository circulationSettingsRepository;
Expand All @@ -52,49 +56,41 @@ public CompletableFuture<Result<MultipleRecords<Request>>> findPrintEventDetails
MultipleRecords<Request> multipleRequests) {
log.debug("findPrintEventDetails:: parameters multipleRequests: {}",
() -> multipleRecordsAsString(multipleRequests));
return validatePrintEventFeatureFlag()
.thenCompose(isEnabled -> {
if (Boolean.TRUE.equals(isEnabled)) {
log.info("findPrintEventDetails:: printEvent feature is enabled for the tenant");
return fetchAndMapPrintEventDetails(multipleRequests);
} else {
log.info("findPrintEventDetails:: printEvent feature is disabled for the tenant");
return completedFuture(succeeded(multipleRequests));
}
});
}

private CompletableFuture<Result<MultipleRecords<Request>>> fetchAndMapPrintEventDetails(
MultipleRecords<Request> multipleRequests) {
log.debug("fetchAndMapPrintEventDetails:: parameters multipleRequests: {}",
() -> multipleRecordsAsString(multipleRequests));
var requestIds = multipleRequests.toKeys(Request::getId);
if (requestIds.isEmpty()) {
log.info("fetchAndMapPrintEventDetails:: No request id found");
return completedFuture(succeeded(multipleRequests));
}
return validatePrintEventFeatureFlag()
.thenCompose(isEnabled -> Boolean.TRUE.equals(isEnabled)
? fetchAndMapPrintEventDetails(multipleRequests, requestIds)
: completedFuture(succeeded(multipleRequests)));
}

private CompletableFuture<Result<MultipleRecords<Request>>> fetchAndMapPrintEventDetails(
MultipleRecords<Request> multipleRequests, Set<String> requestIds) {
log.debug("fetchAndMapPrintEventDetails:: parameters multipleRequests: {}, requestIds {}",
() -> multipleRecordsAsString(multipleRequests), () -> requestIds);
return fetchPrintDetailsByRequestIds(requestIds)
.thenApply(printEventRecordsResult -> printEventRecordsResult
.next(printEventRecords -> mapPrintEventDetailsToRequest(printEventRecords, multipleRequests)));
}

private CompletableFuture<Boolean> validatePrintEventFeatureFlag() {
log.debug("validatePrintEventFeatureFlag:: Fetching and validating enablePrintLog flag from settings");
return circulationSettingsRepository.findBy("query=name=printEventLogFeature")
return circulationSettingsRepository.findBy(PRINT_EVENT_FLAG_QUERY)
.thenApply(res -> Optional.ofNullable(res.value())
.map(records -> records.getRecords().stream()
.map(setting -> Boolean.valueOf(getProperty(setting.getValue(), "enablePrintLog")))
.findFirst()
.orElse(false))
.flatMap(records -> records.getRecords().stream().findFirst())
.map(setting -> Boolean.valueOf(getProperty(setting.getValue(), PRINT_EVENT_FLAG_PROPERTY_NAME)))
.orElse(false));
}

private CompletableFuture<Result<MultipleRecords<PrintEventDetail>>> fetchPrintDetailsByRequestIds
(Collection<String> requestIds) {
log.debug("fetchPrintDetailsByRequestIds:: fetching print event details for requestIds {}", requestIds);
return printEventsStorageStatusClient.post(new JsonObject().put("requestIds", requestIds))
return printEventsStorageStatusClient.post(new JsonObject().put(REQUEST_IDS, requestIds))
.thenApply(flatMapResult(response ->
MultipleRecords.from(response, PrintEventDetail::from, "printEventsStatusResponses")));
MultipleRecords.from(response, PrintEventDetail::from, RECORDS_PROPERTY_NAME)));
}

private Result<MultipleRecords<Request>> mapPrintEventDetailsToRequest(
Expand All @@ -106,5 +102,4 @@ private Result<MultipleRecords<Request>> mapPrintEventDetailsToRequest(
requests.mapRecords(request -> request
.withPrintEventDetail(printEventDetailMap.getOrDefault(request.getId(), null))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@

public class PrintEventsResource extends Resource {
private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass());
private static final String PRINT_EVENT_FLAG_QUERY = "query=name=printEventLogFeature";
public static final String PRINT_EVENT_FLAG_QUERY = "query=name=printEventLogFeature";
private static final String PRINT_EVENT_FEATURE_DISABLED_ERROR = "print event feature is disabled for this tenant";
private static final String NO_CONFIG_FOUND_ERROR = "No configuration found for print event feature";
private static final String MULTIPLE_CONFIGS_ERROR = "Multiple configurations found for print event feature";
private static final String PRINT_EVENT_FLAG_PROPERTY_NAME = "enablePrintLog";
public static final String PRINT_EVENT_FLAG_PROPERTY_NAME = "enablePrintLog";

public PrintEventsResource(HttpClient client) {
super(client);
Expand Down

0 comments on commit ba38f05

Please sign in to comment.