001package org.hl7.fhir.r5.conformance; 002 003import org.hl7.fhir.r5.model.CanonicalType; 004import org.hl7.fhir.r5.model.ElementDefinition; 005import org.hl7.fhir.r5.model.ElementDefinition.ElementDefinitionConstraintComponent; 006import org.hl7.fhir.r5.model.ElementDefinition.TypeRefComponent; 007import org.hl7.fhir.r5.model.Resource; 008import org.hl7.fhir.r5.model.StructureDefinition; 009import org.hl7.fhir.utilities.VersionUtilities; 010 011/** 012 * This works around known issues in struture definitions 013 * 014 * @author graha 015 * 016 */ 017public class StructureDefinitionHacker { 018 019 private String version; 020 021 public StructureDefinitionHacker(String version) { 022 super(); 023 this.version = version; 024 } 025 026 public Resource fixSD(StructureDefinition sd) { 027 if (VersionUtilities.isR4Ver(version) && "http://hl7.org/fhir/StructureDefinition/example-composition".equals(sd.getUrl())) { 028 for (ElementDefinition ed : sd.getSnapshot().getElement()) { 029 fixDocSecURL(ed); 030 } 031 for (ElementDefinition ed : sd.getDifferential().getElement()) { 032 fixDocSecURL(ed); 033 if ("ClinicalImpression.problem".equals(ed.getPath())) { 034 // work around a bidi problem 035 ed.setComment("e.g. The patient is a pregnant, has congestive heart failure, has an Adenocarcinoma, and is allergic to penicillin."); 036 } 037 } 038 } 039 if (VersionUtilities.isR4Ver(version) && "http://hl7.org/fhir/StructureDefinition/ClinicalImpression".equals(sd.getUrl())) { 040 for (ElementDefinition ed : sd.getSnapshot().getElement()) { 041 if ("ClinicalImpression.problem".equals(ed.getPath())) { 042 // work around a bidi problem 043 ed.setComment("e.g. The patient is a pregnant, has congestive heart failure, has an Adenocarcinoma, and is allergic to penicillin."); 044 } 045 } 046 for (ElementDefinition ed : sd.getDifferential().getElement()) { 047 if ("ClinicalImpression.problem".equals(ed.getPath())) { 048 // work around a bidi problem 049 ed.setComment("e.g. The patient is a pregnant, has congestive heart failure, has an Adenocarcinoma, and is allergic to penicillin."); 050 } 051 } 052 } 053 if (VersionUtilities.isR4Ver(version) && "http://hl7.org/fhir/StructureDefinition/Consent".equals(sd.getUrl())) { 054 for (ElementDefinition ed : sd.getSnapshot().getElement()) { 055 if ("Consent.identifier".equals(ed.getPath())) { 056 ed.getExampleFirstRep().getValueIdentifier().setSystem("http://acme.org/identifier/local/eCMS"); 057 } 058 } 059 for (ElementDefinition ed : sd.getDifferential().getElement()) { 060 if ("Consent.identifier".equals(ed.getPath())) { 061 ed.getExampleFirstRep().getValueIdentifier().setSystem("http://acme.org/identifier/local/eCMS"); 062 } 063 } 064 } 065 if (sd.getUrl().startsWith("http://hl7.org/fhir/uv/subscriptions-backport")) { 066 for (ElementDefinition ed : sd.getDifferential().getElement()) { 067 fixMarkdownR4BURLs(ed); 068 } 069 for (ElementDefinition ed : sd.getSnapshot().getElement()) { 070 fixMarkdownR4BURLs(ed); 071 } 072 } 073 if ("http://hl7.org/fhir/StructureDefinition/vitalsigns".equals(sd.getUrl()) || "http://hl7.org/fhir/StructureDefinition/vitalsigns".equals(sd.getBaseDefinition())) { 074 for (ElementDefinition ed : sd.getDifferential().getElement()) { 075 checkVSConstraint(ed); 076 } 077 for (ElementDefinition ed : sd.getSnapshot().getElement()) { 078 checkVSConstraint(ed); 079 } 080 } 081 return sd; 082 } 083 084 private void checkVSConstraint(ElementDefinition ed) { 085 for (ElementDefinitionConstraintComponent constraint : ed.getConstraint()) { 086 if ("vs-1".equals(constraint.getKey())) { 087 constraint.setExpression("$this is dateTime implies $this.toString().length() >= 10"); 088 } 089 } 090 } 091 092 private void fixMarkdownR4BURLs(ElementDefinition ed) { 093 if (ed.hasDefinition()) { 094 ed.setDefinition(ed.getDefinition().replace("http://hl7.org/fhir/R4B/", "http://hl7.org/fhir/R4/")); 095 } 096 if (ed.hasComment()) { 097 ed.setComment(ed.getComment().replace("http://hl7.org/fhir/R4B/", "http://hl7.org/fhir/R4/")); 098 } 099 if (ed.hasRequirements()) { 100 ed.setRequirements(ed.getRequirements().replace("http://hl7.org/fhir/R4B/", "http://hl7.org/fhir/R4/")); 101 } 102 } 103 104 private void fixDocSecURL(ElementDefinition ed) { 105 for (TypeRefComponent tr : ed.getType()) { 106 for (CanonicalType c : tr.getProfile()) { 107 if ("http://hl7.org/fhir/StructureDefinition/document-section-library".equals(c.getValue())) { 108 c.setValue("http://hl7.org/fhir/StructureDefinition/example-section-library"); 109 } 110 } 111 } 112 } 113 114 115}