Skip to content

Commit

Permalink
Fix sonar errors
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanChernetskyi committed Jun 21, 2024
1 parent 18fbd31 commit 22956e7
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/main/java/org/folio/inventory/resources/UpdateOwnershipApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class UpdateOwnershipApi extends AbstractInventoryResource {
public static final String INSTANCE_NOT_FOUND_AT_SOURCE_TENANT = "Instance with id: %s not found at source tenant, tenant: %s";
public static final String TENANT_NOT_IN_CONSORTIA = "%s tenant is not in consortia";
public static final String HOLDINGS_NOT_FOUND = "HoldingsRecord with id: %s not found on tenant: %s";
public static final String LOG_UPDATE_HOLDINGS_OWNERSHIP = "updateHoldingsOwnership:: %s";

private final ConsortiumService consortiumService;

Expand Down Expand Up @@ -96,18 +97,18 @@ private void processUpdateHoldingsOwnership(RoutingContext routingContext) {
return updateOwnershipOfHoldingsRecords(holdingsUpdateOwnership, notUpdatedEntities, routingContext, context, targetTenantContext);
} else {
String instanceNotSharedErrorMessage = String.format(INSTANCE_NOT_SHARED, holdingsUpdateOwnership.getToInstanceId());
LOGGER.warn("updateHoldingsOwnership:: " + instanceNotSharedErrorMessage);
LOGGER.warn(String.format(LOG_UPDATE_HOLDINGS_OWNERSHIP, instanceNotSharedErrorMessage));
return CompletableFuture.failedFuture(new BadRequestException(instanceNotSharedErrorMessage));
}
} else {
String instanceNotFoundErrorMessage = String.format(INSTANCE_NOT_FOUND_AT_SOURCE_TENANT, holdingsUpdateOwnership.getToInstanceId(), context.getTenantId());
LOGGER.warn("updateHoldingsOwnership:: " + instanceNotFoundErrorMessage);
LOGGER.warn(String.format(LOG_UPDATE_HOLDINGS_OWNERSHIP, instanceNotFoundErrorMessage));
return CompletableFuture.failedFuture(new NotFoundException(instanceNotFoundErrorMessage));
}
});
}
String notInConsortiaErrorMessage = String.format(TENANT_NOT_IN_CONSORTIA, context.getTenantId());
LOGGER.warn("updateHoldingsOwnership:: " + notInConsortiaErrorMessage, context);
LOGGER.warn(String.format(LOG_UPDATE_HOLDINGS_OWNERSHIP, notInConsortiaErrorMessage));
return CompletableFuture.failedFuture(new BadRequestException(notInConsortiaErrorMessage));
})
.thenAccept(v -> respond(routingContext, notUpdatedEntities))
Expand Down Expand Up @@ -143,8 +144,8 @@ private CompletableFuture<List<String>> updateOwnershipOfHoldingsRecords(Holding

return holdingsRecordFetchClient.find(holdingsUpdateOwnership.getHoldingsRecordIds(), MoveApiUtil::fetchByIdCql)
.thenCompose(jsons -> {
LOGGER.info("updateOwnershipOfHoldingsRecords:: Founded holdings to update ownership: {}", jsons);
processNotFoundedInstances(holdingsUpdateOwnership.getHoldingsRecordIds(), notUpdatedEntities, context, jsons);
LOGGER.info("updateOwnershipOfHoldingsRecords:: Found holdings to update ownership: {}", jsons);
processNotFoundInstances(holdingsUpdateOwnership.getHoldingsRecordIds(), notUpdatedEntities, context, jsons);
if (!jsons.isEmpty()) {
return createHoldings(jsons, notUpdatedEntities, holdingsUpdateOwnership.getToInstanceId(), targetTenantHoldingsRecordCollection)
.thenCompose(createdHoldings -> {
Expand Down Expand Up @@ -217,8 +218,9 @@ private CompletableFuture<List<Item>> createItems(List<JsonObject> jsons, List<N

private CompletableFuture<List<HoldingsRecord>> createHoldings(List<JsonObject> jsons, List<NotUpdatedEntity> notUpdatedEntities, String instanceId,
HoldingsRecordCollection holdingsRecordCollection) {
jsons.forEach(MoveApiUtil::removeExtraRedundantFields);

List<HoldingsRecord> holdingsRecordsToUpdateOwnership = jsons.stream()
.peek(MoveApiUtil::removeExtraRedundantFields)
.map(json -> json.mapTo(HoldingsRecord.class).withHrid(null))
.filter(holdingsRecord -> holdingsRecord.getInstanceId().equals(instanceId))
.toList();
Expand Down Expand Up @@ -263,8 +265,8 @@ private CompletableFuture<List<String>> deleteHoldings(List<HoldingsRecord> hold
.toList());
}

private CompletableFuture<List<String>> deleteItems(List<Item> itemIds, List<NotUpdatedEntity> notUpdatedEntities, ItemCollection itemCollection) {
List<CompletableFuture<String>> deleteFutures = itemIds.stream()
private CompletableFuture<List<String>> deleteItems(List<Item> items, List<NotUpdatedEntity> notUpdatedEntities, ItemCollection itemCollection) {
List<CompletableFuture<String>> deleteFutures = items.stream()
.map(item -> {
Promise<String> promise = Promise.promise();
itemCollection.delete(item.getId(), success -> promise.complete(item.getId()),
Expand All @@ -285,12 +287,12 @@ private CompletableFuture<List<String>> deleteItems(List<Item> itemIds, List<Not
.toList());
}

private void processNotFoundedInstances(List<String> holdingsRecordIds, List<NotUpdatedEntity> notUpdatedEntities, WebContext context, List<JsonObject> jsons) {
List<String> foundedIds = jsons.stream().map(json -> json.getString("id")).toList();
List<String> notFoundedIds = ListUtils.subtract(holdingsRecordIds, foundedIds);
notFoundedIds.forEach(id -> {
private void processNotFoundInstances(List<String> holdingsRecordIds, List<NotUpdatedEntity> notUpdatedEntities, WebContext context, List<JsonObject> jsons) {
List<String> foundIds = jsons.stream().map(json -> json.getString("id")).toList();
List<String> notFoundIds = ListUtils.subtract(holdingsRecordIds, foundIds);
notFoundIds.forEach(id -> {
String errorMessage = String.format(HOLDINGS_NOT_FOUND, id, context.getTenantId());
LOGGER.warn("processNotFoundedInstances:: " + errorMessage);
LOGGER.warn(String.format("processNotFoundInstances:: %s", errorMessage));
notUpdatedEntities.add(new NotUpdatedEntity().withEntityId(id).withErrorMessage(errorMessage));
});
}
Expand Down

0 comments on commit 22956e7

Please sign in to comment.