Search in sources :

Example 71 with DataType

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

the class JavaParserJsonGenerator method genElementParser.

private void genElementParser(Analysis analysis, TypeInfo ti, ElementDefinition ed, boolean bUseOwner, ElementDefinition inh) throws Exception {
    String name = ed.getName();
    String tn = ed.getUserString("java.type");
    if (name.endsWith("[x]") || name.equals("[type]")) {
        String en = name.endsWith("[x]") && !name.equals("[x]") ? name.replace("[x]", "") : "value";
        String pfx = name.endsWith("[x]") ? name.replace("[x]", "") : "";
        parser.append("    DataType " + getElementName(en, false) + " = parseType(\"" + en + "\", json);\r\n");
        parser.append("    if (" + getElementName(en, false) + " != null)\r\n");
        parser.append("      res.set" + upFirst(getElementName(en, false)) + "(" + getElementName(en, false) + ");\r\n");
    } else {
        String prsr = null;
        String aprsr = null;
        String anprsr = null;
        EnumInfo ei = null;
        String en = null;
        if (ed.hasUserData("java.enum")) {
            // getCodeListType(cd.getBinding());
            ei = (EnumInfo) ed.getUserData("java.enum");
            ValueSet vs = ei.getValueSet();
            if (vs.hasUserData("shared")) {
                en = "Enumerations." + ei.getName();
            } else {
                en = analysis.getClassName() + "." + ei.getName();
            }
            // en+".fromCode(parseString(xpp))";
            prsr = "parseEnumeration(json.get(\"" + name + "\").getAsString(), " + en + ".NULL, new " + en.substring(0, en.indexOf(".")) + "." + en.substring(en.indexOf(".") + 1) + "EnumFactory())";
            // en+".fromCode(parseString(xpp))";
            aprsr = "parseEnumeration(array.get(i).getAsString(), " + en + ".NULL, new " + en.substring(0, en.indexOf(".")) + "." + en.substring(en.indexOf(".") + 1) + "EnumFactory())";
            // en+".fromCode(parseString(xpp))";
            anprsr = "parseEnumeration(null, " + en + ".NULL, new " + en.substring(0, en.indexOf(".")) + "." + en.substring(en.indexOf(".") + 1) + "EnumFactory())";
        // parseEnumeration(xpp, Narratived.NarrativeStatus.additional, new Narratived.NarrativeStatusEnumFactory())
        } else {
            if (name.equals("extension")) {
                name = "extension";
                tn = "Extension";
            }
            if (tn.equals("XhtmlNode")) {
                prsr = "parseXhtml(json.get(\"" + name + "\").getAsString())";
            } else if (tn.contains("Reference(")) {
                prsr = "parseReference(getJObject(json, \"" + name + "\"))";
                aprsr = "parseReference(array.get(i).getAsJsonObject())";
                anprsr = "parseReference(null)";
            } else if (tn.contains("canonical(")) {
                prsr = "parseCanonical(json.get(\"" + name + "\").getAsString())";
                aprsr = "parseCanonical(array.get(i).getAsString())";
                anprsr = "parseCanonical(null)";
            } else if (isPrimitive(ed.typeSummary())) {
                if (tn.endsWith("Type")) {
                    tn = tn.substring(0, tn.length() - 4);
                }
                prsr = "parse" + upFirst(tn) + "(json.get(\"" + name + "\").getAs" + getAsJsonPrimitive(ed.typeSummary(), true) + "())";
                aprsr = "parse" + upFirst(tn) + "(array.get(i).getAs" + getAsJsonPrimitive(ed.typeSummary(), true) + "())";
                anprsr = "parse" + upFirst(tn) + "(null)";
            } else {
                String pn = tn;
                if ((ed.isInlineType() || ed.hasContentReference()) && !pn.startsWith(analysis.getClassName())) {
                    pn = analysis.getClassName() + pn;
                }
                prsr = "parse" + pn + "(getJObject(json, \"" + name + "\"))";
                aprsr = "parse" + pn + "(array.get(i).getAsJsonObject())";
                anprsr = "parse" + pn + "(null)";
            }
        }
        if (ed.unbounded()) {
            if (isPrimitive(ed.typeSummary()) || ed.typeSummary().startsWith("canonical(")) {
                parser.append("    if (json.has(\"" + name + "\")) {\r\n");
                parser.append("      JsonArray array = getJArray(json, \"" + name + "\");\r\n");
                parser.append("      for (int i = 0; i < array.size(); i++) {\r\n");
                parser.append("        if (array.get(i).isJsonNull()) {\r\n");
                if (en == null) {
                    parser.append("          res.get" + upFirst(name) + "().add(new " + tn + "Type());\r\n");
                } else {
                    parser.append("          res.get" + upFirst(name) + "().add(new Enumeration<" + en + ">(new " + en + "EnumFactory(), " + en + ".NULL));\r\n");
                }
                parser.append("        } else {;\r\n");
                parser.append("          res.get" + upFirst(name) + "().add(" + aprsr + ");\r\n");
                parser.append("        }\r\n");
                parser.append("      }\r\n");
                parser.append("    };\r\n");
                parser.append("    if (json.has(\"_" + name + "\")) {\r\n");
                parser.append("      JsonArray array = getJArray(json, \"_" + name + "\");\r\n");
                parser.append("      for (int i = 0; i < array.size(); i++) {\r\n");
                parser.append("        if (i == res.get" + upFirst(name) + "().size())\r\n");
                parser.append("          res.get" + upFirst(name) + "().add(" + anprsr + ");\r\n");
                parser.append("        if (array.get(i) instanceof JsonObject) \r\n");
                parser.append("          parseElementProperties(array.get(i).getAsJsonObject(), res.get" + upFirst(name) + "().get(i));\r\n");
                parser.append("      }\r\n");
                parser.append("    };\r\n");
            } else {
                parser.append("    if (json.has(\"" + name + "\")) {\r\n");
                parser.append("      JsonArray array = getJArray(json, \"" + name + "\");\r\n");
                parser.append("      for (int i = 0; i < array.size(); i++) {\r\n");
                parser.append("        res.get" + upFirst(getElementName(name, false)) + "().add(" + aprsr + ");\r\n");
                parser.append("      }\r\n");
                parser.append("    };\r\n");
            }
        } else if (inh != null && inh.unbounded()) {
            parser.append("    if (json.has(\"" + name + "\"))\r\n");
            if ((isPrimitive(ed.typeSummary()) || ed.typeSummary().startsWith("canonical(")) && !tn.equals("XhtmlNode")) {
                parser.append("      res.add" + upFirst(getElementName(name, false)) + "Element(" + prsr + ");\r\n");
                parser.append("    if (json.has(\"_" + name + "\"))\r\n");
                parser.append("      parseElementProperties(getJObject(json, \"_" + name + "\"), res.get" + upFirst(getElementName(name, false)) + "ElementFirstRep());\r\n");
            } else {
                parser.append("      res.add" + upFirst(getElementName(name, false)) + "(" + prsr + ");\r\n");
            }
        } else {
            parser.append("    if (json.has(\"" + name + "\"))\r\n");
            if ((isPrimitive(ed.typeSummary()) || ed.typeSummary().startsWith("canonical(")) && !tn.equals("XhtmlNode")) {
                parser.append("      res.set" + upFirst(getElementName(name, false)) + "Element(" + prsr + ");\r\n");
                parser.append("    if (json.has(\"_" + name + "\"))\r\n");
                parser.append("      parseElementProperties(getJObject(json, \"_" + name + "\"), res.get" + upFirst(getElementName(name, false)) + "Element());\r\n");
            } else {
                parser.append("      res.set" + upFirst(getElementName(name, false)) + "(" + prsr + ");\r\n");
            }
        }
    }
}
Also used : EnumInfo(org.hl7.fhir.core.generator.analysis.EnumInfo) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 72 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 73 with DataType

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

