EditProgramFormPageController { public String controller(@RequestParam("appId") String appId, @RequestParam("patientProgramId") PatientProgram enrollment, @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); } Patient patient = enrollment.getPatient(); StringBuilder sb = new StringBuilder("redirect:" + EmrConstants.MODULE_ID + "/"); List<Encounter> encounters = Context.getEncounterService().getEncounters(patient, null, enrollment.getDateEnrolled(), enrollment.getDateCompleted(), 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("patientProgramId") PatientProgram enrollment,
							 @RequestParam("formUuid") String formUuid,
							 @RequestParam("returnUrl") String returnUrl);  }
@Test public void controller() { Program hivProgram = MetadataUtils.existing(Program.class, HivMetadata._Program.HIV); Form familyHistory = MetadataUtils.existing(Form.class, HivMetadata._Form.FAMILY_HISTORY); PatientProgram enrollment = TestUtils.enrollInProgram(TestUtils.getPatient(7), hivProgram, TestUtils.date(2012, 5, 1), TestUtils.date(2012, 6, 1)); String result = controller.controller("test.app", enrollment, HivMetadata._Form.FAMILY_HISTORY, "test.html"); Assert.assertThat(result, is("redirect:kenyaemr/enterForm.page?formUuid=7efa0ee0-6617-4cd7-8310-9f95dfee7a82&appId=test.app&patientId=7&returnUrl=test.html")); TestUtils.saveEncounter(TestUtils.getPatient(7), familyHistory, TestUtils.date(2012, 4, 30)); controller.controller("test.app", enrollment, HivMetadata._Form.FAMILY_HISTORY, "test.html"); Assert.assertThat(result, is("redirect:kenyaemr/enterForm.page?formUuid=7efa0ee0-6617-4cd7-8310-9f95dfee7a82&appId=test.app&patientId=7&returnUrl=test.html")); Encounter encounter = TestUtils.saveEncounter(TestUtils.getPatient(7), familyHistory, TestUtils.date(2012, 5, 1)); TestUtils.saveEncounter(TestUtils.getPatient(7), familyHistory, TestUtils.date(2012, 6, 2)); result = controller.controller("test.app", enrollment, HivMetadata._Form.FAMILY_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)); }