Search in sources :

Example 26 with DataType

use of org.hl7.fhir.r5.model.DataType in project org.hl7.fhir.core by hapifhir.

the class ExtensionHelper method setExtension.

/**
 * set the value of an extension on the element. if value == null, make sure it doesn't exist
 *
 * @param element - the element to act on. Can also be a backbone element
 * @param modifier - whether this is a modifier. Note that this is a definitional property of the extension; don't alternate
 * @param uri - the identifier for the extension
 * @param value - the value of the extension. Delete if this is null
 * @throws Exception - if the modifier logic is incorrect
 */
public static void setExtension(Element element, boolean modifier, String uri, DataType value) throws FHIRException {
    if (value == null) {
        // deleting the extension
        if (element instanceof BackboneElement)
            for (Extension e : ((BackboneElement) element).getModifierExtension()) {
                if (uri.equals(e.getUrl()))
                    ((BackboneElement) element).getModifierExtension().remove(e);
            }
        for (Extension e : element.getExtension()) {
            if (uri.equals(e.getUrl()))
                element.getExtension().remove(e);
        }
    } else {
        // it would probably be easier to delete and then create, but this would re-order the extensions
        // not that order matters, but we'll preserve it anyway
        boolean found = false;
        if (element instanceof BackboneElement)
            for (Extension e : ((BackboneElement) element).getModifierExtension()) {
                if (uri.equals(e.getUrl())) {
                    if (!modifier)
                        throw new FHIRException("Error adding extension \"" + uri + "\": found an existing modifier extension, and the extension is not marked as a modifier");
                    e.setValue(value);
                    found = true;
                }
            }
        for (Extension e : element.getExtension()) {
            if (uri.equals(e.getUrl())) {
                if (modifier)
                    throw new FHIRException("Error adding extension \"" + uri + "\": found an existing extension, and the extension is marked as a modifier");
                e.setValue(value);
                found = true;
            }
        }
        if (!found) {
            Extension ex = new Extension().setUrl(uri).setValue(value);
            if (modifier) {
                if (!(element instanceof BackboneElement))
                    throw new FHIRException("Error adding extension \"" + uri + "\": extension is marked as a modifier, but element is not a backbone element");
                ((BackboneElement) element).getModifierExtension().add(ex);
            } else {
                element.getExtension().add(ex);
            }
        }
    }
}
Also used : FHIRException(org.hl7.fhir.exceptions.FHIRException)

Example 27 with DataType

use of org.hl7.fhir.r5.model.DataType in project org.hl7.fhir.core by hapifhir.

the class DiagnosticReportRenderer method addObservationToTable.

