Search in sources :

Example 1 with EnumInfo

use of org.hl7.fhir.core.generator.analysis.EnumInfo in project org.hl7.fhir.core by hapifhir.

the class JavaResourceGenerator method generateEnum.

private void generateEnum(EnumInfo e) throws Exception {
    String tn = e.getName();
    String tns = tn;
    if (tn.startsWith("Enumeration<")) {
        tns = tn.substring(tn.indexOf("<") + 1);
        tns = tns.substring(0, tns.length() - 1);
    } else {
        tn = "Enumeration<" + tn + ">";
    }
    ValueSet vs = e.getValueSet();
    ValueSet vse = (ValueSet) vs.getUserData("expansion");
    if (vs.hasUserData("shared")) {
        return;
    }
    if (vse == null) {
        return;
    }
    List<ValueSetExpansionContainsComponent> codes = vse.getExpansion().getContains();
    String url = vs.getUrl();
    CommaSeparatedStringBuilder el = new CommaSeparatedStringBuilder();
    write("    public enum " + tns + " {\r\n");
    int l = codes.size();
    int i = 0;
    for (ValueSetExpansionContainsComponent c : codes) {
        i++;
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        el.append(cc);
        String definition = definitions.getCodeDefinition(c.getSystem(), c.getCode());
        write("        /**\r\n");
        write("         * " + Utilities.escapeJava(definition) + "\r\n");
        write("         */\r\n");
        write("        " + cc.toUpperCase() + ", \r\n");
    }
    write("        /**\r\n");
    write("         * added to help the parsers with the generic types\r\n");
    write("         */\r\n");
    write("        NULL;\r\n");
    el.append("NULL");
    write("        public static " + tns + " fromCode(String codeString) throws FHIRException {\r\n");
    write("            if (codeString == null || \"\".equals(codeString))\r\n");
    write("                return null;\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        write("        if (\"" + c.getCode() + "\".equals(codeString))\r\n");
        write("          return " + cc + ";\r\n");
    }
    write("        if (Configuration.isAcceptInvalidEnums())\r\n");
    write("          return null;\r\n");
    write("        else\r\n");
    write("          throw new FHIRException(\"Unknown " + tns + " code '\"+codeString+\"'\");\r\n");
    write("        }\r\n");
    write("        public String toCode() {\r\n");
    write("          switch (this) {\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        write("            case " + cc + ": return \"" + c.getCode() + "\";\r\n");
    }
    write("            case NULL: return null;\r\n");
    write("            default: return \"?\";\r\n");
    write("          }\r\n");
    write("        }\r\n");
    write("        public String getSystem() {\r\n");
    write("          switch (this) {\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        write("            case " + cc + ": return \"" + c.getSystem() + "\";\r\n");
    }
    write("            case NULL: return null;\r\n");
    write("            default: return \"?\";\r\n");
    write("          }\r\n");
    write("        }\r\n");
    write("        public String getDefinition() {\r\n");
    write("          switch (this) {\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        String definition = definitions.getCodeDefinition(c.getSystem(), c.getCode());
        write("            case " + cc + ": return \"" + Utilities.escapeJava(definition) + "\";\r\n");
    }
    write("            case NULL: return null;\r\n");
    write("            default: return \"?\";\r\n");
    write("          }\r\n");
    write("        }\r\n");
    write("        public String getDisplay() {\r\n");
    write("          switch (this) {\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        write("            case " + cc + ": return \"" + Utilities.escapeJava(Utilities.noString(c.getDisplay()) ? c.getCode() : c.getDisplay()) + "\";\r\n");
    }
    write("            case NULL: return null;\r\n");
    write("            default: return \"?\";\r\n");
    write("          }\r\n");
    write("        }\r\n");
    write("    }\r\n");
    write("\r\n");
    write("  public static class " + tns + "EnumFactory implements EnumFactory<" + tns + "> {\r\n");
    write("    public " + tns + " fromCode(String codeString) throws IllegalArgumentException {\r\n");
    write("      if (codeString == null || \"\".equals(codeString))\r\n");
    write("            if (codeString == null || \"\".equals(codeString))\r\n");
    write("                return null;\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        write("        if (\"" + c.getCode() + "\".equals(codeString))\r\n");
        write("          return " + tns + "." + cc + ";\r\n");
    }
    write("        throw new IllegalArgumentException(\"Unknown " + tns + " code '\"+codeString+\"'\");\r\n");
    write("        }\r\n");
    write("        public Enumeration<" + tns + "> fromType(Base code) throws FHIRException {\r\n");
    write("          if (code == null)\r\n");
    write("            return null;\r\n");
    write("          if (code.isEmpty())\r\n");
    write("            return new Enumeration<" + tns + ">(this);\r\n");
    write("          String codeString = ((PrimitiveType) code).asStringValue();\r\n");
    write("          if (codeString == null || \"\".equals(codeString))\r\n");
    write("            return null;\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        write("        if (\"" + c.getCode() + "\".equals(codeString))\r\n");
        write("          return new Enumeration<" + tns + ">(this, " + tns + "." + cc + ");\r\n");
    }
    write("        throw new FHIRException(\"Unknown " + tns + " code '\"+codeString+\"'\");\r\n");
    write("        }\r\n");
    write("    public String toCode(" + tns + " code) {\r\n");
    for (ValueSetExpansionContainsComponent c : codes) {
        String cc = Utilities.camelCase(c.getCode());
        cc = makeConst(cc);
        write("      if (code == " + tns + "." + cc + ")\r\n        return \"" + c.getCode() + "\";\r\n");
    }
    write("      return \"?\";\r\n");
    write("      }\r\n");
    write("    public String toSystem(" + tns + " code) {\r\n");
    write("      return code.getSystem();\r\n");
    write("      }\r\n");
    write("    }\r\n");
    write("\r\n");
