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