Search in sources :

Example 1 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project quality-measure-and-cohort-service by Alvearie.

the class ModelUtilsTest method testGetBaseTypeNameIncludedNamespacePrefixMissing.

@Test
public void testGetBaseTypeNameIncludedNamespacePrefixMissing() {
    ModelSpecifier otherModel = new ModelSpecifier().withName("Other").withVersion("1.2.3").withUrl("urn:oid:Other");
    ModelInfo modelInfo = new ModelInfo();
    modelInfo.setName("Dummy");
    modelInfo.setVersion("5.4.3");
    modelInfo.setUrl("urn:oid:Dummy");
    modelInfo.getRequiredModelInfo().add(otherModel);
    ClassInfo typeInfo = new ClassInfo();
    typeInfo.setName("MyType");
    typeInfo.setBaseType("Missing.BaseType");
    IllegalArgumentException iex = assertThrows(IllegalArgumentException.class, () -> ModelUtils.getBaseTypeName(modelInfo, typeInfo));
    assertTrue(iex.getMessage(), iex.getMessage().contains("Missing"));
    assertTrue(iex.getMessage(), iex.getMessage().contains(modelInfo.getName()));
    assertTrue(iex.getMessage(), iex.getMessage().contains(modelInfo.getVersion()));
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) ModelSpecifier(org.hl7.elm_modelinfo.r1.ModelSpecifier) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) Test(org.junit.Test)

Example 2 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project quality-measure-and-cohort-service by Alvearie.

the class ModelUtilsTest method testGetBaseTypeNameNoNamespacePrefix.

@Test
public void testGetBaseTypeNameNoNamespacePrefix() {
    ModelInfo modelInfo = new ModelInfo();
    modelInfo.setName("Dummy");
    modelInfo.setUrl("urn:oid:Dummy");
    ClassInfo typeInfo = new ClassInfo();
    typeInfo.setName("MyType");
    typeInfo.setBaseType("BaseType");
    QName qname = ModelUtils.getBaseTypeName(modelInfo, typeInfo);
    assertEquals(new QName(modelInfo.getUrl(), "BaseType"), qname);
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) QName(javax.xml.namespace.QName) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) Test(org.junit.Test)

Example 3 with TypeInfo

use of org.hl7.fhir.core.generator.analysis.TypeInfo in project quality-measure-and-cohort-service by Alvearie.

the class ModelUtilsTest method testGetBaseTypeNameSystemNamespacePrefix.

@Test
public void testGetBaseTypeNameSystemNamespacePrefix() {
    ModelInfo modelInfo = new ModelInfo();
    modelInfo.setName("Dummy");
    modelInfo.setUrl("urn:oid:Dummy");
    ClassInfo typeInfo = new ClassInfo();
    typeInfo.setName("MyType");
    typeInfo.setBaseType("System.BaseType");
    QName qname = ModelUtils.getBaseTypeName(modelInfo, typeInfo);
    assertEquals(new QName(CqlConstants.SYSTEM_MODEL_URI, "BaseType"), qname);
}
Also used : ModelInfo(org.hl7.elm_modelinfo.r1.ModelInfo) QName(javax.xml.namespace.QName) ClassInfo(org.hl7.elm_modelinfo.r1.ClassInfo) Test(org.junit.Test)

Example 4 with TypeInfo

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

the class JavaResourceGenerator method generateChildAdder.