// enumInfo.put("org.hl7.fhir."+jid+".model."+name+"."+tns, url+"|"+el.toString());
}
Also used : ValueSetExpansionContainsComponent(org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent) CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 2 with EnumInfo

use of org.hl7.fhir.core.generator.analysis.EnumInfo in project org.hl7.fhir.core by hapifhir.

the class JavaResourceGenerator method writeSearchParameterField.

// private void generateImpl(String namenn, ResourceDefn resourceDefn, String jn, ElementDefinition root, String version, Date genDate) throws Exception {
// // TODO - restore this
// //    write("  public "+namenn+" get"+namenn+"() {\r\n");
// //    write("    return new "+jn+namenn+"Impl(this);\r\n");
// //    write("  }\r\n");
// //
// //    // now, generate the implementation
// //    JavaPatternImplGenerator jrg = new JavaPatternImplGenerator(new FileOutputStream(javaPatternDir+jn+namenn+"Impl.java"), definitions, adornments, enumInfo);
// //    jrg.generate(resourceDefn.getRoot(), jn, JavaGenClass.Resource, null, genDate, version, false, null, null, namenn, root);
// //    jrg.close();
// //
// }
private void writeSearchParameterField(String name, JavaGenClass clss, boolean isAbstract, SearchParameter sp, String code, String[] theCompositeOf, List<SearchParameter> searchParams, String rn) throws IOException {
    String constName = cleanSpName(code).toUpperCase();
    /*
     * SearchParamDefinition (SP_[name])
     */
    write(" /**\r\n");
    write("   * Search parameter: <b>" + code + "</b>\r\n");
    write("   * <p>\r\n");
    write("   * Description: <b>" + sp.getDescription() + "</b><br>\r\n");
    write("   * Type: <b>" + sp.getType().toCode() + "</b><br>\r\n");
    write("   * Path: <b>" + sp.getExpression() + "</b><br>\r\n");
    write("   * </p>\r\n");
    write("   */\r\n");
    write("  @SearchParamDefinition(name=\"" + code + "\", path=\"" + defaultString(sp.getExpression()) + "\", description=\"" + Utilities.escapeJava(sp.getDescription()) + "\", type=\"" + sp.getType().toCode() + "\"");
    if (theCompositeOf != null && theCompositeOf.length > 0) {
        write(", compositeOf={");
        for (int i = 0; i < theCompositeOf.length; i++) {
            if (i > 0) {
                write(", ");
            }
            write("\"" + theCompositeOf[i] + "\"");
        }
        write("}");
    }
    Set<String> providesMembershipIn = new TreeSet<String>();
    for (CompartmentDefinition next : this.definitions.getCompartments().getList()) {
        for (CompartmentDefinitionResourceComponent nextEntry : next.getResource()) {
            if (nextEntry.getCode().equals(upFirst(name))) {
                for (StringType nextPart : nextEntry.getParam()) {
                    if (nextPart.toString().equals(code)) {
                        providesMembershipIn.add(next.getName());
                    }
                }
            }
        }
    }
    if (providesMembershipIn.size() > 0) {
        write(", providesMembershipIn={ ");
        boolean first = true;
        for (String next : providesMembershipIn) {
            if (first) {
                first = false;
            } else {
                write(", ");
            }
            write("@ca.uhn.fhir.model.api.annotation.Compartment(name=\"" + upFirst(next) + "\")");
        }
        write(" }");
    }
    Set<String> targets = new TreeSet<>();
    for (CodeType c : sp.getTarget()) {
        targets.add(c.asStringValue());
    }
    if (targets != null && !targets.isEmpty() && !targets.contains("Any")) {
        write(", target={");
        boolean first = true;
        for (String nextTarget : targets) {
            if (first) {
                first = false;
            } else {
                write(", ");
            }
            write("List".equals(nextTarget) ? "ListResource" : nextTarget);
            write(".class");
        }
        write(" }");
    }
    write(" )\r\n");
    write("  public static final String SP_" + constName + " = \"" + code + "\";\r\n");
    String genericTypes = "";
    if (theCompositeOf != null && theCompositeOf.length > 0) {
        SearchParameter typeDef0 = findSearchParam(searchParams, theCompositeOf[0]);
        SearchParameter typeDef1 = findSearchParam(searchParams, theCompositeOf[1]);
        genericTypes = "<ca.uhn.fhir.rest.gclient." + upFirst(typeDef0.getType().toCode()) + "ClientParam" + ", ca.uhn.fhir.rest.gclient." + upFirst(typeDef1.getType().toCode()) + "ClientParam>";
    }
    /*
     * Client parameter ([name])
     */
    write(" /**\r\n");
    write("   * <b>Fluent Client</b> search parameter constant for <b>" + code + "</b>\r\n");
    write("   * <p>\r\n");
    write("   * Description: <b>" + sp.getDescription() + "</b><br>\r\n");
    write("   * Type: <b>" + sp.getType().toCode() + "</b><br>\r\n");
    write("   * Path: <b>" + sp.getExpression() + "</b><br>\r\n");
    write("   * </p>\r\n");
    write("   */\r\n");
    write("  public static final ca.uhn.fhir.rest.gclient." + upFirst(sp.getType().toCode()) + "ClientParam" + genericTypes + " " + constName + " = new ca.uhn.fhir.rest.gclient." + upFirst(sp.getType().toCode()) + "ClientParam" + genericTypes + "(SP_" + constName + ");\r\n\r\n");
    if (sp.getType() == SearchParamType.REFERENCE && clss == JavaGenClass.Resource && !isAbstract) {
        String incName = upFirst(name) + ":" + code;
        write("/**\r\n");
        write("   * Constant for fluent queries to be used to add include statements. Specifies\r\n");
        write("   * the path value of \"<b>" + incName + "</b>\".\r\n");
        write("   */\r\n");
        write("  public static final ca.uhn.fhir.model.api.Include INCLUDE_" + cleanSpName(code).toUpperCase() + " = new ca.uhn.fhir.model.api.Include(\"" + incName + "\").toLocked();\r\n\r\n");
    }
}
Also used : CompartmentDefinitionResourceComponent(org.hl7.fhir.r5.model.CompartmentDefinition.CompartmentDefinitionResourceComponent) StringType(org.hl7.fhir.r5.model.StringType) TreeSet(java.util.TreeSet) CompartmentDefinition(org.hl7.fhir.r5.model.CompartmentDefinition) CodeType(org.hl7.fhir.r5.model.CodeType) SearchParameter(org.hl7.fhir.r5.model.SearchParameter)

