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