Search in sources :

Example 26 with DomainResource

use of org.hl7.fhir.dstu2016may.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class XmlParserBase method parseDomainResourceContained.

protected DomainResource parseDomainResourceContained(XmlPullParser xpp) throws IOException, FHIRFormatError, XmlPullParserException {
    next(xpp);
    int eventType = nextNoWhitespace(xpp);
    if (eventType == XmlPullParser.START_TAG) {
        DomainResource dr = (DomainResource) parseResource(xpp);
        nextNoWhitespace(xpp);
        next(xpp);
        return dr;
    } else {
        unknownContent(xpp);
        return null;
    }
}
Also used : DomainResource(org.hl7.fhir.dstu2016may.model.DomainResource)

Example 27 with DomainResource

use of org.hl7.fhir.dstu2016may.model.DomainResource 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 28 with DomainResource

use of org.hl7.fhir.dstu2016may.model.DomainResource 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 29 with DomainResource

use of org.hl7.fhir.dstu2016may.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class JavaParserXmlGenerator method genElement.

private void genElement(Analysis analysis, TypeInfo ti, ElementDefinition ed, ElementDefinition inh, boolean first) throws Exception {
    String name = ed.getName();
    if (name.endsWith("[x]") || name.equals("[type]")) {
        String en = name.endsWith("[x]") && !name.equals("[x]") ? name.replace("[x]", "") : "value";
        String pfx = name.endsWith("[x]") && !name.equals("[x]") ? name.replace("[x]", "") : "";
        parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && nameIsTypeName(xpp, \"" + pfx + "\")) {\r\n");
        parser.append("      res.set" + upFirst(getElementName(en, false)) + "(parseType(\"" + en + "\", xpp));\r\n");
    } else {
        String prsr = null;
        if (ed.hasUserData("java.enum")) {
            // getCodeListType(cd.getBinding());
            EnumInfo ei = (EnumInfo) ed.getUserData("java.enum");
            ValueSet vs = ei.getValueSet();
            boolean enShared = vs.hasUserData("shared");
            String en;
            if (enShared) {
                en = "Enumerations." + ei.getName();
            } else {
                en = analysis.getClassName() + "." + ei.getName();
            }
            // en+".fromCode(parseString(xpp))";
            prsr = "parseEnumeration(xpp, " + en + ".NULL, new " + en.substring(0, en.indexOf(".")) + "." + en.substring(en.indexOf(".") + 1) + "EnumFactory())";
        // parseEnumeration(xpp, Narrative.NarrativeStatus.additional, new Narrative.NarrativeStatusEnumFactory())
        } else {
            String tn = ed.getUserString("java.type");
            if (name.equals("extension")) {
                name = "extension";
                tn = "Extension";
            }
            if (tn.contains("Reference("))
                prsr = "parseReference(xpp)";
            else if (tn.contains("canonical("))
                prsr = "parseCanonical(xpp)";
            else if (tn.contains("("))
                prsr = "parse" + tn + "(xpp)";
            else if (tn.startsWith(analysis.getName()) && !tn.equals(analysis.getClassName())) /* && !definitions.hasType(tn)*/
            {
                prsr = "parse" + upFirst(tn) + "(xpp)";
            } else if (tn.equals("Resource") || tn.equals("DomainResource"))
                prsr = "parse" + upFirst(tn) + "Contained(xpp)";
            else if (isPrimitive(ed)) {
                prsr = "parse" + upFirst(tn.substring(0, tn.length() - 4)) + "(xpp)";
            } else if ((ed.hasContentReference() || ed.isInlineType()) && !tn.startsWith(analysis.getClassName())) {
                prsr = "parse" + analysis.getClassName() + upFirst(tn) + "(xpp)";
            } else {
                prsr = "parse" + upFirst(tn) + "(xpp)";
            }
        }
        if (ed.unbounded()) {
            parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(\"" + name + "\")) {\r\n");
            parser.append("      res.get" + upFirst(getElementName(name, false)) + "().add(" + prsr + ");\r\n");
        } else if (inh != null && inh.unbounded()) {
            parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(\"" + name + "\")) {\r\n");
            parser.append("      res.add" + upFirst(getElementName(name, false)) + (!ed.typeSummary().equals("xhtml") && (isPrimitive(ed) || ed.typeSummary().startsWith("canonical(")) ? "Element" : "") + "(" + prsr + ");\r\n");
        } else {
            parser.append("    " + (!first ? "} else " : "") + "if (eventType == XmlPullParser.START_TAG && xpp.getName().equals(\"" + name + "\")) {\r\n");
            parser.append("      res.set" + upFirst(getElementName(name, false)) + (!ed.typeSummary().equals("xhtml") && (isPrimitive(ed) || ed.typeSummary().startsWith("canonical(")) ? "Element" : "") + "(" + prsr + ");\r\n");
        }
    }
}
Also used : EnumInfo(org.hl7.fhir.core.generator.analysis.EnumInfo) ValueSet(org.hl7.fhir.r5.model.ValueSet)

Example 30 with DomainResource

use of org.hl7.fhir.dstu2016may.model.DomainResource in project org.hl7.fhir.core by hapifhir.

the class DomainResource method copyValues.

public void copyValues(DomainResource dst) {
    super.copyValues(dst);
    dst.text = text == null ? null : text.copy();
    if (contained != null) {
        dst.contained = new ArrayList<Resource>();
        for (Resource i : contained) dst.contained.add(i.copy());
    }
    ;
    if (extension != null) {
        dst.extension = new ArrayList<Extension>();
        for (Extension i : extension) dst.extension.add(i.copy());
    }
    ;
    if (modifierExtension != null) {
        dst.modifierExtension = new ArrayList<Extension>();
        for (Extension i : modifierExtension) dst.modifierExtension.add(i.copy());
    }
    ;
}
Also used : IDomainResource(org.hl7.fhir.instance.model.api.IDomainResource)

Aggregations

XhtmlNode (org.hl7.fhir.utilities.xhtml.XhtmlNode)24 FHIRException (org.hl7.fhir.exceptions.FHIRException)22 DomainResource (org.hl7.fhir.r4.model.DomainResource)21 DefinitionException (org.hl7.fhir.exceptions.DefinitionException)16 IOException (java.io.IOException)15 DomainResource (org.hl7.fhir.dstu3.model.DomainResource)15 FileOutputStream (java.io.FileOutputStream)12 ArrayList (java.util.ArrayList)11 CommaSeparatedStringBuilder (org.hl7.fhir.utilities.CommaSeparatedStringBuilder)11 Test (org.junit.jupiter.api.Test)11 Resource (org.hl7.fhir.r4.model.Resource)10 IBaseResource (org.hl7.fhir.instance.model.api.IBaseResource)9 FileNotFoundException (java.io.FileNotFoundException)8 List (java.util.List)8 NotImplementedException (org.apache.commons.lang3.NotImplementedException)8 StructureDefinition (org.hl7.fhir.r5.model.StructureDefinition)8 SystemRequestDetails (ca.uhn.fhir.jpa.partition.SystemRequestDetails)7 File (java.io.File)7 ElementDefn (org.hl7.fhir.definitions.model.ElementDefn)7 RestIntegrationTest (org.opencds.cqf.ruler.test.RestIntegrationTest)7