Search in sources :

Example 21 with Element

use of org.hl7.elm.r1.Element in project beneficiary-fhir-data by CMSgov.

the class R4ExplanationOfBenefitResourceProviderIT method assertEobEquals.

/**
 * Compares two ExplanationOfBenefit objects in detail while working around serialization issues
 * like comparing "0" and "0.0" or creation differences like using "Quantity" vs "SimpleQuantity"
 *
 * @param expected the expected
 * @param actual the actual
 */
private static void assertEobEquals(ExplanationOfBenefit expected, ExplanationOfBenefit actual) {
    // ID in the bundle will have `ExplanationOfBenefit/` in front, so make sure the last bit
    // matches up
    assertTrue(actual.getId().endsWith(expected.getId()));
    // Clean them out so we can do a deep compare later
    actual.setId("");
    expected.setId("");
    // If there are any contained resources, they might have lastupdate times in them too
    assertEquals(expected.getContained().size(), actual.getContained().size());
    for (int i = 0; i < expected.getContained().size(); i++) {
        Resource expectedContained = expected.getContained().get(i);
        Resource actualContained = actual.getContained().get(i);
        expectedContained.getMeta().setLastUpdated(null);
        actualContained.getMeta().setLastUpdated(null);
        // TODO: HAPI seems to be inserting the `#` in the ID of the contained element.
        // This is incorrect according to the FHIR spec:
        // https://build.fhir.org/references.html#contained
        // This works around that problem
        assertEquals("#" + expectedContained.getId(), actualContained.getId());
        expectedContained.setId("");
        actualContained.setId("");
    }
    // Dates are not easy to compare so just make sure they are there
    assertNotNull(actual.getMeta().getLastUpdated());
    // We compared all of meta that we care about so get it out of the way
    expected.getMeta().setLastUpdated(null);
    actual.getMeta().setLastUpdated(null);
    // Created is required, but can't be compared
    assertNotNull(actual.getCreated());
    expected.setCreated(null);
    actual.setCreated(null);
    // Extensions may have `valueMoney` elements
    assertEquals(expected.getExtension().size(), actual.getExtension().size());
    for (int i = 0; i < expected.getExtension().size(); i++) {
        Extension expectedEx = expected.getExtension().get(i);
        Extension actualEx = actual.getExtension().get(i);
        // We have to deal with Money objects separately
        if (expectedEx.hasValue() && expectedEx.getValue() instanceof Money) {
            assertTrue(actualEx.getValue() instanceof Money);
            assertCurrencyEquals((Money) expectedEx.getValue(), (Money) actualEx.getValue());
            // Now remove since we validated so we can compare the rest directly
            expectedEx.setValue(null);
            actualEx.setValue(null);
        }
    }
    // SupportingInfo can have `valueQuantity` that has the 0 vs 0.0 issue
    assertEquals(expected.getSupportingInfo().size(), actual.getSupportingInfo().size());
    for (int i = 0; i < expected.getSupportingInfo().size(); i++) {
        SupportingInformationComponent expectedSup = expected.getSupportingInfo().get(i);
        SupportingInformationComponent actualSup = actual.getSupportingInfo().get(i);
        // We have to deal with Money objects separately
        if (expectedSup.hasValueQuantity()) {
            assertTrue(actualSup.hasValueQuantity());
            assertEquals(expectedSup.getValueQuantity().getValue().floatValue(), actualSup.getValueQuantity().getValue().floatValue(), 0.0);
            // Now remove since we validated so we can compare the rest directly
            expectedSup.setValue(null);
            actualSup.setValue(null);
        }
    }
    // line items
    assertEquals(expected.getItem().size(), actual.getItem().size());
    for (int i = 0; i < expected.getItem().size(); i++) {
        ItemComponent expectedItem = expected.getItem().get(i);
        ItemComponent actualItem = actual.getItem().get(i);
        // Compare value directly because SimpleQuantity vs Quantity can't be compared
        assertEquals(expectedItem.getQuantity().getValue().floatValue(), actualItem.getQuantity().getValue().floatValue(), 0.0);
        expectedItem.setQuantity(null);
        actualItem.setQuantity(null);
        assertEquals(expectedItem.getAdjudication().size(), actualItem.getAdjudication().size());
        for (int j = 0; j < expectedItem.getAdjudication().size(); j++) {
            AdjudicationComponent expectedAdj = expectedItem.getAdjudication().get(j);
            AdjudicationComponent actualAdj = actualItem.getAdjudication().get(j);
            // Here is where we start getting into trouble with "0" vs "0.0", so we do this manually
            if (expectedAdj.hasAmount()) {
                assertNotNull(actualAdj.getAmount());
                assertCurrencyEquals(expectedAdj.getAmount(), actualAdj.getAmount());
            } else {
                // If expected doesn't have an amount, actual shouldn't
                assertFalse(actualAdj.hasAmount());
            }
            // We just checked manually, so null them out and check the rest of the fields
            expectedAdj.setAmount(null);
            actualAdj.setAmount(null);
        }
    }
    // Total has the same problem with values
    assertEquals(expected.getTotal().size(), actual.getTotal().size());
    for (int i = 0; i < expected.getTotal().size(); i++) {
        TotalComponent expectedTot = expected.getTotal().get(i);
        TotalComponent actualTot = actual.getTotal().get(i);
        if (expectedTot.hasAmount()) {
            assertNotNull(actualTot.getAmount());
            assertCurrencyEquals(expectedTot.getAmount(), actualTot.getAmount());
        } else {
            // If expected doesn't have an amount, actual shouldn't
            assertFalse(actualTot.hasAmount());
        }
        expectedTot.setAmount(null);
        actualTot.setAmount(null);
    }
    // Benefit Balance Financial components
    assertEquals(expected.getBenefitBalance().size(), actual.getBenefitBalance().size());
    for (int i = 0; i < expected.getBenefitBalance().size(); i++) {
        BenefitBalanceComponent expectedBen = expected.getBenefitBalance().get(i);
        BenefitBalanceComponent actualBen = actual.getBenefitBalance().get(i);
        assertEquals(expectedBen.getFinancial().size(), actualBen.getFinancial().size());
        for (int j = 0; j < expectedBen.getFinancial().size(); j++) {
            BenefitComponent expectedFinancial = expectedBen.getFinancial().get(j);
            BenefitComponent actualFinancial = actualBen.getFinancial().get(j);
            // Are we dealing with Money?
            if (expectedFinancial.hasUsedMoney()) {
                assertNotNull(actualFinancial.getUsedMoney());
                assertCurrencyEquals(expectedFinancial.getUsedMoney(), actualFinancial.getUsedMoney());
                // Clean up
                expectedFinancial.setUsed(null);
                actualFinancial.setUsed(null);
            }
        }
    }
    // As does payment
    if (expected.hasPayment()) {
        assertTrue(actual.hasPayment());
        assertCurrencyEquals(expected.getPayment().getAmount(), actual.getPayment().getAmount());
    } else {
        // If expected doesn't have an amount, actual shouldn't
        assertFalse(actual.hasPayment());
    }
    expected.getPayment().setAmount(null);
    actual.getPayment().setAmount(null);
    // Now for the grand finale, we can do an `equalsDeep` on the rest
    assertTrue(expected.equalsDeep(actual));
}
Also used : Extension(org.hl7.fhir.r4.model.Extension) TotalComponent(org.hl7.fhir.r4.model.ExplanationOfBenefit.TotalComponent) Money(org.hl7.fhir.r4.model.Money) SupportingInformationComponent(org.hl7.fhir.r4.model.ExplanationOfBenefit.SupportingInformationComponent) ItemComponent(org.hl7.fhir.r4.model.ExplanationOfBenefit.ItemComponent) AdjudicationComponent(org.hl7.fhir.r4.model.ExplanationOfBenefit.AdjudicationComponent) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) Resource(org.hl7.fhir.r4.model.Resource) BenefitBalanceComponent(org.hl7.fhir.r4.model.ExplanationOfBenefit.BenefitBalanceComponent) BenefitComponent(org.hl7.fhir.r4.model.ExplanationOfBenefit.BenefitComponent)

