Search in sources :

Example 81 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class Property method getChildProperties.

protected List<Property> getChildProperties(String elementName, String statedType) throws FHIRException {
    ElementDefinition ed = definition;
    StructureDefinition sd = structure;
    List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, ed);
    String url = null;
    if (children.isEmpty() || isElementWithOnlyExtension(ed, children)) {
        // ok, find the right definitions
        String t = null;
        if (ed.getType().size() == 1)
            t = ed.getType().get(0).getWorkingCode();
        else if (ed.getType().size() == 0)
            throw new Error("types == 0, and no children found on " + getDefinition().getPath());
        else {
            t = ed.getType().get(0).getWorkingCode();
            boolean all = true;
            for (TypeRefComponent tr : ed.getType()) {
                if (!tr.getWorkingCode().equals(t)) {
                    all = false;
                    break;
                }
            }
            if (!all) {
                // ok, it's polymorphic
                if (ed.hasRepresentation(PropertyRepresentation.TYPEATTR)) {
                    t = statedType;
                    if (t == null && ToolingExtensions.hasExtension(ed, "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaulttype"))
                        t = ToolingExtensions.readStringExtension(ed, "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaulttype");
                    boolean ok = false;
                    for (TypeRefComponent tr : ed.getType()) {
                        if (tr.getWorkingCode().equals(t))
                            ok = true;
                        if (Utilities.isAbsoluteUrl(tr.getWorkingCode())) {
                            StructureDefinition sdt = context.fetchResource(StructureDefinition.class, tr.getWorkingCode());
                            if (sdt != null && sdt.getType().equals(t)) {
                                url = tr.getWorkingCode();
                                ok = true;
                            }
                        }
                        if (ok)
                            break;
                    }
                    if (!ok)
                        throw new DefinitionException("Type '" + t + "' is not an acceptable type for '" + elementName + "' on property " + definition.getPath());
                } else {
                    t = elementName.substring(tail(ed.getPath()).length() - 3);
                    if (isPrimitive(lowFirst(t)))
                        t = lowFirst(t);
                }
            }
        }
        if (!"xhtml".equals(t)) {
            for (TypeRefComponent aType : ed.getType()) {
                if (aType.getWorkingCode().equals(t)) {
                    if (aType.hasProfile()) {
                        assert aType.getProfile().size() == 1;
                        url = aType.getProfile().get(0).getValue();
                    } else {
                        url = ProfileUtilities.sdNs(t, context.getOverrideVersionNs());
                    }
                    break;
                }
            }
            if (url == null)
                throw new FHIRException("Unable to find type " + t + " for element " + elementName + " with path " + ed.getPath());
            sd = context.fetchResource(StructureDefinition.class, url);
            if (sd == null)
                throw new DefinitionException("Unable to find type '" + t + "' for name '" + elementName + "' on property " + definition.getPath());
            children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElement().get(0));
        }
    }
    List<Property> properties = new ArrayList<Property>();
    for (ElementDefinition child : children) {
        properties.add(new Property(context, child, sd));
    }
    return properties;
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) ElementDefinition(org.hl7.fhir.r4.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 82 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class TurtleParser method parse.

private Element parse(Turtle src, TTLComplex cmp) throws FHIRException {
    TTLObject type = cmp.getPredicates().get("http://www.w3.org/2000/01/rdf-schema#type");
    if (type == null) {
        logError(cmp.getLine(), cmp.getCol(), "(document)", IssueType.INVALID, "Unknown resource type (missing rdfs:type)", IssueSeverity.FATAL);
        return null;
    }
    if (type instanceof TTLList) {
        // this is actually broken - really we have to look through the structure definitions at this point
        for (TTLObject obj : ((TTLList) type).getList()) {
            if (obj instanceof TTLURL && ((TTLURL) obj).getUri().startsWith(FHIR_URI_BASE)) {
                type = obj;
                break;
            }
        }
    }
    if (!(type instanceof TTLURL)) {
        logError(cmp.getLine(), cmp.getCol(), "(document)", IssueType.INVALID, "Unexpected datatype for rdfs:type)", IssueSeverity.FATAL);
        return null;
    }
    String name = ((TTLURL) type).getUri();
    String ns = name.substring(0, name.lastIndexOf("/"));
    name = name.substring(name.lastIndexOf("/") + 1);
    String path = "/" + name;
    StructureDefinition sd = getDefinition(cmp.getLine(), cmp.getCol(), ns, name);
    if (sd == null)
        return null;
    Element result = new Element(name, new Property(context, sd.getSnapshot().getElement().get(0), sd));
    result.markLocation(cmp.getLine(), cmp.getCol());
    result.setType(name);
    parseChildren(src, path, cmp, result, false);
    result.numberChildren();
    return result;
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) SpecialElement(org.hl7.fhir.r4.elementmodel.Element.SpecialElement) TTLURL(org.hl7.fhir.r4.utils.formats.Turtle.TTLURL) TTLObject(org.hl7.fhir.r4.utils.formats.Turtle.TTLObject) TTLList(org.hl7.fhir.r4.utils.formats.Turtle.TTLList)

Example 83 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class TurtleParser method parseChild.

private void parseChild(Turtle src, TTLComplex object, Element context, Set<String> processed, Property property, String path, String name) throws FHIRException {
    processed.add(name);
    String npath = path + "/" + property.getName();
    TTLObject e = object.getPredicates().get(FHIR_URI_BASE + name);
    if (e == null)
        return;
    if (property.isList() && (e instanceof TTLList)) {
        TTLList arr = (TTLList) e;
        for (TTLObject am : arr.getList()) {
            parseChildInstance(src, npath, object, context, property, name, am);
        }
    } else {
        parseChildInstance(src, npath, object, context, property, name, e);
    }
}
Also used : TTLObject(org.hl7.fhir.r4.utils.formats.Turtle.TTLObject) TTLList(org.hl7.fhir.r4.utils.formats.Turtle.TTLList)

Example 84 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class ObjectConverter method convertElement.

private Element convertElement(Property property, Base base) throws FHIRException {
    if (base == null)
        return null;
    String tn = base.fhirType();
    StructureDefinition sd = context.fetchResource(StructureDefinition.class, ProfileUtilities.sdNs(tn, context.getOverrideVersionNs()));
    if (sd == null)
        throw new FHIRException("Unable to find definition for type " + tn);
    Element res = new Element(property.getName(), property);
    if (sd.getKind() == StructureDefinitionKind.PRIMITIVETYPE)
        res.setValue(((PrimitiveType) base).asStringValue());
    List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElementFirstRep());
    for (ElementDefinition child : children) {
        String n = tail(child.getPath());
        if (sd.getKind() != StructureDefinitionKind.PRIMITIVETYPE || !"value".equals(n)) {
            Base[] values = base.getProperty(n.hashCode(), n, false);
            if (values != null)
                for (Base value : values) {
                    res.getChildren().add(convertElement(new Property(context, child, sd), value));
                }
        }
    }
    return res;
}
Also used : StructureDefinition(org.hl7.fhir.r4.model.StructureDefinition) PrimitiveType(org.hl7.fhir.r4.model.PrimitiveType) ElementDefinition(org.hl7.fhir.r4.model.ElementDefinition) FHIRException(org.hl7.fhir.exceptions.FHIRException) Base(org.hl7.fhir.r4.model.Base)

