Search in sources :

Example 21 with TypeRefComponent

use of org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent in project org.hl7.fhir.core by hapifhir.

the class ParserBase method getChildProperties.

protected List<Property> getChildProperties(Property property, String elementName, String statedType) throws DefinitionException {
    ElementDefinition ed = property.getDefinition();
    StructureDefinition sd = property.getStructure();
    List<ElementDefinition> children = ProfileUtilities.getChildMap(sd, ed);
    if (children.isEmpty()) {
        // ok, find the right definitions
        String t = null;
        if (ed.getType().size() == 1)
            t = ed.getType().get(0).getCode();
        else if (ed.getType().size() == 0)
            throw new Error("types == 0, and no children found");
        else {
            t = ed.getType().get(0).getCode();
            boolean all = true;
            for (TypeRefComponent tr : ed.getType()) {
                if (!tr.getCode().equals(t)) {
                    all = false;
                    break;
                }
            }
            if (!all) {
                // ok, it's polymorphic
                if (ed.hasRepresentation(PropertyRepresentation.TYPEATTR)) {
                    t = statedType;
                    if (t == null && ToolingExtensions.hasExtension(ed, "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaultype"))
                        t = ToolingExtensions.readStringExtension(ed, "http://hl7.org/fhir/StructureDefinition/elementdefinition-defaultype");
                    boolean ok = false;
                    for (TypeRefComponent tr : ed.getType()) if (tr.getCode().equals(t))
                        ok = true;
                    if (!ok)
                        throw new DefinitionException("Type '" + t + "' is not an acceptable type for '" + elementName + "' on property " + property.getDefinition().getPath());
                } else {
                    t = elementName.substring(tail(ed.getPath()).length() - 3);
                    if (isPrimitive(lowFirst(t)))
                        t = lowFirst(t);
                }
            }
        }
        if (!"xhtml".equals(t)) {
            sd = context.fetchTypeDefinition(t);
            if (sd == null)
                throw new DefinitionException("Unable to find class '" + t + "' for name '" + elementName + "' on property " + property.getDefinition().getPath());
            children = ProfileUtilities.getChildMap(sd, sd.getSnapshot().getElement().get(0));
        }
    }
    List<Property> properties = new ArrayList<Property>();
    for (ElementDefinition child : children) {
        properties.add(new Property(context, child, sd));
    }
    return properties;
}
Also used : StructureDefinition(org.hl7.fhir.dstu2016may.model.StructureDefinition) TypeRefComponent(org.hl7.fhir.dstu2016may.model.ElementDefinition.TypeRefComponent) ArrayList(java.util.ArrayList) FHIRFormatError(org.hl7.fhir.exceptions.FHIRFormatError) ElementDefinition(org.hl7.fhir.dstu2016may.model.ElementDefinition) DefinitionException(org.hl7.fhir.exceptions.DefinitionException)

Example 22 with TypeRefComponent

use of org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent 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 23 with TypeRefComponent

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

Example 24 with TypeRefComponent

use of org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent in project org.hl7.fhir.core by hapifhir.

the class JavaResourceGenerator method resolvedTypeCode.

private String resolvedTypeCode(ElementDefinition e, String tf) {
    if (e.hasContentReference()) {
        return e.getContentReference().replace("#", "@");
    }
    StringBuilder tn = new StringBuilder();
    boolean first = true;
    for (TypeRefComponent t : e.getType()) {
        if ((tf == null || t.getWorkingCode().equals(tf)) && !Utilities.existsInList(t.getWorkingCode(), "Element", "BackboneElement")) {
            if (!first)
                tn.append("|");
            first = false;
            tn.append(t.getWorkingCode());
            if (t.hasTargetProfile()) {
                tn.append("(");
                boolean f = true;
                for (CanonicalType s : t.getTargetProfile()) {
                    // } else {
                    if (!f)
                        tn.append("|");
                    f = false;
                    String stn = s.asStringValue().substring(40);
                    tn.append("Resource".equals(stn) ? "Any" : stn);
                // }
                }
                tn.append(")");
            }
        }
    }
    return tn.toString();
}
Also used : CommaSeparatedStringBuilder(org.hl7.fhir.utilities.CommaSeparatedStringBuilder) TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent) CanonicalType(org.hl7.fhir.r5.model.CanonicalType)

Example 25 with TypeRefComponent

use of org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent in project org.hl7.fhir.core by hapifhir.

the class JavaResourceGenerator method generateUnimplementedAccessors.

