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 (VersionUtilities.isR4Ver(version) && "http://hl7.org/fhir/StructureDefinition/ExplanationOfBenefit".equals(sd.getUrl())) {
066      for (ElementDefinition ed : sd.getSnapshot().getElement()) {
067        if (ed.hasBinding() && "http://terminology.hl7.org/CodeSystem/processpriority".equals(ed.getBinding().getValueSet())) {
068          ed.getBinding().setValueSet("http://hl7.org/fhir/ValueSet/process-priority");
069        }
070      }
071      for (ElementDefinition ed : sd.getDifferential().getElement()) {
072        if (ed.hasBinding() && "http://terminology.hl7.org/CodeSystem/processpriority".equals(ed.getBinding().getValueSet())) {
073          ed.getBinding().setValueSet("http://hl7.org/fhir/ValueSet/process-priority");
074        }
075      }
076    }
077    if (sd.getUrl().startsWith("http://hl7.org/fhir/uv/subscriptions-backport")) {
078      for (ElementDefinition ed : sd.getDifferential().getElement()) {
079        fixMarkdownR4BURLs(ed);
080      }
081      for (ElementDefinition ed : sd.getSnapshot().getElement()) {
082        fixMarkdownR4BURLs(ed);
083      }
084    }
085    if ("http://hl7.org/fhir/StructureDefinition/vitalsigns".equals(sd.getUrl()) || "http://hl7.org/fhir/StructureDefinition/vitalsigns".equals(sd.getBaseDefinition())) {
086      for (ElementDefinition ed : sd.getDifferential().getElement()) {
087        checkVSConstraint(ed);
088      }
089      for (ElementDefinition ed : sd.getSnapshot().getElement()) {
090        checkVSConstraint(ed);
091      }
092    }
093    return sd;
094  }
095
096  private void checkVSConstraint(ElementDefinition ed) {
097    for (ElementDefinitionConstraintComponent constraint : ed.getConstraint()) {
098      if ("vs-1".equals(constraint.getKey())) {
099        constraint.setExpression("$this is dateTime implies $this.toString().length() >= 10");
100      }
101    }
102  }
103
104  private void fixMarkdownR4BURLs(ElementDefinition ed) {
105    if (ed.hasDefinition()) {
106      ed.setDefinition(ed.getDefinition().replace("http://hl7.org/fhir/R4B/", "http://hl7.org/fhir/R4/"));
107    } 
108    if (ed.hasComment()) {
109      ed.setComment(ed.getComment().replace("http://hl7.org/fhir/R4B/", "http://hl7.org/fhir/R4/"));
110    }
111    if (ed.hasRequirements()) {
112      ed.setRequirements(ed.getRequirements().replace("http://hl7.org/fhir/R4B/", "http://hl7.org/fhir/R4/"));
113    }
114  }
115
116  private void fixDocSecURL(ElementDefinition ed) {
117    for (TypeRefComponent tr : ed.getType()) {
118      for (CanonicalType c : tr.getProfile()) {
119        if ("http://hl7.org/fhir/StructureDefinition/document-section-library".equals(c.getValue())) {
120          c.setValue("http://hl7.org/fhir/StructureDefinition/example-section-library");
121        }
122      }
123    }
124  }
125
126
127}