Skip to content

Commit

Permalink
SELV3-781: Corrected calculation logic, backend validation for import…
Browse files Browse the repository at this point in the history
… of non lowest geo zones
  • Loading branch information
druchniewicz committed Dec 12, 2024
1 parent 881e804 commit 94ea2ba
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

package org.openlmis.referencedata.aspect;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand All @@ -40,11 +38,8 @@
@Component
public class CatchmentPopulationAspect {

@Autowired
private GeographicZoneRepository geographicZoneRepository;

@Autowired
private GeographicLevelRepository geographicLevelRepository;
@Autowired private GeographicZoneRepository geographicZoneRepository;
@Autowired private GeographicLevelRepository geographicLevelRepository;

@Value("${referencedata.catchmentPopulationAutoCalc.enabled}")
private boolean catchmentPopulationAutoCalc;
Expand Down Expand Up @@ -82,13 +77,8 @@ public void afterGeographicZoneSaveAllReturningAdvice(JoinPoint joinPoint) {
}

private void updateCatchmentPopulation(GeographicZone parent) {
List<GeographicLevel> allLevels = StreamSupport.stream(geographicLevelRepository.findAll()
.spliterator(), false).collect(Collectors.toList());
if (shouldCatchmentPopulationBePerformed(parent, allLevels)) {
Optional<GeographicLevel> childLevel = allLevels.stream()
.filter(level ->
Objects.equals(level.getLevelNumber(), parent.getLevel().getLevelNumber() + 1))
.findFirst();
if (catchmentPopulationAutoCalc && parent != null) {
Optional<GeographicLevel> childLevel = findChildLevel(parent);
if (childLevel.isPresent()) {
List<GeographicZone> children = geographicZoneRepository
.findByParentAndLevel(parent, childLevel.get());
Expand All @@ -99,21 +89,18 @@ private void updateCatchmentPopulation(GeographicZone parent) {
}
}

private boolean shouldCatchmentPopulationBePerformed(GeographicZone parent,
List<GeographicLevel> allLevels) {
if (parent == null) {
return false;
}

int lowestLevelNumber = Collections.min(allLevels,
Comparator.comparing(GeographicLevel::getLevelNumber)).getLevelNumber();
int childLevel = parent.getLevel().getLevelNumber() + 1;
private Optional<GeographicLevel> findChildLevel(GeographicZone parent) {
List<GeographicLevel> allLevels = StreamSupport.stream(geographicLevelRepository.findAll()
.spliterator(), false).collect(Collectors.toList());

return childLevel == lowestLevelNumber && catchmentPopulationAutoCalc;
return allLevels.stream()
.filter(level ->
Objects.equals(level.getLevelNumber(), parent.getLevel().getLevelNumber() + 1))
.findFirst();
}

private int sumCatchmentPopulation(List<GeographicZone> geoZones) {
return geoZones.stream()
private int sumCatchmentPopulation(List<GeographicZone> elements) {
return elements.stream()
.map(GeographicZone::getCatchmentPopulation)
.filter(Objects::nonNull)
.mapToInt(Integer::intValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private List<GeographicZone> createListToUpdate(List<GeographicZoneDto> dtoList)
private void validateZone(GeographicZone zone) {
List<GeographicLevel> allLevels = StreamSupport.stream(geographicLevelRepository.findAll()
.spliterator(), false).collect(Collectors.toList());
int lowestLevelNumber = Collections.min(allLevels,
int lowestLevelNumber = Collections.max(allLevels,
Comparator.comparing(GeographicLevel::getLevelNumber)).getLevelNumber();
if (zone.getLevel().getLevelNumber() != lowestLevelNumber) {
throw new ValidationMessageException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class GeographicZoneMessageKeys extends MessageKeys {
public static final String ERROR_EXTRA_DATA_MODIFIED_KEY = join(ERROR, EXTRA_DATA, MODIFIED_KEY);
public static final String ERROR_FIELD_IS_INVARIANT = join(ERROR, FIELD_IS_INVARIANT);
public static final String TRYING_TO_UPDATE_NON_LOWEST_GEOGRAPHIC_ZONE =
"You are trying to update a geographic zone that is not the lowest in the hierarchy";
"youAreTryingToUpdateGeographicZoneThatIsNotTheLowestInTheHierarchy";
public static final String ERROR_TRYING_TO_UPDATE_NON_LOWEST_GEOGRAPHIC_ZONE = join(ERROR,
TRYING_TO_UPDATE_NON_LOWEST_GEOGRAPHIC_ZONE);
}

0 comments on commit 94ea2ba

Please sign in to comment.