Example 22 with Element

use of org.hl7.elm.r1.Element in project bigwarp by saalfeldlab.

the class BigWarp method loadSettings.

protected void loadSettings(final String xmlFilename) throws IOException, JDOMException {
    final SAXBuilder sax = new SAXBuilder();
    final Document doc = sax.build(xmlFilename);
    final Element root = doc.getRootElement();
    viewerP.stateFromXml(root.getChild("viewerP"));
    viewerQ.stateFromXml(root.getChild("viewerQ"));
    setupAssignments.restoreFromXml(root);
    bookmarks.restoreFromXml(root);
    activeSourcesDialogP.update();
    activeSourcesDialogQ.update();
    // auto-save settings
    Element autoSaveElem = root.getChild("autosave");
    final String autoSavePath = autoSaveElem.getChild("location").getText();
    final long autoSavePeriod = Integer.parseInt(autoSaveElem.getChild("period").getText());
    setAutosaveFolder(new File(autoSavePath));
    BigWarpAutoSaver.setAutosaveOptions(this, autoSavePeriod, autoSavePath);
    viewerFrameP.repaint();
    viewerFrameQ.repaint();
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) Element(org.jdom2.Element) Document(org.jdom2.Document) File(java.io.File)

Example 23 with Element

use of org.hl7.elm.r1.Element in project kindling by HL7.

