@Test public void shouldSave() throws Exception { EstimateForm form = new EstimateForm(); EstimateFormLineItem estimateLineItem = new EstimateFormLineItem(); estimateLineItem.setFacilityEstimates(new ArrayList<AnnualFacilityEstimateEntry>()); estimateLineItem.getFacilityEstimates().add(new AnnualFacilityEstimateEntry(2000, 12L, 1L, false, 2L, null, 1.0d, 2L)); form.setEstimateLineItems(asList(estimateLineItem)); when(repository.insert(estimateLineItem.getFacilityEstimates().get(0))).thenReturn(1); service.save(form, 1L); verify(repository, atLeastOnce()).insert(Matchers.<AnnualFacilityEstimateEntry>any()); }
public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualFacilityEstimateEntry estimate : ListUtil.emptyIfNull(dto.getFacilityEstimates())) { AnnualFacilityEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getFacilityId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setFacilityId(dto.getId()); if (!estimate.hasId()) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } }
AnnualFacilityDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualFacilityEstimateEntry estimate : ListUtil.emptyIfNull(dto.getFacilityEstimates())) { AnnualFacilityEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getFacilityId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setFacilityId(dto.getId()); if (!estimate.hasId()) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } } }
AnnualFacilityDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualFacilityEstimateEntry estimate : ListUtil.emptyIfNull(dto.getFacilityEstimates())) { AnnualFacilityEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getFacilityId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setFacilityId(dto.getId()); if (!estimate.hasId()) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } }  }
AnnualFacilityDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualFacilityEstimateEntry estimate : ListUtil.emptyIfNull(dto.getFacilityEstimates())) { AnnualFacilityEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getFacilityId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setFacilityId(dto.getId()); if (!estimate.hasId()) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } }  void save(EstimateForm estimateForm, Long userId); void finalizeEstimate(EstimateForm form, Long userId); void undoFinalize(EstimateForm form, Long userId); EstimateForm getEstimateForm(Long userId, Long programId, Integer year); List<AnnualFacilityEstimateEntry> getEstimateValuesForFacility(Long facilityId, Long programId, Integer year); }
AnnualFacilityDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualFacilityEstimateEntry estimate : ListUtil.emptyIfNull(dto.getFacilityEstimates())) { AnnualFacilityEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getFacilityId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setFacilityId(dto.getId()); if (!estimate.hasId()) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } }  void save(EstimateForm estimateForm, Long userId); void finalizeEstimate(EstimateForm form, Long userId); void undoFinalize(EstimateForm form, Long userId); EstimateForm getEstimateForm(Long userId, Long programId, Integer year); List<AnnualFacilityEstimateEntry> getEstimateValuesForFacility(Long facilityId, Long programId, Integer year);  }