001package org.hl7.fhir.convertors.conv14_40.datatypes14_40;
002
003import java.util.List;
004import java.util.stream.Collectors;
005
006import org.apache.commons.lang3.StringUtils;
007import org.hl7.fhir.convertors.VersionConvertorConstants;
008import org.hl7.fhir.convertors.context.ConversionContext14_40;
009import org.hl7.fhir.convertors.conv14_40.datatypes14_40.complextypes14_40.Coding14_40;
010import org.hl7.fhir.convertors.conv14_40.datatypes14_40.primitivetypes14_40.Boolean14_40;
011import org.hl7.fhir.convertors.conv14_40.datatypes14_40.primitivetypes14_40.Code14_40;
012import org.hl7.fhir.convertors.conv14_40.datatypes14_40.primitivetypes14_40.Id14_40;
013import org.hl7.fhir.convertors.conv14_40.datatypes14_40.primitivetypes14_40.Integer14_40;
014import org.hl7.fhir.convertors.conv14_40.datatypes14_40.primitivetypes14_40.MarkDown14_40;
015import org.hl7.fhir.convertors.conv14_40.datatypes14_40.primitivetypes14_40.String14_40;
016import org.hl7.fhir.convertors.conv14_40.datatypes14_40.primitivetypes14_40.Uri14_40;
017import org.hl7.fhir.convertors.conv14_40.resources14_40.Enumerations14_40;
018import org.hl7.fhir.dstu2016may.model.ElementDefinition;
019import org.hl7.fhir.exceptions.FHIRException;
020import org.hl7.fhir.r4.conformance.ProfileUtilities;
021import org.hl7.fhir.utilities.Utilities;
022
023public class ElementDefinition14_40 {
024  public static org.hl7.fhir.r4.model.ElementDefinition convertElementDefinition(org.hl7.fhir.dstu2016may.model.ElementDefinition src, List<ElementDefinition> context, int pos) throws FHIRException {
025    if (src == null || src.isEmpty()) return null;
026    org.hl7.fhir.r4.model.ElementDefinition tgt = new org.hl7.fhir.r4.model.ElementDefinition();
027    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
028    if (src.hasPathElement()) tgt.setPathElement(String14_40.convertString(src.getPathElement()));
029    tgt.setRepresentation(src.getRepresentation().stream().map(ElementDefinition14_40::convertPropertyRepresentation).collect(Collectors.toList()));
030    if (src.hasName()) tgt.setSliceNameElement(String14_40.convertString(src.getNameElement()));
031    if (src.hasLabel()) tgt.setLabelElement(String14_40.convertString(src.getLabelElement()));
032    for (org.hl7.fhir.dstu2016may.model.Coding t : src.getCode()) tgt.addCode(Coding14_40.convertCoding(t));
033    if (src.hasSlicing()) tgt.setSlicing(convertElementDefinitionSlicingComponent(src.getSlicing(), context, pos));
034    if (src.hasShort()) tgt.setShortElement(String14_40.convertString(src.getShortElement()));
035    if (src.hasDefinition()) tgt.setDefinitionElement(MarkDown14_40.convertMarkdown(src.getDefinitionElement()));
036    if (src.hasComments()) tgt.setCommentElement(MarkDown14_40.convertMarkdown(src.getCommentsElement()));
037    if (src.hasRequirements()) tgt.setRequirementsElement(MarkDown14_40.convertMarkdown(src.getRequirementsElement()));
038    for (org.hl7.fhir.dstu2016may.model.StringType t : src.getAlias()) tgt.addAlias(t.getValue());
039    if (src.hasMin()) tgt.setMin(src.getMin());
040    if (src.hasMax()) tgt.setMaxElement(String14_40.convertString(src.getMaxElement()));
041    if (src.hasBase()) tgt.setBase(convertElementDefinitionBaseComponent(src.getBase()));
042    if (src.hasContentReference())
043      tgt.setContentReferenceElement(Uri14_40.convertUri(src.getContentReferenceElement()));
044    for (ElementDefinition.TypeRefComponent t : src.getType())
045      convertTypeRefComponent(t, tgt.getType());
046    if (src.hasDefaultValue())
047      tgt.setDefaultValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getDefaultValue()));
048    if (src.hasMeaningWhenMissing())
049      tgt.setMeaningWhenMissingElement(MarkDown14_40.convertMarkdown(src.getMeaningWhenMissingElement()));
050    if (src.hasFixed())
051      tgt.setFixed(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getFixed()));
052    if (src.hasPattern())
053      tgt.setPattern(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getPattern()));
054    if (src.hasExample())
055      tgt.addExample().setLabel("General").setValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getExample()));
056    if (src.hasMinValue())
057      tgt.setMinValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getMinValue()));
058    if (src.hasMaxValue())
059      tgt.setMaxValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getMaxValue()));
060    if (src.hasMaxLength()) tgt.setMaxLengthElement(Integer14_40.convertInteger(src.getMaxLengthElement()));
061    for (org.hl7.fhir.dstu2016may.model.IdType t : src.getCondition()) tgt.addCondition(t.getValue());
062    for (ElementDefinition.ElementDefinitionConstraintComponent t : src.getConstraint())
063      tgt.addConstraint(convertElementDefinitionConstraintComponent(t));
064    if (src.hasMustSupport()) tgt.setMustSupportElement(Boolean14_40.convertBoolean(src.getMustSupportElement()));
065    if (src.hasIsModifier()) tgt.setIsModifierElement(Boolean14_40.convertBoolean(src.getIsModifierElement()));
066    if (tgt.getIsModifier()) {
067      String reason = org.hl7.fhir.dstu2016may.utils.ToolingExtensions.readStringExtension(src, VersionConvertorConstants.MODIFIER_REASON_EXTENSION);
068      if (Utilities.noString(reason)) reason = VersionConvertorConstants.MODIFIER_REASON_LEGACY;
069      tgt.setIsModifierReason(reason);
070    }
071    if (src.hasIsSummary()) tgt.setIsSummaryElement(Boolean14_40.convertBoolean(src.getIsSummaryElement()));
072    if (src.hasBinding()) tgt.setBinding(convertElementDefinitionBindingComponent(src.getBinding()));
073    for (ElementDefinition.ElementDefinitionMappingComponent t : src.getMapping())
074      tgt.addMapping(convertElementDefinitionMappingComponent(t));
075    return tgt;
076  }
077
078  public static ElementDefinition convertElementDefinition(org.hl7.fhir.r4.model.ElementDefinition src) throws FHIRException {
079    if (src == null || src.isEmpty()) return null;
080    ElementDefinition tgt = new ElementDefinition();
081    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
082    if (src.hasPathElement()) tgt.setPathElement(String14_40.convertString(src.getPathElement()));
083    tgt.setRepresentation(src.getRepresentation().stream().map(ElementDefinition14_40::convertPropertyRepresentation).collect(Collectors.toList()));
084    if (src.hasSliceName()) tgt.setNameElement(String14_40.convertString(src.getSliceNameElement()));
085    if (src.hasLabel()) tgt.setLabelElement(String14_40.convertString(src.getLabelElement()));
086    for (org.hl7.fhir.r4.model.Coding t : src.getCode()) tgt.addCode(Coding14_40.convertCoding(t));
087    if (src.hasSlicing()) tgt.setSlicing(convertElementDefinitionSlicingComponent(src.getSlicing()));
088    if (src.hasShort()) tgt.setShortElement(String14_40.convertString(src.getShortElement()));
089    if (src.hasDefinition()) tgt.setDefinitionElement(MarkDown14_40.convertMarkdown(src.getDefinitionElement()));
090    if (src.hasComment()) tgt.setCommentsElement(MarkDown14_40.convertMarkdown(src.getCommentElement()));
091    if (src.hasRequirements()) tgt.setRequirementsElement(MarkDown14_40.convertMarkdown(src.getRequirementsElement()));
092    for (org.hl7.fhir.r4.model.StringType t : src.getAlias()) tgt.addAlias(t.getValue());
093    if (src.hasMin()) tgt.setMin(src.getMin());
094    if (src.hasMax()) tgt.setMaxElement(String14_40.convertString(src.getMaxElement()));
095    if (src.hasBase()) tgt.setBase(convertElementDefinitionBaseComponent(src.getBase()));
096    if (src.hasContentReference())
097      tgt.setContentReferenceElement(Uri14_40.convertUri(src.getContentReferenceElement()));
098    for (org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent t : src.getType())
099      convertTypeRefComponent(t, tgt.getType());
100    if (src.hasDefaultValue())
101      tgt.setDefaultValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getDefaultValue()));
102    if (src.hasMeaningWhenMissing())
103      tgt.setMeaningWhenMissingElement(MarkDown14_40.convertMarkdown(src.getMeaningWhenMissingElement()));
104    if (src.hasFixed())
105      tgt.setFixed(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getFixed()));
106    if (src.hasPattern())
107      tgt.setPattern(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getPattern()));
108    if (src.hasExample())
109      tgt.setExample(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getExample().get(0).getValue()));
110    if (src.hasMinValue())
111      tgt.setMinValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getMinValue()));
112    if (src.hasMaxValue())
113      tgt.setMaxValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getMaxValue()));
114    if (src.hasMaxLength()) tgt.setMaxLengthElement(Integer14_40.convertInteger(src.getMaxLengthElement()));
115    for (org.hl7.fhir.r4.model.IdType t : src.getCondition()) tgt.addCondition(t.getValue());
116    for (org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionConstraintComponent t : src.getConstraint())
117      tgt.addConstraint(convertElementDefinitionConstraintComponent(t));
118    if (src.hasMustSupport()) tgt.setMustSupportElement(Boolean14_40.convertBoolean(src.getMustSupportElement()));
119    if (src.hasIsModifier()) tgt.setIsModifierElement(Boolean14_40.convertBoolean(src.getIsModifierElement()));
120    if (src.hasIsModifierReason() && !VersionConvertorConstants.MODIFIER_REASON_LEGACY.equals(src.getIsModifierReason()))
121      org.hl7.fhir.dstu2016may.utils.ToolingExtensions.setStringExtension(tgt, VersionConvertorConstants.MODIFIER_REASON_EXTENSION, src.getIsModifierReason());
122    if (src.hasIsSummary()) tgt.setIsSummaryElement(Boolean14_40.convertBoolean(src.getIsSummaryElement()));
123    if (src.hasBinding()) tgt.setBinding(convertElementDefinitionBindingComponent(src.getBinding()));
124    for (org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionMappingComponent t : src.getMapping())
125      tgt.addMapping(convertElementDefinitionMappingComponent(t));
126    return tgt;
127  }
128
129  static public org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation> convertPropertyRepresentation(org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.PropertyRepresentation> src) throws FHIRException {
130    if (src == null || src.isEmpty()) return null;
131    org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation> tgt = new org.hl7.fhir.r4.model.Enumeration<>(new org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentationEnumFactory());
132    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
133    if (src.getValue() == null) {
134      tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation.NULL);
135    } else {
136      switch (src.getValue()) {
137        case XMLATTR:
138          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation.XMLATTR);
139          break;
140        case XMLTEXT:
141          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation.XMLTEXT);
142          break;
143        case TYPEATTR:
144          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation.TYPEATTR);
145          break;
146        case CDATEXT:
147          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation.CDATEXT);
148          break;
149        default:
150          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation.NULL);
151          break;
152      }
153    }
154    return tgt;
155  }
156
157  static public org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.PropertyRepresentation> convertPropertyRepresentation(org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.PropertyRepresentation> src) throws FHIRException {
158    if (src == null || src.isEmpty()) return null;
159    org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.PropertyRepresentation> tgt = new org.hl7.fhir.dstu2016may.model.Enumeration<>(new ElementDefinition.PropertyRepresentationEnumFactory());
160    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
161    if (src.getValue() == null) {
162      tgt.setValue(ElementDefinition.PropertyRepresentation.NULL);
163    } else {
164      switch (src.getValue()) {
165        case XMLATTR:
166          tgt.setValue(ElementDefinition.PropertyRepresentation.XMLATTR);
167          break;
168        case XMLTEXT:
169          tgt.setValue(ElementDefinition.PropertyRepresentation.XMLTEXT);
170          break;
171        case TYPEATTR:
172          tgt.setValue(ElementDefinition.PropertyRepresentation.TYPEATTR);
173          break;
174        case CDATEXT:
175          tgt.setValue(ElementDefinition.PropertyRepresentation.CDATEXT);
176          break;
177        default:
178          tgt.setValue(ElementDefinition.PropertyRepresentation.NULL);
179          break;
180      }
181    }
182    return tgt;
183  }
184
185  /*
186   * This process deals with the fact that 'exists' slicing didn't have a mechanism to flag it in 2016May.
187   * (Pattern and profile both had '@' flags in the discriminator to distinguish this, but exists did not.)
188   * 'Exists' can thus only be determined by looking at the available slices - and checking to see that there
189   * are exactly two slices, one which is mandatory and one which is prohibited.  We need to do that check
190   * at the level where the slices are defined, rather than only inside the 'slicing' element where we don't
191   * have access to the slices themselves.
192   *
193   * This process checks to see if we have a 'value' discriminator (i.e. no '@') and if so, checks for all
194   * matching slices.  If there are exactly two and one's required and one's prohibited, then it sets a flag
195   * so that the converter will declare a discriminator.type of 'exists'.
196   *
197   * Note that we only need complex processing on the R2B -> newer release, not on the reverse.  On the reverse,
198   * we just strip the discriminator type.  What slices exist is still the same.  In theory, that means that the
199   * exists type is unnecessary, but it's far more efficient (and clear) to have it.
200   */
201  public static org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionSlicingComponent convertElementDefinitionSlicingComponent(ElementDefinition.ElementDefinitionSlicingComponent src, List<ElementDefinition> context, int pos) throws FHIRException {
202    if (src == null || src.isEmpty()) return null;
203    org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionSlicingComponent tgt = new org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionSlicingComponent();
204    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
205    ElementDefinition slicingElement = context.get(pos);
206    for (org.hl7.fhir.dstu2016may.model.StringType t : src.getDiscriminator()) {
207      boolean isExists = false;
208      if (!t.asStringValue().contains("@")) {
209        int slices = 0;
210        boolean existsSlicePresent = false;
211        boolean notExistsSlicePresent = false;
212        String url = null;
213        String existsPath = slicingElement.getPath() + "." + t.asStringValue();
214        if (existsPath.contains(".extension(")) {
215          String suffix = StringUtils.substringAfter(existsPath, "(").substring(1);
216          existsPath = StringUtils.substringBefore(existsPath, "(");
217          suffix = StringUtils.substringBefore(suffix, ")");
218          url = suffix.substring(0, suffix.length() - 1);
219        }
220        for (int i = pos + 1; i < context.size(); i++) {
221          ElementDefinition e = context.get(i);
222          if (e.getPath().equals(slicingElement.getPath())) slices++;
223          else if (!e.getPath().startsWith(slicingElement.getPath() + ".")) break;
224          else if (e.getPath().equals(existsPath)) {
225            if (url == null || (e.getType().get(0).hasProfile() && e.getType().get(0).getProfile().get(0).equals(url))) {
226              if (e.hasMin() && e.getMin() > 0 && !e.hasFixed()) existsSlicePresent = true;
227              else if (e.hasMax() && e.getMax().equals("0")) notExistsSlicePresent = true;
228            }
229          }
230        }
231        isExists = (slices == 2 && existsSlicePresent && notExistsSlicePresent) || (slices == 1 && existsSlicePresent != notExistsSlicePresent);
232      }
233      tgt.addDiscriminator(ProfileUtilities.interpretR2Discriminator(t.getValue(), isExists));
234    }
235    if (src.hasDescription()) tgt.setDescriptionElement(String14_40.convertString(src.getDescriptionElement()));
236    if (src.hasOrdered()) tgt.setOrderedElement(Boolean14_40.convertBoolean(src.getOrderedElement()));
237    if (src.hasRules()) tgt.setRulesElement(convertSlicingRules(src.getRulesElement()));
238    return tgt;
239  }
240
241  public static ElementDefinition.ElementDefinitionSlicingComponent convertElementDefinitionSlicingComponent(org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionSlicingComponent src) throws FHIRException {
242    if (src == null || src.isEmpty()) return null;
243    ElementDefinition.ElementDefinitionSlicingComponent tgt = new ElementDefinition.ElementDefinitionSlicingComponent();
244    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
245    for (org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionSlicingDiscriminatorComponent t : src.getDiscriminator())
246      tgt.addDiscriminator(ProfileUtilities.buildR2Discriminator(t));
247    if (src.hasDescription()) tgt.setDescriptionElement(String14_40.convertString(src.getDescriptionElement()));
248    if (src.hasOrdered()) tgt.setOrderedElement(Boolean14_40.convertBoolean(src.getOrderedElement()));
249    if (src.hasRules()) tgt.setRulesElement(convertSlicingRules(src.getRulesElement()));
250    return tgt;
251  }
252
253  static public org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.SlicingRules> convertSlicingRules(org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.SlicingRules> src) throws FHIRException {
254    if (src == null || src.isEmpty()) return null;
255    org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.SlicingRules> tgt = new org.hl7.fhir.r4.model.Enumeration<>(new org.hl7.fhir.r4.model.ElementDefinition.SlicingRulesEnumFactory());
256    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
257    if (src.getValue() == null) {
258      tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.SlicingRules.NULL);
259    } else {
260      switch (src.getValue()) {
261        case CLOSED:
262          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.SlicingRules.CLOSED);
263          break;
264        case OPEN:
265          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.SlicingRules.OPEN);
266          break;
267        case OPENATEND:
268          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.SlicingRules.OPENATEND);
269          break;
270        default:
271          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.SlicingRules.NULL);
272          break;
273      }
274    }
275    return tgt;
276  }
277
278  static public org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.SlicingRules> convertSlicingRules(org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.SlicingRules> src) throws FHIRException {
279    if (src == null || src.isEmpty()) return null;
280    org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.SlicingRules> tgt = new org.hl7.fhir.dstu2016may.model.Enumeration<>(new ElementDefinition.SlicingRulesEnumFactory());
281    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
282    if (src.getValue() == null) {
283      tgt.setValue(ElementDefinition.SlicingRules.NULL);
284    } else {
285      switch (src.getValue()) {
286        case CLOSED:
287          tgt.setValue(ElementDefinition.SlicingRules.CLOSED);
288          break;
289        case OPEN:
290          tgt.setValue(ElementDefinition.SlicingRules.OPEN);
291          break;
292        case OPENATEND:
293          tgt.setValue(ElementDefinition.SlicingRules.OPENATEND);
294          break;
295        default:
296          tgt.setValue(ElementDefinition.SlicingRules.NULL);
297          break;
298      }
299    }
300    return tgt;
301  }
302
303  public static org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBaseComponent convertElementDefinitionBaseComponent(ElementDefinition.ElementDefinitionBaseComponent src) throws FHIRException {
304    if (src == null || src.isEmpty()) return null;
305    org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBaseComponent tgt = new org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBaseComponent();
306    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
307    if (src.hasPathElement()) tgt.setPathElement(String14_40.convertString(src.getPathElement()));
308    tgt.setMin(src.getMin());
309    if (src.hasMaxElement()) tgt.setMaxElement(String14_40.convertString(src.getMaxElement()));
310    return tgt;
311  }
312
313  public static ElementDefinition.ElementDefinitionBaseComponent convertElementDefinitionBaseComponent(org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBaseComponent src) throws FHIRException {
314    if (src == null || src.isEmpty()) return null;
315    ElementDefinition.ElementDefinitionBaseComponent tgt = new ElementDefinition.ElementDefinitionBaseComponent();
316    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
317    if (src.hasPathElement()) tgt.setPathElement(String14_40.convertString(src.getPathElement()));
318    tgt.setMin(src.getMin());
319    if (src.hasMaxElement()) tgt.setMaxElement(String14_40.convertString(src.getMaxElement()));
320    return tgt;
321  }
322
323  static public void convertTypeRefComponent(ElementDefinition.TypeRefComponent src, List<org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent> list) throws FHIRException {
324    if (src == null) return;
325    org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent tgt = null;
326    for (org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent t : list)
327      if (t.getCode().equals(src.getCode())) tgt = t;
328    if (tgt == null) {
329      tgt = new org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent();
330      list.add(tgt);
331      ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
332      tgt.setCode(src.getCode());
333    }
334    if (tgt.hasTarget()) {
335      for (org.hl7.fhir.dstu2016may.model.UriType u : src.getProfile()) {
336        if (src.getCode().equals("Reference")) tgt.addTargetProfile(u.getValue());
337        else tgt.addProfile(u.getValue());
338      }
339      for (org.hl7.fhir.dstu2016may.model.Extension t : src.getExtensionsByUrl(VersionConvertorConstants.PROFILE_EXTENSION)) {
340        String s = ((org.hl7.fhir.dstu2016may.model.PrimitiveType<String>) t.getValue()).getValue();
341        tgt.addProfile(s);
342      }
343    } else {
344      for (org.hl7.fhir.dstu2016may.model.UriType u : src.getProfile()) tgt.addProfile(u.getValue());
345    }
346    for (org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.AggregationMode> t : src.getAggregation()) {
347      org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.AggregationMode> a = convertAggregationMode(t);
348      if (!tgt.hasAggregation(a.getValue()))
349        ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(t, tgt.addAggregation(a.getValue()));
350    }
351    if (src.hasVersioning()) tgt.setVersioningElement(convertReferenceVersionRules(src.getVersioningElement()));
352  }
353
354  public static void convertTypeRefComponent(org.hl7.fhir.r4.model.ElementDefinition.TypeRefComponent src, List<ElementDefinition.TypeRefComponent> list) throws FHIRException {
355    if (src == null) return;
356    ElementDefinition.TypeRefComponent tgt = new ElementDefinition.TypeRefComponent();
357    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
358    tgt.setCode(src.getCode());
359    list.add(tgt);
360    if (src.hasTarget()) {
361      for (org.hl7.fhir.r4.model.UriType u : src.getTargetProfile()) {
362        tgt.addProfile(u.getValue());
363        // disabled 2022-11-11 - gdg - I don't see what value this is bringing anywhere?
364//        String baseName = u.getValue().toLowerCase();
365//        if (baseName.contains("reference") && !baseName.contains("documentreference"))
366//          throw new Error("2016May Target profile contains the word 'reference':" + u);
367      }
368      for (org.hl7.fhir.r4.model.UriType u : src.getProfile()) {
369        if (src.getCode().equals("Reference")) {
370          org.hl7.fhir.dstu2016may.model.Extension t = new org.hl7.fhir.dstu2016may.model.Extension(VersionConvertorConstants.PROFILE_EXTENSION);
371          t.setValue(ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(u));
372          tgt.addExtension(t);
373        } else tgt.addProfile(u.getValue());
374      }
375    } else {
376      for (org.hl7.fhir.r4.model.UriType u : src.getProfile()) {
377        tgt.addProfile(u.getValue());
378      }
379    }
380    for (org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.AggregationMode> t : src.getAggregation()) {
381      org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.AggregationMode> a = convertAggregationMode(t);
382      if (!tgt.hasAggregation(a.getValue()))
383        ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(t, tgt.addAggregation(a.getValue()));
384    }
385    if (src.hasVersioning()) tgt.setVersioningElement(convertReferenceVersionRules(src.getVersioningElement()));
386  }
387
388  static public org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.AggregationMode> convertAggregationMode(org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.AggregationMode> src) throws FHIRException {
389    if (src == null || src.isEmpty()) return null;
390    org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.AggregationMode> tgt = new org.hl7.fhir.r4.model.Enumeration<>(new org.hl7.fhir.r4.model.ElementDefinition.AggregationModeEnumFactory());
391    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
392    if (src.getValue() == null) {
393      tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.AggregationMode.NULL);
394    } else {
395      switch (src.getValue()) {
396        case CONTAINED:
397          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.AggregationMode.CONTAINED);
398          break;
399        case REFERENCED:
400          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.AggregationMode.REFERENCED);
401          break;
402        case BUNDLED:
403          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.AggregationMode.BUNDLED);
404          break;
405        default:
406          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.AggregationMode.NULL);
407          break;
408      }
409    }
410    return tgt;
411  }
412
413  static public org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.AggregationMode> convertAggregationMode(org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.AggregationMode> src) throws FHIRException {
414    if (src == null || src.isEmpty()) return null;
415    org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.AggregationMode> tgt = new org.hl7.fhir.dstu2016may.model.Enumeration<>(new ElementDefinition.AggregationModeEnumFactory());
416    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
417    if (src.getValue() == null) {
418      tgt.setValue(ElementDefinition.AggregationMode.NULL);
419    } else {
420      switch (src.getValue()) {
421        case CONTAINED:
422          tgt.setValue(ElementDefinition.AggregationMode.CONTAINED);
423          break;
424        case REFERENCED:
425          tgt.setValue(ElementDefinition.AggregationMode.REFERENCED);
426          break;
427        case BUNDLED:
428          tgt.setValue(ElementDefinition.AggregationMode.BUNDLED);
429          break;
430        default:
431          tgt.setValue(ElementDefinition.AggregationMode.NULL);
432          break;
433      }
434    }
435    return tgt;
436  }
437
438  static public org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules> convertReferenceVersionRules(org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.ReferenceVersionRules> src) throws FHIRException {
439    if (src == null || src.isEmpty()) return null;
440    org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules> tgt = new org.hl7.fhir.r4.model.Enumeration<>(new org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRulesEnumFactory());
441    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
442    if (src.getValue() == null) {
443      tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules.NULL);
444    } else {
445      switch (src.getValue()) {
446        case EITHER:
447          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules.EITHER);
448          break;
449        case INDEPENDENT:
450          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules.INDEPENDENT);
451          break;
452        case SPECIFIC:
453          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules.SPECIFIC);
454          break;
455        default:
456          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules.NULL);
457          break;
458      }
459    }
460    return tgt;
461  }
462
463  static public org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.ReferenceVersionRules> convertReferenceVersionRules(org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.ReferenceVersionRules> src) throws FHIRException {
464    if (src == null || src.isEmpty()) return null;
465    org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.ReferenceVersionRules> tgt = new org.hl7.fhir.dstu2016may.model.Enumeration<>(new ElementDefinition.ReferenceVersionRulesEnumFactory());
466    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
467    if (src.getValue() == null) {
468      tgt.setValue(ElementDefinition.ReferenceVersionRules.NULL);
469    } else {
470      switch (src.getValue()) {
471        case EITHER:
472          tgt.setValue(ElementDefinition.ReferenceVersionRules.EITHER);
473          break;
474        case INDEPENDENT:
475          tgt.setValue(ElementDefinition.ReferenceVersionRules.INDEPENDENT);
476          break;
477        case SPECIFIC:
478          tgt.setValue(ElementDefinition.ReferenceVersionRules.SPECIFIC);
479          break;
480        default:
481          tgt.setValue(ElementDefinition.ReferenceVersionRules.NULL);
482          break;
483      }
484    }
485    return tgt;
486  }
487
488  public static org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionConstraintComponent convertElementDefinitionConstraintComponent(ElementDefinition.ElementDefinitionConstraintComponent src) throws FHIRException {
489    if (src == null || src.isEmpty()) return null;
490    org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionConstraintComponent tgt = new org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionConstraintComponent();
491    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
492    if (src.hasKeyElement()) tgt.setKeyElement(Id14_40.convertId(src.getKeyElement()));
493    if (src.hasRequirements()) tgt.setRequirementsElement(String14_40.convertString(src.getRequirementsElement()));
494    if (src.hasSeverity()) tgt.setSeverityElement(convertConstraintSeverity(src.getSeverityElement()));
495    if (src.hasHumanElement()) tgt.setHumanElement(String14_40.convertString(src.getHumanElement()));
496    if (src.hasExpression()) tgt.setExpression(Expression14_40.convertToR4Expression(src.getExpression()));
497    if (src.hasXpathElement()) tgt.setXpathElement(String14_40.convertString(src.getXpathElement()));
498    return tgt;
499  }
500
501  public static ElementDefinition.ElementDefinitionConstraintComponent convertElementDefinitionConstraintComponent(org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionConstraintComponent src) throws FHIRException {
502    if (src == null || src.isEmpty()) return null;
503    ElementDefinition.ElementDefinitionConstraintComponent tgt = new ElementDefinition.ElementDefinitionConstraintComponent();
504    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
505    if (src.hasKeyElement()) tgt.setKeyElement(Id14_40.convertId(src.getKeyElement()));
506    if (src.hasRequirements()) tgt.setRequirementsElement(String14_40.convertString(src.getRequirementsElement()));
507    if (src.hasSeverity()) tgt.setSeverityElement(convertConstraintSeverity(src.getSeverityElement()));
508    if (src.hasHumanElement()) tgt.setHumanElement(String14_40.convertString(src.getHumanElement()));
509    if (src.hasExpression()) tgt.setExpression(Expression14_40.convertTo2016MayExpression(src.getExpression()));
510    if (src.hasXpathElement()) tgt.setXpathElement(String14_40.convertString(src.getXpathElement()));
511    return tgt;
512  }
513
514  static public org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverity> convertConstraintSeverity(org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.ConstraintSeverity> src) throws FHIRException {
515    if (src == null || src.isEmpty()) return null;
516    org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverity> tgt = new org.hl7.fhir.r4.model.Enumeration<>(new org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverityEnumFactory());
517    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
518    if (src.getValue() == null) {
519      tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverity.NULL);
520    } else {
521      switch (src.getValue()) {
522        case ERROR:
523          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverity.ERROR);
524          break;
525        case WARNING:
526          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverity.WARNING);
527          break;
528        default:
529          tgt.setValue(org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverity.NULL);
530          break;
531      }
532    }
533    return tgt;
534  }
535
536  static public org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.ConstraintSeverity> convertConstraintSeverity(org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.ElementDefinition.ConstraintSeverity> src) throws FHIRException {
537    if (src == null || src.isEmpty()) return null;
538    org.hl7.fhir.dstu2016may.model.Enumeration<ElementDefinition.ConstraintSeverity> tgt = new org.hl7.fhir.dstu2016may.model.Enumeration<>(new ElementDefinition.ConstraintSeverityEnumFactory());
539    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
540    if (src.getValue() == null) {
541      tgt.setValue(ElementDefinition.ConstraintSeverity.NULL);
542    } else {
543      switch (src.getValue()) {
544        case ERROR:
545          tgt.setValue(ElementDefinition.ConstraintSeverity.ERROR);
546          break;
547        case WARNING:
548          tgt.setValue(ElementDefinition.ConstraintSeverity.WARNING);
549          break;
550        default:
551          tgt.setValue(ElementDefinition.ConstraintSeverity.NULL);
552          break;
553      }
554    }
555    return tgt;
556  }
557
558  public static org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBindingComponent convertElementDefinitionBindingComponent(ElementDefinition.ElementDefinitionBindingComponent src) throws FHIRException {
559    if (src == null || src.isEmpty()) return null;
560    org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBindingComponent tgt = new org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBindingComponent();
561    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
562    if (src.hasStrength()) tgt.setStrengthElement(Enumerations14_40.convertBindingStrength(src.getStrengthElement()));
563    if (src.hasDescription()) tgt.setDescriptionElement(String14_40.convertString(src.getDescriptionElement()));
564    if (src.hasValueSet()) {
565      org.hl7.fhir.r4.model.Type t = ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().convertType(src.getValueSet());
566      if (t instanceof org.hl7.fhir.r4.model.Reference)
567        tgt.setValueSet(((org.hl7.fhir.r4.model.Reference) t).getReference());
568      else tgt.setValueSet(t.primitiveValue());
569      tgt.setValueSet(VersionConvertorConstants.refToVS(tgt.getValueSet()));
570    }
571    return tgt;
572  }
573
574  public static ElementDefinition.ElementDefinitionBindingComponent convertElementDefinitionBindingComponent(org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionBindingComponent src) throws FHIRException {
575    if (src == null || src.isEmpty()) return null;
576    ElementDefinition.ElementDefinitionBindingComponent tgt = new ElementDefinition.ElementDefinitionBindingComponent();
577    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
578    if (src.hasStrength()) tgt.setStrengthElement(Enumerations14_40.convertBindingStrength(src.getStrengthElement()));
579    if (src.hasDescription()) tgt.setDescriptionElement(String14_40.convertString(src.getDescriptionElement()));
580    if (src.hasValueSet()) {
581      String vsr = VersionConvertorConstants.vsToRef(src.getValueSet());
582      if (vsr != null) tgt.setValueSet(new org.hl7.fhir.dstu2016may.model.UriType(vsr));
583      else tgt.setValueSet(new org.hl7.fhir.dstu2016may.model.Reference(src.getValueSet()));
584    }
585    return tgt;
586  }
587
588  public static org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionMappingComponent convertElementDefinitionMappingComponent(ElementDefinition.ElementDefinitionMappingComponent src) throws FHIRException {
589    if (src == null || src.isEmpty()) return null;
590    org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionMappingComponent tgt = new org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionMappingComponent();
591    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
592    if (src.hasIdentityElement()) tgt.setIdentityElement(Id14_40.convertId(src.getIdentityElement()));
593    if (src.hasLanguage()) tgt.setLanguageElement(Code14_40.convertCode(src.getLanguageElement()));
594    if (src.hasMapElement()) tgt.setMapElement(String14_40.convertString(src.getMapElement()));
595    return tgt;
596  }
597
598  public static ElementDefinition.ElementDefinitionMappingComponent convertElementDefinitionMappingComponent(org.hl7.fhir.r4.model.ElementDefinition.ElementDefinitionMappingComponent src) throws FHIRException {
599    if (src == null || src.isEmpty()) return null;
600    ElementDefinition.ElementDefinitionMappingComponent tgt = new ElementDefinition.ElementDefinitionMappingComponent();
601    ConversionContext14_40.INSTANCE.getVersionConvertor_14_40().copyElement(src, tgt);
602    if (src.hasIdentityElement()) tgt.setIdentityElement(Id14_40.convertId(src.getIdentityElement()));
603    if (src.hasLanguage()) tgt.setLanguageElement(Code14_40.convertCode(src.getLanguageElement()));
604    if (src.hasMapElement()) tgt.setMapElement(String14_40.convertString(src.getMapElement()));
605    return tgt;
606  }
607}