CDA-FHIR-Maps (R4)
0.1.1 - CI build

CDA-FHIR-Maps (R4), published by ahdis. This is not an authorized publication; it is the continuous build for version 0.1.1). This version is based on the current content of https://github.com/hl7ch/cda-fhir-maps/ and changes regularly. See the Directory of published versions

StructureMap: Bundle to CDA-CH-EMED

map "http://fhir.ch/ig/cda-fhir-maps/StructureMap/BundleToCdaChEmed" = "BundleToCdaChEmed"

// Medication Entries
// 2020-11-11 Michaela Ziegler, copyright ahdis ag, Apache License
// CDA-CH-EMED:  https://art-decor.org/art-decor/decor-project--cdachemed-
// FHIR CH-EMED: http://fhir.ch/ig/ch-emed/index.html

uses "http://hl7.org/fhir/cda/StructureDefinition/ClinicalDocument" alias ClinicalDocument as target
uses "http://hl7.org/fhir/cda/StructureDefinition/AssignedAuthor" alias AssignedAuthor as target
uses "http://hl7.org/fhir/cda/StructureDefinition/AssignedEntity" alias AssignedEntity as target
uses "http://hl7.org/fhir/cda/StructureDefinition/Author" alias Author as target
uses "http://hl7.org/fhir/cda/StructureDefinition/CustodianOrganization" alias CustodianOrganization as target
uses "http://hl7.org/fhir/cda/StructureDefinition/IVL_TS" alias IVL_TS as target
uses "http://hl7.org/fhir/cda/StructureDefinition/EIVL_TS" alias EIVL_TS as target
uses "http://hl7.org/fhir/cda/StructureDefinition/PatientRole" alias PatientRole as target
uses "http://hl7.org/fhir/cda/StructureDefinition/RecordTarget" alias RecordTarget as target
uses "http://hl7.org/fhir/cda/StructureDefinition/Section" alias Section as target
uses "http://hl7.org/fhir/cda/StructureDefinition/SubstanceAdministration" alias SubstanceAdministration as target
uses "http://hl7.org/fhir/cda/StructureDefinition/SXPR_TS" alias SXPR_TS as target
uses "http://hl7.org/fhir/cda/StructureDefinition/Supply" alias Supply as target
uses "http://hl7.org/fhir/StructureDefinition/Bundle" alias Bundle as source
uses "http://hl7.org/fhir/StructureDefinition/Composition" alias Composition as source
uses "http://hl7.org/fhir/StructureDefinition/Patient" alias Patient as source
uses "http://hl7.org/fhir/StructureDefinition/Practitioner" alias Practitioner as source
uses "http://hl7.org/fhir/StructureDefinition/Organization" alias Organization as source
uses "http://hl7.org/fhir/StructureDefinition/MedicationStatement" alias MedicationStatement as source
uses "http://hl7.org/fhir/StructureDefinition/Dosage" alias Dosage as source

imports "http://fhir.ch/ig/cda-fhir-maps/StructureMap/FhirToCdaTypes"
imports "http://fhir.ch/ig/cda-fhir-maps/StructureMap/BundleToCda"
imports "http://fhir.ch/ig/cda-fhir-maps/StructureMap/BundleToCdaCh"

// source: Annotation note (e.g. http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationstatement.html)
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.2
group AnnotationComment(source note : Annotation, target act : Act) {
  note -> act.classCode = 'ACT' "ACT";
  note -> act.moodCode = 'EVN' "EVN";
  note ->  act.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.2' "templateId";
  note ->  act.templateId as templateId,  templateId.root = '2.16.840.1.113883.10.20.1.40' "templateId";
  note ->  act.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.2' "templateId";
  note ->  act.code as code,  code.code = '48767-8',  code.codeSystem = '2.16.840.1.113883.6.1',  code.displayName = 'Annotation comment',  code.codeSystemName = 'LOINC' "code";
  note.text as noteText -> act.text as text then {
    noteText -> text.data = noteText "text";
    note.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' then {
      extension.valueUrl as valueUrl ->  text.reference as reference,  reference.value = valueUrl "reference";
    } "id";
    note where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  text.reference as reference,  reference.value = '#refpdf' "reference";
  };
  note ->  act.statusCode as code,  code.code = 'completed' "statusCode";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationstatement.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.33&effectiveDate=2019-12-11T11:34:24&language=en-US
