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