private void generateUnimplementedAccessors(Analysis analysis, TypeInfo ti, ElementDefinition e, String indent) throws Exception {
    System.out.println("   .. unimplemented: " + e.getPath());
    String tn = e.getUserString("java.type");
    String className = ti.getName();
    if (Utilities.noString(tn)) {
        tn = e.getUserString("type");
        if (Utilities.noString(tn)) {
            throw new Error("??");
        }
    }
    boolean isReferenceRefField = (analysis.getName().equals("Reference") && e.getName().equals("reference"));
    jdoc(indent, "not supported on this implementation");
    write(indent + "@Override\r\n");
    write(indent + "public int get" + getTitle(getElementName(e.getName(), false)) + "Max() { \r\n");
    write(indent + "  return 0;\r\n");
    write(indent + "}\r\n");
    String simpleType = getSimpleType(tn);
    if (e.unbounded()) {
        /*
       * 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");
        write(indent + "  return new ArrayList<>();\r\n");
        write(indent + "}\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");
        write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
        write(indent + "}\r\n");
        /*
       * hasXXX() for repeatable type
       */
        write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "() { \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");
            write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
            write(indent + "}\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 + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
            write(indent + "}\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 + "  return false;\r\n");
            write(indent + "}\r\n");
        } else {
            if (!definitions.hasResource(tn)) {
                /*
           * addXXX() for repeatable composite
           */
                write(indent + "public " + tn + " add" + getTitle(getElementName(e.getName(), false)) + "() { //3\r\n");
                write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
                write(indent + "}\r\n");
                /*
           * addXXX(foo) for repeatable composite
           */
                write(indent + "public " + className + " add" + getTitle(getElementName(e.getName(), false)) + "(" + tn + " t) { //3\r\n");
                write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
                write(indent + "}\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 + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
                write(indent + "}\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 {2}");
                write(indent + "public " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "FirstRep() { \r\n");
                write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
                write(indent + "}\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");
            } else {
                write(indent + "public " + tn + " get" + getTitle(getElementName(e.getName(), false)) + "Element() { \r\n");
            }
            write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\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 false;\r\n");
            write(indent + "}\r\n");
            write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "() {\r\n");
            write(indent + "  return false;\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 + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
            write(indent + "}\r\n");
            write(indent + "public " + simpleType + " get" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
            write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
            write(indent + "}\r\n");
            generateUnimplementedSetter(analysis, e, indent, className, tn, simpleType, analysis.getName());
            // BigDecimal sugar methods
            if (simpleType.equals("BigDecimal")) {
                generateUnimplementedSetter(analysis, e, indent, className, tn, "long", analysis.getName());
                generateUnimplementedSetter(analysis, e, indent, className, tn, "double", analysis.getName());
            }
        } 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");
            write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
            write(indent + "}\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 + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
                    write(indent + "}\r\n");
                    write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + ttn + "() { \r\n");
                }
            }
            write(indent + "public boolean has" + getTitle(getElementName(e.getName(), false)) + "() { \r\n");
            write(indent + "  return false;\r\n");
            write(indent + "}\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");
            write(indent + "  throw new Error(\"The resource type \\\"" + analysis.getName() + "\\\" does not implement the property \\\"" + e.getName() + "\\\"\");\r\n");
            write(indent + "}\r\n");
            write("\r\n");
        }
    }
}
Also used : TypeRefComponent(org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)

Aggregations

TypeRefComponent (org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent)71 ArrayList (java.util.ArrayList)57 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)47 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)43 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)37 ElementDefinition (org.hl7.fhir.r5.model.ElementDefinition)31 TypeRefComponent (org.hl7.fhir.r4b.model.ElementDefinition.TypeRefComponent)30 FHIRException (org.hl7.fhir.exceptions.FHIRException)28 TypeRefComponent (org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent)24 StructureDefinition (org.hl7.fhir.r4b.model.StructureDefinition)24 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)19 HashSet (java.util.HashSet)18 TypeRefComponent (org.hl7.fhir.dstu3.model.ElementDefinition.TypeRefComponent)18 ValidationMessage (org.hl7.fhir.utilities.validation.ValidationMessage)18 StructureDefinition (org.hl7.fhir.r4.model.StructureDefinition)17 UriType (org.hl7.fhir.r5.model.UriType)15 CanonicalType (org.hl7.fhir.r5.model.CanonicalType)14 List (java.util.List)13 StructureDefinition (org.hl7.fhir.dstu3.model.StructureDefinition)12 Piece (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece)12