Search in sources :

Example 16 with DataType

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

the class OperationDefinition14_50 method convertOperationDefinitionParameterBindingComponent.

public static org.hl7.fhir.r5.model.OperationDefinition.OperationDefinitionParameterBindingComponent convertOperationDefinitionParameterBindingComponent(org.hl7.fhir.dstu2016may.model.OperationDefinition.OperationDefinitionParameterBindingComponent src) throws FHIRException {
    if (src == null || src.isEmpty())
        return null;
    org.hl7.fhir.r5.model.OperationDefinition.OperationDefinitionParameterBindingComponent tgt = new org.hl7.fhir.r5.model.OperationDefinition.OperationDefinitionParameterBindingComponent();
    ConversionContext14_50.INSTANCE.getVersionConvertor_14_50().copyElement(src, tgt);
    if (src.hasStrength())
        tgt.setStrengthElement(Enumerations14_50.convertBindingStrength(src.getStrengthElement()));
    DataType t = ConversionContext14_50.INSTANCE.getVersionConvertor_14_50().convertType(src.getValueSet());
    if (t != null) {
        if (t instanceof org.hl7.fhir.r5.model.Reference)
            tgt.setValueSet(((org.hl7.fhir.r5.model.Reference) t).getReference());
        else
            tgt.setValueSet(t.primitiveValue());
        tgt.setValueSet(VersionConvertorConstants.refToVS(tgt.getValueSet()));
    }
    return tgt;
}
Also used : DataType(org.hl7.fhir.r5.model.DataType)

Example 17 with DataType

use of org.hl7.fhir.r5.model.DataType 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 18 with DataType

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

Example 19 with DataType

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

the class Analyser method scanNestedTypes.

private void scanNestedTypes(Analysis analysis, TypeInfo type, String path, ElementDefinition e) throws Exception {
    String tn = null;
    if (e.typeSummary().equals("code") && e.hasBinding()) {
        ElementDefinitionBindingComponent cd = e.getBinding();
        if (isEnum(cd)) {
            ValueSet vs = definitions.getValuesets().get(cd.getValueSet());
            if (vs != null) {
                tn = getCodeListType(vs.getName());
                EnumInfo ei = analysis.getEnums().get(tn);
                if (ei == null) {
                    ei = new EnumInfo(tn);
                    analysis.getEnums().put(tn, ei);
                    ei.setValueSet(vs);
                }
                if (tn.equals("SubscriptionStatus")) {
                    // work around cause there's a Resource with the same name
                    tn = "org.hl7.fhir.r4b.model.Enumerations." + tn;
                }
                e.setUserData("java.type", "Enumeration<" + tn + ">");
                e.setUserData("java.enum", ei);
            }
        }
    }
    if (tn == null) {
        if (e.getType().size() > 0 && !e.hasContentReference() && (!Utilities.existsInList(e.getType().get(0).getCode(), "Element", "BackboneElement"))) {
            tn = getTypeName(e);
            if (e.typeSummary().equals("xml:lang"))
                tn = "CodeType";
            if (e.typeSummary().equals("xhtml"))
                tn = "XhtmlNode";
            else if (e.getType().size() > 1)
                tn = "DataType";
            else if (definitions.hasPrimitiveType(tn))
                tn = upFirst(tn) + "Type";
            e.setUserData("java.type", tn);
        } else {
            if (e.hasContentReference()) {
                ElementDefinition er = getElementForPath(analysis.getStructure(), e.getContentReference().substring(1));
                tn = er.getUserString("java.type");
                if (Utilities.noString(tn)) {
                    // have to resolve this later
                    e.setUserData("java.type", "@" + er.getPath());
                } else {
                    e.setUserData("java.type", tn);
                }
            } else {
                String cpath;
                if (e.hasExtension("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name")) {
                    tn = upFirst(e.getExtensionString("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name")) + "Component";
                    cpath = e.getExtensionString("http://hl7.org/fhir/StructureDefinition/structuredefinition-explicit-type-name");
                } else if (config.getIni().hasProperty("typenames", e.getPath())) {
                    tn = upFirst(config.getIni().getStringProperty("typenames", e.getPath())) + "Component";
                    cpath = config.getIni().getStringProperty("typenames", e.getPath());
                } else {
                    tn = path + upFirst(getTitle(e.getName())) + "Component";
                    cpath = path + getTitle(e.getName());
                }
                if (tn.equals("Element"))
                    tn = "Element_";
                if (analysis.getTypes().containsKey(tn)) {
                    char i = 'A';
                    while (analysis.getTypes().containsKey(tn + i)) {
                        i++;
                    }
                    tn = tn + i;
                }
                e.setUserData("java.type", tn);
                tn = upFirst(tn);
                TypeInfo ctype = new TypeInfo();
                ctype.setName(tn);
                analysis.getTypes().put(ctype.getName(), ctype);
                analysis.getTypeList().add(ctype);
                ctype.setDefn(e);
                ctype.setAncestorName(e.typeSummary());
                ctype.setChildren(filterChildren(new ProfileUtilities(null, null, null).getChildList(analysis.getStructure(), ctype.getDefn())));
                for (ElementDefinition c : ctype.getChildren()) {
                    scanNestedTypes(analysis, ctype, cpath, c);
                }
            }
        }
    }
}
Also used : ProfileUtilities(org.hl7.fhir.r5.conformance.ProfileUtilities) ElementDefinition(org.hl7.fhir.r5.model.ElementDefinition) ElementDefinitionBindingComponent(org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionBindingComponent) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 20 with DataType

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

