@Test public void shouldDerivePopulationForRegionalVaccineStoreFromChildFacilityPopulations() { Long returnedPopulation = populationService.getPopulation(rvs1, program, populationSourceId_1); Long dvs1Population = dvs1.getId() * populationSourceId_1 * 100; Long dvs2Population = dvs2.getId() * populationSourceId_1 * 100; Long expectedPopulation = dvs1Population + dvs2Population; assertThat(returnedPopulation, is(expectedPopulation)); }
public Long getPopulation(Facility facility, Program program, Long populationSource) { if(program == null) return getNonNullFacilityCatchmentPopulation(facility); int currentYear = Calendar.getInstance().get(Calendar.YEAR); String facilityCode = facility.getFacilityType().getCode().toLowerCase(); if (facilityCode.equals("heac") || facilityCode.equals("disp")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); List<AnnualFacilityEstimateEntry> estimates = annualFacilityDemographicEstimateService.getEstimateValuesForFacility(facility.getId(), program.getId(), currentYear); for (AnnualFacilityEstimateEntry estimate : estimates) { if (estimate.getDemographicEstimateId() != null && estimate.getDemographicEstimateId().equals(populationSource)) { if(estimate.getValue() != null) return estimate.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } } } else if(facilityCode.equals("dvs")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); GeographicZone geoZone = facility.getGeographicZone(); if(geoZone == null) return getNonNullFacilityCatchmentPopulation(facility); AnnualDistrictEstimateEntry estimateEntry = annualDistrictEstimateRepository.getEntryBy(currentYear, geoZone.getId(), program.getId(), populationSource); if(estimateEntry != null) return estimateEntry.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } else if(facilityCode.equals("rvs")) { SupervisoryNode supNodeForReqGroup = supervisoryNodeRepository.getFor(facility, program); List<SupervisoryNode> childNodes = supervisoryNodeRepository.getAllChildSupervisoryNodesInHierarchy(supNodeForReqGroup); SupervisoryNode childNode = getSupervisoryNodeAssociatedWithFacility(childNodes, facility); if(childNode == null) return getNonNullFacilityCatchmentPopulation(facility); List<RequisitionGroup> requisitionGroups = requisitionGroupRepository.getRequisitionGroups(Collections.singletonList(childNode)); if(requisitionGroups == null || requisitionGroups.size() < 1) return getNonNullFacilityCatchmentPopulation(facility); RequisitionGroup requisitionGroup = requisitionGroups.get(0); List<Facility> facilities = getFacilitiesInSpecifiedRequisitionGroup(requisitionGroup, program.getId()); Long totalPopulation = 0L; for(Facility childFacility : facilities) { totalPopulation += getPopulation(childFacility, program, populationSource); } return totalPopulation; } else if(facilityCode.equals("cvs")) { List<Facility> facilities = facilityRepository.getAllByFacilityTypeCode("rvs"); Long totalPopulation = 0L; for(Facility rvs : facilities) { totalPopulation += getPopulation(rvs, program, populationSource); } if(totalPopulation.intValue() > 0) return totalPopulation; else return getNonNullFacilityCatchmentPopulation(facility); } return getNonNullFacilityCatchmentPopulation(facility); }
PopulationService { public Long getPopulation(Facility facility, Program program, Long populationSource) { if(program == null) return getNonNullFacilityCatchmentPopulation(facility); int currentYear = Calendar.getInstance().get(Calendar.YEAR); String facilityCode = facility.getFacilityType().getCode().toLowerCase(); if (facilityCode.equals("heac") || facilityCode.equals("disp")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); List<AnnualFacilityEstimateEntry> estimates = annualFacilityDemographicEstimateService.getEstimateValuesForFacility(facility.getId(), program.getId(), currentYear); for (AnnualFacilityEstimateEntry estimate : estimates) { if (estimate.getDemographicEstimateId() != null && estimate.getDemographicEstimateId().equals(populationSource)) { if(estimate.getValue() != null) return estimate.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } } } else if(facilityCode.equals("dvs")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); GeographicZone geoZone = facility.getGeographicZone(); if(geoZone == null) return getNonNullFacilityCatchmentPopulation(facility); AnnualDistrictEstimateEntry estimateEntry = annualDistrictEstimateRepository.getEntryBy(currentYear, geoZone.getId(), program.getId(), populationSource); if(estimateEntry != null) return estimateEntry.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } else if(facilityCode.equals("rvs")) { SupervisoryNode supNodeForReqGroup = supervisoryNodeRepository.getFor(facility, program); List<SupervisoryNode> childNodes = supervisoryNodeRepository.getAllChildSupervisoryNodesInHierarchy(supNodeForReqGroup); SupervisoryNode childNode = getSupervisoryNodeAssociatedWithFacility(childNodes, facility); if(childNode == null) return getNonNullFacilityCatchmentPopulation(facility); List<RequisitionGroup> requisitionGroups = requisitionGroupRepository.getRequisitionGroups(Collections.singletonList(childNode)); if(requisitionGroups == null || requisitionGroups.size() < 1) return getNonNullFacilityCatchmentPopulation(facility); RequisitionGroup requisitionGroup = requisitionGroups.get(0); List<Facility> facilities = getFacilitiesInSpecifiedRequisitionGroup(requisitionGroup, program.getId()); Long totalPopulation = 0L; for(Facility childFacility : facilities) { totalPopulation += getPopulation(childFacility, program, populationSource); } return totalPopulation; } else if(facilityCode.equals("cvs")) { List<Facility> facilities = facilityRepository.getAllByFacilityTypeCode("rvs"); Long totalPopulation = 0L; for(Facility rvs : facilities) { totalPopulation += getPopulation(rvs, program, populationSource); } if(totalPopulation.intValue() > 0) return totalPopulation; else return getNonNullFacilityCatchmentPopulation(facility); } return getNonNullFacilityCatchmentPopulation(facility); } }
PopulationService { public Long getPopulation(Facility facility, Program program, Long populationSource) { if(program == null) return getNonNullFacilityCatchmentPopulation(facility); int currentYear = Calendar.getInstance().get(Calendar.YEAR); String facilityCode = facility.getFacilityType().getCode().toLowerCase(); if (facilityCode.equals("heac") || facilityCode.equals("disp")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); List<AnnualFacilityEstimateEntry> estimates = annualFacilityDemographicEstimateService.getEstimateValuesForFacility(facility.getId(), program.getId(), currentYear); for (AnnualFacilityEstimateEntry estimate : estimates) { if (estimate.getDemographicEstimateId() != null && estimate.getDemographicEstimateId().equals(populationSource)) { if(estimate.getValue() != null) return estimate.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } } } else if(facilityCode.equals("dvs")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); GeographicZone geoZone = facility.getGeographicZone(); if(geoZone == null) return getNonNullFacilityCatchmentPopulation(facility); AnnualDistrictEstimateEntry estimateEntry = annualDistrictEstimateRepository.getEntryBy(currentYear, geoZone.getId(), program.getId(), populationSource); if(estimateEntry != null) return estimateEntry.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } else if(facilityCode.equals("rvs")) { SupervisoryNode supNodeForReqGroup = supervisoryNodeRepository.getFor(facility, program); List<SupervisoryNode> childNodes = supervisoryNodeRepository.getAllChildSupervisoryNodesInHierarchy(supNodeForReqGroup); SupervisoryNode childNode = getSupervisoryNodeAssociatedWithFacility(childNodes, facility); if(childNode == null) return getNonNullFacilityCatchmentPopulation(facility); List<RequisitionGroup> requisitionGroups = requisitionGroupRepository.getRequisitionGroups(Collections.singletonList(childNode)); if(requisitionGroups == null || requisitionGroups.size() < 1) return getNonNullFacilityCatchmentPopulation(facility); RequisitionGroup requisitionGroup = requisitionGroups.get(0); List<Facility> facilities = getFacilitiesInSpecifiedRequisitionGroup(requisitionGroup, program.getId()); Long totalPopulation = 0L; for(Facility childFacility : facilities) { totalPopulation += getPopulation(childFacility, program, populationSource); } return totalPopulation; } else if(facilityCode.equals("cvs")) { List<Facility> facilities = facilityRepository.getAllByFacilityTypeCode("rvs"); Long totalPopulation = 0L; for(Facility rvs : facilities) { totalPopulation += getPopulation(rvs, program, populationSource); } if(totalPopulation.intValue() > 0) return totalPopulation; else return getNonNullFacilityCatchmentPopulation(facility); } return getNonNullFacilityCatchmentPopulation(facility); }  }
PopulationService { public Long getPopulation(Facility facility, Program program, Long populationSource) { if(program == null) return getNonNullFacilityCatchmentPopulation(facility); int currentYear = Calendar.getInstance().get(Calendar.YEAR); String facilityCode = facility.getFacilityType().getCode().toLowerCase(); if (facilityCode.equals("heac") || facilityCode.equals("disp")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); List<AnnualFacilityEstimateEntry> estimates = annualFacilityDemographicEstimateService.getEstimateValuesForFacility(facility.getId(), program.getId(), currentYear); for (AnnualFacilityEstimateEntry estimate : estimates) { if (estimate.getDemographicEstimateId() != null && estimate.getDemographicEstimateId().equals(populationSource)) { if(estimate.getValue() != null) return estimate.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } } } else if(facilityCode.equals("dvs")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); GeographicZone geoZone = facility.getGeographicZone(); if(geoZone == null) return getNonNullFacilityCatchmentPopulation(facility); AnnualDistrictEstimateEntry estimateEntry = annualDistrictEstimateRepository.getEntryBy(currentYear, geoZone.getId(), program.getId(), populationSource); if(estimateEntry != null) return estimateEntry.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } else if(facilityCode.equals("rvs")) { SupervisoryNode supNodeForReqGroup = supervisoryNodeRepository.getFor(facility, program); List<SupervisoryNode> childNodes = supervisoryNodeRepository.getAllChildSupervisoryNodesInHierarchy(supNodeForReqGroup); SupervisoryNode childNode = getSupervisoryNodeAssociatedWithFacility(childNodes, facility); if(childNode == null) return getNonNullFacilityCatchmentPopulation(facility); List<RequisitionGroup> requisitionGroups = requisitionGroupRepository.getRequisitionGroups(Collections.singletonList(childNode)); if(requisitionGroups == null || requisitionGroups.size() < 1) return getNonNullFacilityCatchmentPopulation(facility); RequisitionGroup requisitionGroup = requisitionGroups.get(0); List<Facility> facilities = getFacilitiesInSpecifiedRequisitionGroup(requisitionGroup, program.getId()); Long totalPopulation = 0L; for(Facility childFacility : facilities) { totalPopulation += getPopulation(childFacility, program, populationSource); } return totalPopulation; } else if(facilityCode.equals("cvs")) { List<Facility> facilities = facilityRepository.getAllByFacilityTypeCode("rvs"); Long totalPopulation = 0L; for(Facility rvs : facilities) { totalPopulation += getPopulation(rvs, program, populationSource); } if(totalPopulation.intValue() > 0) return totalPopulation; else return getNonNullFacilityCatchmentPopulation(facility); } return getNonNullFacilityCatchmentPopulation(facility); }  Long getPopulation(Facility facility, Program program, Long populationSource); }
PopulationService { public Long getPopulation(Facility facility, Program program, Long populationSource) { if(program == null) return getNonNullFacilityCatchmentPopulation(facility); int currentYear = Calendar.getInstance().get(Calendar.YEAR); String facilityCode = facility.getFacilityType().getCode().toLowerCase(); if (facilityCode.equals("heac") || facilityCode.equals("disp")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); List<AnnualFacilityEstimateEntry> estimates = annualFacilityDemographicEstimateService.getEstimateValuesForFacility(facility.getId(), program.getId(), currentYear); for (AnnualFacilityEstimateEntry estimate : estimates) { if (estimate.getDemographicEstimateId() != null && estimate.getDemographicEstimateId().equals(populationSource)) { if(estimate.getValue() != null) return estimate.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } } } else if(facilityCode.equals("dvs")) { if(populationSource == null) return getNonNullFacilityCatchmentPopulation(facility); GeographicZone geoZone = facility.getGeographicZone(); if(geoZone == null) return getNonNullFacilityCatchmentPopulation(facility); AnnualDistrictEstimateEntry estimateEntry = annualDistrictEstimateRepository.getEntryBy(currentYear, geoZone.getId(), program.getId(), populationSource); if(estimateEntry != null) return estimateEntry.getValue(); else return getNonNullFacilityCatchmentPopulation(facility); } else if(facilityCode.equals("rvs")) { SupervisoryNode supNodeForReqGroup = supervisoryNodeRepository.getFor(facility, program); List<SupervisoryNode> childNodes = supervisoryNodeRepository.getAllChildSupervisoryNodesInHierarchy(supNodeForReqGroup); SupervisoryNode childNode = getSupervisoryNodeAssociatedWithFacility(childNodes, facility); if(childNode == null) return getNonNullFacilityCatchmentPopulation(facility); List<RequisitionGroup> requisitionGroups = requisitionGroupRepository.getRequisitionGroups(Collections.singletonList(childNode)); if(requisitionGroups == null || requisitionGroups.size() < 1) return getNonNullFacilityCatchmentPopulation(facility); RequisitionGroup requisitionGroup = requisitionGroups.get(0); List<Facility> facilities = getFacilitiesInSpecifiedRequisitionGroup(requisitionGroup, program.getId()); Long totalPopulation = 0L; for(Facility childFacility : facilities) { totalPopulation += getPopulation(childFacility, program, populationSource); } return totalPopulation; } else if(facilityCode.equals("cvs")) { List<Facility> facilities = facilityRepository.getAllByFacilityTypeCode("rvs"); Long totalPopulation = 0L; for(Facility rvs : facilities) { totalPopulation += getPopulation(rvs, program, populationSource); } if(totalPopulation.intValue() > 0) return totalPopulation; else return getNonNullFacilityCatchmentPopulation(facility); } return getNonNullFacilityCatchmentPopulation(facility); }  Long getPopulation(Facility facility, Program program, Long populationSource);  }