@Test public void shouldCreateNewRecords() throws Exception { AnnualDistrictEstimateEntry districtEstimateEntry = make(a(AnnualDistrictEstimateBuilder.defaultAnnualDistrictEstimateEntry)); EstimateForm form = getDemographicEstimateFormForOneDistrict(districtEstimateEntry); service.save(form, 2L); verify(repository, atLeastOnce()).insert(districtEstimateEntry); verify(repository, never()).update(districtEstimateEntry); }
public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualDistrictEstimateEntry estimate : ListUtil.emptyIfNull(dto.getDistrictEstimates())) { AnnualDistrictEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getDistrictId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setDistrictId(dto.getId()); if (estimate.getId() == null) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } }
AnnualDistrictDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualDistrictEstimateEntry estimate : ListUtil.emptyIfNull(dto.getDistrictEstimates())) { AnnualDistrictEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getDistrictId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setDistrictId(dto.getId()); if (estimate.getId() == null) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } } }
AnnualDistrictDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualDistrictEstimateEntry estimate : ListUtil.emptyIfNull(dto.getDistrictEstimates())) { AnnualDistrictEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getDistrictId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setDistrictId(dto.getId()); if (estimate.getId() == null) { estimate.setCreatedBy(userId); repository.insert(estimate); } else { estimate.setModifiedBy(userId); repository.update(estimate); } } } }  }
AnnualDistrictDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualDistrictEstimateEntry estimate : ListUtil.emptyIfNull(dto.getDistrictEstimates())) { AnnualDistrictEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getDistrictId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setDistrictId(dto.getId()); if (estimate.getId() == null) { 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(Integer year, Long programId, Long userId); }
AnnualDistrictDemographicEstimateService { public void save(EstimateForm estimateForm, Long userId) { for (EstimateFormLineItem dto : ListUtil.emptyIfNull(estimateForm.getEstimateLineItems())) { for (AnnualDistrictEstimateEntry estimate : ListUtil.emptyIfNull(dto.getDistrictEstimates())) { AnnualDistrictEstimateEntry existingEstimateEntry = repository.getEntryBy(estimate.getYear(), estimate.getDistrictId(), estimate.getProgramId(), estimate.getDemographicEstimateId()); if (existingEstimateEntry != null && (!existingEstimateEntry.getId().equals(estimate.getId()) || existingEstimateEntry.getIsFinal())) { continue; } estimate.setDistrictId(dto.getId()); if (estimate.getId() == null) { 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(Integer year, Long programId, Long userId);  }