private void generateChildAdder(Analysis analysis, TypeInfo ti, String indent) throws Exception {
    List<ElementDefinition> children = ti.getChildren();
    boolean isInterface = analysis.isInterface();
    String parent = ti.getDefn().getPath();
    write(indent + "  @Override\r\n");
    write(indent + "  public Base addChild(String name) throws FHIRException {\r\n");
    boolean first = true;
    for (ElementDefinition e : children) {
        if (!isInterface) {
            if (!e.typeSummary().equals("xhtml")) {
                if (e.getType().size() <= 1 && !e.typeSummary().equals("*")) {
                    String tn = e.getUserString("java.type");
                    String name = e.getName();
                    String namet = e.getName();
                    first = generateChildAddItem(indent, parent, first, e, tn, name, namet);
                } else {
                    for (TypeRefComponent t : getTypes(e.getType())) {
                        String tn = getTypename(t);
                        String name = e.getName().replace("[x]", "");
                        String namet = e.getName().replace("[x]", upFirst(checkConstraint(t.getName())));
                        first = generateChildAddItem(indent, parent, first, e, tn, name, namet);
                    }
                }
            }
        }
    }
    if (!first)
        write(indent + "    else\r\n");
    write(indent + "      return super.addChild(name);\r\n");
    write(indent + "  }\r\n\r\n");
}
Also used : TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition)

Example 5 with TypeInfo

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

the class JavaResourceGenerator method generateAccessors.

