@Test
  public void testParsingDispositionWithTransferLocation() throws Exception {
    Concept admit =
        new ConceptBuilder(
                null,
                conceptService.getConceptDatatypeByName("N/A"),
                conceptService.getConceptClassByName("Misc"))
            .addName("Transfer")
            .get();
    when(emrConceptService.getConcept("test:transfer")).thenReturn(admit);

    Obs dispositionObs =
        dispositionDescriptor.buildObsGroup(
            new Disposition(
                "emrapi.transfer",
                "Transfer",
                "test:transfer",
                Collections.<String>emptyList(),
                Collections.<DispositionObs>emptyList()),
            emrConceptService);

    Obs transferLocationObs = new Obs();
    transferLocationObs.setObsId(100);
    transferLocationObs.setConcept(dispositionDescriptor.getInternalTransferLocationConcept());
    transferLocationObs.setValueText("3");
    dispositionObs.addGroupMember(transferLocationObs);

    Location transferLocation = new Location();
    transferLocation.setName("Outpatient clinic");
    when(locationService.getLocation(3)).thenReturn(transferLocation);

    encounter.addObs(doNotGoToServiceToFormatMembers(dispositionObs));
    ParsedObs parsed = parser.parseObservations(Locale.ENGLISH);

    SimpleObject expectedTransferLocationObject =
        SimpleObject.create("obsId", transferLocationObs.getObsId());
    expectedTransferLocationObject.put("question", "Transfer location");
    expectedTransferLocationObject.put("answer", "Outpatient clinic");

    List<SimpleObject> expectedAdditionalObsList = new ArrayList<SimpleObject>();
    expectedAdditionalObsList.add(expectedTransferLocationObject);

    assertThat(parsed.getDiagnoses().size(), is(0));
    assertThat(parsed.getDispositions().size(), is(1));
    assertThat(parsed.getObs().size(), is(0));
    assertThat(path(parsed.getDispositions(), 0, "disposition"), is((Object) "Transfer"));
    assertThat(
        path(parsed.getDispositions(), 0, "additionalObs"), is((Object) expectedAdditionalObsList));
  }
  @Test
  public void testParsingDispositionWithDateOfDeath() throws Exception {
    Concept admit =
        new ConceptBuilder(
                null,
                conceptService.getConceptDatatypeByName("N/A"),
                conceptService.getConceptClassByName("Misc"))
            .addName("Death")
            .get();
    when(emrConceptService.getConcept("test:death")).thenReturn(admit);

    Obs dispositionObs =
        dispositionDescriptor.buildObsGroup(
            new Disposition(
                "emrapi.death",
                "Death",
                "test:death",
                Collections.<String>emptyList(),
                Collections.<DispositionObs>emptyList()),
            emrConceptService);

    Date dateOfDeath = new DateTime(2012, 2, 20, 10, 10, 10).toDate();
    Obs dateOfDeathObs = new Obs();
    dateOfDeathObs.setObsId(100);
    dateOfDeathObs.setConcept(dispositionDescriptor.getDateOfDeathConcept());
    dateOfDeathObs.setValueDate(dateOfDeath);
    dispositionObs.addGroupMember(dateOfDeathObs);

    encounter.addObs(doNotGoToServiceToFormatMembers(dispositionObs));
    ParsedObs parsed = parser.parseObservations(Locale.ENGLISH);

    SimpleObject expectedAdmissionLocationObject =
        SimpleObject.create("obsId", dateOfDeathObs.getObsId());
    expectedAdmissionLocationObject.put("question", "Date of death");
    expectedAdmissionLocationObject.put("answer", "20 Feb 2012 10:10 AM");

    List<SimpleObject> expectedAdditionalObsList = new ArrayList<SimpleObject>();
    expectedAdditionalObsList.add(expectedAdmissionLocationObject);

    assertThat(parsed.getDiagnoses().size(), is(0));
    assertThat(parsed.getDispositions().size(), is(1));
    assertThat(parsed.getObs().size(), is(0));
    assertThat(path(parsed.getDispositions(), 0, "disposition"), is((Object) "Death"));
    assertThat(
        path(parsed.getDispositions(), 0, "additionalObs"), is((Object) expectedAdditionalObsList));
  }
  public void controller(
      @RequestParam(value = "patientId", required = false) Patient patient,
      @RequestParam(value = "headerForm") String headerForm,
      @RequestParam(value = "flowsheets") String[] flowsheets,
      @RequestParam(value = "viewOnly", required = false) Boolean viewOnly,
      @RequestParam(value = "requireEncounter", required = false) Boolean requireEncounter,
      UiUtils ui,
      PageModel model,
      @SpringBean("htmlFormEntryService") HtmlFormEntryService htmlFormEntryService,
      @SpringBean("formService") FormService formService,
      @SpringBean("locationService") LocationService locationService,
      @SpringBean("coreResourceFactory") ResourceFactory resourceFactory,
      @InjectBeans PatientDomainWrapper patientDomainWrapper,
      PageRequest pageRequest) {

    patientDomainWrapper.setPatient(patient);
    model.addAttribute("patient", patientDomainWrapper);
    model.addAttribute("headerForm", headerForm);
    model.addAttribute("flowsheets", flowsheets);
    model.addAttribute("requireEncounter", (requireEncounter == null || requireEncounter));

    Location defaultLocation = null;
    Integer locationId =
        pageRequest
            .getSession()
            .getAttribute(PihMalawiWebConstants.SESSION_LOCATION_ID, Integer.TYPE);
    if (locationId != null) {
      defaultLocation = locationService.getLocation(locationId);
    }

    List<Encounter> allEncounters = new ArrayList<Encounter>();

    List<String> alerts = new ArrayList<String>();

    String headerFormResource = "pihmalawi:htmlforms/" + headerForm + ".xml";

    HtmlForm headerHtmlForm =
        getHtmlFormFromResource(
            headerFormResource, resourceFactory, formService, htmlFormEntryService);
    model.addAttribute("headerForm", headerForm);

    Encounter headerEncounter = null;
    List<Encounter> headerEncounters = getEncountersForForm(patient, headerHtmlForm);
    if (headerEncounters.size() > 0) {
      headerEncounter = headerEncounters.get(headerEncounters.size() - 1); // Most recent
      if (headerEncounters.size() > 1) {
        alerts.add(
            "WARNING:  More than one "
                + headerHtmlForm.getName()
                + " encounters exist for this patient.  Displaying the most recent only.");
      }
      allEncounters.add(headerEncounter);
    }
    model.addAttribute("headerEncounter", headerEncounter);

    Map<String, HtmlForm> flowsheetForms = new LinkedHashMap<String, HtmlForm>();
    Map<String, List<Integer>> flowsheetEncounters = new LinkedHashMap<String, List<Integer>>();
    if (flowsheets != null) {
      for (String flowsheet : flowsheets) {
        String flowsheetResource = "pihmalawi:htmlforms/" + flowsheet + ".xml";
        HtmlForm htmlForm =
            getHtmlFormFromResource(
                flowsheetResource, resourceFactory, formService, htmlFormEntryService);
        flowsheetForms.put(flowsheet, htmlForm);
        List<Integer> encIds = new ArrayList<Integer>();
        List<Encounter> encounters = getEncountersForForm(patient, htmlForm);
        for (Encounter e : encounters) {
          encIds.add(e.getEncounterId());
          allEncounters.add(e);
        }
        flowsheetEncounters.put(flowsheet, encIds);
      }
    }
    model.addAttribute("flowsheetForms", flowsheetForms);
    model.addAttribute("flowsheetEncounters", flowsheetEncounters);

    model.addAttribute("alerts", alerts);

    if (defaultLocation == null) {
      Date maxDate = null;
      if (allEncounters.size() > 0) {
        for (Encounter e : allEncounters) {
          if (maxDate == null || maxDate.compareTo(e.getEncounterDatetime()) < 0) {
            maxDate = e.getEncounterDatetime();
            defaultLocation = e.getLocation();
          }
        }
      }
    }
    model.addAttribute(
        "defaultLocationId", defaultLocation == null ? null : defaultLocation.getLocationId());
    model.addAttribute("viewOnly", viewOnly == Boolean.TRUE);

    model.addAttribute(
        "returnUrl",
        ui.pageLink(
            "pihmalawi",
            "mastercard",
            SimpleObject.create(
                "patientId",
                patient.getId(),
                "headerForm",
                headerForm,
                "flowsheets",
                flowsheets,
                "viewOnly",
                viewOnly)));
  }
 /** Gets whether report profiling is enabled */
 @AppAction(EmrConstants.APP_DEVELOPER)
 public SimpleObject getReportProfilingEnabled() {
   return SimpleObject.create(
       "enabled", Level.TRACE.equals(LogManager.getLogger(EvaluationProfiler.class).getLevel()));
 }
 /**
  * Executes a groovy script
  *
  * @param script the script
  * @return the result as a simple object {result, output, stacktrace}
  */
 @AppAction(EmrConstants.APP_DEVELOPER)
 public SimpleObject executeGroovy(@RequestParam("script") String script) {
   String[] result = GroovyUtil.getService().evaluate(script);
   return SimpleObject.create("result", result[0], "output", result[1], "stacktrace", result[2]);
 }