the class ProfileUtilities method describeCoded.

private Piece describeCoded(HierarchicalTableGenerator gen, DataType fixed) {
    if (fixed instanceof Coding) {
        Coding c = (Coding) fixed;
        ValidationResult vr = context.validateCode(terminologyServiceOptions, c.getSystem(), c.getVersion(), c.getCode(), c.getDisplay());
        if (vr.getDisplay() != null)
            return gen.new Piece(null, " (" + vr.getDisplay() + ")", null).addStyle("color: darkgreen");
    } else if (fixed instanceof CodeableConcept) {
        CodeableConcept cc = (CodeableConcept) fixed;
        for (Coding c : cc.getCoding()) {
            ValidationResult vr = context.validateCode(terminologyServiceOptions, c.getSystem(), c.getVersion(), c.getCode(), c.getDisplay());
            if (vr.getDisplay() != null)
                return gen.new Piece(null, " (" + vr.getDisplay() + ")", null).addStyle("color: darkgreen");
        }
    }
    return null;
}
Also used : Coding(org.hl7.fhir.r4b.model.Coding) Piece(org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece) ValidationResult(org.hl7.fhir.r4b.context.IWorkerContext.ValidationResult) CodeableConcept(org.hl7.fhir.r4b.model.CodeableConcept)

Aggregations

DataType (org.hl7.fhir.r5.model.DataType)14 FHIRException (org.hl7.fhir.exceptions.FHIRException)11 XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)11 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 FHIRFormatError (org.hl7.fhir.exceptions.FHIRFormatError)7 Collectors (java.util.stream.Collectors)6 DataType (org.hl7.cql.model.DataType)6 ModelInfo (org.hl7.elm_modelinfo.r1.ModelInfo)6 Piece (org.hl7.fhir.utilities.xhtml.HierarchicalTableGenerator.Piece)6 ArrayList (java.util.ArrayList)5 StructType (org.apache.spark.sql.types.StructType)5 BooleanType (org.hl7.fhir.r5.model.BooleanType)5 ValueSet (org.hl7.fhir.r5.model.ValueSet)5 Arrays (java.util.Arrays)4 QName (javax.xml.namespace.QName)4 DataType (org.hl7.fhir.r4b.model.DataType)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3