private void stripLocalAttributesFromXmlHelper(Pattern pattern) { Matcher matcher = pattern.matcher(formToExport.getXmlData()); StringBuffer buffer = new StringBuffer(); // search through the xml data for the Pattern specified in the pattern parameter, and remove group(1) while (matcher.find()) { matcher.appendReplacement(buffer, matcher.group().substring(0, matcher.start(1) - matcher.start())); } matcher.appendTail(buffer); formToExport.setXmlData(buffer.toString()); }
private HtmlFormSchema generateSchema(String xml, HttpSession httpSession) throws Exception { Patient p = HtmlFormEntryUtil.getFakePerson(); HtmlForm fakeForm = new HtmlForm(); fakeForm.setXmlData(xml); FormEntrySession fes = new FormEntrySession(p, null, FormEntryContext.Mode.ENTER, fakeForm, httpSession); fes.getHtmlToDisplay(); return fes.getContext().getSchema(); } }
private HtmlFormSchema generateSchema(String xml, HttpSession httpSession, Encounter encounter) throws Exception { HtmlForm fakeForm = new HtmlForm(); fakeForm.setXmlData(xml); FormEntrySession fes = new FormEntrySession(encounter.getPatient(), encounter, FormEntryContext.Mode.VIEW, fakeForm, httpSession); fes.getHtmlToDisplay(); return fes.getContext().getSchema(); }
/** * This method returns a FormEntrySession object using data in the instance object * @return FormEntrySession object * @throws Exception */ private void createSession() throws Exception { //TODO: Check for null patientUuid and try to parse the xml to obtain the patient ID instead. Patient patient = Context.getPatientService().getPatientByUuid(getPatientUuid()); session = new FormEntrySession(patient,getXmlDefinition(),null); HtmlForm htmlForm = HtmlFormEntryUtil.getService().getHtmlForm(htmlFormId); htmlForm.setXmlData(xmlDefinition); session.setHtmlForm(htmlForm); //getHtmlToDisplay() is called to generate necessary tag handlers and cache the form session.getHtmlToDisplay(); //PrepareForSubmit is called to set patient and encounter if specified in tags session.prepareForSubmit(); }
@Test @Verifies(value = "should recognize and return section tags in xml", method = "getSectionNodes(HtmlForm)") public void getSectionNodes_shouldReturnSectionNodesCorrectly() throws Exception { Form form = new Form(); HtmlForm htmlform = new HtmlForm(); htmlform.setForm(form); form.setEncounterType(new EncounterType()); htmlform.setDateChanged(new Date()); htmlform.setXmlData(new TestUtil().loadXmlFromFile(XML_DATASET_PATH + "returnSectionsAndConceptsInSectionsTestForm.xml")); Map<Integer, String> map = HtmlFormEntryExportUtil.getSectionIndex(htmlform); String st = ""; for (Map.Entry<Integer, String> e : map.entrySet()){ st += "|" + e.getKey() + " : " + e.getValue(); } //System.out.println(st); TestUtil.assertFuzzyContains("Section One", st); TestUtil.assertFuzzyContains("Section One Inner One", st); TestUtil.assertFuzzyContains("Section One Inner Two", st); TestUtil.assertFuzzyContains("no name specified", st); }
@Test public void testExportWithEncounterRoleAndProvider() throws Exception { HtmlForm form = new HtmlForm(); form.setXmlData(new TestUtil().loadXmlFromFile("org/openmrs/module/htmlformentry/include/metadataSharingExportTest.xml")); HtmlFormExporter exporter = new HtmlFormExporter(form); HtmlForm formClone = exporter.export(true, true, true, true); Collection<OpenmrsObject> dependencies = formClone.getDependencies(); Assert.assertTrue(dependencies.contains(Context.getEncounterService().getEncounterRoleByUuid("e5c5cc92-5283-11e1-bb6a-d975bd577a5e"))); Assert.assertTrue(dependencies.contains(Context.getEncounterService().getEncounterRoleByUuid("eb75d754-5283-11e1-bb6a-d975bd577a5e"))); Assert.assertTrue(dependencies.contains(Context.getProviderService().getProviderByUuid("d2299800-cca9-11e0-9572-0800200c9a66"))); Assert.assertTrue(dependencies.contains(Context.getProviderService().getProviderByUuid("c2299800-cca9-11e0-9572-0800200c9a66"))); }
@Test @Verifies(value = "should return section as a new htmlform", method = "getSectionAsForm(HtmlForm)") public void getSectionAsForm_shouldReturnStringCorrectly() throws Exception { Form form = new Form(); HtmlForm htmlform = new HtmlForm(); htmlform.setForm(form); form.setEncounterType(new EncounterType()); htmlform.setDateChanged(new Date()); htmlform.setXmlData(new TestUtil().loadXmlFromFile(XML_DATASET_PATH + "returnSectionsAndConceptsInSectionsTestForm.xml")); String newXml = HtmlFormEntryExportUtil.getSectionAsFormXml(htmlform, 1); //System.out.println(newXML); htmlform.setXmlData(newXml); FormEntrySession session = new FormEntrySession(HtmlFormEntryUtil.getFakePerson(), htmlform, null); String html = session.getHtmlToDisplay(); System.out.println(html); TestUtil.assertFuzzyContains("<span class=\"sectionHeader\">Section One Inner One</span>", html); TestUtil.assertFuzzyContains("ISONIAZID <select id=\"w2\" name=\"w2\" size=\"1\"><option value=\"\" selected=\"true\"></option><option value=\"2474\">Susceptible</option><option value=\"3017\">Intermediate</option><option value=\"1441\">Resistant</option></select>", html); }
/** * @return a new VelocityFunctions instance for the given patientId */ private VelocityFunctions setupFunctionsForPatient(Integer patientId) throws Exception { HtmlForm htmlform = new HtmlForm(); Form form = new Form(); form.setEncounterType(new EncounterType(1)); htmlform.setForm(form); htmlform.setDateChanged(new Date()); htmlform.setXmlData("<htmlform></htmlform>"); Patient p = new Patient(patientId); String[] datePattern = {"yyyy.MM.dd"}; p.setBirthdate(DateUtils.parseDate("1970.01.01",datePattern )); measureAgeInDaysAndMonths(new Date(), p.getBirthdate()); FormEntrySession session = new FormEntrySession(p, htmlform, null); return new VelocityFunctions(session); }
/** * calls session.createForm on a form that has both conceptIds with labels, and conceptIds without labels in obs tags. * Verifies that the dropdown options for selecting a concept are correctly labeled. * @throws Exception */ @Test @Verifies(value = "should test labels generation for concept selects on dropdown options", method = "") public void getSectionNodes_shouldReturnDropdownForConceptSelects() throws Exception { Form form = new Form(); HtmlForm htmlform = new HtmlForm(); htmlform.setForm(form); form.setEncounterType(new EncounterType()); htmlform.setDateChanged(new Date()); htmlform.setXmlData(new TestUtil().loadXmlFromFile(XML_DATASET_PATH + "obsGroupDataExportTest.xml")); FormEntrySession session = new FormEntrySession(HtmlFormEntryUtil.getFakePerson(), htmlform, null); String xml = session.createForm(htmlform.getXmlData()); // System.out.println(xml); Assert.assertTrue(xml.contains("<option value=\"\" selected=\"true\"></option><option value=\"2474\">Susc</option><option value=\"3017\">Interm</option>")); Assert.assertTrue(xml.contains("<option value=\"\" selected=\"true\"></option><option value=\"2474\">Susceptible</option><option value=\"3017\">Intermediate</option><option value=\"1441\">Resistant</option>")); }
@Test public void encounterLocationTag_shouldShowRetiredLocationIfPreviouslySelected() throws Exception { // create an encounter associated with a retired location Location location = Context.getLocationService().getLocation(3); Patient patient = Context.getPatientService().getPatient(2); Encounter encounter = Context.getEncounterService().getEncounter(101); // (sanity check) Assert.assertTrue(location.isRetired()); // set the location on this encounter to the retired location encounter.setLocation(location); // now render a form using that encounter HtmlForm htmlform = new HtmlForm(); htmlform.setXmlData( "<htmlform><encounterLocation /></htmlform>"); FormEntrySession session = new FormEntrySession(patient, encounter, FormEntryContext.Mode.EDIT, htmlform, null); TestUtil.assertFuzzyContains("<option value=\"3\" selected=\"true\">Never Never Land</option>", session.getHtmlToDisplay()); }
@Test public void validate_shouldAllowXmlWithValidObsGroupAndSetMembers() throws Exception { String xml = "<htmlform>" + "<obsgroup groupingConceptId=\"23\">" + "<obs conceptId=\"18\" />" + "</obsgroup>" + "Date: <encounterDate/>Location: <encounterLocation/>" + "Provider: <encounterProvider role=\"Provider\"/>" + "Encounter Type: <encounterType /> <submit/>" + "</htmlform>"; HtmlForm htmlForm = new HtmlForm(); Form form = Context.getFormService().getForm(1); htmlForm.setXmlData(xml); htmlForm.setForm(form); Errors errors = new BindException(htmlForm, "htmlForm"); HtmlFormValidator validator = new HtmlFormValidator(); validator.validate(htmlForm, errors); Assert.assertEquals(0, errors.getErrorCount()); Assert.assertEquals(0, validator.getHtmlFormWarnings().size()); }
@Test public void validate_shouldWarnWhenXmlObsGroupIsNotASet() throws Exception { String xml = "<htmlform>" + "<obsgroup groupingConceptId=\"10\">\n" + "<obs conceptId=\"3\" />\n" + "<obs conceptId=\"4\" />\n" + "</obsgroup>\n" + "Date: <encounterDate/>Location: <encounterLocation/>" + "Provider: <encounterProvider role=\"Provider\"/>" + "Encounter Type: <encounterType />" + "<submit/>" + "</htmlform>"; HtmlForm htmlForm = new HtmlForm(); Form form = Context.getFormService().getForm(1); htmlForm.setXmlData(xml); htmlForm.setForm(form); Errors errors = new BindException(htmlForm, "htmlForm"); HtmlFormValidator validator = new HtmlFormValidator(); validator.validate(htmlForm, errors); Assert.assertEquals(0, errors.getErrorCount()); Assert.assertEquals(3, validator.getHtmlFormWarnings().size()); } }
/** * see {@link HtmlFormSubstitutionUtils#replaceProgramNamesWithUuids(HtmlForm)} * @throws Exception */ @Test @Verifies(value = "should replace program names with uuids", method = "replaceProgramNamesWithUuids(HtmlForm)") public void replaceProgamNamesWithUuids_shouldReplaceProgramNamesWithUuids() throws Exception { HtmlForm form = new HtmlForm(); form.setXmlData(new TestUtil().loadXmlFromFile(XML_DATASET_PATH + "metadataSharingTestForm.xml")); HtmlFormSubstitutionUtils.replaceProgramNamesWithUuids(form); // make the program is no longer referenced by name TestUtil.assertFuzzyDoesNotContain("programId=\"MDR-TB PROGRAM\"", form.getXmlData()); // make sure the program is now referenced by uuid TestUtil.assertFuzzyContains("programId=\"71779c39-d289-4dfe-91b5-e7cfaa27c78b\"", form.getXmlData()); }
private HtmlForm copyOf(HtmlForm form) { HtmlForm copy = new HtmlForm(); copy.setChangedBy(form.getChangedBy()); copy.setCreator(form.getCreator()); copy.setDateChanged(form.getDateChanged()); copy.setDateCreated(form.getDateCreated()); copy.setDateRetired(form.getDateRetired()); copy.setForm(form.getForm()); copy.setId(form.getId()); copy.setRetired(form.getRetired()); copy.setRetiredBy(form.getRetiredBy()); copy.setRetireReason(form.getRetireReason()); copy.setUuid(form.getUuid()); copy.setXmlData(form.getXmlData()); return copy; }
private FormEntrySession setupFormViewSession(Patient patient, Encounter encounter, String filename) throws Exception { String xml = loadXmlFromFile(getXmlDatasetPath() + filename + ".xml"); HtmlForm fakeForm = new HtmlForm(); fakeForm.setXmlData(xml); fakeForm.setForm(new Form(1)); fakeForm.getForm().setEncounterType(new EncounterType(1)); FormEntrySession session = new FormEntrySession(patient, encounter, FormEntryContext.Mode.VIEW, fakeForm, new MockHttpSession()); session.setAttributes(getFormEntrySessionAttributes()); session.getHtmlToDisplay(); return session; }
private FormEntrySession setupFormEntrySession(Patient patient, String filename) throws Exception { String xml = loadXmlFromFile(getXmlDatasetPath() + filename + ".xml"); HtmlForm fakeForm = new HtmlForm(); fakeForm.setXmlData(xml); fakeForm.setForm(new Form(1)); fakeForm.getForm().setEncounterType(new EncounterType(1)); FormEntrySession session = new FormEntrySession(patient, null, FormEntryContext.Mode.ENTER, fakeForm, new MockHttpSession()); session.setAttributes(getFormEntrySessionAttributes()); session.getHtmlToDisplay(); return session; }
private FormEntrySession setupFormEditSession(Patient patient, Encounter encounter, String filename) throws Exception { String xml = loadXmlFromFile(getXmlDatasetPath() + filename + ".xml"); HtmlForm fakeForm = new HtmlForm(); fakeForm.setXmlData(xml); fakeForm.setForm(new Form(1)); fakeForm.getForm().setEncounterType(new EncounterType(1)); FormEntrySession session = new FormEntrySession(patient, encounter, FormEntryContext.Mode.EDIT, fakeForm, new MockHttpSession()); session.setAttributes(getFormEntrySessionAttributes()); session.getHtmlToDisplay(); return session; }
/** * @see {@link HtmlFormValidator#validate(Object,Errors)} */ @Test @Verifies(value = "should allow xml containing encounter type tag for a form with no encounter type", method = "validate(Object,Errors)") public void validate_shouldAllowXmlContainingEncounterTypeTagForAFormWithNoEncounterType() throws Exception { String xml = "<htmlform>Date: <encounterDate/>Location: <encounterLocation/>Provider: <encounterProvider role=\"Provider\"/>Encounter Type: <encounterType /><submit/></htmlform>"; HtmlForm htmlForm = new HtmlForm(); htmlForm.setName("testForm"); Form form = Context.getFormService().getForm(1); Assert.assertNull(form.getEncounterType()); htmlForm.setForm(form); htmlForm.setXmlData(xml); Errors errors = new BindException(htmlForm, "htmlForm"); new HtmlFormValidator().validate(htmlForm, errors); Assert.assertFalse(errors.hasErrors()); }
/** * @see {@link HtmlFormValidator#validate(Object,Errors)} */ @Test @Verifies(value = "should reject xml containing encounter type tag for a form with an encounter type", method = "validate(Object,Errors)") public void validate_shouldRejectXmlContainingEncounterTypeTagForAFormWithAnEncounterType() throws Exception { String xml = "<htmlform>Date: <encounterDate/>Location: <encounterLocation/>Provider: <encounterProvider role=\"Provider\"/>Encounter Type: <encounterType /><submit/></htmlform>"; HtmlForm htmlForm = new HtmlForm(); htmlForm.setName("testForm"); htmlForm.setXmlData(xml); Form form = Context.getFormService().getForm(1); //set the encounter type on the form for testing purposes EncounterType encType = Context.getEncounterService().getEncounterType(1); Assert.assertNotNull(encType); form.setEncounterType(encType); htmlForm.setForm(form); Errors errors = new BindException(htmlForm, "htmlForm"); new HtmlFormValidator().validate(htmlForm, errors); Assert.assertTrue(errors.hasFieldErrors("xmlData")); }
/** * see {@link HtmlFormSubstitutionUtils#replaceIdsWithUuids(HtmlForm)} * @throws Exception */ @Test @Verifies(value = "should convert ids to uuids within repeat tags", method = "replaceConceptIdsWithUuids(HtmlForm)") public void replaceConceptIdsWithUuids_shouldReplaceConceptIdsWithUuidsWithMacros() throws Exception { executeDataSet(XML_DATASET_PATH + new TestUtil().getTestDatasetFilename(XML_REGRESSION_TEST_DATASET)); HtmlForm form = new HtmlForm(); form.setXmlData(new TestUtil().loadXmlFromFile(XML_DATASET_PATH + "metadataSharingWithMacrosTestForm.xml")); HtmlFormSubstitutionUtils.replaceIdsWithUuids(form); // make sure it's left the macro references alone TestUtil.assertFuzzyContains("groupingConceptId=\"\\$allergyGroup\"", form.getXmlData()); TestUtil.assertFuzzyContains("conceptId=\"\\$allergy\"", form.getXmlData()); TestUtil.assertFuzzyContains("answerConceptIds=\"\\$allergyAnswers\"", form.getXmlData()); // test that the macros themselves have been substituted TestUtil.assertFuzzyContains("allergyGroup=32296060-03aa-102d-b0e3-001ec94a0cc1", form.getXmlData()); TestUtil.assertFuzzyContains("allergy=32296060-03aa-102d-b0e3-001ec94a0cc4", form.getXmlData()); TestUtil.assertFuzzyContains("allergyAnswers=32296060-03aa-102d-b0e3-001ec94a0cc5,32296060-03aa-102d-b0e3-001ec94a0cc6", form.getXmlData()); }