@Test public void sdpShouldThrowErrorIfPeriodValidationFails() throws Exception { expectedException.expect(DataException.class); expectedException.expectMessage("rnr.error"); report.setPeriodId(2L); when(programService.getValidatedProgramByCode(anyString())).thenReturn(new Program()); when(facilityService.getOperativeFacilityByCode(anyString())).thenReturn(new Facility()); doThrow(new DataException("rnr.error")).when(restRequisitionCalculator).validateCustomPeriod(any(Facility.class), any(Program.class), any(ProcessingPeriod.class), any(Long.class)); service.submitSdpReport(report, 1l); verify(requisitionService, never()).initiate(any(Facility.class), any(Program.class), any(Long.class), any(Boolean.class), any(ProcessingPeriod.class)); verify(requisitionService, never()).save(any(Rnr.class)); verify(requisitionService, never()).submit(any(Rnr.class)); }
@Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); } }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }  }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }  @Transactional Rnr submitReport(Report report, Long userId); @Transactional Rnr submitSdpReport(Report report, Long userId); @Transactional void approve(Report report, Long requisitionId, Long userId); ReplenishmentDTO getReplenishmentDetails(Long id); List<Report> getRequisitionsByFacility(String facilityCode); }
RestRequisitionService { @Transactional public Rnr submitSdpReport(Report report, Long userId) { report.validate(); Facility reportingFacility = facilityService.getOperativeFacilityByCode(report.getAgentCode()); Program reportingProgram = programService.getValidatedProgramByCode(report.getProgramCode()); ProcessingPeriod period = processingPeriodService.getById(report.getPeriodId()); Rnr rnr; List<Rnr> rnrs = null; RequisitionSearchCriteria searchCriteria = new RequisitionSearchCriteria(); searchCriteria.setProgramId(reportingProgram.getId()); searchCriteria.setFacilityId(reportingFacility.getId()); searchCriteria.setWithoutLineItems(true); searchCriteria.setUserId(userId); if(report.getPeriodId() != null) { restRequisitionCalculator.validateCustomPeriod(reportingFacility, reportingProgram, period, userId); rnrs = requisitionService.getRequisitionsFor(searchCriteria, asList(period)); } if(rnrs != null && rnrs.size() > 0){ rnr = requisitionService.getFullRequisitionById( rnrs.get(0).getId() ); }else{ rnr = requisitionService.initiate(reportingFacility, reportingProgram, userId, report.getEmergency(), period); } List<RnrLineItem> fullSupplyProducts = new ArrayList<>(); List<RnrLineItem> nonFullSupplyProducts = new ArrayList<>(); Iterator<RnrLineItem> iterator = report.getProducts().iterator(); nonFullSupplyFacilityApprovedProductByFacilityAndProgram = facilityApprovedProductService.getNonFullSupplyFacilityApprovedProductByFacilityAndProgram( reportingFacility.getId(), reportingProgram.getId() ); while(iterator.hasNext()){ final RnrLineItem lineItem = iterator.next(); if(lineItem.getFullSupply()){ fullSupplyProducts.add(lineItem); }else{ setNonFullSupplyCreatorFields(lineItem); nonFullSupplyProducts.add(lineItem); } } report.setProducts(fullSupplyProducts); report.setNonFullSupplyProducts(nonFullSupplyProducts); restRequisitionCalculator.validateProducts(report.getProducts(), rnr); markSkippedLineItems(rnr, report); copyRegimens(rnr, report); requisitionService.save(rnr); rnr = requisitionService.submit(rnr); return requisitionService.authorize(rnr); }  @Transactional Rnr submitReport(Report report, Long userId); @Transactional Rnr submitSdpReport(Report report, Long userId); @Transactional void approve(Report report, Long requisitionId, Long userId); ReplenishmentDTO getReplenishmentDetails(Long id); List<Report> getRequisitionsByFacility(String facilityCode); static final boolean EMERGENCY; }