001package org.hl7.fhir.convertors.conv10_40.resources10_40;
002
003import org.hl7.fhir.convertors.VersionConvertorConstants;
004import org.hl7.fhir.convertors.context.ConversionContext10_40;
005import org.hl7.fhir.convertors.conv10_40.datatypes10_40.Reference10_40;
006import org.hl7.fhir.convertors.conv10_40.datatypes10_40.complextypes10_40.CodeableConcept10_40;
007import org.hl7.fhir.convertors.conv10_40.datatypes10_40.complextypes10_40.Ratio10_40;
008import org.hl7.fhir.convertors.conv10_40.datatypes10_40.complextypes10_40.SimpleQuantity10_40;
009import org.hl7.fhir.convertors.conv10_40.datatypes10_40.primitivetypes10_40.Boolean10_40;
010import org.hl7.fhir.exceptions.FHIRException;
011
012public class Medication10_40 {
013
014  public static org.hl7.fhir.r4.model.Medication convertMedication(org.hl7.fhir.dstu2.model.Medication src) throws FHIRException {
015    if (src == null || src.isEmpty())
016      return null;
017    org.hl7.fhir.r4.model.Medication tgt = new org.hl7.fhir.r4.model.Medication();
018    ConversionContext10_40.INSTANCE.getVersionConvertor_10_40().copyDomainResource(src, tgt);
019    if (src.hasCode())
020      tgt.setCode(CodeableConcept10_40.convertCodeableConcept(src.getCode()));
021    if (src.hasIsBrandElement())
022      tgt.addExtension(
023        VersionConvertorConstants.EXT_MED_ISBRAND,
024        Boolean10_40.convertBoolean(src.getIsBrandElement())
025      );
026    if (src.hasManufacturer())
027      tgt.setManufacturer(Reference10_40.convertReference(src.getManufacturer()));
028    if (src.hasProduct()) {
029      if (src.getProduct().hasForm())
030        tgt.setForm(CodeableConcept10_40.convertCodeableConcept(src.getProduct().getForm()));
031      for (org.hl7.fhir.dstu2.model.Medication.MedicationProductIngredientComponent ingridient : src.getProduct().getIngredient())
032        tgt.addIngredient(convertMedicationIngridient(ingridient));
033      if (src.getProduct().hasBatch())
034        tgt.setBatch(batch(src.getProduct().getBatch().get(0)));
035    }
036    if (src.hasPackage()) {
037      org.hl7.fhir.dstu2.model.Medication.MedicationPackageComponent package_ = src.getPackage();
038      if (package_.hasContainer())
039        tgt.addExtension(
040          VersionConvertorConstants.EXT_MED_PACK_CONTAINER,
041          CodeableConcept10_40.convertCodeableConcept(package_.getContainer())
042        );
043      for (org.hl7.fhir.dstu2.model.Medication.MedicationPackageContentComponent c : package_.getContent())
044        tgt.addExtension(
045          VersionConvertorConstants.EXT_MED_PACK_CONTENT,
046          Medication10_40.content(c)
047        );
048    }
049    return tgt;
050  }
051
052  private static org.hl7.fhir.r4.model.Medication.MedicationBatchComponent batch(org.hl7.fhir.dstu2.model.Medication.MedicationProductBatchComponent src) {
053    if (src == null || src.isEmpty())
054      return null;
055    org.hl7.fhir.r4.model.Medication.MedicationBatchComponent tgt = new org.hl7.fhir.r4.model.Medication.MedicationBatchComponent();
056    ConversionContext10_40.INSTANCE.getVersionConvertor_10_40().copyBackboneElement(src,tgt);
057    if (src.hasLotNumber())
058      tgt.setLotNumber(src.getLotNumber());
059    if (src.hasExpirationDate())
060      tgt.setExpirationDate(src.getExpirationDate());
061    return tgt;
062  }
063
064  private static org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent convertMedicationIngridient(org.hl7.fhir.dstu2.model.Medication.MedicationProductIngredientComponent src) {
065    if (src == null || src.isEmpty())
066      return null;
067    org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent tgt = new org.hl7.fhir.r4.model.Medication.MedicationIngredientComponent();
068    ConversionContext10_40.INSTANCE.getVersionConvertor_10_40().copyBackboneElement(src,tgt);
069    if (src.hasItem())
070      tgt.setItem(Reference10_40.convertReference(src.getItem()));
071    if (src.hasAmount())
072      tgt.setStrength(Ratio10_40.convertRatio(src.getAmount()));
073    return tgt;
074  }
075
076  public static org.hl7.fhir.r4.model.Extension content(org.hl7.fhir.dstu2.model.Medication.MedicationPackageContentComponent src) {
077    if (src == null || src.isEmpty())
078      return null;
079    org.hl7.fhir.r4.model.Extension tgt = new org.hl7.fhir.r4.model.Extension();
080    ConversionContext10_40.INSTANCE.getVersionConvertor_10_40().copyElement(src, tgt);
081    if (src.hasItem())
082      tgt.addExtension(
083        VersionConvertorConstants.EXT_MED_PACK_CONTENT,
084        Reference10_40.convertReference(src.getItem())
085      );
086    if (src.hasAmount())
087      tgt.addExtension(
088        VersionConvertorConstants.EXT_MED_PACK_AMOUNT,
089        SimpleQuantity10_40.convertSimpleQuantity(src.getAmount())
090      );
091    return tgt;
092  }
093}