001package org.hl7.fhir.r5.utils;
002
003import org.hl7.fhir.r5.context.IWorkerContext;
004import org.hl7.fhir.r5.model.CanonicalResource;
005import org.hl7.fhir.r5.model.ElementDefinition;
006import org.hl7.fhir.r5.model.Enumerations.BindingStrength;
007import org.hl7.fhir.r5.model.Extension;
008import org.hl7.fhir.r5.model.MarkdownType;
009import org.hl7.fhir.r5.model.StructureDefinition.TypeDerivationRule;
010import org.hl7.fhir.r5.model.StructureDefinition;
011
012public class R5Hacker {
013
014  public static void fixR5BrokenResources(IWorkerContext context) {
015    for (StructureDefinition sd : context.fetchResourcesByType(StructureDefinition.class)) {
016      fixSD(sd);
017    }
018  }
019
020  private static void fixSD(StructureDefinition sd) {
021    for (ElementDefinition ed : sd.getDifferential().getElement()) {
022      fix(ed);
023    }
024    for (ElementDefinition ed : sd.getSnapshot().getElement()) {
025      fix(ed);
026    }
027  }
028
029  private static void fix(ElementDefinition ed) {
030    if (ed.hasDefinition()) {
031      ed.setDefinition(ed.getDefinition().replace("http://hl7.org/fhir/5.0.0-snapshot3/", "http://hl7.org/fhir/R5/"));
032    }
033    if (ed.hasBinding() && ed.getBinding().hasExtension(ToolingExtensions.EXT_BINDING_DEFINITION)) {
034      Extension ext = ed.getBinding().getExtensionByUrl(ToolingExtensions.EXT_BINDING_DEFINITION);
035      ext.setValue(new MarkdownType(ext.getValue().primitiveValue()));
036    }
037  }
038
039
040  public static CanonicalResource fixR5BrokenResource(CanonicalResource cr) {
041    if (cr instanceof StructureDefinition) {
042      StructureDefinition sd = (StructureDefinition) cr;
043      fixSD(sd);
044    }
045    return cr;
046  }
047  
048}