Example 3 with EnumInfo

use of org.hl7.fhir.core.generator.analysis.EnumInfo in project org.hl7.fhir.core by hapifhir.

the class JavaResourceGenerator method generate.

// public void generate(ElementDefinition root, String name, JavaGenClass clss, ProfiledType cd, Date genDate, String version, boolean isAbstract, Map<String, SearchParameterDefn> nameToSearchParamDef, ElementDefinition template) throws Exception {
public void generate(Analysis analysis) throws Exception {
    if (analysis.getStructure().getKind() == StructureDefinitionKind.RESOURCE) {
        clss = JavaGenClass.Resource;
    } else {
        clss = JavaGenClass.Type;
    }
    write("package org.hl7.fhir." + jid + ".model;\r\n");
    startMark(version, genDate);
    // hasList(root);
    boolean hl = true;
    boolean hh = hasXhtml(analysis.getStructure().getSnapshot().getElement());
    boolean hd = hasDecimal(analysis.getStructure().getSnapshot().getElement());
    boolean hs = hasString(analysis.getStructure().getSnapshot().getElement());
    boolean he = hasSharedEnums(analysis.getStructure().getSnapshot().getElement());
    boolean hn = hasNestedTypes(analysis.getStructure().getSnapshot().getElement());
    if (hl || hh || hd || he) {
        if (hl) {
            write("import java.util.ArrayList;\r\n");
            write("import java.util.Date;\r\n");
            write("import java.util.List;\r\n");
        } else {
            write("import java.util.Date;\r\n");
        }
        if (hh) {
            write("import org.hl7.fhir.utilities.xhtml.NodeType;\r\n");
            write("import org.hl7.fhir.utilities.xhtml.XhtmlNode;\r\n");
        }
        if (hd)
            write("import java.math.*;\r\n");
        if (hs)
            write("import org.hl7.fhir.utilities.Utilities;\r\n");
        if (he)
            write("import org.hl7.fhir." + jid + ".model.Enumerations.*;\r\n");
    }
    if (hn) {
        if (clss == JavaGenClass.Resource) {
            write("import org.hl7.fhir.instance.model.api.IBaseBackboneElement;\r\n");
        } else {
            write("import org.hl7.fhir.instance.model.api.IBaseDatatypeElement;\r\n");
        }
    }
    write("import org.hl7.fhir.exceptions.FHIRException;\r\n");
    write("import org.hl7.fhir.instance.model.api.ICompositeType;\r\n");
    if (clss == JavaGenClass.Resource) {
        write("import ca.uhn.fhir.model.api.annotation.ResourceDef;\r\n");
        write("import ca.uhn.fhir.model.api.annotation.SearchParamDefinition;\r\n");
    }
    if (clss == JavaGenClass.Resource || "BackboneElement".equals(analysis.getName()) || "BackboneType".equals(analysis.getName())) {
        write("import org.hl7.fhir.instance.model.api.IBaseBackboneElement;\r\n");
    }
    write("import ca.uhn.fhir.model.api.annotation.Child;\r\n");
    write("import ca.uhn.fhir.model.api.annotation.ChildOrder;\r\n");
    if (clss != JavaGenClass.Resource) {
        write("import ca.uhn.fhir.model.api.annotation.DatatypeDef;\r\n");
    }
    write("import ca.uhn.fhir.model.api.annotation.Description;\r\n");
    write("import ca.uhn.fhir.model.api.annotation.Block;\r\n");
    write("\r\n");
    if (config.getIni().hasProperty("imports", analysis.getName())) {
        for (String imp : config.getIni().getStringProperty("imports", analysis.getName()).split("\\,")) {
            write("import " + imp.replace("{{jid}}", jid) + ";\r\n");
        }
    }
    jdoc("", replaceTitle(analysis.getName(), analysis.getStructure().getDescription()));
    TypeInfo ti = analysis.getRootType();
    boolean hasChildren = ti.getChildren().size() > 0;
    String superName = analysis.getAncestor() == null ? null : analysis.getAncestor().getName();
    if (VersionUtilities.isR4BVer(version) && !Utilities.noString(config.getIni().getStringProperty("R4B.CanonicalResources", analysis.getName()))) {
        superName = "CanonicalResource";
    }
    String hierarchy = analysis.getAncestor() != null ? "extends " + superName : "";
    if (clss == JavaGenClass.Resource) {
        if (!analysis.isAbstract()) {
            write("@ResourceDef(name=\"" + upFirst(analysis.getName()).replace("ListResource", "List") + "\", profile=\"http://hl7.org/fhir/StructureDefinition/" + upFirst(analysis.getName()) + "\")\r\n");
        }
    } else {
        write("@DatatypeDef(name=\"" + upFirst(analysis.getName()) + "\")\r\n");
        hierarchy = hierarchy + " implements ICompositeType";
    }
    if (config.getIni().hasProperty("hierarchy", analysis.getName())) {
        String h = config.getIni().getStringProperty("hierarchy", analysis.getName());
        if (analysis.getAncestor() != null) {
            h = h.replace("{{super}}", superName);
        }
        hierarchy = h;
    }
    write("public " + (analysis.isAbstract() ? "abstract " : "") + "class " + analysis.getClassName() + " " + hierarchy.trim() + " {\r\n");
    write("\r\n");
    for (String s : sorted(analysis.getEnums().keySet())) {
        EnumInfo e = analysis.getEnums().get(s);
        generateEnum(e);
    }
    for (TypeInfo t : analysis.getTypeList()) {
        generateType(analysis, t);
    }
    allfields = "";
    int i = 0;
    for (ElementDefinition e : ti.getChildren()) {
        if (!analysis.isInterface()) {
            generateField(analysis, ti, e, "    ", i++);
        }
    }
    write("    private static final long serialVersionUID = " + Long.toString(allfields.hashCode()) + "L;\r\n\r\n");
    hashSum = hashSum + allfields.hashCode();
    List<ElementDefinition> mandatory = new ArrayList<ElementDefinition>();
    generateConstructor(analysis.getClassName(), mandatory, "  ");
    if (hasChildren) {
        for (ElementDefinition e : ti.getChildren()) {
            if (e.getMin() > 0)
                mandatory.add(e);
        }
        if (mandatory.size() > 0)
            generateConstructor(analysis.getClassName(), mandatory, "  ");
        generateTypeSpecificConstructors(analysis.getClassName());
        for (ElementDefinition e : ti.getChildren()) {
            if (analysis.isInterface()) {
                generateAbstractAccessors(analysis, ti, e, "    ");
            } else {
                generateAccessors(analysis, ti, e, "    ", matchingInheritedElement(ti.getInheritedChildren(), e, analysis.getName()));
            }
        }
        if (!analysis.isInterface() && ti.getInheritedChildren() != null) {
            for (ElementDefinition e : filterInherited(ti.getInheritedChildren(), ti.getChildren())) {
                generateUnimplementedAccessors(analysis, ti, e, "    ");
            }
        }
        generateChildrenRegister(analysis, ti, "    ");
        generatePropertyGetterId(analysis, ti, "    ");
        generatePropertySetterId(analysis, ti, "    ");
        generatePropertySetterName(analysis, ti, "    ");
        generatePropertyMaker(analysis, ti, "    ");
        generatePropertyTypeGetter(analysis, ti, "    ");
        generateChildAdder(analysis, ti, "    ");
    }
    generateFhirType(analysis.getName());
    // // check for mappings
    // for (String map : root.getMappings().keySet()) {
    // if ("http://hl7.org/fhir/workflow".equals(map)) {
    // String namenn = root.getMapping(map);
    // if (patterns.containsKey(namenn)) {
    // generateImpl(namenn, patterns.get(namenn), upFirst(name), root, version, genDate);
    // }
    // }
    // }
    generateCopy(analysis, ti, false);
    if (hasChildren) {
        generateEquals(analysis, ti, false);
        generateIsEmpty(analysis, ti, false);
    }
    if (clss == JavaGenClass.Resource && !analysis.isAbstract()) {
        write("  @Override\r\n");
        write("  public ResourceType getResourceType() {\r\n");
        write("    return ResourceType." + analysis.getName() + ";\r\n");
        write("   }\r\n");
        write("\r\n");
    } else if (analysis.isAbstract() && analysis.getAncestor() != null && Utilities.noString(superName)) {
        write("\r\n");
        write("  @Override\r\n");
        write("  public String getIdBase() {\r\n");
        write("    return getId();\r\n");
        write("  }\r\n");
        write("  \r\n");
        write("  @Override\r\n");
        write("  public void setIdBase(String value) {\r\n");
        write("    setId(value);\r\n");
        write("  }\r\n");
        write("  public abstract ResourceType getResourceType();\r\n");
    } else if (analysis.isAbstract() && analysis.getAncestor() != null && Utilities.noString(superName)) {
        write("  @Override\r\n");
        write("  public String getIdBase() {\r\n");
        write("    return getId();\r\n");
        write("  }\r\n");
        write("  \r\n");
        write("  @Override\r\n");
        write("  public void setIdBase(String value) {\r\n");
        write("    setId(value);\r\n");
        write("  }\r\n");
    }
    // Write resource fields which can be used as constants in client code
    // to refer to standard search params
    Set<String> spcodes = new HashSet<>();
    for (SearchParameter sp : analysis.getSearchParams()) {
        String code = sp.getCode();
        if (!spcodes.contains(code)) {
            spcodes.add(code);
            /* 
		     * For composite codes we want to find the two param this is a composite
		     * of. We generate search parameter constants which reference the 
		     * component parts of the composite.  
		     */
            if (sp.getType() == SearchParamType.COMPOSITE) {
                if (code.endsWith("-[x]")) {
                    // partialCode will have "value" in this example
                    String partialCode = code.substring(0, code.length() - 4);
                    partialCode = partialCode.substring(partialCode.lastIndexOf('-') + 1);
                    // rootCode will have "component-code"
                    String rootCode = code.substring(0, code.indexOf("-" + partialCode));
                    /*
		         * If the composite has the form "foo-bar[x]" we expand this to create 
		         * a constant for each of the possible [x] values, so that client have
		         * static binding to the individual possibilities. AFAIK this is only
		         * used right now in Observation (e.g. for code-value-[x]) 
		         */
                    for (SearchParameter nextCandidate : analysis.getSearchParams()) {
                        if (nextCandidate.getCode().startsWith(partialCode)) {
                            String nextCompositeCode = rootCode + "-" + nextCandidate.getCode();
                            String[] compositeOf = new String[] { rootCode, nextCandidate.getCode() };
                            writeSearchParameterField(analysis.getName(), clss, analysis.isAbstract(), sp, nextCompositeCode, compositeOf, analysis.getSearchParams(), analysis.getName());
                        }
                    }
                } else {
                    SearchParameter comp0 = definitions.getSearchParams().get(sp.getComponent().get(0).getDefinition());
                    SearchParameter comp1 = definitions.getSearchParams().get(sp.getComponent().get(1).getDefinition());
                    if (comp0 != null && comp1 != null) {
                        String[] compositeOf = new String[] { comp0.getCode(), comp1.getCode() };
                        writeSearchParameterField(analysis.getName(), clss, analysis.isAbstract(), sp, sp.getCode(), compositeOf, analysis.getSearchParams(), analysis.getName());
                    }
                }
            } else if (code.contains("[x]")) {
                /*
		       * We only know how to handle search parameters with [x] in the name
		       * where it's a composite, and the [x] comes last. Are there other possibilities?
		       */
                throw new Exception("Unable to generate constant for search parameter: " + code);
            } else {
                writeSearchParameterField(analysis.getName(), clss, analysis.isAbstract(), sp, code, null, analysis.getSearchParams(), analysis.getName());
            }
        }
    }
    if (VersionUtilities.isR4BVer(version)) {
        String extras = config.getIni().getStringProperty("R4B.NullImplementation", analysis.getName());
        if (!Utilities.noString(extras)) {
            for (String n : extras.split("\\,")) {
                String t = n.substring(n.indexOf(":") + 1);
                n = n.substring(0, n.indexOf(":"));
                if (n.endsWith("[]")) {
                    n = Utilities.capitalize(n.substring(0, n.length() - 2));
                    write("      @Override\r\n");
                    write("      public List<" + t + "> get" + n + "() {\r\n");
                    write("        return new ArrayList<>();\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public CanonicalResource set" + n + "(List<" + t + "> the" + n + ") {\r\n");
                    write("        return this;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public boolean has" + n + "() {\r\n");
                    write("        return false;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public " + t + " add" + n + "() {\r\n");
                    write("	        return null;\r\n");
                    write("	      }\r\n");
                    write("	      \r\n");
                    write("      @Override\r\n");
                    write("      public CanonicalResource add" + n + "(" + t + " t) {\r\n");
                    write("        return null;\r\n");
                    write("	      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public " + t + " get" + n + "FirstRep() {\r\n");
                    write("        return new " + t + "();\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                } else if (t.contains("|")) {
                    n = Utilities.capitalize(n);
                    String t1 = t.substring(0, t.indexOf("|"));
                    String t2 = t.substring(t.indexOf("|") + 1);
                    write("      @Override\r\n");
                    write("      public " + t1 + " get" + n + "() {\r\n");
                    if ("boolean".equals(t1)) {
                        write("        return false;\r\n");
                    } else {
                        write("        return new " + t1 + "();\r\n");
                    }
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public " + t2 + " get" + n + "Element() {\r\n");
                    write("        return new " + t2 + "();\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public CanonicalResource set" + n + "(" + t1 + " the" + n + ") {\r\n");
                    write("        return this;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public CanonicalResource set" + n + "Element(" + t2 + " the" + n + ") {\r\n");
                    write("        return this;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public boolean has" + n + "() {\r\n");
                    write("        return false;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public boolean has" + n + "Element() {\r\n");
                    write("        return false;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      \r\n");
                } else {
                    n = Utilities.capitalize(n);
                    write("      @Override\r\n");
                    write("      public " + t + " get" + n + "() {\r\n");
                    write("        return new " + t + "();\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public CanonicalResource set" + n + "(" + t + " the" + n + ") {\r\n");
                    write("        return this;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public boolean has" + n + "() {\r\n");
                    write("        return false;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      @Override\r\n");
                    write("      public boolean has" + n + "Element() {\r\n");
                    write("        return false;\r\n");
                    write("      }\r\n");
                    write("      \r\n");
                    write("      \r\n");
                }
            }
        }
    }
    if (config.getAdornments().containsKey(analysis.getClassName())) {
        write("// Manual code (from Configuration.txt):\r\n");
        write(config.getAdornments().get(analysis.getClassName()) + "\r\n");
        write("// end addition\r\n");
    }
    write("\r\n");
    write("}\r\n");
    write("\r\n");
    flush();
}
Also used : EnumInfo(org.hl7.fhir.core.generator.analysis.EnumInfo) ArrayList(java.util.ArrayList) TypeInfo(org.hl7.fhir.core.generator.analysis.TypeInfo) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) SearchParameter(org.hl7.fhir.r5.model.SearchParameter) HashSet(java.util.HashSet)

Example 4 with EnumInfo

use of org.hl7.fhir.core.generator.analysis.EnumInfo in project org.hl7.fhir.core by hapifhir.

the class JavaParserJsonGenerator method genElementComposer.

private void genElementComposer(Analysis analysis, TypeInfo ti, ElementDefinition ed, ElementDefinition inh) throws Exception {
    String name = ed.getName();
    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]", "") : "";
        composer.append("      if (element.has" + upFirst(en) + "()) {\r\n");
        composer.append("        composeType(\"" + pfx + "\", element.get" + upFirst(en) + "());\r\n");
        composer.append("      }\r\n");
    } else {
        String tn = ed.getUserString("java.type");
        String comp = null;
        String en = null;
        boolean enShared = false;
        if (ed.hasUserData("java.enum")) {
            // getCodeListType(cd.getBinding());
            EnumInfo ei = (EnumInfo) ed.getUserData("java.enum");
            ValueSet vs = ei.getValueSet();
            enShared = vs.hasUserData("shared");
            if (enShared) {
                en = "Enumerations." + ei.getName();
            } else {
                en = analysis.getClassName() + "." + ei.getName();
            }
        } else {
            if (name.equals("extension")) {
                name = "extension";
                tn = "Extension";
            }
            if (tn.equals("XhtmlNode")) {
                tn = "xhtml";
                comp = "composeXhtml";
            } else if (tn.equals("code")) {
                tn = "Code";
                comp = "composeCode";
            } else if (tn.equals("instant")) {
                tn = "Instant";
            } else if (tn.contains("Reference(")) {
                comp = "composeReference";
                tn = "Reference";
            } else if (tn.contains("canonical(")) {
                comp = "composeCanonical";
                tn = "CanonicalType";
            } else if (tn.contains("(")) {
                comp = "compose" + tn;
            } else if (tn.startsWith(analysis.getName()) && !tn.equals(analysis.getClassName())) {
                comp = "compose" + leaf(tn);
            } else if (isPrimitive(ed)) {
                comp = "compose" + leaf(tn);
                // remove Type"
                comp = comp.substring(0, comp.length() - 4);
            } else {
                comp = "compose" + leaf(tn);
            }
        }
        if (ed.unbounded()) {
            tn = ed.getUserString("java.type");
            if (tn.contains("Reference(")) {
                comp = "composeReference";
                tn = "Reference";
            } else if (tn.contains("canonical(")) {
                comp = "composeCanonical";
                tn = "CanonicalType";
            }
            String stn = tn;
            if (ed.isInlineType() || ed.hasContentReference()) {
                stn = analysis.getClassName() + "." + tn;
            }
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            if (en == null) {
                if (tn.equals("String"))
                    tn = "StringType";
                if (definitions.hasPrimitiveType(tn)) {
                    tn = upFirst(tn) + "Type";
                }
                if (isPrimitive(ed) || ed.typeSummary().startsWith("canonical(")) {
                    composer.append("        if (anyHasValue(element.get" + upFirst(getElementName(name, false)) + "())) {\r\n");
                    composer.append("          openArray(\"" + name + "\");\r\n");
                    composer.append("          for (" + (tn.contains("(") ? stn : upFirst(tn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                    composer.append("            " + comp + "Core(null, e, e != element.get" + upFirst(getElementName(name, false)) + "().get(element.get" + upFirst(getElementName(name, false)) + "().size()-1));\r\n");
                    composer.append("          closeArray();\r\n");
                    composer.append("        }\r\n");
                    composer.append("        if (anyHasExtras(element.get" + upFirst(getElementName(name, false)) + "())) {\r\n");
                    composer.append("          openArray(\"_" + name + "\");\r\n");
                    composer.append("          for (" + (stn.contains("(") ? stn : upFirst(stn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                    composer.append("            " + comp + "Extras(null, e, true);\r\n");
                    composer.append("          closeArray();\r\n");
                    composer.append("        }\r\n");
                } else if (ed.typeSummary().equals("Resource")) {
                    composer.append("        openArray(\"" + name + "\");\r\n");
                    composer.append("        for (" + (stn.contains("(") ? tn : upFirst(tn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) {\r\n");
                    composer.append("          open(null);\r\n");
                    composer.append("          " + comp + "(e);\r\n");
                    composer.append("          close();\r\n");
                    composer.append("        }\r\n");
                    composer.append("        closeArray();\r\n");
                } else {
                    composer.append("        openArray(\"" + name + "\");\r\n");
                    composer.append("        for (" + (stn.contains("(") ? stn : upFirst(stn)) + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                    composer.append("          " + comp + "(null, e);\r\n");
                    composer.append("        closeArray();\r\n");
                }
            } else {
                composer.append("        openArray(\"" + name + "\");\r\n");
                composer.append("        for (Enumeration<" + prepEnumName(en) + "> e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("          composeEnumerationCore(null, e, new " + prepEnumName(en) + "EnumFactory(), true);\r\n");
                composer.append("        closeArray();\r\n");
                composer.append("        if (anyHasExtras(element.get" + upFirst(getElementName(name, false)) + "())) {\r\n");
                composer.append("          openArray(\"_" + name + "\");\r\n");
                composer.append("          for (Enumeration<" + prepEnumName(en) + "> e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("            composeEnumerationExtras(null, e, new " + prepEnumName(en) + "EnumFactory(), true);\r\n");
                composer.append("          closeArray();\r\n");
                composer.append("        }\r\n");
            }
            composer.append("      };\r\n");
        } else if (en != null) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "Element()) {\r\n");
            if (enShared) {
                composer.append("        composeEnumerationCore(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
                composer.append("        composeEnumerationExtras(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
            } else {
                composer.append("        composeEnumerationCore(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
                composer.append("        composeEnumerationExtras(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + prepEnumName(en) + "EnumFactory(), false);\r\n");
            }
            composer.append("      }\r\n");
        // composer.append("        composeString(\""+name+"\", element.get"+upFirst(getElementName(name, false))+"().toCode());\r\n");
        } else if (ed.typeSummary().equals("Resource")) {
            composer.append("        if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("          open(\"" + name + "\");\r\n");
            composer.append("          " + comp + "(element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("          close();\r\n");
            composer.append("        }\r\n");
        } else if (!"xhtml".equals(ed.typeSummary()) && (isPrimitive(ed) || ed.typeSummary().startsWith("canonical("))) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "Element()) {\r\n");
            composer.append("        " + comp + "Core(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), false);\r\n");
            composer.append("        " + comp + "Extras(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), false);\r\n");
            composer.append("      }\r\n");
        } else if (tn.equals("xhtml")) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("        XhtmlNode node = element.getDiv();\r\n");
            composer.append("        if (node.getNsDecl() == null) {\r\n");
            composer.append("          node.attribute(\"xmlns\", XHTML_NS);\r\n");
            composer.append("        }\r\n");
            composer.append("        " + comp + "(\"" + name + "\", node);\r\n");
            composer.append("      }\r\n");
        } else if (inh != null && inh.unbounded()) {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("        " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "FirstRep());\r\n");
            composer.append("      }\r\n");
        } else {
            composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("        " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("      }\r\n");
        }
    }
}
Also used : EnumInfo(org.hl7.fhir.core.generator.analysis.EnumInfo) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 5 with EnumInfo

use of org.hl7.fhir.core.generator.analysis.EnumInfo in project org.hl7.fhir.core by hapifhir.

the class JavaParserXmlGenerator method genElementCompose.

private void genElementCompose(Analysis analysis, TypeInfo ti, ElementDefinition ed, ElementDefinition inh) throws Exception {
    String name = ed.getName();
    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]", "") : "";
        composer.append("    if (element.has" + upFirst(getElementName(en, false)) + "()) {\r\n");
        composer.append("      composeType(\"" + pfx + "\", element.get" + upFirst(getElementName(en, false)) + "());\r\n");
        composer.append("    }");
    } else {
        String comp = null;
        String en = null;
        String tn = ed.getUserString("java.type");
        if (ed.hasUserData("java.enum")) {
            EnumInfo ei = (EnumInfo) ed.getUserData("java.enum");
            ValueSet vs = ei.getValueSet();
            boolean enShared = vs.hasUserData("shared");
            if (enShared) {
                en = "Enumerations." + ei.getName();
            } else {
                en = analysis.getClassName() + "." + ei.getName();
            }
        } else {
            if (name.equals("extension")) {
                name = "extension";
                tn = "Extension";
            }
            if (tn.equals("XhtmlNode")) {
                tn = "xhtml";
                comp = "composeXhtml";
            } else if (isPrimitive(ed)) {
                comp = "compose" + tn.substring(0, tn.length() - 4);
            } else if (tn.contains("Reference(")) {
                comp = "composeReference";
                tn = "Reference";
            } else if (tn.contains("canonical(")) {
                comp = "composeCanonical";
                tn = "CanonicalType";
            } else if (tn.contains("(")) {
                comp = "compose" + tn;
            } else if ((ed.hasContentReference() || ed.isInlineType()) && !tn.startsWith(analysis.getClassName())) {
                comp = "compose" + analysis.getClassName() + tn;
            } else {
                comp = "compose" + tn;
            }
        }
        if (ed.unbounded()) {
            if (en != null) {
                composer.append("      if (element.has" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("        for (Enumeration<" + en + "> e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                composer.append("          composeEnumeration(\"" + name + "\", e, new " + en + "EnumFactory());\r\n");
            } else {
                String stn = ed.isInlineType() || ed.hasContentReference() ? analysis.getClassName() + "." + tn : tn;
                // String pfx = ed.isInlineType() || ed.hasContentReference() ? analysis.getClassName() : "";
                composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) { \r\n");
                composer.append("      for (" + stn + " e : element.get" + upFirst(getElementName(name, false)) + "()) \r\n");
                if (ed.typeSummary().equals("Resource")) {
                    composer.append("        {\r\n");
                    composer.append("          xml.enter(FHIR_NS, \"" + name + "\");\r\n");
                    composer.append("          " + comp + "(e);\r\n");
                    composer.append("          xml.exit(FHIR_NS, \"" + name + "\");\r\n");
                    composer.append("        }\r\n");
                } else {
                    composer.append("          " + comp + "(\"" + name + "\", e);\r\n");
                }
                composer.append("    }\r\n");
            }
        } else if (en != null) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "Element())\r\n");
            composer.append("      composeEnumeration(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element(), new " + en + "EnumFactory());\r\n");
        } else if (!"xhtml".equals(ed.typeSummary()) && (isJavaPrimitive(ed) || ed.typeSummary().startsWith("canonical("))) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "Element()) {\r\n");
            composer.append("      " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "Element());\r\n");
            composer.append("    }\r\n");
        } else if (ed.typeSummary().equals("Resource")) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("      xml.enter(FHIR_NS, \"" + name + "\");\r\n");
            composer.append("      " + comp + "(element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("      xml.exit(FHIR_NS, \"" + name + "\");\r\n");
            composer.append("    }\r\n");
        } else if (inh != null && inh.unbounded()) {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("      " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "FirstRep());\r\n");
            composer.append("    }\r\n");
        } else {
            composer.append("    if (element.has" + upFirst(getElementName(name, false)) + "()) {\r\n");
            composer.append("      " + comp + "(\"" + name + "\", element.get" + upFirst(getElementName(name, false)) + "());\r\n");
            composer.append("    }\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)6 EnumInfo (org.hl7.fhir.core.generator.analysis.EnumInfo)5 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)2 SearchParameter (org.hl7.fhir.r5.model.SearchParameter)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 TypeInfo (org.hl7.fhir.core.generator.analysis.TypeInfo)1 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)1 CodeType (org.hl7.fhir.r5.model.CodeType)1 CompartmentDefinition (org.hl7.fhir.r5.model.CompartmentDefinition)1 CompartmentDefinitionResourceComponent (org.hl7.fhir.r5.model.CompartmentDefinition.CompartmentDefinitionResourceComponent)1 ElementDefinitionBindingComponent (org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent)1 StringType (org.hl7.fhir.r5.model.StringType)1 ValueSetExpansionContainsComponent (org.hl7.fhir.r5.model.ValueSet.ValueSetExpansionContainsComponent)1 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)1