001package org.hl7.fhir.convertors.misc.utg;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.net.URISyntaxException;
008import java.text.ParseException;
009import java.util.ArrayList;
010import java.util.Date;
011import java.util.HashMap;
012import java.util.List;
013import java.util.Map;
014
015import org.hl7.fhir.convertors.factory.VersionConvertorFactory_10_50;
016import org.hl7.fhir.convertors.factory.VersionConvertorFactory_30_50;
017import org.hl7.fhir.convertors.factory.VersionConvertorFactory_40_50;
018import org.hl7.fhir.exceptions.FHIRException;
019import org.hl7.fhir.r5.formats.IParser.OutputStyle;
020import org.hl7.fhir.r5.formats.JsonParser;
021import org.hl7.fhir.r5.formats.XmlParser;
022import org.hl7.fhir.r5.model.Base;
023import org.hl7.fhir.r5.model.Bundle;
024import org.hl7.fhir.r5.model.CanonicalResource;
025import org.hl7.fhir.r5.model.CodeSystem;
026import org.hl7.fhir.r5.model.Provenance;
027import org.hl7.fhir.r5.model.Provenance.ProvenanceAgentComponent;
028import org.hl7.fhir.r5.model.Resource;
029import org.hl7.fhir.r5.model.ValueSet;
030import org.hl7.fhir.r5.utils.ToolingExtensions;
031import org.hl7.fhir.utilities.Utilities;
032import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
033import org.hl7.fhir.utilities.npm.FilesystemPackageCacheManager;
034import org.hl7.fhir.utilities.npm.NpmPackage;
035import org.hl7.fhir.utilities.npm.NpmPackage.PackageResourceInformation;
036
037import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
038
039public class UTGVersionSorter {
040
041  private final Date runTime = new Date();
042  private FilesystemPackageCacheManager pcm;
043
044  public static void main(String[] args) throws FHIRException, IOException, ParseException, URISyntaxException {
045    new UTGVersionSorter().execute("C:\\work\\org.hl7.fhir.igs\\UTG\\input\\sourceOfTruth");
046  }
047
048  private void execute(String source) throws IOException {
049    List<CanonicalResourceAnalysis> list = new ArrayList<>();
050    System.out.println("Loading UTG");
051    loadFromSource(list, ManagedFileAccess.file(source));
052
053    Map<String, CanonicalResource> r2 = loadPackageR2("hl7.fhir.r2.core");
054    Map<String, CanonicalResource> r3 = loadPackageR3("hl7.fhir.r3.core");
055    Map<String, CanonicalResource> r4 = loadPackageR4("hl7.fhir.r4.core");
056
057    System.out.println("Processing");
058    for (CanonicalResourceAnalysis cr : list) {
059      cr.analyse(r2, r3, r4);
060    }
061
062    Bundle b = (Bundle) new JsonParser().parse(ManagedFileAccess.inStream("C:\\work\\org.hl7.fhir.igs\\UTG\\input\\sourceOfTruth\\history\\utgrel1hx-1-0-6.json"));
063
064    System.out.println("Summary");
065    for (CanonicalResourceAnalysis cr : list) {
066      System.out.println(cr.summary());
067      Provenance p = new Provenance();
068      b.addEntry().setResource(p).setFullUrl("http://terminology.hl7.org/fhir/Provenance/fhir-1.0.51-" + cr.getId());
069      p.setId("hx-fhir-1.0.51-" + cr.getId());
070      p.addTarget().setReference(cr.fhirType() + "/" + cr.getId());
071      p.getOccurredPeriod().setEnd(runTime, TemporalPrecisionEnum.DAY);
072      p.setRecorded(runTime);
073      p.addAuthorization().getConcept().setText("Reset Version after migration to UTG").addCoding("http://terminology.hl7.org/CodeSystem/v3-ActReason", "METAMGT", null);
074      p.getActivity().addCoding("http://terminology.hl7.org/CodeSystem/v3-DataOperation", "UPDATE", null);
075      ProvenanceAgentComponent pa = p.addAgent();
076      pa.getType().addCoding("http://terminology.hl7.org/CodeSystem/provenance-participant-type", "author", null);
077      pa.getWho().setDisplay("Grahame Grieve");
078      pa = p.addAgent();
079      pa.getType().addCoding("http://terminology.hl7.org/CodeSystem/provenance-participant-type", "custodian", null);
080      pa.getWho().setDisplay("Vocabulary WG");
081      CanonicalResource res = cr.resource;
082      res.setVersion(cr.recommendation);
083      new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(cr.filename), res);
084    }
085    System.out.println();
086    new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream("C:\\work\\org.hl7.fhir.igs\\UTG\\input\\sourceOfTruth\\history\\utgrel1hx-1-0-6.json"), b);
087    System.out.println("Done");
088  }
089
090  private Map<String, CanonicalResource> loadPackageR2(String id) throws IOException {
091    Map<String, CanonicalResource> res = new HashMap<>();
092    if (pcm == null) {
093      pcm = new FilesystemPackageCacheManager.Builder().build();
094    }
095    System.out.println("Load " + id);
096    NpmPackage npm = pcm.loadPackage(id);
097    for (PackageResourceInformation p : npm.listIndexedResources("CodeSystem", "ValueSet")) {
098      CanonicalResource r = (CanonicalResource) VersionConvertorFactory_10_50.convertResource(new org.hl7.fhir.dstu2.formats.JsonParser().parse(npm.load(p)));
099      res.put(r.getUrl(), r);
100    }
101    return res;
102  }
103
104  private Map<String, CanonicalResource> loadPackageR3(String id) throws IOException {
105    Map<String, CanonicalResource> res = new HashMap<>();
106    if (pcm == null) {
107      pcm = new FilesystemPackageCacheManager.Builder().build();
108    }
109    System.out.println("Load " + id);
110    NpmPackage npm = pcm.loadPackage(id);
111    for (PackageResourceInformation p : npm.listIndexedResources("CodeSystem", "ValueSet")) {
112      CanonicalResource r = (CanonicalResource) VersionConvertorFactory_30_50.convertResource(new org.hl7.fhir.dstu3.formats.JsonParser().parse(npm.load(p)));
113      res.put(r.getUrl(), r);
114    }
115    return res;
116  }
117
118  private Map<String, CanonicalResource> loadPackageR4(String id) throws IOException {
119    Map<String, CanonicalResource> res = new HashMap<>();
120    if (pcm == null) {
121      pcm = new FilesystemPackageCacheManager.Builder().build();
122    }
123    System.out.println("Load " + id);
124    NpmPackage npm = pcm.loadPackage(id);
125    for (PackageResourceInformation p : npm.listIndexedResources("CodeSystem", "ValueSet")) {
126      CanonicalResource r = (CanonicalResource) VersionConvertorFactory_40_50.convertResource(new org.hl7.fhir.r4.formats.JsonParser().parse(npm.load(p)));
127      res.put(r.getUrl(), r);
128    }
129    return res;
130  }
131
132  private void loadFromSource(List<CanonicalResourceAnalysis> list, File source) {
133    for (File f : source.listFiles()) {
134      if (f.isDirectory()) {
135        loadFromSource(list, f);
136      } else if (f.getName().endsWith(".xml")) {
137        try {
138          Resource r = new XmlParser().parse(ManagedFileAccess.inStream(f));
139          if (r instanceof CanonicalResource) {
140            CanonicalResource cr = (CanonicalResource) r;
141            cr.setWebPath(f.getAbsolutePath());
142            if (cr.hasVersion() && cr.getVersion().startsWith("4.") && !Utilities.existsInList(cr.getId(), "v3-DataOperation", "v3-KnowledgeSubjectObservationValue")) {
143              list.add(new CanonicalResourceAnalysis(cr, f.getAbsolutePath()));
144            }
145          }
146        } catch (Exception e) {
147          System.out.println(f.getAbsolutePath() + " not a resource? " + e.getMessage());
148        }
149      }
150    }
151
152  }
153
154  public class CanonicalResourceAnalysis {
155
156    private final CanonicalResource resource;
157    private final String filename;
158    private CanonicalResource r2;
159    private CanonicalResource r3;
160    private CanonicalResource r4;
161    private String fmm;
162    private boolean normative;
163    private String recommendation;
164
165    public CanonicalResourceAnalysis(CanonicalResource cr, String filename) {
166      this.resource = cr;
167      this.filename = filename;
168    }
169
170    public String summary() {
171//      return "Relevant: "+resource.getUrl()+" [r2: "+r2Ver+"/"+r2Fmm+"]"+" [r3: "+r3Ver+"/"+r3Fmm+"]"+" [r4: "+r4Ver+"/"+r4Fmm+"/"+r4Normative+"] ---> "+recommendation;
172      return resource.getUrl() + " ---> " + recommendation + "   in " + filename;
173    }
174
175    public void analyse(Map<String, CanonicalResource> r2l, Map<String, CanonicalResource> r3l, Map<String, CanonicalResource> r4l) {
176      r2 = findMatch(r2l);
177      r3 = findMatch(r3l);
178      r4 = findMatch(r4l);
179
180      fmm = r4 != null ? ToolingExtensions.readStringExtension(r4, ToolingExtensions.EXT_FMM_LEVEL) : null;
181      normative = (r4 != null) && ToolingExtensions.readStringExtension(r4, ToolingExtensions.EXT_NORMATIVE_VERSION) != null;
182      if (normative) {
183        recommendation = "1.0.0";
184      } else if (Utilities.existsInList(fmm, "3", "4", "5")) {
185        recommendation = "0.5.0";
186      } else {
187        int i = 1;
188        if (r2 != null && r3 != null && !match(r2, r3, r2l, r3l)) {
189          i++;
190        }
191        if (r3 != null && r4 != null && !match(r3, r4, r3l, r4l)) {
192          i++;
193        }
194        recommendation = "0." + i + ".0";
195      }
196    }
197
198    private boolean match(CanonicalResource l, CanonicalResource r, Map<String, CanonicalResource> ll, Map<String, CanonicalResource> rl) {
199      if (l instanceof CodeSystem && r instanceof CodeSystem) {
200        return matchCS((CodeSystem) l, (CodeSystem) r);
201      } else if (l instanceof ValueSet && r instanceof ValueSet) {
202        return matchVS((ValueSet) l, (ValueSet) r, ll, rl);
203      } else {
204        return false;
205      }
206    }
207
208    private boolean matchVS(ValueSet l, ValueSet r, Map<String, CanonicalResource> ll, Map<String, CanonicalResource> rl) {
209      if (l.getCompose().getInclude().size() == 1 && l.getCompose().getExclude().isEmpty() && l.getCompose().getIncludeFirstRep().hasSystem() && !l.getCompose().getIncludeFirstRep().hasConcept() && !l.getCompose().getIncludeFirstRep().hasFilter() &&
210        r.getCompose().getInclude().size() == 1 && r.getCompose().getExclude().isEmpty() && r.getCompose().getIncludeFirstRep().hasSystem() && !r.getCompose().getIncludeFirstRep().hasConcept() && !r.getCompose().getIncludeFirstRep().hasFilter()) {
211        CodeSystem lc = (CodeSystem) ll.get(l.getCompose().getIncludeFirstRep().getSystem());
212        CodeSystem rc = (CodeSystem) rl.get(l.getCompose().getIncludeFirstRep().getSystem());
213        if (lc != null && rc != null) {
214          return matchCS(lc, rc);
215        }
216      }
217      return false;
218    }
219
220    private boolean matchCS(CodeSystem l, CodeSystem r) {
221      return Base.compareDeep(l.getConcept(), r.getConcept(), false);
222    }
223
224    public CanonicalResource findMatch(Map<String, CanonicalResource> r2) {
225      CanonicalResource r = r2.get(resource.getUrl());
226      if (r == null) {
227        r = r2.get(resource.getUrl().replaceAll("http://terminology.hl7.org/", "http://hl7.org/fhir/"));
228      }
229      if (r == null) {
230        r = r2.get(resource.getUrl().replaceAll("http://terminology.hl7.org/CodeSystem", "http://hl7.org/fhir/"));
231      }
232      return r;
233    }
234
235    public String getId() {
236      return resource.getId();
237    }
238
239    public String fhirType() {
240      return resource.fhirType();
241    }
242
243  }
244
245
246}