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