group ManufacturedMaterialEntryContentModuleStatement(source bundle : Bundle, source medicationStatement : MedicationStatement, source medication : Medication, target manufacturedMaterial : ManufacturedMaterial) {
  medication -> manufacturedMaterial.classCode = 'MMAT' "MMAT";
  medication -> manufacturedMaterial.determinerCode = 'KIND' "KIND";
  medication ->  manufacturedMaterial.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.33' "CH-PHARM-ManufacturedMaterialContentModule";
  medication ->  manufacturedMaterial.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.1' "templateId";
  medication.code as code then {
    code.text as text ->  manufacturedMaterial.name as name,  name.other = text "brandname";
  } "name";
  medication.code as code then {
    // #mtpc.no.brandedmedication
    code.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' -> manufacturedMaterial.code as code then {
      extension.valueUrl as valueUrl ->  code.originalText as text,  text.reference as reference,  reference.value = valueUrl "reference";
    } "id";
    code where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  manufacturedMaterial.code as code,  code.originalText as text,  text.reference as reference,  reference.value = '#refpdf' "reference";
    code.coding as coding -> manufacturedMaterial.code as ce then CodingCE(coding, ce) "code";
    code where coding.exists() = false ->  manufacturedMaterial.code as ce,  ce.nullFlavor = 'NA' "nullFlavor";
  };
  medication.form as form then {
    form.coding as coding -> manufacturedMaterial.formCode as formCode then CodingCE(coding, formCode) "formCode";
  };
  medication.code as code ->  manufacturedMaterial.asContent as asContent,  asContent.classCode = 'CONT',  asContent.containerPackagedMedicine as containerPackagedMedicine then {
    code -> containerPackagedMedicine.classCode = 'CONT' "CONT";
    code -> containerPackagedMedicine.determinerCode = 'INSTANCE' "INSTANCE";
    code.coding as coding -> containerPackagedMedicine.code as ce then CodingCE(coding, ce) "pharm-code";
    code.text as text ->  containerPackagedMedicine.name as name,  name.other = text "pharm-name";
    medication.form as form then {
      form.coding as coding -> containerPackagedMedicine.formCode as formCode then CodingCE(coding, formCode) "pharm-formCode";
    };
    // Package size
    medication.amount as amount then {
      amount.numerator as numerator -> containerPackagedMedicine.capacityQuantity as capacityQuantity then {
        numerator.value as value -> capacityQuantity.value = value;
        numerator.code as unit -> capacityQuantity.unit = unit "unit";
      };
    };
  };
  medication.ingredient as ingredient ->  manufacturedMaterial.ingredient as ingredient,  ingredient.classCode = 'ACTI' then {
    ingredient.itemCodeableConcept as itemCodeableConcept -> ingredient.ingredient as pharmsubstance then {
      itemCodeableConcept -> pharmsubstance.classCode = 'MMAT' "MMAT";
      itemCodeableConcept -> pharmsubstance.determinerCode = 'KIND' "KIND";
      itemCodeableConcept.text as text ->  pharmsubstance.name as name,  name.other = text "name";
      itemCodeableConcept.coding as coding then {
        coding -> pharmsubstance.code as ce then CodingCE(coding, ce) "formCode";
      };
    } "PharmSubstance";
    ingredient.strength as strength -> ingredient.quantity as quantity then RatioRTOPQPQ(strength, quantity);
  };
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationstatement.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.34&effectiveDate=2019-12-11T11:31:52&language=en-US
group MedicationTreatmentPlanItemEntryContentModule(source bundle : Bundle, source medicationStatement : MedicationStatement, target substanceAdministration : SubstanceAdministration) {
  medicationStatement -> substanceAdministration.classCode = 'SBADM' "SBADM";
  medicationStatement -> substanceAdministration.moodCode = 'INT' "INT";
  medicationStatement ->  substanceAdministration.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.34' "CH-PHARM-MedicationTreatmentPlanItemEntryContentModule";
  medicationStatement ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.7' "IHE-PHARM-MTP";
  medicationStatement ->  substanceAdministration.templateId as templateId,  templateId.root = '2.16.840.1.113883.10.20.1.24' "CCD";
  medicationStatement ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7' "PCC-MedicationEntryContentModule";
  medicationStatement.identifier as identifier -> substanceAdministration.id as id then IdentifierII(identifier, id) "id";
  medicationStatement.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' ->  substanceAdministration.text as text,  text.reference as reference then {
    extension.valueUrl as valueUrl -> reference.value = valueUrl;
  } "narrativeLink";
  medicationStatement where extension.where(url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists().not() ->  substanceAdministration.text as text,  text.reference as reference then {
    medicationStatement -> reference.value = '#refpdf' "referencetopdf";
  } "cat1narrativelink";
  medicationStatement ->  substanceAdministration.statusCode as statusCode,  statusCode.code = 'completed' "code";
  medicationStatement.contained as medication where ('#' + $this.id) in %medicationStatement.medicationReference.reference ->  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.classCode = 'MANU' then {
    medication ->  manufacturedProduct.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.2' "MP-TemplateId";
    medication ->  manufacturedProduct.templateId as templateId,  templateId.root = '2.16.840.1.113883.10.20.1.53' "MP-TemplateId";
    medication -> manufacturedProduct.manufacturedMaterial as manufacturedMaterial then ManufacturedMaterialEntryContentModuleStatement(bundle, medicationStatement, medication, manufacturedMaterial) "manufacturedMaterial";
  };
  // TODO: AUTHOR1 Medication Treatment Plan Author
  medicationStatement where dosage.count() = 1 then {
    medicationStatement.dosage as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-nonstructured') ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.1' "normalDose";
  } "onylonedosage";
  // dosage for normal dosing, as no sequences are present there
  medicationStatement.dosage as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-normal') then DosageInstructionsStartStopFrequencySubstanceAdministration(dosage, substanceAdministration) "DosageSubstanceAdministration";
  // dosage for split dosing (with sequences): effectiveTime (start/stop) & routeCode
  medicationStatement.dosage first as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split') -> substanceAdministration.entryRelationship as entryRelationship then DosageInstructionsEntryStartStopRoute(dosage, substanceAdministration, entryRelationship) "DosageSubstanceAdministration";
  // dosage for split dosing (with sequences): sequence number, effectiveTime (when), dose quantity, consumable
  medicationStatement.dosage as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split') ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP' then DosageInstructionsEntryDosageChangeSubstanceAdministration(dosage, substanceAdministration, entryRelationship) "DosageSubstanceAdministration";
  medicationStatement.reasonCode as reasonCode ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'RSON',  entryRelationship.observation as observation then TreatmentReasonEntryContentModule(reasonCode, observation) "TreatmentReasonEntryContentModule";
  medicationStatement.extension as extension where url = 'http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-ext-treatmentplan' ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'REFR',  entryRelationship.substanceAdministration as substanceAdministration then MTPReferenceEntryContentModule(extension, substanceAdministration) "MTP-Reference";
  // https://github.com/ehealthsuisse/ch-emed/issues/56
  medicationStatement ->  substanceAdministration.repeatNumber as repeatNumber,  repeatNumber.nullFlavor = 'NI' "repeatNumberDefault";
  // TODO: Patient Medication Instructions Contains 1.3.6.1.4.1.19376.1.5.3.1.4.3 IHE Patient Medication Instructions (DYNAMIC)
  medicationStatement.dosage as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-nonstructured') ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.substanceAdministration as substanceAdministration then DosageInstructionsNonStructuredEntryContentModule(dosage, substanceAdministration) "DosageInstructionsNonStructuredEntryContentModule";
  medicationStatement.note as note ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.act as act then AnnotationComment(note, act) "AnnotationComment";
  medicationStatement.extension as extension where $this.url = 'http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-ext-substitution' ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.act as act then SubstitutionStatement(extension, act) "Substitution";
}

