TransferInDateCalculation extends AbstractPatientCalculation { @Override public CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues, PatientCalculationContext context) { Concept transferInDate = Dictionary.getConcept(Dictionary.TRANSFER_IN_DATE); Concept transferInStatus = Dictionary.getConcept(Dictionary.TRANSFER_IN); CalculationResultMap transferInStatusResults = Calculations.lastObs(transferInStatus,cohort,context); CalculationResultMap transferInDateResults = Calculations.lastObs(transferInDate, cohort, context); CalculationResultMap result = new CalculationResultMap(); for (Integer ptId : cohort) { Date transferInDateValue = null; Date tDate = EmrCalculationUtils.datetimeObsResultForPatient(transferInDateResults, ptId); Obs statusObs = EmrCalculationUtils.obsResultForPatient(transferInStatusResults, ptId); if(tDate != null){ transferInDateValue = tDate; } else if(statusObs != null && statusObs.getValueCoded().equals(Dictionary.getConcept(Dictionary.YES))) { transferInDateValue = statusObs.getObsDatetime(); } result.put(ptId, new SimpleResult(transferInDateValue, this)); } return result; }  @Override CalculationResultMap evaluate(Collection<Integer> cohort, Map<String, Object> parameterValues,
										 PatientCalculationContext context);  }
@Test public void evaluate_shouldCalculateTransferInDateCalculation() throws Exception { Concept transferInDate = Dictionary.getConcept(Dictionary.TRANSFER_IN_DATE); TestUtils.saveObs(TestUtils.getPatient(2), transferInDate, TestUtils.date(2014, 3, 1), TestUtils.date(2014, 3, 1)); TestUtils.saveObs(TestUtils.getPatient(7), transferInDate, TestUtils.date(2014, 3, 10), TestUtils.date(2014, 3, 10)); List<Integer> ptIds = Arrays.asList(2, 6, 7, 8, 999); CalculationResultMap resultMap = new TransferInDateCalculation().evaluate(ptIds, null, Context.getService(PatientCalculationService.class).createCalculationContext()); Assert.assertEquals(TestUtils.date(2014, 3, 1), resultMap.get(2).getValue()); Assert.assertEquals(TestUtils.date(2014, 3, 10), resultMap.get(7).getValue()); }