/**
   * @see DecliningCd4Calculation#evaluate(java.util.Collection, java.util.Map,
   *     org.openmrs.calculation.patient.PatientCalculationContext)
   * @verifies determine whether patients have a decline in CD4
   */
  @Test
  public void evaluate_shouldDetermineWhetherPatientsHasDeclinedCD4() throws Exception {

    // Get HIV Program
    Program hivProgram = Metadata.getProgram(Metadata.HIV_PROGRAM);

    // Enroll patients #6, #7 and #8 in the HIV Program
    PatientService ps = Context.getPatientService();
    for (int i = 6; i <= 8; ++i) {
      TestUtils.enrollInProgram(ps.getPatient(i), hivProgram, new Date());
    }

    // Give patients #7 and #8 a CD4 count 180 days ago
    Concept cd4 = Dictionary.getConcept(Dictionary.CD4_COUNT);
    Calendar calendar = Calendar.getInstance();
    calendar.add(Calendar.DATE, -180);
    TestUtils.saveObs(ps.getPatient(7), cd4, 123d, calendar.getTime());
    TestUtils.saveObs(ps.getPatient(8), cd4, 123d, calendar.getTime());

    // Give patient #7 a lower CD4 count today
    TestUtils.saveObs(ps.getPatient(7), cd4, 120d, new Date());

    // Give patient #8 a higher CD4 count today
    TestUtils.saveObs(ps.getPatient(8), cd4, 126d, new Date());

    Context.flushSession();

    List<Integer> ptIds = Arrays.asList(6, 7, 8, 999);

    CalculationResultMap resultMap =
        Context.getService(PatientCalculationService.class)
            .evaluate(ptIds, new DecliningCd4Calculation());
    Assert.assertFalse(
        (Boolean) resultMap.get(6).getValue()); // in Hiv program but without cd4 i.e needs cd4
    Assert.assertTrue((Boolean) resultMap.get(7).getValue()); // has decline in CD4
    Assert.assertFalse((Boolean) resultMap.get(8).getValue()); // has increase in CD4
    Assert.assertFalse((Boolean) resultMap.get(999).getValue()); // not in HIV Program
  }
  /**
   * Searches for locations by name of MFL code
   *
   * @param term the search term
   * @return the list of locations as simple objects
   */
  public List<SimpleObject> locations(
      @RequestParam("term") String term, UiUtils ui, @SpringBean KenyaEmrUiUtils kenyaUi) {
    LocationService svc = Context.getLocationService();
    LocationAttributeType mflCodeAttrType =
        Metadata.getLocationAttributeType(Metadata.MASTER_FACILITY_CODE_LOCATION_ATTRIBUTE_TYPE);

    // Results will be sorted by name
    Set<Location> results =
        new TreeSet<Location>(
            new Comparator<Location>() {
              @Override
              public int compare(Location location1, Location location2) {
                return location1.getName().compareTo(location2.getName());
              }
            });

    // If term looks like an MFL code, add location with that code
    if (StringUtils.isNumeric(term) && term.length() >= 5) {
      Location locationByMflCode =
          Context.getService(KenyaEmrService.class).getLocationByMflCode(term);
      if (locationByMflCode != null) {
        results.add(locationByMflCode);
      }
    }

    // Add first 20 results of search by name
    if (StringUtils.isNotBlank(term)) {
      results.addAll(svc.getLocations(term, true, 0, 20));
    }

    // Convert to simple objects
    List<SimpleObject> ret = new ArrayList<SimpleObject>();
    for (Location l : results) {
      ret.add(kenyaUi.simpleLocation(l, mflCodeAttrType, ui));
    }
    return ret;
  }
 /**
  * Gets a location by it's id
  *
  * @param location the location
  * @param ui
  * @param kenyaUi
  * @return the simplified location
  */
 public SimpleObject location(
     @RequestParam("id") Location location, UiUtils ui, @SpringBean KenyaEmrUiUtils kenyaUi) {
   LocationAttributeType mflCodeAttrType =
       Metadata.getLocationAttributeType(Metadata.MASTER_FACILITY_CODE_LOCATION_ATTRIBUTE_TYPE);
   return kenyaUi.simpleLocation(location, mflCodeAttrType, ui);
 }