the class XmlParserBase method compose.

@Override
public void compose(OutputStream stream, DataType type, String rootName) throws IOException {
    xml = new XMLWriter(stream, "UTF-8");
    xml.setPretty(style == OutputStyle.PRETTY);
    xml.start();
    xml.setDefaultNamespace(FHIR_NS);
    composeType(Utilities.noString(rootName) ? "value" : rootName, type);
    xml.end();
}
Also used : IXMLWriter(org.hl7.fhir.utilities.xml.IXMLWriter) XMLWriter(org.hl7.fhir.utilities.xml.XMLWriter)

Example 74 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 CHOICE:
            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.r4b.model.Enumeration) StringType(org.hl7.fhir.r4b.model.StringType) Reference(org.hl7.fhir.r4b.model.Reference) BooleanType(org.hl7.fhir.r4b.model.BooleanType) Quantity(org.hl7.fhir.r4b.model.Quantity) FHIRException(org.hl7.fhir.exceptions.FHIRException) TimeType(org.hl7.fhir.r4b.model.TimeType) DateTimeType(org.hl7.fhir.r4b.model.DateTimeType) UriType(org.hl7.fhir.r4b.model.UriType) IntegerType(org.hl7.fhir.r4b.model.IntegerType) DateTimeType(org.hl7.fhir.r4b.model.DateTimeType) Coding(org.hl7.fhir.r4b.model.Coding) DecimalType(org.hl7.fhir.r4b.model.DecimalType) DateType(org.hl7.fhir.r4b.model.DateType)