the class Publisher method stripWhitespaceAndComments.

private void stripWhitespaceAndComments(Node node) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) node;
        Map<String, String> attrs = new HashMap<String, String>();
        for (int i = e.getAttributes().getLength() - 1; i >= 0; i--) {
            attrs.put(e.getAttributes().item(i).getNodeName(), e.getAttributes().item(i).getNodeValue());
            e.removeAttribute(e.getAttributes().item(i).getNodeName());
        }
        for (String n : attrs.keySet()) {
            e.setAttribute(n, attrs.get(n));
        }
    }
    for (int i = node.getChildNodes().getLength() - 1; i >= 0; i--) {
        Node c = node.getChildNodes().item(i);
        if (c.getNodeType() == Node.TEXT_NODE && c.getTextContent().trim().length() == 0)
            node.removeChild(c);
        else if (c.getNodeType() == Node.TEXT_NODE)
            c.setTextContent(c.getTextContent().trim());
        else if (c.getNodeType() == Node.COMMENT_NODE)
            node.removeChild(c);
        else if (c.getNodeType() == Node.ELEMENT_NODE)
            stripWhitespaceAndComments(c);
    }
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        node.appendChild(node.getOwnerDocument().createTextNode("\r\n"));
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode) ContactPoint(org.hl7.fhir.r5.model.ContactPoint)

Example 24 with Element

use of org.hl7.elm.r1.Element in project kindling by HL7.

the class Publisher method listLinks.