private void addObservationToTable(XhtmlNode tr, ResourceWrapper obs, int i, String ref, boolean refRange, boolean flags, boolean note, boolean effectiveTime, boolean issued, DataType eff, DataType iss) throws UnsupportedEncodingException, FHIRException, IOException {
    // code (+bodysite)
    XhtmlNode td = tr.td();
    PropertyWrapper pw = getProperty(obs, "code");
    if (valued(pw)) {
        render(td.ah(ref), pw.value());
    }
    pw = getProperty(obs, "bodySite");
    if (valued(pw)) {
        td.tx(" (");
        render(td, pw.value());
        td.tx(")");
    }
    // value / dataAbsentReason (in red)
    td = tr.td();
    pw = getProperty(obs, "value[x]");
    if (valued(pw)) {
        render(td, pw.value());
    } else {
        pw = getProperty(obs, "dataAbsentReason");
        if (valued(pw)) {
            XhtmlNode span = td.span("color: maroon", "Error");
            span.tx("Error: ");
            render(span.b(), pw.value());
        }
    }
    if (refRange) {
        // reference range
        td = tr.td();
        pw = getProperty(obs, "referenceRange");
        if (valued(pw)) {
            boolean first = true;
            for (BaseWrapper v : pw.getValues()) {
                if (first)
                    first = false;
                else
                    td.br();
                PropertyWrapper pwr = getProperty(v, "type");
                if (valued(pwr)) {
                    render(td, pwr.value());
                    td.tx(": ");
                }
                PropertyWrapper pwt = getProperty(v, "text");
                if (valued(pwt)) {
                    render(td, pwt.value());
                } else {
                    PropertyWrapper pwl = getProperty(v, "low");
                    PropertyWrapper pwh = getProperty(v, "high");
                    if (valued(pwl) && valued(pwh)) {
                        render(td, pwl.value());
                        td.tx(" - ");
                        render(td, pwh.value());
                    } else if (valued(pwl)) {
                        td.tx(">");
                        render(td, pwl.value());
                    } else if (valued(pwh)) {
                        td.tx("<");
                        render(td, pwh.value());
                    } else {
                        td.tx("??");
                    }
                }
                pwr = getProperty(v, "appliesTo");
                PropertyWrapper pwrA = getProperty(v, "age");
                if (valued(pwr) || valued(pwrA)) {
                    boolean firstA = true;
                    td.tx(" for ");
                    if (valued(pwr)) {
                        for (BaseWrapper va : pwr.getValues()) {
                            if (firstA)
                                firstA = false;
                            else
                                td.tx(", ");
                            render(td, va);
                        }
                    }
                    if (valued(pwrA)) {
                        if (firstA)
                            firstA = false;
                        else
                            td.tx(", ");
                        td.tx("Age ");
                        render(td, pwrA.value());
                    }
                }
            }
        }
    }
    if (flags) {
        // flags (status other than F, interpretation, )
        td = tr.td();
        boolean first = true;
        pw = getProperty(obs, "status");
        if (valued(pw)) {
            if (!pw.value().getBase().primitiveValue().equals("final")) {
                if (first)
                    first = false;
                else
                    td.br();
                render(td, pw.value());
            }
        }
        pw = getProperty(obs, "interpretation");
        if (valued(pw)) {
            for (BaseWrapper v : pw.getValues()) {
                if (first)
                    first = false;
                else
                    td.br();
                render(td, v);
            }
        }
    }
    if (note) {
        td = tr.td();
        pw = getProperty(obs, "note");
        if (valued(pw)) {
            render(td, pw.value());
        }
    }
    if (effectiveTime) {
        // effective if different to DR
        td = tr.td();
        pw = getProperty(obs, "effective[x]");
        if (valued(pw)) {
            if (!Base.compareDeep(pw.value().getBase(), eff, true)) {
                render(td, pw.value());
            }
        }
    }
    if (issued) {
        // issued if different to DR
        td = tr.td();
        pw = getProperty(obs, "issued");
        if (valued(pw)) {
            if (!Base.compareDeep(pw.value().getBase(), eff, true)) {
                render(td, pw.value());
            }
        }
    }
}
Also used : PropertyWrapper(org.hl7.fhir.r5.renderers.utils.BaseWrappers.PropertyWrapper) BaseWrapper(org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Example 28 with DataType

use of org.hl7.fhir.r5.model.DataType in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireBuilder method convertType.

@SuppressWarnings("unchecked")
private DataType convertType(Base value, QuestionnaireItemType af, ValueSet vs, String path) throws FHIRException {
    switch(af) {
        // simple cases
        case BOOLEAN:
            if (value instanceof BooleanType)
                return (DataType) value;
            break;
        case DECIMAL:
            if (value instanceof DecimalType)
                return (DataType) value;
            break;
        case INTEGER:
            if (value instanceof IntegerType)
                return (DataType) value;
            break;
        case DATE:
            if (value instanceof DateType)
                return (DataType) value;
            break;
        case DATETIME:
            if (value instanceof DateTimeType)
                return (DataType) value;
            break;
        case TIME:
            if (value instanceof TimeType)
                return (DataType) value;
            break;
        case STRING:
            if (value instanceof StringType)
                return (DataType) value;
            else if (value instanceof UriType)
                return new StringType(((UriType) value).asStringValue());
            break;
        case TEXT:
            if (value instanceof StringType)
                return (DataType) value;
            break;
        case QUANTITY:
            if (value instanceof Quantity)
                return (DataType) value;
            break;
        // ? QuestionnaireItemTypeAttachment: ...?
        case CODING:
            if (value instanceof Coding)
                return (DataType) value;
            else if (value instanceof Enumeration) {
                Coding cc = new Coding();
                cc.setCode(((Enumeration<Enum<?>>) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            } else if (value instanceof StringType) {
                Coding cc = new Coding();
                cc.setCode(((StringType) value).asStringValue());
                cc.setSystem(getSystemForCode(vs, cc.getCode(), path));
                return cc;
            }
            break;
        case REFERENCE:
            if (value instanceof Reference)
                return (DataType) value;
            else if (value instanceof StringType) {
                Reference r = new Reference();
                r.setReference(((StringType) value).asStringValue());
            }
            break;
        default:
            break;
    }
    throw new FHIRException("Unable to convert from '" + value.getClass().toString() + "' for Answer Format " + af.toCode() + ", path = " + path);
}
Also used : Enumeration(org.hl7.fhir.r5.model.Enumeration) StringType(org.hl7.fhir.r5.model.StringType) Reference(org.hl7.fhir.r5.model.Reference) BooleanType(org.hl7.fhir.r5.model.BooleanType) Quantity(org.hl7.fhir.r5.model.Quantity) FHIRException(org.hl7.fhir.exceptions.FHIRException) TimeType(org.hl7.fhir.r5.model.TimeType) DateTimeType(org.hl7.fhir.r5.model.DateTimeType) UriType(org.hl7.fhir.r5.model.UriType) IntegerType(org.hl7.fhir.r5.model.IntegerType) DateTimeType(org.hl7.fhir.r5.model.DateTimeType) Coding(org.hl7.fhir.r5.model.Coding) DecimalType(org.hl7.fhir.r5.model.DecimalType) DateType(org.hl7.fhir.r5.model.DateType)

Example 29 with DataType

use of org.hl7.fhir.r5.model.DataType 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 30 with DataType

use of org.hl7.fhir.r5.model.DataType in project org.hl7.fhir.core by hapifhir.

the class QuestionnaireResponseRenderer method renderAnswer.

public void renderAnswer(HierarchicalTableGenerator gen, ResourceWrapper q, Row r, BaseWrapper ans) throws UnsupportedEncodingException, IOException {
    List<BaseWrapper> items;
    Base b = ans.get("value[x]");
    if (b == null) {
        r.getCells().add(gen.new Cell(null, null, "null!", null, null));
    } else if (b.isPrimitive()) {
        r.getCells().add(gen.new Cell(null, null, b.primitiveValue(), null, null));
    } else {
        XhtmlNode x = new XhtmlNode(NodeType.Element, "span");
        Cell cell = gen.new Cell(null, null, null, null, null);
        Piece p = gen.new Piece("span");
        p.getChildren().add(x);
        cell.addPiece(p);
        render(x, (DataType) b);
        r.getCells().add(cell);
    }
    items = ans.children("item");
    for (BaseWrapper si : items) {
        renderTreeItem(gen, r.getSubRows(), q, si);
    }
}
Also used : BaseWrapper(org.hl7.fhir.r5.renderers.utils.BaseWrappers.BaseWrapper) Piece(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece) DataType(org.hl7.fhir.r5.model.DataType) Cell(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Cell) Base(org.hl7.fhir.r5.model.Base) XhtmlNode(org.hl7.fhir.utilities.xhtml.XhtmlNode)

Aggregations

DataType (org.hl7.fhir.r5.model.DataType)14 FHIRException (org.hl7.fhir.exceptions.FHIRException)11 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)11 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)7 Collectors (java.util.stream.Collectors)6 DataType (org.hl7.cql.model.DataType)6 ModelInfo (org.hl7.elm_modelinfo.r1.ModelInfo)6 Piece (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece)6 ArrayList (java.util.ArrayList)5 StructType (org.apache.spark.sql.types.StructType)5 BooleanType (org.hl7.fhir.r5.model.BooleanType)5 ValueSet (org.hl7.fhir.r5.model.ValueSet)5 Arrays (java.util.Arrays)4 QName (javax.xml.namespace.QName)4 DataType (org.hl7.fhir.r4b.model.DataType)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3