// TODO: Tapered Dose
// medicationStatement -> substanceAdministration.templateId as templateId, templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.8' "taperedDose";
// source: http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-normal
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?id=2.16.756.5.30.1.1.10.4.35
// dosage for normal dosing (without sequences) (Statement, Prescription)
group DosageInstructionsStartStopFrequencySubstanceAdministration(source dosage : Dosage, target substanceAdministration : SubstanceAdministration) {
  dosage ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.1' "normalDose";
  dosage as dosage -> substanceAdministration as substanceAdministration then EffectiveTimeStartEnd(dosage, substanceAdministration) "effectiveTimeStartEnd";
  dosage as dosage -> substanceAdministration as substanceAdministration then EffectiveTimeWhenNormal(dosage, substanceAdministration) "effectiveTimeWhen";
  dosage.route as route -> substanceAdministration.routeCode as routeCode then CodeableConceptCE(route, routeCode);
  dosage as dosage -> substanceAdministration as substanceAdministration then DoseQuantity(dosage, substanceAdministration) "doseQuantity";
}

// source: http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?id=2.16.756.5.30.1.1.10.4.36
// dosage for split dosing: effectiveTime (start/stop) & routeCode (Statement, Prescription)
group DosageInstructionsEntryStartStopRoute(source dosage : Dosage, target substanceAdministration : substanceAdministration, target entry : entryRelationship) {
  // if the FHIR doucment has no dosage templateId -> create templateId for split dose
  dosage ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.9' "splitDose";
  dosage as dosage -> substanceAdministration as substanceAdministration then EffectiveTimeStartEnd(dosage, substanceAdministration) "effectiveTimeStartEnd";
  dosage.route as route -> substanceAdministration.routeCode as routeCode then CodeableConceptCE(route, routeCode);
}

// source: http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?id=2.16.756.5.30.1.1.10.4.36
// dosage for split dosing: sequence number, effectiveTime (when), dose quantity, consumable (Statement, Prescription)
group DosageInstructionsEntryDosageChangeSubstanceAdministration(source dosage : Dosage, target substanceAdministration : substanceAdministration, target entry : entryRelationship) {
  dosage.sequence as sequence ->  entry.sequenceNumber as sequenceNumber,  sequenceNumber.value = sequence;
  dosage ->  entry.substanceAdministration as substanceAdministration,  substanceAdministration.classCode = 'SBADM',  substanceAdministration.moodCode = 'INT' then {
    dosage as dosage -> substanceAdministration as substanceAdministration then EffectiveTimeWhenSplit(dosage, substanceAdministration) "effectiveTimeWhen";
    dosage as dosage -> substanceAdministration as substanceAdministration then DoseQuantity(dosage, substanceAdministration) "doseQuantity";
    dosage as dosage ->  substanceAdministration as substanceAdministration,  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.manufacturedMaterial as manufacturedMaterial,  manufacturedMaterial.nullFlavor = 'NA' "consumable";
  } "dosage";
}

// effective time low and high for dosage (Statement, Prescription)
group EffectiveTimeStartEnd(source dosage : Dosage, target substanceAdministration : SubstanceAdministration) {
  dosage.timing as timing then {
    timing.repeat as repeat then {
      repeat.bounds : Period as bounds -> substanceAdministration.effectiveTime = create('IVL_TS') as effectiveTime then {
        bounds.start as start -> effectiveTime.low as low then DateTS(start, low) "low";
        bounds where start.exists().not() ->  effectiveTime.low as low,  low.nullFlavor = 'UNK' "NullFlavorLow";
        bounds.end as end -> effectiveTime.high as high then DateTS(end, high) "high";
        bounds where end.exists().not() ->  effectiveTime.high as high,  high.nullFlavor = 'UNK' "NullFlavor high";
      };
    };
  };
}

// dose quantity for dosage (application schema) (Statement)
group DoseQuantity(source dosage : Dosage, target substanceAdministration : SubstanceAdministration) {
  dosage.doseAndRate as doseAndRate -> substanceAdministration.doseQuantity as doseQuantity then {
    doseAndRate.dose : Quantity as quantity then {
      quantity.value as value -> doseQuantity.value = value;
      quantity.code as code -> doseQuantity.unit = code;
    } "quantity";
  } "doseQuantity";
}

