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