Skip to content

Commit

Permalink
[MODORDERS-1165] Populate piece receivingTenentId when openning order…
Browse files Browse the repository at this point in the history
… with Instance, Holding (#999)

* [MODORDERS-1165] Populate piece receivingTenentId when openning order with Instance, Holding

* [MODORDERS-1165] Check for null tenantIds

* [MODORDERS-1165] Refactor location collectors

* [MODORDERS-1165] Refactor for more code clarity

* [MODORDERS-1165] Minor adjustments

* [MODORDERS-1165] Refactor calculatePiecesWithoutItemIdQuantity

* [MODORDERS-1165] Remove redundant/repeated code

* [MODORDERS-1165] Fix filtering logic for holdingIds

* [MODORDERS-1165] Refactor OpenCompositeOrderPieceService

* [MODORDERS-1165] Fix failing test

* [MODORDERS-1165] Attempt increase coverage

* [MODORDERS-1165] Add unit test for update scenario

* [MODORDERS-1165] Improve logging and readability

* [MODORDERS-1165] Update tests

---------

Co-authored-by: Serhii Nosko <[email protected]>
  • Loading branch information
Saba-Zedginidze-EPAM and SerhiiNosko authored Aug 22, 2024
1 parent 573134f commit 77571bc
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 209 deletions.
61 changes: 44 additions & 17 deletions src/main/java/org/folio/orders/utils/PoLineCommonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@
import static org.folio.rest.jaxrs.model.PoLine.ReceiptStatus.ONGOING;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import one.util.streamex.StreamEx;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.folio.rest.core.exceptions.HttpException;
Expand Down Expand Up @@ -182,32 +183,58 @@ public static boolean isHoldingCreationRequiredForLocation(CompositePoLine compP
* @return map of grouped locations where key is location id and value is list of locations with the same id
*/
public static Map<String, List<Location>> groupLocationsByLocationId(CompositePoLine compPOL) {
if (CollectionUtils.isEmpty(compPOL.getLocations())) {
return Collections.emptyMap();
}
return extractLocationsForPoLineByLocationId(compPOL)
.collect(Collectors.groupingBy(Location::getLocationId));
}

return compPOL.getLocations()
.stream()
.filter(location -> Objects.nonNull(location.getLocationId()))
.filter(location -> !isHoldingCreationRequiredForLocation(compPOL, location))
.collect(Collectors.groupingBy(Location::getLocationId));
/**
* Map all PO Line's location to tenantIds for which the holding should be created by location identifier
* @param compPOL PO line with locations
* @return map of locations and tenantIds where key is location id and value is the tenantId of the specified location
*/
public static Map<String, String> mapLocationIdsToTenantIds(CompositePoLine compPOL) {
return extractLocationsForPoLineByLocationId(compPOL)
.filter(location -> Objects.nonNull(location.getTenantId()))
.collect(Collectors.toMap(Location::getLocationId, Location::getTenantId));
}

/**
* Group all PO Line's locations for which the holding should be created by location identifier
* Group all PO Line's locations for which the holding should be created by holding identifier
* @param compPOL PO line with locations to group
* @return map of grouped locations where key is holding id and value is list of locations with the same id
*/
public static Map<String, List<Location>> groupLocationsByHoldingId(CompositePoLine compPOL) {
return extractLocationsForPoLineByHoldingId(compPOL)
.collect(Collectors.groupingBy(Location::getHoldingId));
}

/**
* Map all PO Line's location to tenantIds for which the holding should be created by holding identifier
* @param compPOL PO line with locations
* @return map of locations and tenantIds where key is holding id and value is the tenantId of the specified location
*/
public static Map<String, String> mapHoldingIdsToTenantIds(CompositePoLine compPOL) {
return extractLocationsForPoLineByHoldingId(compPOL)
.filter(location -> Objects.nonNull(location.getTenantId()))
.collect(Collectors.toMap(Location::getHoldingId, Location::getTenantId));
}

private static StreamEx<Location> extractLocationsForPoLineByLocationId(CompositePoLine compPOL) {
return extractLocationsForPoLine(compPOL, Location::getLocationId)
.filter(location -> !isHoldingCreationRequiredForLocation(compPOL, location));
}

private static StreamEx<Location> extractLocationsForPoLineByHoldingId(CompositePoLine compPOL) {
return extractLocationsForPoLine(compPOL, Location::getHoldingId)
.filter(location -> isHoldingCreationRequiredForLocation(compPOL, location));
}

private static StreamEx<Location> extractLocationsForPoLine(CompositePoLine compPOL, Function<Location, String> fieldExtractor) {
if (CollectionUtils.isEmpty(compPOL.getLocations())) {
return Collections.emptyMap();
return StreamEx.empty();
}

return compPOL.getLocations()
.stream()
.filter(location -> Objects.nonNull(location.getHoldingId()))
.filter(location -> isHoldingCreationRequiredForLocation(compPOL, location))
.collect(Collectors.groupingBy(Location::getHoldingId));
return StreamEx.of(compPOL.getLocations())
.filter(location -> Objects.nonNull(fieldExtractor.apply(location)));
}

public static List<String> getTenantsFromLocations(CompositePoLine poLine) {
Expand Down
Loading

0 comments on commit 77571bc

Please sign in to comment.