private void listLinks(Element xml, List<ExampleReference> refs) throws Exception {
    if (xml.getLocalName().equals("feed")) {
        Element n = XMLUtil.getFirstChild(xml);
        while (n != null) {
            if (n.getLocalName().equals("entry")) {
                Element c = XMLUtil.getNamedChild(n, "content");
                listLinks(XMLUtil.getFirstChild(c), refs);
            }
            n = XMLUtil.getNextSibling(n);
        }
    } else {
        String n = xml.getLocalName();
        if (!n.equals("Binary")) {
            ResourceDefn r = page.getDefinitions().getResourceByName(n);
            if (r == null)
                throw new Exception("Unable to find resource definition for " + n);
            List<Element> nodes = new ArrayList<Element>();
            nodes.add(xml);
            listLinks("/f:" + n, r.getRoot(), nodes, refs);
            Element e = XMLUtil.getFirstChild(xml);
            while (e != null) {
                if (e.getNodeName().equals("contained")) {
                    listLinks(XMLUtil.getFirstChild(e), refs);
                }
                e = XMLUtil.getNextSibling(e);
            }
        }
    }
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) ResourceDefn(org.hl7.fhir.definitions.model.ResourceDefn) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) FHIRException(org.hl7.fhir.exceptions.FHIRException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 25 with Element

use of org.hl7.elm.r1.Element in project kindling by HL7.

the class Publisher method checkElement.

private void checkElement(StructureDefinition sd, ElementDefinition ed, boolean inDiff) {
    check(ed.hasPath(), sd, "Element has no path");
    Set<String> codes = new HashSet<String>();
    for (TypeRefComponent tr : ed.getType()) {
        String tc = tr.getWorkingCode();
        if (codes.contains(tc))
            check(false, sd, ed.getPath() + ": type '" + tc + "' is duplicated");
        if ((!inDiff || tr.hasCode()) && tc != null)
            if (ed.getPath().contains("."))
                check(page.getDefinitions().hasBaseType(tc) || tc.equals("Resource"), sd, ed.getPath() + ": type '" + tc + "' is not valid (a)");
            else if (sd.hasBaseDefinition()) {
                if (sd.getDerivation() == TypeDerivationRule.CONSTRAINT)
                    check(page.getDefinitions().hasConcreteResource(tc) || page.getDefinitions().hasBaseType(tc), sd, ed.getPath() + ": type '" + tc + "' is not valid (b)");
                else
                    check(page.getDefinitions().hasAbstractResource(tc) || tc.equals("Element"), sd, ed.getPath() + ": type '" + tc + "' is not valid (c)");
            }
        if (tr.hasProfile()) {
            check(tr.getProfile().size() == 1, sd, ed.getPath() + ": multiple profiles found: " + tr.getProfile());
            String pt = tr.getProfile().get(0).getValue();
            if (pt.contains("#")) {
                String[] parts = pt.split("\\#");
                StructureDefinition exd = page.getWorkerContext().fetchResource(StructureDefinition.class, parts[0]);
                if (exd == null)
                    check(false, sd, ed.getPath() + ": profile '" + pt + "' is not valid (definition not found)");
                else {
                    ElementDefinition ex = null;
                    for (ElementDefinition et : exd.getSnapshot().getElement()) if (et.hasFixed() && et.getFixed() instanceof UriType && ((UriType) et.getFixed()).asStringValue().equals(parts[1]))
                        ex = et;
                    check(ex != null, sd, ed.getPath() + ": profile '" + pt + "' is not valid (inner path not found)");
                }
            } else
                check((page.getWorkerContext().hasResource(StructureDefinition.class, pt)) || isStringPattern(tail(pt)), sd, ed.getPath() + ": profile '" + pt + "' is not valid (d)");
        }
        if (tr.hasTargetProfile()) {
            String pt = tr.getTargetProfile().get(0).getValue();
            if (pt.contains("#")) {
                String[] parts = pt.split("\\#");
                StructureDefinition exd = page.getWorkerContext().fetchResource(StructureDefinition.class, parts[0]);
                if (exd == null)
                    check(false, sd, ed.getPath() + ": target profile '" + pt + "' is not valid (definition not found)");
                else {
                    ElementDefinition ex = null;
                    for (ElementDefinition et : exd.getSnapshot().getElement()) if (et.hasFixed() && et.getFixed() instanceof UriType && ((UriType) et.getFixed()).asStringValue().equals(parts[1]))
                        ex = et;
                    check(ex != null, sd, ed.getPath() + ": target profile '" + pt + "' is not valid (inner path not found)");
                }
            } else
                check((page.getWorkerContext().hasResource(StructureDefinition.class, pt)) || isStringPattern(tail(pt)), sd, ed.getPath() + ": target profile '" + pt + "' is not valid (d)");
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) HashSet(java.util.HashSet) UriType(org.hl7.fhir.r5.model.UriType)

Aggregations

Element (org.jdom2.Element)3601 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)659 ArrayList (java.util.ArrayList)616 Document (org.jdom2.Document)589 Complex (org.hl7.fhir.dstu2016may.formats.RdfGenerator.Complex)488 Complex (org.hl7.fhir.dstu3.utils.formats.Turtle.Complex)486 Test (org.junit.Test)474 IOException (java.io.IOException)414 JDOMException (org.jdom2.JDOMException)230 Attribute (org.jdom2.Attribute)217 List (java.util.List)189 SAXBuilder (org.jdom2.input.SAXBuilder)169 File (java.io.File)162 FHIRException (org.hl7.fhir.exceptions.FHIRException)162 HashMap (java.util.HashMap)159 Namespace (org.jdom2.Namespace)157 Test (org.junit.jupiter.api.Test)154 Element (org.osate.aadl2.Element)143 XMLOutputter (org.jdom2.output.XMLOutputter)136 XConfiguration (org.apache.oozie.util.XConfiguration)98