String mapExistingObject = destination.assign(format("(%s)%s"/*(%s, %s, mappingContext)"*/, destination.typeName(), code.callMapper(source, destination), source, destination));
String mapStmt = format(" %s { %s; } else { %s; }", destination.ifNull(), mapNewObject, mapExistingObject);
String ipStmt = "";
if (fieldMap.getInverse() != null) {
VariableRef inverse = new VariableRef(fieldMap.getInverse(), destination);
if (inverse.isCollection()) {
MultiOccurrenceVariableRef inverseCollection = MultiOccurrenceVariableRef.from(inverse);
ipStmt += inverse.ifNull() + inverse.assign(inverseCollection.newCollection()) + ";";
ipStmt += format("%s.add(%s);", inverse, destination.owner());
} else if (inverse.isArray()) {
ipStmt += "/* TODO Orika source code does not support Arrays */";
} else {
ipStmt += statement(inverse.assign(destination.owner()));
}
}
String mapNull = shouldMapNulls(fieldMap, code) ? format(" else {\n %s;\n}\n", destination.assign("null")): "";
return statement("%s { %s; %s } %s", source.ifNotNull(), mapStmt, ipStmt, mapNull);