001package org.hl7.fhir.convertors.misc;
002
003import java.io.IOException;
004import java.util.HashMap;
005import java.util.Map;
006
007import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
008import org.hl7.fhir.r5.model.CanonicalResource;
009import org.hl7.fhir.r5.model.Enumerations.PublicationStatus;
010import org.hl7.fhir.r5.model.Resource;
011import org.hl7.fhir.r5.utils.ToolingExtensions;
012import org.hl7.fhir.r5.utils.formats.ResourceFolderVisitor;
013import org.hl7.fhir.r5.utils.formats.ResourceFolderVisitor.IResourceObserver;
014import org.hl7.fhir.utilities.StandardsStatus;
015import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
016import org.hl7.fhir.utilities.npm.NpmPackage;
017
018public class ExtensionStatusUpgrader implements IResourceObserver {
019
020  public static void main(String[] args) throws IOException {
021    new ExtensionStatusUpgrader().execute(args[0]);
022
023  }
024  Map<String, CanonicalResource> r4b = new HashMap<>();
025  Map<String, CanonicalResource> r5 = new HashMap<>();
026
027  private void execute(String folder) throws IOException {
028    FilesystemPackageCacheManager pcm = new FilesystemPackageCacheManager.Builder().build();
029    
030    // load R4
031    NpmPackage npm = pcm.loadPackage("hl7.fhir.r4.core");
032    for (String n : npm.listResources("StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter")) {
033      CanonicalResource cr = (CanonicalResource) VersionConvertorFactory_40_50.convertResource(new org.hl7.fhir.r4.formats.JsonParser().parse(npm.load(n)));
034      r4b.put(cr.getUrl(), cr);
035    }
036    // load R5-ballot3 
037    npm = pcm.loadPackage("hl7.fhir.r5.core#5.0.0-ballot");
038    for (String n : npm.listResources("StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter")) {
039      CanonicalResource cr = (CanonicalResource) new org.hl7.fhir.r5.formats.JsonParser().parse(npm.load(n));
040      r5.put(cr.getUrl(), cr);
041    }
042    
043    new ResourceFolderVisitor(this, "StructureDefinition", "ValueSet", "CodeSystem", "SearchParameter").visit(folder);
044  }
045
046  @Override
047  public boolean visitResource(String filename, Resource resource) {
048    boolean changed = false;
049    CanonicalResource cr = (CanonicalResource) resource;
050    if (r4b.containsKey(cr.getUrl())) {
051      CanonicalResource ocr = r4b.get(cr.getUrl());
052      if (cr.getStatus() != PublicationStatus.ACTIVE) {
053        cr.setStatus(PublicationStatus.ACTIVE);
054        changed = true;
055      }
056      if (ocr.getStandardsStatus() == StandardsStatus.NORMATIVE) {
057        if (cr.getStandardsStatus() != StandardsStatus.NORMATIVE) {
058          cr.setStandardsStatus(StandardsStatus.NORMATIVE);
059          changed = true;
060        }
061      } else {
062        if (cr.getStandardsStatus() != StandardsStatus.TRIAL_USE) {
063          cr.setStandardsStatus(StandardsStatus.TRIAL_USE);
064          changed = true;
065        }
066      }
067      if (cr.getExperimental()) {
068        cr.setExperimental(false);
069        changed = true;        
070      }
071      if (!"3".equals(ToolingExtensions.readStringExtension(cr, ToolingExtensions.EXT_FMM_LEVEL))) {
072        ToolingExtensions.setIntegerExtension(cr, ToolingExtensions.EXT_FMM_LEVEL, 3);
073        changed = true;        
074      }
075    } else if (r5.containsKey(cr.getUrl())) {
076      CanonicalResource ocr = r5.get(cr.getUrl());
077      if (cr.getStatus() != PublicationStatus.DRAFT) {
078        cr.setStatus(PublicationStatus.DRAFT);
079        changed = true;
080      }
081      if (ocr.getStandardsStatus() == StandardsStatus.NORMATIVE) {
082        if (cr.getStandardsStatus() != StandardsStatus.NORMATIVE) {
083          cr.setStandardsStatus(StandardsStatus.NORMATIVE);
084          changed = true;
085        }
086      } else {
087        if (cr.getStandardsStatus() != StandardsStatus.TRIAL_USE) {
088          cr.setStandardsStatus(StandardsStatus.TRIAL_USE);
089          changed = true;
090        }
091      }
092      if (cr.getExperimental()) {
093        cr.setExperimental(false);
094        changed = true;        
095      }
096      if (!"2".equals(ToolingExtensions.readStringExtension(cr, ToolingExtensions.EXT_FMM_LEVEL))) {
097        ToolingExtensions.setIntegerExtension(cr, ToolingExtensions.EXT_FMM_LEVEL, 2);
098        changed = true;        
099      }
100    } else {
101      if (cr.getStatus() != PublicationStatus.DRAFT) {
102        cr.setStatus(PublicationStatus.DRAFT);
103        changed = true;
104      }
105      if (cr.getStandardsStatus() != StandardsStatus.DRAFT) {
106        cr.setStandardsStatus(StandardsStatus.DRAFT);
107        changed = true;
108      }
109      if (!cr.getExperimental()) {
110        cr.setExperimental(true);
111        changed = true;        
112      }
113    }
114    return changed;
115  }
116
117}