private void generateAccessors(Analysis analysis, TypeInfo ti, ElementDefinition e, String indent, ElementDefinition inh) throws Exception {
    String tn = e.getUserString("java.type");
    StructureDefinition sd = e.hasType() ? definitions.getStructures().get(pfxType(e.getTypeFirstRep().getCode())) : null;
    boolean abstractTarget = (sd != null) && sd.getAbstract() && !sd.getUrl().equals("http://hl7.org/fhir/StructureDefinition/Element") && !sd.getUrl().equals("http://hl7.org/fhir/StructureDefinition/BackboneElement");
    String className = ti.getName();
    if (Utilities.noString(tn)) {
        throw new Error("??");
    }
    boolean isReferenceRefField = (analysis.getName().equals("Reference") && e.getName().equals("reference"));
    String simpleType = getSimpleType(tn);
    if (e.unbounded() || (inh != null && inh.unbounded())) {
        if (!e.unbounded()) {
            jdoc(indent, "only one on this implementation");
            write(indent + "@Override\r\n");
            write(indent + "public int get" + getTitle(getElementName(e.getName(), false)) + "Max() { \r\n");
            write(indent + "  return 1;\r\n");
            write(indent + "}\r\n");
        }
        /*
		   * getXXX()for repeatable type
		   */
        jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
        String listGenericType;
        if (tn == null && e.hasContentReference()) {
            listGenericType = analysis.getName();
        } else {
            listGenericType = tn;
        }
        write(indent + "public List<" + listGenericType + "> get" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
        if (!e.unbounded()) {
            write(indent + "  List<" + listGenericType + "> list = new ArrayList<" + listGenericType + ">();\r\n");
            write(indent + "  if (this." + getElementName(e.getName(), true) + " == null) {\r\n");
            write(indent + "    list.add(" + getElementName(e.getName(), true) + ");\r\n");
            write(indent + "  }\r\n");
            write(indent + "  return list;\r\n");
        } else {
            write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
            write(indent + "    this." + getElementName(e.getName(), true) + " = new ArrayList<" + listGenericType + ">();\r\n");
            write(indent + "  return this." + getElementName(e.getName(), true) + ";\r\n");
        }
        write(indent + "}\r\n\r\n");
        /*
		   * setXXX(List<foo>) for repeating type
		   */
        jdoc(indent, "@return Returns a reference to <code>this</code> for easy method chaining");
        write(indent + "public " + className + " set" + getTitle(getElementName(e.getName(), false)) + "(" + "List<" + listGenericType + "> the" + getTitle(getElementName(e.getName(), false)) + ") { \r\n");
        if (!e.unbounded()) {
            write(indent + "  if (the" + getTitle(getElementName(e.getName(), false)) + ".size() == 0) {\r\n");
            write(indent + "    this." + getElementName(e.getName(), true) + " = null;\r\n");
            write(indent + "  } else if (the" + getTitle(getElementName(e.getName(), false)) + ".size() == 1) {\r\n");
            write(indent + "    this." + getElementName(e.getName(), true) + " = the" + getTitle(getElementName(e.getName(), false)) + ".get(0);\r\n");
            write(indent + "  } else {\r\n");
            write(indent + "    throw new Error(\"Cannot have more than one " + e.getPath() + "\");\r\n");
            write(indent + "  }\r\n");
        } else {
            write(indent + "  this." + getElementName(e.getName(), true) + " = the" + getTitle(getElementName(e.getName(), false)) + ";\r\n");
        }
        write(indent + "  return this;\r\n");
        write(indent + "}\r\n\r\n");
        /*
		   * hasXXX() for repeatable type
		   */
        write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
        if (!e.unbounded()) {
            write(indent + "  return this." + getElementName(e.getName(), true) + " != null && !this." + getElementName(e.getName(), true) + ".isEmpty();\r\n");
        } else {
            write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
            write(indent + "    return false;\r\n");
            write(indent + "  for (" + tn + " item : this." + getElementName(e.getName(), true) + ")\r\n");
            write(indent + "    if (!item.isEmpty())\r\n");
            write(indent + "      return true;\r\n");
            write(indent + "  return false;\r\n");
        }
        write(indent + "}\r\n");
        write("\r\n");
        if (e.getType().size() == 1 && (definitions.hasPrimitiveType(e.typeSummary()) || e.typeSummary().equals("xml:lang") || e.typeSummary().startsWith("canonical("))) {
            /*
		     * addXXXElement() for repeatable primitive
		     */
            jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
            write(indent + "public " + tn + " add" + getTitle(getElementName(e.getName(), false)) + "Element() {//2 \r\n");
            if (!e.unbounded()) {
                write(indent + "  if (this." + getElementName(e.getName(), true) + " == null) {\r\n");
                write(indent + "    this." + getElementName(e.getName(), true) + " = new " + tn + "();\r\n");
                write(indent + "  } else {\r\n");
                write(indent + "    throw new Error(\"Cannot have more than one " + e.getPath() + "\");\r\n");
                write(indent + "  }\r\n");
                write(indent + "  return this." + getElementName(e.getName(), true) + ";\r\n");
            } else {
                write(indent + "  " + tn + " t = new " + tn + "(" + (tn.startsWith("Enum") ? "new " + tn.substring(12, tn.length() - 1) + "EnumFactory()" : "") + ");\r\n");
                write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                write(indent + "    this." + getElementName(e.getName(), true) + " = new ArrayList<" + tn + ">();\r\n");
                write(indent + "  this." + getElementName(e.getName(), true) + ".add(t);\r\n");
                write(indent + "  return t;\r\n");
            }
            write(indent + "}\r\n");
            write("\r\n");
            /*
		     * addXXX(foo) for repeatable primitive
		     */
            jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
            write(indent + "public " + className + " add" + getTitle(getElementName(e.getName(), false)) + "(" + simpleType + " value) { //1\r\n");
            write(indent + "  " + tn + " t = new " + tn + "(" + (tn.startsWith("Enum") ? "new " + tn.substring(12, tn.length() - 1) + "EnumFactory()" : "") + ");\r\n");
            write(indent + "  t.setValue(value);\r\n");
            write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
            write(indent + "    this." + getElementName(e.getName(), true) + " = new ArrayList<" + tn + ">();\r\n");
            write(indent + "  this." + getElementName(e.getName(), true) + ".add(t);\r\n");
            write(indent + "  return this;\r\n");
            write(indent + "}\r\n");
            write("\r\n");
            /*
		     * hasXXX(foo) for repeatable primitive
		     */
            jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
            write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "(" + simpleType + " value) { \r\n");
            write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
            write(indent + "    return false;\r\n");
            write(indent + "  for (" + tn + " v : this." + getElementName(e.getName(), true) + ")\r\n");
            if (// GG: not sure why this is different?
            isJavaPrimitive(e) && !tn.startsWith("Enum"))
                write(indent + "    if (v.getValue().equals(value)) // " + e.typeSummary() + "\r\n");
            else
                write(indent + "    if (v.getValue().equals(value)) // " + e.typeSummary() + "\r\n");
            write(indent + "      return true;\r\n");
            write(indent + "  return false;\r\n");
            write(indent + "}\r\n");
            write("\r\n");
        } else {
            if (!definitions.hasResource(tn)) {
                if (abstractTarget) {
                    System.out.println(e.getPath() + " is abstract");
                } else {
                    /*
		         * addXXX() for repeatable composite
		         */
                    write(indent + "public " + tn + " add" + getTitle(getElementName(e.getName(), false)) + "() { //3\r\n");
                    if (!e.unbounded()) {
                        write(indent + "  if (this." + getElementName(e.getName(), true) + " == null) {\r\n");
                        write(indent + "    this." + getElementName(e.getName(), true) + " = new " + tn + "();\r\n");
                        write(indent + "  } else {\r\n");
                        write(indent + "    throw new Error(\"Cannot have more than one " + e.getPath() + "\");\r\n");
                        write(indent + "  }\r\n");
                        write(indent + "  return this." + getElementName(e.getName(), true) + ";\r\n");
                    } else {
                        write(indent + "  " + tn + " t = new " + tn + "();\r\n");
                        write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                        write(indent + "    this." + getElementName(e.getName(), true) + " = new ArrayList<" + tn + ">();\r\n");
                        write(indent + "  this." + getElementName(e.getName(), true) + ".add(t);\r\n");
                        write(indent + "  return t;\r\n");
                    }
                    write(indent + "}\r\n");
                    write("\r\n");
                }
                /*
		       * addXXX(foo) for repeatable composite
		       */
                write(indent + "public " + className + " add" + getTitle(getElementName(e.getName(), false)) + "(" + tn + " t) { //3\r\n");
                if (!e.unbounded()) {
                    write(indent + "  if (this." + getElementName(e.getName(), true) + " == null) {\r\n");
                    write(indent + "    this." + getElementName(e.getName(), true) + " = t;\r\n");
                    write(indent + "  } else {\r\n");
                    write(indent + "    throw new Error(\"Cannot have more than one " + e.getPath() + "\");\r\n");
                    write(indent + "  }\r\n");
                } else {
                    write(indent + "  if (t == null)\r\n");
                    write(indent + "    return this;\r\n");
                    write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                    write(indent + "    this." + getElementName(e.getName(), true) + " = new ArrayList<" + tn + ">();\r\n");
                    write(indent + "  this." + getElementName(e.getName(), true) + ".add(t);\r\n");
                }
                write(indent + "  return this;\r\n");
                write(indent + "}\r\n");
                write("\r\n");
            } else {
                /*
		       * addXXX(foo) for repeatable composite
		       */
                write(indent + "public " + className + " add" + getTitle(getElementName(e.getName(), false)) + "(" + tn + " t) { //3\r\n");
                write(indent + "  if (t == null)\r\n");
                write(indent + "    return this;\r\n");
                write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                write(indent + "    this." + getElementName(e.getName(), true) + " = new ArrayList<" + tn + ">();\r\n");
                write(indent + "  this." + getElementName(e.getName(), true) + ".add(t);\r\n");
                write(indent + "  return this;\r\n");
                write(indent + "}\r\n");
                write("\r\n");
            }
            /*
		     * getXXXFirstRep() for repeatable element
		     */
            if (!"DomainResource".equals(className)) {
                jdoc(indent, "@return The first repetition of repeating field {@link #" + getElementName(e.getName(), true) + "}, creating it if it does not already exist {3}");
                write(indent + "public " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "FirstRep() { \r\n");
                if (e.unbounded()) {
                    write(indent + "  if (get" + getTitle(getElementName(e.getName(), false)) + "().isEmpty()) {\r\n");
                } else {
                    write(indent + "  if (" + getElementName(e.getName(), false) + " == null) {\r\n");
                }
                if ((definitions.hasPrimitiveType(e.typeSummary()))) {
                    write(indent + "    add" + getTitle(getElementName(e.getName(), false)) + "Element();\r\n");
                } else {
                    write(indent + "    add" + getTitle(getElementName(e.getName(), false)) + "();\r\n");
                }
                write(indent + "  }\r\n");
                if (e.unbounded()) {
                    write(indent + "  return get" + getTitle(getElementName(e.getName(), false)) + "().get(0);\r\n");
                } else {
                    write(indent + "  return " + getElementName(e.getName(), false) + ";\r\n");
                }
                write(indent + "}\r\n\r\n");
            }
        }
    } else {
        if (!"xhtml".equals(e.typeSummary()) && isJavaPrimitive(e) || (e.getType().size() == 1 && e.typeSummary().startsWith("canonical("))) {
            jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + "). This is the underlying object with id, value and extensions. The accessor \"get" + getTitle(getElementName(e.getName(), false)) + "\" gives direct access to the value");
            if (isReferenceRefField) {
                /*
           * Reference#getReferenceElement is defined differently in BaseReference.java?
           */
                write(indent + "public " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "Element_() { \r\n");
                write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                write(indent + "    if (Configuration.errorOnAutoCreate())\r\n");
                write(indent + "      throw new Error(\"Attempt to auto-create " + className + "." + getElementName(e.getName(), true) + "\");\r\n");
                write(indent + "    else if (Configuration.doAutoCreate())\r\n");
                write(indent + "      this." + getElementName(e.getName(), true) + " = new " + tn + "(" + (tn.startsWith("Enum") ? "new " + tn.substring(12, tn.length() - 1) + "EnumFactory()" : "") + "); // bb\r\n");
                write(indent + "  return this." + getElementName(e.getName(), true) + ";\r\n");
                write(indent + "}\r\n");
            } else {
                write(indent + "public " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "Element() { \r\n");
                write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                write(indent + "    if (Configuration.errorOnAutoCreate())\r\n");
                write(indent + "      throw new Error(\"Attempt to auto-create " + className + "." + getElementName(e.getName(), true) + "\");\r\n");
                write(indent + "    else if (Configuration.doAutoCreate())\r\n");
                write(indent + "      this." + getElementName(e.getName(), true) + " = new " + tn + "(" + (tn.startsWith("Enum") ? "new " + tn.substring(12, tn.length() - 1) + "EnumFactory()" : "") + "); // bb\r\n");
                write(indent + "  return this." + getElementName(e.getName(), true) + ";\r\n");
                write(indent + "}\r\n");
            }
            write("\r\n");
            write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "Element() { \r\n");
            write(indent + "  return this." + getElementName(e.getName(), true) + " != null && !this." + getElementName(e.getName(), true) + ".isEmpty();\r\n");
            write(indent + "}\r\n");
            write("\r\n");
            write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
            write(indent + "  return this." + getElementName(e.getName(), true) + " != null && !this." + getElementName(e.getName(), true) + ".isEmpty();\r\n");
            write(indent + "}\r\n");
            write("\r\n");
            jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + "). This is the underlying object with id, value and extensions. The accessor \"get" + getTitle(getElementName(e.getName(), false)) + "\" gives direct access to the value");
            write(indent + "public " + className + " set" + getTitle(getElementName(e.getName(), false)) + "Element(" + tn + " value) { \r\n");
            write(indent + "  this." + getElementName(e.getName(), true) + " = value;\r\n");
            write(indent + "  return this;\r\n");
            write(indent + "}\r\n");
            write("\r\n");
            jdoc(indent, "@return " + replaceTitle(analysis.getName(), e.getDefinition()));
            write(indent + "public " + simpleType + " get" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
            if (e.typeSummary().equals("boolean"))
                write(indent + "  return this." + getElementName(e.getName(), true) + " == null || this." + getElementName(e.getName(), true) + ".isEmpty() ? false : this." + getElementName(e.getName(), true) + ".getValue();\r\n");
            else if (e.typeSummary().equals("integer") || e.typeSummary().equals("unsignedInt") || e.typeSummary().equals("positiveInt"))
                write(indent + "  return this." + getElementName(e.getName(), true) + " == null || this." + getElementName(e.getName(), true) + ".isEmpty() ? 0 : this." + getElementName(e.getName(), true) + ".getValue();\r\n");
            else if (e.typeSummary().equals("integer64"))
                write(indent + "  return this." + getElementName(e.getName(), true) + " == null || this." + getElementName(e.getName(), true) + ".isEmpty() ? 0 : this." + getElementName(e.getName(), true) + ".getValue();\r\n");
            else
                write(indent + "  return this." + getElementName(e.getName(), true) + " == null ? null : this." + getElementName(e.getName(), true) + ".getValue();\r\n");
            write(indent + "}\r\n");
            write("\r\n");
            generateSetter(e, indent, className, tn, simpleType, analysis.getName());
            // BigDecimal sugar methods
            if (simpleType.equals("BigDecimal")) {
                generateSetter(e, indent, className, tn, "long", analysis.getName());
                generateSetter(e, indent, className, tn, "double", analysis.getName());
            }
        // // code sugar methods
        // if (e.typeSummary().equals("code")) {
        // jdoc(indent, "@return a string code value for "+replaceTitle(rn, e.getDefinition()));
        // write(indent+"public "+simpleType+" get"+getTitle(getElementName(e.getName(), false))+"AsCode() { \r\n");
        // write(indent+"  return this."+getElementName(e.getName(), true)+" == null ? null : this."+getElementName(e.getName(), true)+".getValue();\r\n");
        // write(indent+"}\r\n");
        // write("\r\n");
        // 
        // jdoc(indent, "@param value String value for "+replaceTitle(rn, e.getDefinition()));
        // write(indent+"public "+className+" set"+getTitle(getElementName(e.getName(), false))+"AsCode(String value) throws FHIRException { \r\n");
        // write(indent+"  if (!Utilities.noString(value)) \r\n");
        // write(indent+"    this."+getElementName(e.getName(), true)+" = null;\r\n");
        // write(indent+"  else {\r\n");
        // write(indent+"    if (this."+getElementName(e.getName(), true)+" == null)\r\n");
        // write(indent+"      this."+getElementName(e.getName(), true)+" = new "+tn+"("+( tn.startsWith("Enum") ? "new "+tn.substring(12, tn.length()-1)+"EnumFactory()" : "")+");\r\n");
        // write(indent+"    this."+getElementName(e.getName(), true)+".setValue("+(tn.startsWith("Enum") ? tn.substring(12, tn.length()-1)+".fromCode(value)" : "value")+");\r\n");
        // write(indent+"  }\r\n");
        // write(indent+"  return this;\r\n");
        // write(indent+"}\r\n");
        // write("\r\n");
        // 
        // }
        } else {
            jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
            write(indent + "public " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
            if (!tn.equals("Resource") && !tn.equals("DataType") && !tn.endsWith(".DataType")) {
                write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                write(indent + "    if (Configuration.errorOnAutoCreate())\r\n");
                write(indent + "      throw new Error(\"Attempt to auto-create " + className + "." + getElementName(e.getName(), true) + "\");\r\n");
                write(indent + "    else if (Configuration.doAutoCreate())\r\n");
                if ("XhtmlNode".equals(tn))
                    write(indent + "      this." + getElementName(e.getName(), true) + " = new XhtmlNode(NodeType.Element, \"div\"); // cc.1\r\n");
                else
                    write(indent + "      this." + getElementName(e.getName(), true) + " = new " + tn + "(); // cc\r\n");
            }
            write(indent + "  return this." + getElementName(e.getName(), true) + ";\r\n");
            write(indent + "}\r\n");
            write("\r\n");
            if (e.getType().size() > 1 && (tn.equals("DataType") || !tn.endsWith(".DataType"))) {
                for (TypeRefComponent t : e.getType()) {
                    jdoc(indent, "@return {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
                    String ttn = getTypename(t);
                    write(indent + "public " + ttn + " get" + getTitle(getElementName(e.getName(), false)) + ttn + "() throws FHIRException { \r\n");
                    write(indent + "  if (this." + getElementName(e.getName(), true) + " == null)\r\n");
                    write(indent + "    this." + getElementName(e.getName(), true) + " = new " + ttn + "();\r\n");
                    write(indent + "  if (!(this." + getElementName(e.getName(), true) + " instanceof " + ttn + "))\r\n");
                    write(indent + "    throw new FHIRException(\"Type mismatch: the type " + ttn + " was expected, but \"+this." + getElementName(e.getName(), true) + ".getClass().getName()+\" was encountered\");\r\n");
                    write(indent + "  return (" + ttn + ") this." + getElementName(e.getName(), true) + ";\r\n");
                    write(indent + "}\r\n");
                    write("\r\n");
                    write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + ttn + "() { \r\n");
                    write(indent + "  return this != null && this." + getElementName(e.getName(), true) + " instanceof " + ttn + ";\r\n");
                    write(indent + "}\r\n");
                    write("\r\n");
                }
            }
            write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
            write(indent + "  return this." + getElementName(e.getName(), true) + " != null && !this." + getElementName(e.getName(), true) + ".isEmpty();\r\n");
            write(indent + "}\r\n");
            write("\r\n");
            jdoc(indent, "@param value {@link #" + getElementName(e.getName(), true) + "} (" + replaceTitle(analysis.getName(), e.getDefinition()) + ")");
            write(indent + "public " + className + " set" + getTitle(getElementName(e.getName(), false)) + "(" + tn + " value) { \r\n");
            if (e.getType().size() > 1 && (tn.equals("DataType") || !tn.endsWith(".DataType"))) {
                write(indent + "  if (value != null && !(");
                boolean first = true;
                for (TypeRefComponent t : e.getType()) {
                    if (first)
                        first = false;
                    else
                        write(" || ");
                    write("value instanceof ");
                    write(getTypename(t));
                }
                write("))\r\n");
                write(indent + "    throw new Error(\"Not the right type for " + e.getPath() + ": \"+value.fhirType());\r\n");
            }
            write(indent + "  this." + getElementName(e.getName(), true) + " = value;\r\n");
            write(indent + "  return this;\r\n");
            write(indent + "}\r\n");
            write("\r\n");
        }
    }
}
Also used : StructureDefinition(org.hl7.fhir.r5.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)

Aggregations

ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)22 ModelInfo (org.hl7.elm_modelinfo.r1.ModelInfo)9 Test (org.junit.Test)9 ClassInfo (org.hl7.elm_modelinfo.r1.ClassInfo)8 EnumInfo (org.hl7.fhir.core.generator.analysis.EnumInfo)5 TypeRefComponent (org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)5 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)5 ValueSet (org.hl7.fhir.r5.model.ValueSet)5 QName (javax.xml.namespace.QName)4 TypeInfo (org.hl7.fhir.core.generator.analysis.TypeInfo)4 ArrayList (java.util.ArrayList)3 TypeInfo (org.hl7.elm_modelinfo.r1.TypeInfo)3 File (java.io.File)2 ModelSpecifier (org.hl7.elm_modelinfo.r1.ModelSpecifier)2 ProfileUtilities (org.hl7.fhir.r5.conformance.ProfileUtilities)2 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 ChoiceTypeSpecifier (org.hl7.elm_modelinfo.r1.ChoiceTypeSpecifier)1