Example 75 with DataType

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

the class StructureMapUtilities method analyseTarget.

private void analyseTarget(String ruleId, TransformContext context, VariablesForProfiling vars, StructureMap map, StructureMapGroupRuleTargetComponent tgt, String tv, TargetWriter tw, List<StructureDefinition> profiles, String sliceName) throws FHIRException {
    VariableForProfiling var = null;
    if (tgt.hasContext()) {
        var = vars.get(VariableMode.OUTPUT, tgt.getContext());
        if (var == null)
            throw new FHIRException("Rule \"" + ruleId + "\": target context not known: " + tgt.getContext());
        if (!tgt.hasElement())
            throw new FHIRException("Rule \"" + ruleId + "\": Not supported yet");
    }
    TypeDetails type = null;
    if (tgt.hasTransform()) {
        type = analyseTransform(context, map, tgt, var, vars);
    } else {
        Property vp = var.getProperty().getBaseProperty().getChild(tgt.getElement(), tgt.getElement());
        if (vp == null)
            throw new FHIRException("Unknown Property " + tgt.getElement() + " on " + var.getProperty().getPath());
        type = new TypeDetails(CollectionStatus.SINGLETON, vp.getType(tgt.getElement()));
    }
    if (tgt.getTransform() == StructureMapTransform.CREATE) {
        String s = getParamString(vars, tgt.getParameter().get(0));
        if (worker.getResourceNames().contains(s))
            tw.newResource(tgt.getVariable(), s);
    } else {
        boolean mapsSrc = false;
        for (StructureMapGroupRuleTargetParameterComponent p : tgt.getParameter()) {
            DataType pr = p.getValue();
            if (pr instanceof IdType && ((IdType) pr).asStringValue().equals(tv))
                mapsSrc = true;
        }
        if (mapsSrc) {
            if (var == null)
                throw new Error("Rule \"" + ruleId + "\": Attempt to assign with no context");
            tw.valueAssignment(tgt.getContext(), var.getProperty().getPath() + "." + tgt.getElement() + getTransformSuffix(tgt.getTransform()));
        } else if (tgt.hasContext()) {
            if (isSignificantElement(var.getProperty(), tgt.getElement())) {
                String td = describeTransform(tgt);
                if (td != null)
                    tw.keyAssignment(tgt.getContext(), var.getProperty().getPath() + "." + tgt.getElement() + " = " + td);
            }
        }
    }
    DataType fixed = generateFixedValue(tgt);
    PropertyWithType prop = updateProfile(var, tgt.getElement(), type, map, profiles, sliceName, fixed, tgt);
    if (tgt.hasVariable())
        if (tgt.hasElement())
            vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
        else
            vars.add(VariableMode.OUTPUT, tgt.getVariable(), prop);
}
Also used : FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) FHIRException(org.hl7.fhir.exceptions.FHIRException) Property(org.hl7.fhir.r5.elementmodel.Property)

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