// source: reasonCode Coding (e.g. http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-medicationstatement)
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.41
// Treatment Reason (Statement, Prescription)
group TreatmentReasonEntryContentModule(source reasonCode : Coding, target observation : Observation) {
  reasonCode -> observation.classCode = 'OBS' "OBS";
  reasonCode -> observation.moodCode = 'EVN' "EVN";
  reasonCode ->  observation.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.41' "TreatmentReasonEntryContentModule";
  reasonCode ->  observation.code as code,  code.code = '75326-9',  code.codeSystem = '2.16.840.1.113883.6.1',  code.displayName = 'Problem',  code.codeSystemName = 'LOINC' "code";
  reasonCode.text as reasonText -> observation.text as text then {
    reasonText -> text.data = reasonText "text";
    reasonCode.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' then {
      extension.valueUrl as valueUrl ->  text.reference as reference,  reference.value = valueUrl "reference";
    } "id";
    reasonCode where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  text.reference as reference,  reference.value = '#refpdf' "reference";
  };
  reasonCode ->  observation.statusCode as code,  code.code = 'completed' "statusCode";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-ext-treatmentplan.html
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?section=templates&id=2.16.756.5.30.1.1.10.4.45&effectiveDate=2017-01-10T15:34:25&language=en-US
group MTPReferenceEntryContentModule(source ext : Extension, target substanceAdministration : SubstanceAdministration) {
  ext -> substanceAdministration.classCode = 'SBADM' "SBADM";
  ext -> substanceAdministration.moodCode = 'INT' "INT";
  ext ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.10' "referenceTo-MTP-PlanItemGeneralSpecificationTemplateId";
  ext ->  substanceAdministration.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.45' "CH-PHARM-templateId";
  ext.extension as extension where $this.url = 'id' then {
    extension.valueIdentifier as valueIdentifier -> substanceAdministration.id as id then IdentifierII(valueIdentifier, id) "id";
  } "id";
  ext ->  substanceAdministration.code as code,  code.code = 'MTPItem',  code.codeSystem = '1.3.6.1.4.1.19376.1.9.2.2',  code.displayName = 'Medication Treatment Plan Item',  code.codeSystemName = 'IHE Pharmacy Item Type List' "code";
  ext ->  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.manufacturedMaterial as manufacturedMaterial,  manufacturedMaterial.nullFlavor = 'NA' "consumable";
  ext.extension as extension where $this.url = 'externalDocumentId' then {
    extension.valueIdentifier as valueIdentifier ->  substanceAdministration.reference as reference,  reference.typeCode = 'XCRPT',  reference.externalDocument as externalDocument,  externalDocument.id as id then IdentifierII(valueIdentifier, id) "id";
  } "externalDocumentId";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-dosage-nonstructured.html
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?section=templates&id=2.16.756.5.30.1.1.10.4.52
// Dosage Instruction (Statement, Prescription)
group DosageInstructionsNonStructuredEntryContentModule(source dosage : Dosage, target substanceAdministration : SubstanceAdministration) {
  dosage -> substanceAdministration.classCode = 'SBADM' "SBADM";
  dosage -> substanceAdministration.moodCode = 'INT' "INT";
  dosage ->  substanceAdministration.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.52' "templateId";
  dosage.text as noteText -> substanceAdministration.text as text then {
    noteText -> text.data = noteText "text";
    dosage.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' then {
      extension.valueUrl as valueUrl ->  text.reference as reference,  reference.value = valueUrl "reference";
    } "id";
    dosage where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  text.reference as reference,  reference.value = '#refpdf' "reference";
  };
  dosage ->  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.manufacturedMaterial as manufacturedMaterial,  manufacturedMaterial.nullFlavor = 'NA' "consumable";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationdispense.html
// target: Substitution act Contains 1.3.6.1.4.1.19376.1.9.1.3.9.2 IHE Substitution Act Content Module
// Substitution (Dispense)
group SubstitutionDispense(source substitution : Substitution, target act : Act) {
  substitution -> act.classCode = 'ACT' "ACT";
  substitution -> act.moodCode = 'EVN' "EVN";
  substitution ->  act.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.9.2' "templateId";
  substitution.type as type -> act.code as code then CodeableConceptCE(type, code) "substitution";
  substitution ->  act.statusCode as statusCode,  statusCode.code = 'completed' "statusCode";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationdispense.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.33&effectiveDate=2019-12-11T11:34:24&language=en-US
group ManufacturedMaterialEntryContentModuleDispense(source bundle : Bundle, source medicationDispense : MedicationDispense, source medication : Medication, target manufacturedMaterial : ManufacturedMaterial) {
  medication -> manufacturedMaterial.classCode = 'MMAT' "MMAT";
  medication -> manufacturedMaterial.determinerCode = 'KIND' "KIND";
  medication ->  manufacturedMaterial.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.33' "CH-PHARM-ManufacturedMaterialContentModule";
  medication ->  manufacturedMaterial.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.1' "templateId";
  medication.code as code then {
    code.text as text ->  manufacturedMaterial.name as name,  name.other = text "brandname";
  } "name";
  medication.code as code then {
    // #dis.no.brandedmedication
    code.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' -> manufacturedMaterial.code as code then {
      extension.valueUrl as valueUrl ->  code.originalText as text,  text.reference as reference,  reference.value = valueUrl "reference";
    } "id";
    code where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  manufacturedMaterial.code as code,  code.originalText as text,  text.reference as reference,  reference.value = '#refpdf' "reference";
    code.coding as coding -> manufacturedMaterial.code as ce then CodingCE(coding, ce) "code";
  };
  medication.form as form then {
    form.coding as coding -> manufacturedMaterial.formCode as formCode then CodingCE(coding, formCode) "formCode";
  };
  medication.code as code ->  manufacturedMaterial.asContent as asContent,  asContent.classCode = 'CONT',  asContent.containerPackagedMedicine as containerPackagedMedicine then {
    code -> containerPackagedMedicine.classCode = 'CONT' "CONT";
    code -> containerPackagedMedicine.determinerCode = 'INSTANCE' "INSTANCE";
    code.coding as coding -> containerPackagedMedicine.code as ce then CodingCE(coding, ce) "pharm-code";
    code.text as text ->  containerPackagedMedicine.name as name,  name.other = text "pharm-name";
    medication.form as form then {
      form.coding as coding -> containerPackagedMedicine.formCode as formCode then CodingCE(coding, formCode) "pharm-formCode";
    };
    // Package size
    medication.amount as amount then {
      amount.numerator as numerator -> containerPackagedMedicine.capacityQuantity as capacityQuantity then {
        numerator.value as value -> capacityQuantity.value = value;
        numerator.code as unit -> capacityQuantity.unit = unit "unit";
      };
    };
  };
  medication.ingredient as ingredient ->  manufacturedMaterial.ingredient as ingredient,  ingredient.classCode = 'ACTI' then {
    ingredient.itemCodeableConcept as itemCodeableConcept -> ingredient.ingredient as pharmsubstance then {
      itemCodeableConcept -> pharmsubstance.classCode = 'MMAT' "MMAT";
      itemCodeableConcept -> pharmsubstance.determinerCode = 'KIND' "KIND";
      itemCodeableConcept.text as text ->  pharmsubstance.name as name,  name.other = text "name";
      itemCodeableConcept.coding as coding then {
        coding -> pharmsubstance.code as ce then CodingCE(coding, ce) "formCode";
      };
    } "PharmSubstance";
    ingredient.strength as strength -> ingredient.quantity as quantity then RatioRTOPQPQ(strength, quantity);
  };
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationdispense.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.42
group DispenseItemEntryContentModule(source bundle : Bundle, source medicationDispense : MedicationDispense, target supply : Supply) {
  medicationDispense -> supply.classCode = 'SPLY' "SPLY";
  medicationDispense -> supply.moodCode = 'EVN' "EVN";
  medicationDispense ->  supply.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.42' "CH-PHARM-DispenseItemEntryContentModule";
  medicationDispense ->  supply.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.4' "IHE-PHARM-DIS";
  medicationDispense ->  supply.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.3' "supplyEntry";
  medicationDispense ->  supply.templateId as templateId,  templateId.root = '2.16.840.1.113883.10.20.1.34' "CCD";
  medicationDispense.identifier as identifier -> supply.id as id then IdentifierII(identifier, id) "id";
  medicationDispense.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' ->  supply.text as text,  text.reference as reference then {
    extension.valueUrl as valueUrl -> reference.value = valueUrl;
  } "narrativeLink";
  medicationDispense where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  supply.text as text,  text.reference as reference,  text.reference as reference,  reference.value = '#refpdf' "reference";
  medicationDispense.contained as medication where ('#' + $this.id) in %medicationDispense.medicationReference.reference ->  supply.product as product,  product.manufacturedProduct as manufacturedProduct,  manufacturedProduct.classCode = 'MANU' then {
    medication ->  manufacturedProduct.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.2' "MP-TemplateId";
    medication ->  manufacturedProduct.templateId as templateId,  templateId.root = '2.16.840.1.113883.10.20.1.53' "MP-TemplateId";
    medication -> manufacturedProduct.manufacturedMaterial as manufacturedMaterial then ManufacturedMaterialEntryContentModuleDispense(bundle, medicationDispense, medication, manufacturedMaterial) "manufacturedMaterial";
  };
  // TODO: AUTHOR1 Medication Treatment Plan Author
  medicationDispense.extension as extension where url = 'http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-ext-treatmentplan' ->  supply.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'REFR',  entryRelationship.substanceAdministration as substanceAdministration then MTPReferenceEntryContentModule(extension, substanceAdministration) "MTP-Reference";
  // #24 missing template it if ch-emed-dosage-nonstructured
  medicationDispense where dosageInstruction.count() = 1 then {
    medicationDispense.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-nonstructured') ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.1' "normalDose";
  } "onylonedosage";
  // dosage for normal dosing, as no sequences are present there
  medicationDispense.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-normal') then DosageInstructionsStartStopFrequencySupply(dosage, supply) "DosageSubstanceAdministration";
  // dosage for split dosing, as sequences are present there
  medicationDispense -> supply.entryRelationship as entryRelationship then {
    medicationDispense -> entryRelationship.substanceAdministration as substanceAdministration then {
      // split dosing (with sequences): effectiveTime (start/stop) & routeCode & consumable
      medicationDispense.dosageInstruction first as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split') -> substanceAdministration then DosageInstructionsEntryStartStopRouteConsumable(dosage, entryRelationship, substanceAdministration) "DosageSubstanceAdministration";
      // split dosing (with sequences): sequence number, effectiveTime (when), dose quantity, consumable
      medicationDispense.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split') ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP' then DosageInstructionsEntryDosageChangeEntryRelationship(dosage, entryRelationship) "DosageSubstanceAdministration";
    } "substanceAdministration";
  } "entryRelationship";
  // TODO: Precondition Criterion
  medicationDispense.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-nonstructured') ->  supply.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.substanceAdministration as substanceAdministration then DosageInstructionsNonStructuredEntryContentModule(dosage, substanceAdministration) "DosageInstructionsNonStructuredEntryContentModule";
  // Number of packages
  medicationDispense.quantity as quantity -> supply.quantity as supplyQuantity then {
    quantity.value as value -> supplyQuantity.value = value;
  };
  medicationDispense.note as note ->  supply.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.act as act then AnnotationComment(note, act) "AnnotationComment";
  medicationDispense.substitution as substitution ->  supply.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.act as act then SubstitutionDispense(substitution, act) "Substitution";
}

// source: http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-normal
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?id=2.16.756.5.30.1.1.10.4.35
// dosage for normal dosing (without sequences) (Dispense)
group DosageInstructionsStartStopFrequencySupply(source dosage : Dosage, target supply : Supply) {
  dosage -> supply.entryRelationship as entryRelationship then {
    dosage -> entryRelationship.typeCode = 'COMP' "COMP";
    dosage -> entryRelationship.substanceAdministration as substanceAdministration then {
      dosage -> substanceAdministration.classCode = 'SBADM' "SBADM";
      dosage -> substanceAdministration.moodCode = 'INT' "INT";
      dosage ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.6' "templateId";
      dosage ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.1' "normalDose";
      dosage -> substanceAdministration as substanceAdministration then EffectiveTimeStartEnd(dosage, substanceAdministration) "effectiveTimeStartEnd";
      dosage -> substanceAdministration as substanceAdministration then EffectiveTimeWhenNormal(dosage, substanceAdministration) "effectiveTimeWhen";
      dosage.route as route -> substanceAdministration.routeCode as routeCode then CodeableConceptCE(route, routeCode);
      dosage -> substanceAdministration as substanceAdministration then DoseQuantity(dosage, substanceAdministration) "doseQuantity";
      dosage ->  substanceAdministration as substanceAdministration,  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.manufacturedMaterial as manufacturedMaterial,  manufacturedMaterial.nullFlavor = 'NA' "consumable";
    } "substanceAdministration";
  } "entryRelationship";
}

// source: http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?id=2.16.756.5.30.1.1.10.4.36
// dosage for split dosing: effectiveTime (start/stop) & routeCode & consumable (Dispense)
group DosageInstructionsEntryStartStopRouteConsumable(source dosage : Dosage, target entryRelationship : entryRelationship, target substanceAdministration : SubstanceAdministration) {
  dosage -> entryRelationship.typeCode = 'COMP' "COMP";
  dosage -> substanceAdministration.classCode = 'SBADM' "SBADM";
  dosage -> substanceAdministration.moodCode = 'INT' "INT";
  dosage ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.6' "templateId";
  dosage ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.9' "splitDose";
  dosage -> substanceAdministration as substanceAdministration then EffectiveTimeStartEnd(dosage, substanceAdministration) "effectiveTimeStartEnd";
  dosage.route as route -> substanceAdministration.routeCode as routeCode then CodeableConceptCE(route, routeCode);
  dosage ->  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.manufacturedMaterial as manufacturedMaterial,  manufacturedMaterial.nullFlavor = 'NA' "consumable";
}

// source: http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split
// target: https://art-decor.org/art-decor/decor-templates--ch-pharm-?id=2.16.756.5.30.1.1.10.4.36
// dosage for split dosing: sequence number, effectiveTime (when), dose quantity, consumable (Dispense)
group DosageInstructionsEntryDosageChangeEntryRelationship(source dosage : Dosage, target entryRelationship : entryRelationship) {
  dosage.sequence as sequence ->  entryRelationship.sequenceNumber as sequenceNumber,  sequenceNumber.value = sequence;
  dosage ->  entryRelationship.substanceAdministration as substanceAdministration,  substanceAdministration.classCode = 'SBADM',  substanceAdministration.moodCode = 'INT' then {
    dosage as dosage -> substanceAdministration then EffectiveTimeWhenSplit(dosage, substanceAdministration) "effectiveTimeWhen";
    dosage as dosage -> substanceAdministration then DoseQuantity(dosage, substanceAdministration) "doseQuantity";
    dosage as dosage ->  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.manufacturedMaterial as manufacturedMaterial,  manufacturedMaterial.nullFlavor = 'NA' "consumable";
  } "dosage";
}

// effective time when for normal dosage (Dispense, Statement, Prescription)
group EffectiveTimeWhenNormal(source dosage : Dosage, target substanceAdministration : SubstanceAdministration) {
  dosage.timing as timing then {
    timing.repeat as repeat then {
      repeat where $this.when.count() = 1 ->  substanceAdministration.effectiveTime = create('EIVL_TS') as effectiveTime,  effectiveTime.operator = 'A' then {
        repeat.when as when ->  effectiveTime.event as event,  event.code = when;
      } "when";
      repeat where $this.when.count() > 1 ->  substanceAdministration.effectiveTime = create('SXPR_TS') as effectiveTime,  effectiveTime.operator = 'A' then {
        repeat.when first as when -> effectiveTime.comp = create('EIVL_TS') as comp then {
          when ->  comp.event as event,  event.code = when "when";
        } "comp";
        repeat.when not_first as when ->  effectiveTime.comp = create('EIVL_TS') as comp,  comp.operator = 'I' then {
          when ->  comp.event as event,  event.code = when "when";
        } "compnotfirst";
      } "when";
    };
  };
}

// effective time when for split dosage (Dispense, Statement, Prescription)
group EffectiveTimeWhenSplit(source dosage : Dosage, target substanceAdministration : SubstanceAdministration) {
  dosage.timing as timing then {
    timing.repeat as repeat then {
      repeat where $this.when.count() = 1 -> substanceAdministration.effectiveTime = create('EIVL_TS') as effectiveTime then {
        repeat.when as when ->  effectiveTime.event as event,  event.code = when;
      } "when";
      repeat where $this.when.count() > 1 ->  substanceAdministration.effectiveTime = create('SXPR_TS') as effectiveTime,  effectiveTime.operator = 'A' then {
        repeat.when first as when -> effectiveTime.comp = create('EIVL_TS') as comp then {
          when ->  comp.event as event,  event.code = when "when";
        } "comp";
        repeat.when not_first as when ->  effectiveTime.comp = create('EIVL_TS') as comp,  comp.operator = 'I' then {
          when ->  comp.event as event,  event.code = when "when";
        } "compnotfirst";
      } "when";
    };
  };
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationrequest.html
// target: Substitution permission Contains 1.3.6.1.4.1.19376.1.9.1.3.9.1 IHE Substitution Permission Content Module
group SubstitutionRequest(source substitution : Substitution, target act : Act) {
  substitution -> act.classCode = 'ACT' "ACT";
  substitution -> act.moodCode = 'DEF' "DEF";
  substitution ->  act.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.9.1' "templateId";
  substitution.allowedCodeableConcept as allowedCC -> act.code as code then CodeableConceptCE(allowedCC, code) "substitution";
  substitution ->  act.statusCode as statusCode,  statusCode.code = 'completed' "statusCode";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationrequest.html
// target: Substitution permission Contains 1.3.6.1.4.1.19376.1.9.1.3.9.1 IHE Substitution Permission Content Module
group SubstitutionStatement(source extension : Extension, target act : Act) {
  extension -> act.classCode = 'ACT' "ACT";
  extension -> act.moodCode = 'DEF' "DEF";
  extension ->  act.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.9.1' "templateId";
  extension.valueCodeableConcept as valueCC -> act.code as code then CodeableConceptCE(valueCC, code) "extension";
  extension ->  act.statusCode as statusCode,  statusCode.code = 'completed' "statusCode";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationrequest.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?id=2.16.756.5.30.1.1.10.4.38
group PrescribedQuantity(source dispenseRequest : DispenseRequest, target supply : Supply) {
  dispenseRequest -> supply.classCode = 'SPLY' "SPLY";
  dispenseRequest -> supply.moodCode = 'RQO' "RQO";
  dispenseRequest ->  supply.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.8' "templateId";
  dispenseRequest ->  supply.independentInd as independentInd,  independentInd.value = 'false' "independentInd";
  // Number of packages
  dispenseRequest.quantity as quantity -> supply.quantity as quant then {
    quantity.value as value -> quant.value = value;
  };
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationrequest.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?id=2.16.756.5.30.1.1.10.4.38
group PrescribedQuantityUnit(source dispenseRequest : DispenseRequest, target supply : Supply) {
  dispenseRequest -> supply.classCode = 'SPLY' "SPLY";
  dispenseRequest -> supply.moodCode = 'RQO' "RQO";
  dispenseRequest ->  supply.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.8' "TemplateId";
  dispenseRequest ->  supply.independentInd as independentInd,  independentInd.value = 'false' "independentInd";
  // Number of packages
  dispenseRequest.quantity as quantity -> supply.quantity as quant then {
    quantity.value as value -> quant.value = value;
    quantity.unit as unit -> quant.unit = unit;
  };
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationrequest.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.33
group ManufacturedMaterialEntryContentModulePrescription(source bundle : Bundle, source medicationRequest : MedicationRequest, source medication : Medication, target manufacturedMaterial : ManufacturedMaterial, target substanceAdministration : SubstanceAdministration) {
  medication -> manufacturedMaterial.classCode = 'MMAT' "MMAT";
  medication -> manufacturedMaterial.determinerCode = 'KIND' "KIND";
  medication ->  manufacturedMaterial.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.33' "CH-PHARM-ManufacturedMaterialContentModule";
  medication ->  manufacturedMaterial.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.1' "templateId";
  medication.code as code then {
    code.text as text ->  manufacturedMaterial.name as name,  name.other = text "brandname";
  } "name";
  medication.code as code then {
    // #pre.no.brandedmedication
    code.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' -> manufacturedMaterial.code as code then {
      extension.valueUrl as valueUrl ->  code.originalText as text,  text.reference as reference,  reference.value = valueUrl "reference";
    } "id";
    code where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  manufacturedMaterial.code as code,  code.originalText as text,  text.reference as reference,  reference.value = '#refpdf' "reference";
    code.coding as coding -> manufacturedMaterial.code as ce then CodingCE(coding, ce) "code";
  };
  medication.form as form then {
    form.coding as coding -> manufacturedMaterial.formCode as formCode then CodingCE(coding, formCode) "formCode";
  };
  medication.code as code ->  manufacturedMaterial.asContent as asContent,  asContent.classCode = 'CONT',  asContent.containerPackagedMedicine as containerPackagedMedicine then {
    code -> containerPackagedMedicine.classCode = 'CONT' "CONT";
    code -> containerPackagedMedicine.determinerCode = 'INSTANCE' "INSTANCE";
    code.coding as coding -> containerPackagedMedicine.code as ce then CodingCE(coding, ce) "pharm-code";
    code.text as text ->  containerPackagedMedicine.name as name,  name.other = text "pharm-name";
    medication.form as form then {
      form.coding as coding -> containerPackagedMedicine.formCode as formCode then CodingCE(coding, formCode) "pharm-formCode";
    };
    // Package size
    medication.amount as amount then {
      amount.numerator as numerator -> containerPackagedMedicine.capacityQuantity as capacityQuantity then {
        numerator.value as value -> capacityQuantity.value = value;
        numerator.code as unit -> capacityQuantity.unit = unit "unit";
      };
      // IHE-DIS: Case 1: If the product-element contains package information, the unit attribute is not be present
      medicationRequest.dispenseRequest as dispenseRequest ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.supply as supply then PrescribedQuantity(dispenseRequest, supply) "PrescribedQuantity";
    };
    // IHE-DIS: Case 2: If the product-element does not contain package information, the unit attribut is present and the value SHALL be out of the UCUM code system
    medication where $this.amount.exists() = false then {
      medicationRequest.dispenseRequest as dispenseRequest ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.supply as supply then PrescribedQuantityUnit(dispenseRequest, supply) "PrescribedQuantityUnit";
    } "noAmount";
  };
  medication.ingredient as ingredient ->  manufacturedMaterial.ingredient as ingredient,  ingredient.classCode = 'ACTI' then {
    ingredient.itemCodeableConcept as itemCodeableConcept -> ingredient.ingredient as pharmsubstance then {
      itemCodeableConcept -> pharmsubstance.classCode = 'MMAT' "MMAT";
      itemCodeableConcept -> pharmsubstance.determinerCode = 'KIND' "KIND";
      itemCodeableConcept.text as text ->  pharmsubstance.name as name,  name.other = text "name";
      itemCodeableConcept.coding as coding then {
        coding -> pharmsubstance.code as ce then CodingCE(coding, ce) "formCode";
      };
    } "PharmSubstance";
    ingredient.strength as strength -> ingredient.quantity as quantity then RatioRTOPQPQ(strength, quantity);
  };
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/branches/master/StructureDefinition-ch-emed-medicationrequest.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.43
group PrescriptionItemEntryContentModule(source bundle : Bundle, source medicationRequest : MedicationRequest, target substanceAdministration : SubstanceAdministration) {
  medicationRequest -> substanceAdministration.classCode = 'SBADM' "SBADM";
  medicationRequest -> substanceAdministration.moodCode = 'INT' "INT";
  medicationRequest ->  substanceAdministration.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.43' "CH-PHARM-PrescriptionItemEntryContentModule";
  medicationRequest ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.2' "PrescriptionItemEntryTemplateId";
  medicationRequest ->  substanceAdministration.templateId as templateId,  templateId.root = '2.16.840.1.113883.10.20.1.24' "CCD";
  medicationRequest ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7' "PCC-MedicationEntryContentModule";
  medicationRequest.identifier as identifier -> substanceAdministration.id as id then IdentifierII(identifier, id) "id";
  medicationRequest.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' ->  substanceAdministration.text as text,  text.reference as reference then {
    extension.valueUrl as valueUrl -> reference.value = valueUrl;
  } "narrativeLink";
  medicationRequest where extension.where(url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists().not() ->  substanceAdministration.text as text,  text.reference as reference then {
    medicationRequest -> reference.value = '#refpdf' "referencetopdf";
  } "cat1narrativelink";
  medicationRequest ->  substanceAdministration.statusCode as statusCode,  statusCode.code = 'completed' "code";
  medicationRequest.dispenseRequest as dispenseRequest -> substanceAdministration.repeatNumber as repeatNumber then {
    dispenseRequest.numberOfRepeatsAllowed as number -> repeatNumber.value = number "repeatNumber";
    dispenseRequest where numberOfRepeatsAllowed.exists() = false -> repeatNumber.nullFlavor = 'NI' "repeatNumberNull";
  };
  medicationRequest.contained as medication where ('#' + $this.id) in %medicationRequest.medicationReference.reference ->  substanceAdministration.consumable as consumable,  consumable.manufacturedProduct as manufacturedProduct,  manufacturedProduct.classCode = 'MANU' then {
    medication ->  manufacturedProduct.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.2' "MP-templateId";
    medication ->  manufacturedProduct.templateId as templateId,  templateId.root = '2.16.840.1.113883.10.20.1.53' "MP-templateId";
    medication -> manufacturedProduct.manufacturedMaterial as manufacturedMaterial then ManufacturedMaterialEntryContentModulePrescription(bundle, medicationRequest, medication, manufacturedMaterial, substanceAdministration) "manufacturedMaterial";
  };
  // TODO: AUTHOR1 Medication Treatment Plan Author
  medicationRequest where dosageInstruction.count() = 1 then {
    medicationRequest.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-nonstructured') ->  substanceAdministration.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.5.3.1.4.7.1' "normalDose";
  } "onylonedosage";
  medicationRequest.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-normal-medicationrequest') then DosageInstructionsStartStopFrequencySubstanceAdministration(dosage, substanceAdministration) "DosageSubstanceAdministration";
  // dosage for split dosing (with sequences): effectiveTime (start/stop) & routeCode
  medicationRequest.dosageInstruction first as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split-medicationrequest') -> substanceAdministration.entryRelationship as entryRelationship then DosageInstructionsEntryStartStopRoute(dosage, substanceAdministration, entryRelationship) "DosageSubstanceAdministration";
  // dosage for split dosing (with sequences): sequence number, effectiveTime (when), dose quantity, consumable
  medicationRequest.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-structured-split-medicationrequest') ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP' then DosageInstructionsEntryDosageChangeSubstanceAdministration(dosage, substanceAdministration, entryRelationship) "DosageSubstanceAdministration";
  medicationRequest.reasonCode as reasonCode ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'RSON',  entryRelationship.observation as observation then TreatmentReasonEntryContentModule(reasonCode, observation) "TreatmentReasonEntryContentModule";
  medicationRequest.extension as extension where url = 'http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-ext-treatmentplan' ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'REFR',  entryRelationship.substanceAdministration as substanceAdministration then MTPReferenceEntryContentModule(extension, substanceAdministration) "MTP-Reference";
  // TODO: Patient Medication Instructions Contains 1.3.6.1.4.1.19376.1.5.3.1.4.3 IHE Patient Medication Instructions (DYNAMIC)
  medicationRequest.dosageInstruction as dosage where $this.conformsTo('http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-dosage-nonstructured') ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.substanceAdministration as substanceAdministration then DosageInstructionsNonStructuredEntryContentModule(dosage, substanceAdministration) "DosageInstructionsNonStructuredEntryContentModule";
  medicationRequest.note as note ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.act as act then AnnotationComment(note, act) "AnnotationComment";
  medicationRequest.substitution as substitution ->  substanceAdministration.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'COMP',  entryRelationship.act as act then SubstitutionRequest(substitution, act) "Substitution";
}

// source: http://build.fhir.org/ig/hl7ch/ch-emed/StructureDefinition-ch-emed-observation.html
// target: https://art-decor.org/art-decor/decor-templates--cdachemed-?section=templates&id=2.16.756.5.30.1.1.10.4.44
group PharmaceuticalAdviceItemEntryContentModule(source bundle : Bundle, source fhirobservation : Observation, target cdaobservation : Observation) {
  fhirobservation -> cdaobservation.classCode = 'OBS' "OBS";
  fhirobservation -> cdaobservation.moodCode = 'EVN' "EVN";
  fhirobservation ->  cdaobservation.templateId as templateId,  templateId.root = '1.3.6.1.4.1.19376.1.9.1.3.3' "IHE-PHARM-PADV";
  fhirobservation ->  cdaobservation.templateId as templateId,  templateId.root = '2.16.756.5.30.1.1.10.4.44' "CH-PHARM-PharmaceuticalAdviceItemEntryContentModule";
  fhirobservation.identifier as identifier -> cdaobservation.id as id then IdentifierII(identifier, id) "id";
  fhirobservation.code as fhirCode -> cdaobservation.code as cdaCode then CodeableConceptCE(fhirCode, cdaCode);
  fhirobservation ->  cdaobservation.statusCode as statusCode,  statusCode.code = 'completed' "code";
  fhirobservation.effectiveDateTime as effectiveDateTime -> cdaobservation.effectiveTime as effectiveTime then {
    effectiveDateTime -> effectiveTime.value = effectiveDateTime "value";
  } "effectiveTime";
  fhirobservation.extension as extension where url = 'http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-ext-treatmentplan' ->  cdaobservation.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'REFR',  entryRelationship.substanceAdministration as substanceAdministration then MTPReferenceEntryContentModule(extension, substanceAdministration) "MTP-Reference";
  fhirobservation.extension as extension where url = 'http://fhir.ch/ig/ch-emed/StructureDefinition/ch-emed-ext-medicationstatement-changed' ->  cdaobservation.entryRelationship as entryRelationship,  entryRelationship.typeCode = 'REFR' then {
    bundle.entry as entry then {
      entry.fullUrl where ($this in %extension.valueReference.reference) and $this.startsWith('urn:uuid') then {
        entry.resource as medicationStatement where $this.ofType(FHIR.MedicationStatement) -> entryRelationship.substanceAdministration as substanceAdministration then MedicationTreatmentPlanItemEntryContentModule(bundle, medicationStatement, substanceAdministration) "MedicationStatement";
      } "fullUrlAsUuid";
      entry.resource as medicationStatement where $this.ofType(FHIR.MedicationStatement) and (('MedicationStatement' + '/' + $this.id) in %extension.valueReference.reference) -> entryRelationship.substanceAdministration as substanceAdministration then MedicationTreatmentPlanItemEntryContentModule(bundle, medicationStatement, substanceAdministration) "MedicationStatement";
    };
  } "changedmtp";
  fhirobservation.note as note then {
    note.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' ->  cdaobservation.text as text,  text.reference as reference then {
      extension.valueUrl as valueUrl -> reference.value = valueUrl;
    } "narrativeLink";
    note where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  cdaobservation.text as text,  text.reference as reference,  reference.value = '#refpdf' "reference";
  };
  fhirobservation.note as note -> cdaobservation.text as text then {
    note.text as noteText -> text.data = noteText;
    note.extension as extension where $this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink' then {
      extension.valueUrl as valueUrl ->  text.reference as reference,  reference.value = valueUrl "reference";
    } "id";
    note where (extension.where($this.url = 'http://hl7.org/fhir/StructureDefinition/narrativeLink').exists() = false) ->  text.reference as reference,  reference.value = '#refpdf' "reference";
  } "text";
}