001package org.hl7.fhir.convertors.conv30_40.resources30_40;
002
003import org.hl7.fhir.convertors.VersionConvertorConstants;
004import org.hl7.fhir.convertors.context.ConversionContext30_40;
005import org.hl7.fhir.convertors.conv30_40.datatypes30_40.Reference30_40;
006import org.hl7.fhir.convertors.conv30_40.datatypes30_40.complextypes30_40.Attachment30_40;
007import org.hl7.fhir.convertors.conv30_40.datatypes30_40.complextypes30_40.CodeableConcept30_40;
008import org.hl7.fhir.convertors.conv30_40.datatypes30_40.complextypes30_40.Ratio30_40;
009import org.hl7.fhir.convertors.conv30_40.datatypes30_40.complextypes30_40.SimpleQuantity30_40;
010import org.hl7.fhir.convertors.conv30_40.datatypes30_40.primitivetypes30_40.Boolean30_40;
011import org.hl7.fhir.convertors.conv30_40.datatypes30_40.primitivetypes30_40.DateTime30_40;
012import org.hl7.fhir.convertors.conv30_40.datatypes30_40.primitivetypes30_40.String30_40;
013import org.hl7.fhir.exceptions.FHIRException;
014
015import java.util.List;
016
017
018/**
019 * Conversion is based on mapping defined at https://hl7.org/fhir/R4/medication-version-maps.html
020 * Only deviation is the status mapping, which seems incorrect in the definition.
021 * Because they define an extension based mapping, but both dstu3 and r4 have a corresponding status field,
022 * so it seems incorrect to not just map the status fields
023 */
024
025public class Medication30_40 {
026  private static final String[] IGNORED_EXTENSION_URLS = new String[]{
027    VersionConvertorConstants.EXT_MED_ISBRAND, VersionConvertorConstants.EXT_MED_OTC,
028    VersionConvertorConstants.EXT_MED_PACK_CONTAINER, VersionConvertorConstants.EXT_MED_PACK_CONTENT,
029    VersionConvertorConstants.EXT_MED_IMAGE
030  };
031
032  public static org.hl7.fhir.r4.model.Medication convertMedication(org.hl7.fhir.dstu3.model.Medication src) throws FHIRException {
033    if (src == null)
034      return null;
035    org.hl7.fhir.r4.model.Medication tgt = new org.hl7.fhir.r4.model.Medication();
036    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyDomainResource(src, tgt);
037    if (src.hasCode())
038      tgt.setCode(CodeableConcept30_40.convertCodeableConcept(src.getCode()));
039    if (src.hasStatus())
040      tgt.setStatusElement(convertMedicationStatus(src.getStatusElement()));
041    if (src.hasIsBrand())
042      tgt.addExtension(new org.hl7.fhir.r4.model.Extension(VersionConvertorConstants.EXT_MED_ISBRAND, new org.hl7.fhir.r4.model.BooleanType(src.getIsBrand())));
043    if (src.hasIsOverTheCounter())
044      tgt.addExtension(new org.hl7.fhir.r4.model.Extension(VersionConvertorConstants.EXT_MED_OTC, new org.hl7.fhir.r4.model.BooleanType(src.getIsOverTheCounter())));
045    if (src.hasManufacturer())
046      tgt.setManufacturer(Reference30_40.convertReference(src.getManufacturer()));
047    if (src.hasForm())
048      tgt.setForm(CodeableConcept30_40.convertCodeableConcept(src.getForm()));
049    for (org.hl7.fhir.dstu3.model.Medication.MedicationIngredientComponent t : src.getIngredient())
050      tgt.addIngredient(convertMedicationIngredientComponent(t));
051
052    if (src.hasPackage()) {
053      if (src.getPackage().hasContainer())
054        tgt.addExtension(
055          VersionConvertorConstants.EXT_MED_PACK_CONTAINER,
056          CodeableConcept30_40.convertCodeableConcept(src.getPackage().getContainer())
057        );
058      for (org.hl7.fhir.dstu3.model.Medication.MedicationPackageContentComponent c : src.getPackage().getContent())
059        tgt.addExtension(
060          VersionConvertorConstants.EXT_MED_PACK_CONTENT,
061          Medication30_40.convertMedicationPackageContentComponent(c)
062        );
063      tgt.setBatch(convertMedicationPackageBatchComponent(src.getPackage().getBatchFirstRep()));
064    }
065
066    for (org.hl7.fhir.dstu3.model.Attachment attachment : src.getImage()) {
067      tgt.addExtension(new org.hl7.fhir.r4.model.Extension(VersionConvertorConstants.EXT_MED_IMAGE, Attachment30_40.convertAttachment(attachment)));
068    }
069    return tgt;
070  }
071
072  public static org.hl7.fhir.dstu3.model.Medication convertMedication(org.hl7.fhir.r4.model.Medication src) throws FHIRException {
073    if (src == null)
074      return null;
075
076    org.hl7.fhir.dstu3.model.Medication tgt = new org.hl7.fhir.dstu3.model.Medication();
077    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyDomainResource(src, tgt, IGNORED_EXTENSION_URLS);
078    if (src.hasCode())
079      tgt.setCode(CodeableConcept30_40.convertCodeableConcept(src.getCode()));
080    if (src.hasStatus())
081      tgt.setStatusElement(convertMedicationStatus(src.getStatusElement()));
082    if (src.hasExtension(VersionConvertorConstants.EXT_MED_ISBRAND)) {
083      org.hl7.fhir.r4.model.Extension ext = src.getExtensionByUrl(VersionConvertorConstants.EXT_MED_ISBRAND);
084      if (ext.hasValue() && ext.getValue() instanceof org.hl7.fhir.r4.model.BooleanType) {
085        tgt.setIsBrandElement(Boolean30_40.convertBoolean((org.hl7.fhir.r4.model.BooleanType) ext.getValue()));
086      }
087    }
088    if (src.hasExtension(VersionConvertorConstants.EXT_MED_OTC)) {
089      org.hl7.fhir.r4.model.Extension ext = src.getExtensionByUrl(VersionConvertorConstants.EXT_MED_OTC);
090      if (ext.hasValue() && ext.getValue() instanceof org.hl7.fhir.r4.model.BooleanType) {
091        tgt.setIsOverTheCounterElement(Boolean30_40.convertBoolean((org.hl7.fhir.r4.model.BooleanType) ext.getValue()));
092      }
093    }
094    if (src.hasManufacturer())
095      tgt.setManufacturer(Reference30_40.convertReference(src.getManufacturer()));
096    if (src.hasForm())
097      tgt.setForm(CodeableConcept30_40.convertCodeableConcept(src.getForm()));
098    for (org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent t : src.getIngredient())
099      tgt.addIngredient(convertMedicationIngredientComponent(t));
100
101    if (src.hasBatch() || src.hasExtension(VersionConvertorConstants.EXT_MED_PACK_CONTAINER) || src.hasExtension(VersionConvertorConstants.EXT_MED_PACK_CONTENT)) {
102      tgt.setPackage(convertMedicationPackageComponent(src));
103    }
104
105    if (src.hasExtension(VersionConvertorConstants.EXT_MED_IMAGE)) {
106      List<org.hl7.fhir.r4.model.Extension> extensions = src.getExtensionsByUrl(VersionConvertorConstants.EXT_MED_IMAGE);
107      for (org.hl7.fhir.r4.model.Extension ext : extensions) {
108        if (ext.getValue() instanceof org.hl7.fhir.r4.model.Attachment) {
109          tgt.addImage(Attachment30_40.convertAttachment((org.hl7.fhir.r4.model.Attachment) ext.getValue()));
110        }
111      }
112    }
113
114    return tgt;
115  }
116
117  public static org.hl7.fhir.dstu3.model.Medication.MedicationPackageComponent convertMedicationPackageComponent(org.hl7.fhir.r4.model.Medication src) throws FHIRException {
118    if (src == null)
119      return null;
120    org.hl7.fhir.dstu3.model.Medication.MedicationPackageComponent tgt = new org.hl7.fhir.dstu3.model.Medication.MedicationPackageComponent();
121
122    if (src.hasExtension(VersionConvertorConstants.EXT_MED_PACK_CONTAINER)) {
123      org.hl7.fhir.r4.model.Extension ext = src.getExtensionByUrl(VersionConvertorConstants.EXT_MED_PACK_CONTAINER);
124      if (ext.hasValue() && ext.getValue() instanceof org.hl7.fhir.r4.model.CodeableConcept) {
125        tgt.setContainer(CodeableConcept30_40.convertCodeableConcept((org.hl7.fhir.r4.model.CodeableConcept) ext.getValue()));
126      }
127    }
128
129    if (src.hasExtension(VersionConvertorConstants.EXT_MED_PACK_CONTENT)) {
130      org.hl7.fhir.r4.model.Extension ext = src.getExtensionByUrl(VersionConvertorConstants.EXT_MED_PACK_CONTENT);
131      if (ext.hasValue() && ext.getValue() instanceof org.hl7.fhir.r4.model.Extension) {
132        tgt.addContent(convertMedicationPackageContentComponent((org.hl7.fhir.r4.model.Extension) ext.getValue()));
133      }
134    }
135
136    if (src.hasBatch())
137      tgt.addBatch(convertMedicationPackageBatchComponent(src.getBatch()));
138
139    return tgt;
140  }
141
142  public static org.hl7.fhir.r4.model.Extension convertMedicationPackageContentComponent(org.hl7.fhir.dstu3.model.Medication.MedicationPackageContentComponent src) {
143    if (src == null || src.isEmpty())
144      return null;
145    org.hl7.fhir.r4.model.Extension tgt = new org.hl7.fhir.r4.model.Extension();
146    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyElement(src, tgt);
147
148    if (src.hasItem())
149      tgt.addExtension(
150        VersionConvertorConstants.EXT_MED_PACK_CONTENT,
151        ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().convertType(src.getItem())
152      );
153    if (src.hasAmount())
154      tgt.addExtension(
155        VersionConvertorConstants.EXT_MED_PACK_AMOUNT,
156        SimpleQuantity30_40.convertSimpleQuantity(src.getAmount())
157      );
158    return tgt;
159  }
160
161  public static org.hl7.fhir.dstu3.model.Medication.MedicationPackageContentComponent convertMedicationPackageContentComponent(org.hl7.fhir.r4.model.Extension src) {
162    if (src == null || src.isEmpty())
163      return null;
164    org.hl7.fhir.dstu3.model.Medication.MedicationPackageContentComponent tgt = new org.hl7.fhir.dstu3.model.Medication.MedicationPackageContentComponent();
165    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyElement(src, tgt, VersionConvertorConstants.EXT_MED_PACK_CONTENT, VersionConvertorConstants.EXT_MED_PACK_AMOUNT);
166
167    if (src.hasExtension(VersionConvertorConstants.EXT_MED_PACK_CONTENT)) {
168      org.hl7.fhir.r4.model.Extension ext = src.getExtensionByUrl(VersionConvertorConstants.EXT_MED_PACK_CONTENT);
169      if (ext.hasValue() && (ext.getValue() instanceof org.hl7.fhir.r4.model.CodeableConcept || ext.getValue() instanceof org.hl7.fhir.r4.model.Reference)) {
170        tgt.setItem(ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().convertType(ext.getValue()));
171      }
172    }
173
174    if (src.hasExtension(VersionConvertorConstants.EXT_MED_PACK_AMOUNT)) {
175      org.hl7.fhir.r4.model.Extension ext = src.getExtensionByUrl(VersionConvertorConstants.EXT_MED_PACK_AMOUNT);
176      if (ext.hasValue() && ext.getValue() instanceof org.hl7.fhir.r4.model.Quantity) {
177        tgt.setAmount(SimpleQuantity30_40.convertSimpleQuantity((org.hl7.fhir.r4.model.Quantity) ext.getValue()));
178      }
179    }
180
181    return tgt;
182  }
183
184  public static org.hl7.fhir.dstu3.model.Medication.MedicationIngredientComponent convertMedicationIngredientComponent(org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent src) throws FHIRException {
185    if (src == null)
186      return null;
187    org.hl7.fhir.dstu3.model.Medication.MedicationIngredientComponent tgt = new org.hl7.fhir.dstu3.model.Medication.MedicationIngredientComponent();
188    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyBackboneElement(src, tgt);
189    if (src.hasItem())
190      tgt.setItem(ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().convertType(src.getItem()));
191    if (src.hasIsActive())
192      tgt.setIsActiveElement(Boolean30_40.convertBoolean(src.getIsActiveElement()));
193    if (src.hasStrength())
194      tgt.setAmount(Ratio30_40.convertRatio(src.getStrength()));
195    return tgt;
196  }
197
198  public static org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent convertMedicationIngredientComponent(org.hl7.fhir.dstu3.model.Medication.MedicationIngredientComponent src) throws FHIRException {
199    if (src == null)
200      return null;
201    org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent tgt = new org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent();
202    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyBackboneElement(src, tgt);
203    if (src.hasItem())
204      tgt.setItem(ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().convertType(src.getItem()));
205    if (src.hasIsActive())
206      tgt.setIsActiveElement(Boolean30_40.convertBoolean(src.getIsActiveElement()));
207    if (src.hasAmount())
208      tgt.setStrength(Ratio30_40.convertRatio(src.getAmount()));
209    return tgt;
210  }
211
212  public static org.hl7.fhir.dstu3.model.Medication.MedicationPackageBatchComponent convertMedicationPackageBatchComponent(org.hl7.fhir.r4.model.Medication.MedicationBatchComponent src) throws FHIRException {
213    if (src == null)
214      return null;
215    org.hl7.fhir.dstu3.model.Medication.MedicationPackageBatchComponent tgt = new org.hl7.fhir.dstu3.model.Medication.MedicationPackageBatchComponent();
216    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyBackboneElement(src, tgt);
217    if (src.hasLotNumber())
218      tgt.setLotNumberElement(String30_40.convertString(src.getLotNumberElement()));
219    if (src.hasExpirationDate())
220      tgt.setExpirationDateElement(DateTime30_40.convertDateTime(src.getExpirationDateElement()));
221    return tgt;
222  }
223
224  public static org.hl7.fhir.r4.model.Medication.MedicationBatchComponent convertMedicationPackageBatchComponent(org.hl7.fhir.dstu3.model.Medication.MedicationPackageBatchComponent src) throws FHIRException {
225    if (src == null)
226      return null;
227    org.hl7.fhir.r4.model.Medication.MedicationBatchComponent tgt = new org.hl7.fhir.r4.model.Medication.MedicationBatchComponent();
228    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyBackboneElement(src, tgt);
229    if (src.hasLotNumber())
230      tgt.setLotNumberElement(String30_40.convertString(src.getLotNumberElement()));
231    if (src.hasExpirationDate())
232      tgt.setExpirationDateElement(DateTime30_40.convertDateTime(src.getExpirationDateElement()));
233    return tgt;
234  }
235
236  static public org.hl7.fhir.dstu3.model.Enumeration<org.hl7.fhir.dstu3.model.Medication.MedicationStatus> convertMedicationStatus(org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.Medication.MedicationStatus> src) throws FHIRException {
237    if (src == null || src.isEmpty())
238      return null;
239    org.hl7.fhir.dstu3.model.Enumeration<org.hl7.fhir.dstu3.model.Medication.MedicationStatus> tgt = new org.hl7.fhir.dstu3.model.Enumeration<>(new org.hl7.fhir.dstu3.model.Medication.MedicationStatusEnumFactory());
240    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyElement(src, tgt);
241    if (src.getValue() == null) {
242      tgt.setValue(null);
243    } else {
244      switch (src.getValue()) {
245        case ACTIVE:
246          tgt.setValue(org.hl7.fhir.dstu3.model.Medication.MedicationStatus.ACTIVE);
247          break;
248        case INACTIVE:
249          tgt.setValue(org.hl7.fhir.dstu3.model.Medication.MedicationStatus.INACTIVE);
250          break;
251        case ENTEREDINERROR:
252          tgt.setValue(org.hl7.fhir.dstu3.model.Medication.MedicationStatus.ENTEREDINERROR);
253          break;
254        default:
255          tgt.setValue(org.hl7.fhir.dstu3.model.Medication.MedicationStatus.NULL);
256          break;
257      }
258    }
259    return tgt;
260  }
261
262  static public org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.Medication.MedicationStatus> convertMedicationStatus(org.hl7.fhir.dstu3.model.Enumeration<org.hl7.fhir.dstu3.model.Medication.MedicationStatus> src) throws FHIRException {
263    if (src == null || src.isEmpty())
264      return null;
265    org.hl7.fhir.r4.model.Enumeration<org.hl7.fhir.r4.model.Medication.MedicationStatus> tgt = new org.hl7.fhir.r4.model.Enumeration<>(new org.hl7.fhir.r4.model.Medication.MedicationStatusEnumFactory());
266    ConversionContext30_40.INSTANCE.getVersionConvertor_30_40().copyElement(src, tgt);
267    if (src.getValue() == null) {
268      tgt.setValue(null);
269    } else {
270      switch (src.getValue()) {
271        case ACTIVE:
272          tgt.setValue(org.hl7.fhir.r4.model.Medication.MedicationStatus.ACTIVE);
273          break;
274        case INACTIVE:
275          tgt.setValue(org.hl7.fhir.r4.model.Medication.MedicationStatus.INACTIVE);
276          break;
277        case ENTEREDINERROR:
278          tgt.setValue(org.hl7.fhir.r4.model.Medication.MedicationStatus.ENTEREDINERROR);
279          break;
280        default:
281          tgt.setValue(org.hl7.fhir.r4.model.Medication.MedicationStatus.NULL);
282          break;
283      }
284    }
285    return tgt;
286  }
287}