Example 85 with Property

use of org.hl7.fhir.r4b.model.Property in project org.hl7.fhir.core by hapifhir.

the class Element method setProperty.

@Override
public Base setProperty(int hash, String name, Base value) throws FHIRException {
    if ("xhtml".equals(getType()) && (hash == "value".hashCode())) {
        this.xhtml = castToXhtml(value);
        this.value = castToXhtmlString(value);
        return this;
    }
    if (isPrimitive() && (hash == "value".hashCode())) {
        this.value = castToString(value).asStringValue();
        return this;
    }
    if (!value.isPrimitive() && !(value instanceof Element)) {
        if (isDataType(value))
            value = convertToElement(property.getChild(name), value);
        else
            throw new FHIRException("Cannot set property " + name + " on " + this.name + " - value is not a primitive type (" + value.fhirType() + ") or an ElementModel type");
    }
    if (children == null)
        children = new ArrayList<Element>();
    Element childForValue = null;
    // look through existing children
    for (Element child : children) {
        if (child.getName().equals(name)) {
            if (!child.isList()) {
                childForValue = child;
                break;
            } else {
                Element ne = new Element(child);
                children.add(ne);
                numberChildren();
                childForValue = ne;
                break;
            }
        }
    }
    int i = 0;
    if (childForValue == null)
        for (Property p : property.getChildProperties(this.name, type)) {
            int t = -1;
            for (int c = 0; c < children.size(); c++) {
                Element e = children.get(c);
                if (p.getName().equals(e.getName()))
                    t = c;
            }
            if (t > i)
                i = t;
            if (p.getName().equals(name) || p.getName().equals(name + "[x]")) {
                Element ne = new Element(name, p);
                children.add(i, ne);
                childForValue = ne;
                break;
            }
        }
    if (childForValue == null)
        throw new Error("Cannot set property " + name + " on " + this.name);
    else if (value.isPrimitive()) {
        if (childForValue.property.getName().endsWith("[x]"))
            childForValue.name = name + Utilities.capitalize(value.fhirType());
        childForValue.setValue(value.primitiveValue());
    } else {
        Element ve = (Element) value;
        childForValue.type = ve.getType();
        if (childForValue.property.getName().endsWith("[x]"))
            childForValue.name = name + Utilities.capitalize(childForValue.type);
        else if (value.isResource()) {
            if (childForValue.elementProperty == null)
                childForValue.elementProperty = childForValue.property;
            childForValue.property = ve.property;
            childForValue.special = SpecialElement.BUNDLE_ENTRY;
        }
        if (ve.children != null) {
            if (childForValue.children == null)
                childForValue.children = new ArrayList<Element>();
            else
                childForValue.children.clear();
            childForValue.children.addAll(ve.children);
        }
    }
    return childForValue;
}
Also used : ArrayList(java.util.ArrayList) FHIRException(org.hl7.fhir.exceptions.FHIRException)

Aggregations

ArrayList (java.util.ArrayList)35 FHIRException (org.hl7.fhir.exceptions.FHIRException)35 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)30 JsonElement (com.google.gson.JsonElement)23 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)21 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)15 JsonObject (com.google.gson.JsonObject)14 List (java.util.List)14 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)14 Complex (org.hl7.fhir.r4.utils.formats.Turtle.Complex)13 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)13 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)13 ElementDefinition (org.hl7.fhir.dstu3.model.ElementDefinition)11 HashMap (java.util.HashMap)8 HashSet (java.util.HashSet)8 Collectors (java.util.stream.Collectors)8 ElementDefinition (org.hl7.fhir.r4.model.ElementDefinition)8 SpecialElement (org.hl7.fhir.r4b.elementmodel.Element.SpecialElement)8 NamedElement (org.hl7.fhir.r4b.elementmodel.ParserBase.NamedElement)8 SpecialElement (org.hl7.fhir.r5.elementmodel.Element.SpecialElement)8