Search in sources :

Example 6 with Enumerations

use of org.hl7.fhir.r4.model.Enumerations 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)

Aggregations

ValueSet (org.hl7.fhir.r5.model.ValueSet)5 EnumInfo (org.hl7.fhir.core.generator.analysis.EnumInfo)4 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Configuration (org.hl7.fhir.core.generator.codegen.Configuration)1 JavaConstantsGenerator (org.hl7.fhir.core.generator.codegen.JavaConstantsGenerator)1 JavaEnumerationsGenerator (org.hl7.fhir.core.generator.codegen.JavaEnumerationsGenerator)1 JavaFactoryGenerator (org.hl7.fhir.core.generator.codegen.JavaFactoryGenerator)1 JavaParserJsonGenerator (org.hl7.fhir.core.generator.codegen.JavaParserJsonGenerator)1 JavaParserRdfGenerator (org.hl7.fhir.core.generator.codegen.JavaParserRdfGenerator)1 JavaParserXmlGenerator (org.hl7.fhir.core.generator.codegen.JavaParserXmlGenerator)1 JavaTypeGenerator (org.hl7.fhir.core.generator.codegen.JavaTypeGenerator)1 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)1 FilesystemPackageCacheManager (org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager)1 NpmPackage (org.hl7.fhir.utilities.npm.NpmPackage)1