private Set<HtmlFormField> getAllFieldsRecursiveSectionHelper(Set<HtmlFormField> fields, HtmlFormSection section) { getAllFieldsRecursiveFieldHelper(fields, section == null ? this.getFields() : section.getFields()); for (HtmlFormSection s : section == null ? this.getSections() : section.getSections()) { getAllFieldsRecursiveSectionHelper(fields, s); } return fields; }
public Set<HtmlFormSection> getAllSectionsRecursiveHelper(Set<HtmlFormSection> sections, HtmlFormSection section) { if (sections != null) { for (HtmlFormSection s : section == null ? this.getSections() : section.getSections()) { sections.add(s); getAllSectionsRecursiveHelper(sections, s); } } return sections; }
private Set<HtmlFormField> getAllFieldsRecursiveFieldHelper(Set<HtmlFormField> fields, List<HtmlFormField> fieldsToAdd) { if (fieldsToAdd != null) { for (HtmlFormField f : fieldsToAdd) { fields.add(f); if (f instanceof ObsGroup) { getAllFieldsRecursiveFieldHelper(fields, ((ObsGroup) f).getChildren()); } } } return fields; }
@Test public void shouldBuildSchemaWithSectionsAndNestedObsGroup() { HtmlFormSchema schema = new HtmlFormSchema(); HtmlFormSection section1 = new HtmlFormSection(); section1.setName("Section 1"); schema.getSections().add(section1); Concept groupConcept = mock(Concept.class); when(groupConcept.getName()).thenReturn(groupConceptName); ObsGroup obsGroup = new ObsGroup(groupConcept); section1.getFields().add(obsGroup); Concept nestedGroupConcept = mock(Concept.class); when(nestedGroupConcept.getName()).thenReturn(nestedGroupName); ObsGroup nestedGroup = new ObsGroup(nestedGroupConcept); obsGroup.addChild(nestedGroup); ObsField field1 = new ObsField(); field1.setName("Field 1"); ConceptName conceptName1 = new ConceptName(); conceptName1.setName("Should Not Be Overridden by field name"); when(datatype1.getName()).thenReturn("Numeric"); when(concept1.getDatatype()).thenReturn(datatype1); field1.setQuestion(concept1); nestedGroup.addChild(field1);
@Test public void shouldBuildSchemaWithSectionAndSeparateField() { HtmlFormSchema schema = new HtmlFormSchema(); HtmlFormSection section1 = new HtmlFormSection(); section1.setName("Section 1"); schema.getSections().add(section1); ObsField field1 = new ObsField(); field1.setName("Field 1"); schema.getFields().add(field1); JsonNode schemaAsJson = new HtmlFormEncounterController().buildSchemaAsJsonNode(schema, new ObjectMapper()); assertThat(schemaAsJson.get("sections").get(0).get("name").getValueAsText(), is("Section 1")); assertThat(schemaAsJson.get("fields").get(0).get("name").getValueAsText(), is("Field 1")); }
@Test public void shouldBuildSchemaWithSections() { HtmlFormSchema schema = new HtmlFormSchema(); HtmlFormSection section1 = new HtmlFormSection(); section1.setName("Section 1"); schema.getSections().add(section1); HtmlFormSection section2 = new HtmlFormSection(); section2.setName("Section 2"); schema.getSections().add(section2); JsonNode schemaAsJson = new HtmlFormEncounterController().buildSchemaAsJsonNode(schema, new ObjectMapper()); assertThat(schemaAsJson.get("sections").get(0).get("name").getValueAsText(), is("Section 1")); assertThat(schemaAsJson.get("sections").get(1).get("name").getValueAsText(), is("Section 2")); }
private JsonNode addSections(List<HtmlFormSection> sections, ObjectMapper jackson) { ArrayNode node = jackson.createArrayNode(); for (HtmlFormSection section : sections) { ObjectNode sectionNode = jackson.createObjectNode(); sectionNode.put("name", section.getName()); if (section.getSections() != null && section.getSections().size() > 0) { sectionNode.put("sections", (addSections(section.getSections(), jackson))); } if (section.getFields() != null && section.getFields().size() > 0) { sectionNode.put("fields", (addFields(section.getFields(), jackson))); } node.add(sectionNode); } return node; }
public JsonNode buildSchemaAsJsonNode(HtmlFormSchema schema, ObjectMapper jackson) { ObjectNode schemaAsJson = jackson.createObjectNode(); schemaAsJson.put("sections", addSections(schema.getSections(), jackson)); schemaAsJson.put("fields", addFields(schema.getFields(), jackson)); return schemaAsJson; }
@Test public void shouldAddConceptAnswersDefinedInConceptSet() throws Exception { String htmlform = "<htmlform><obs id=\"obs-id\" conceptId=\"1000\" answerConceptSetIds=\"1004\" labelText=\"Allergy\"/></htmlform>"; FormEntrySession session = new FormEntrySession(patient, htmlform, null); String htmlToDisplay = session.getHtmlToDisplay(); HtmlFormSchema schema = session.getContext().getSchema(); Assert.assertEquals(1, schema.getFields().size()); ObsField field = (ObsField) schema.getFields().get(0); // Expected Concept set = Context.getConceptService().getConcept(1004); List<Concept> setMembers = Context.getConceptService().getConceptsByConceptSet(set); Assert.assertEquals(setMembers.size(), field.getAnswers().size()); for (ObsFieldAnswer answer : field.getAnswers()) { Assert.assertTrue(setMembers.contains(answer.getConcept())); } }
/** * * Builds the root column name for the concept from the conceptID * * @param of * @return */ private static String buildColumnHeader(ObsField of){ StringBuilder sb = new StringBuilder(EMPTY); Locale loc = Context.getLocale(); if (of.getQuestion() != null){ //TODO: add fieldId, fieldPart, Page??? sb.append(of.getQuestion().getShortestName(loc, false)); } else if (of.getAnswers().size() == 1){ sb.append(of.getAnswers().get(0).getConcept().getShortestName(loc, false)); } else { throw new RuntimeException("Obs Field has no conceptId, and multiple answers -- this isn't yet supported."); } return sb.toString().replaceAll("\\s", "_").replaceAll("-", "_").toUpperCase(); }
/** * Adds a new section */ public void beginSection(HtmlFormSection section) { // is this a top-level section or it is a child of the existing section if (sectionsStack.size() > 0) { sectionsStack.peek().addChildSection(section); } else { schema.getSections().add(section); } sectionsStack.push(section); }
private String getName(ObsField field) { String name = field.getName(); if (!StringUtils.hasText(name)) { if (field.getQuestion() != null && field.getQuestion().getName() != null) { name = field.getQuestion().getName().getName(); } } return name; }
public void beginSection() { beginSection(new HtmlFormSection()); }
private String getName(ObsGroup obsGroup) { String name = obsGroup.getLabel(); if (!StringUtils.hasText(name)) { if (obsGroup.getConcept() != null && obsGroup.getConcept().getName() != null) { name = obsGroup.getConcept().getName().getName(); } } return name; }
public void addFieldToActiveObsGroup(HtmlFormField field) { getActiveObsGroup().getChildren().add(field); }
private String getDatatype(ObsField field) { return field.getQuestion() != null ? field.getQuestion().getDatatype().getName().toLowerCase() : ""; }
/** * Returns the entire flattened set of fields */ public Set<HtmlFormField> getAllFields() { return getAllFieldsRecursiveSectionHelper(new HashSet<HtmlFormField>(), null); }
/** * Returns the entire flattened set of sections */ public Set<HtmlFormSection> getAllSections() { return getAllSectionsRecursiveHelper(new HashSet<HtmlFormSection>(), null); }
@Test public void shouldBuildSchemaWithSectionsNestedSections() { HtmlFormSchema schema = new HtmlFormSchema(); HtmlFormSection section1 = new HtmlFormSection(); section1.setName("Section 1"); HtmlFormSection section1a = new HtmlFormSection(); section1a.setName("Section 1a"); section1.addChildSection(section1a); HtmlFormSection section1b = new HtmlFormSection(); section1b.setName("Section 1b"); section1.addChildSection(section1b); schema.getSections().add(section1); HtmlFormSection section2 = new HtmlFormSection(); section2.setName("Section 2"); schema.getSections().add(section2); JsonNode schemaAsJson = new HtmlFormEncounterController().buildSchemaAsJsonNode(schema, new ObjectMapper()); assertThat(schemaAsJson.get("sections").get(0).get("name").getValueAsText(), is("Section 1")); assertThat(schemaAsJson.get("sections").get(0).get("sections").get(0).get("name").getValueAsText(), is("Section 1a")); assertThat(schemaAsJson.get("sections").get(0).get("sections").get(1).get("name").getValueAsText(), is("Section 1b")); assertThat(schemaAsJson.get("sections").get(1).get("name").getValueAsText(), is("Section 2")); }