EditPatientFormPageController { public String controller(@RequestParam("appId") String appId, @RequestParam("patientId") Patient patient, @RequestParam("formUuid") String formUuid, @RequestParam("returnUrl") String returnUrl) { Form form = Context.getFormService().getFormByUuid(formUuid); if (form == null) { throw new IllegalArgumentException("Cannot find form with uuid = " + formUuid); } StringBuilder sb = new StringBuilder("redirect:" + EmrConstants.MODULE_ID + "/"); List<Encounter> encounters = Context.getEncounterService().getEncounters(patient, null, null, null, Collections.singleton(form), null, null, null, null, false); if (encounters.size() == 0) { sb.append("enterForm.page?formUuid=" + formUuid); } else { Encounter encounter = encounters.get(encounters.size() - 1); sb.append("editForm.page?encounterId=" + encounter.getEncounterId()); } sb.append("&appId=" + appId); sb.append("&patientId=" + patient.getId()); sb.append("&returnUrl=" + java.net.URLEncoder.encode(returnUrl)); return sb.toString(); }  String controller(@RequestParam("appId") String appId,
							 @RequestParam("patientId") Patient patient,
	                         @RequestParam("formUuid") String formUuid,
	                         @RequestParam("returnUrl") String returnUrl);  }
@Test public void controller() { Form obsHistory = MetadataUtils.existing(Form.class, CommonMetadata._Form.OBSTETRIC_HISTORY); String result = controller.controller("test.app", TestUtils.getPatient(7), CommonMetadata._Form.OBSTETRIC_HISTORY, "test.html"); Assert.assertThat(result, is("redirect:kenyaemr/enterForm.page?formUuid=8e4e1abf-7c08-4ba8-b6d8-19a9f1ccb6c9&appId=test.app&patientId=7&returnUrl=test.html")); Encounter encounter = TestUtils.saveEncounter(TestUtils.getPatient(7), obsHistory, TestUtils.date(2012, 4, 30)); result = controller.controller("test.app", TestUtils.getPatient(7), CommonMetadata._Form.OBSTETRIC_HISTORY, "test.html"); String expected = "redirect:kenyaemr/editForm.page?encounterId=" + encounter.getId() + "&appId=test.app&patientId=7&returnUrl=test.html"; Assert.assertThat(result, is(expected)); }