001package org.hl7.fhir.r5.utils;
002
003import java.io.File;
004import java.io.FileInputStream;
005import java.io.FileOutputStream;
006import java.io.IOException;
007import java.util.HashSet;
008import java.util.Set;
009
010import org.hl7.fhir.exceptions.FHIRException;
011import org.hl7.fhir.r5.formats.IParser.OutputStyle;
012import org.hl7.fhir.r5.formats.JsonParser;
013import org.hl7.fhir.r5.formats.XmlParser;
014import org.hl7.fhir.r5.model.CodeSystem;
015import org.hl7.fhir.r5.model.Enumerations.CodeSystemContentMode;
016import org.hl7.fhir.r5.model.CodeSystem.CodeSystemHierarchyMeaning;
017import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent;
018import org.hl7.fhir.r5.model.Resource;
019import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
020
021public class ResourceFixer {
022
023
024  public static void main(String[] args) throws IOException {
025    new ResourceFixer().vistAllResources(args[0]);
026
027  }
028
029  private Set<String> refs = new HashSet<>();
030  
031  private void vistAllResources(String folder) throws IOException {
032    
033    for (File f : ManagedFileAccess.file(folder).listFiles()) {
034      if (f.isDirectory()) {
035        vistAllResources(f.getAbsolutePath());
036      } else if (f.getName().endsWith(".json")) {
037        Resource r = null;
038        try {
039          r = new JsonParser().parse(ManagedFileAccess.inStream(f));
040        } catch (Throwable e) {
041          // nothing at all
042        }
043        if (r != null) {
044          try {
045            if (visitResource(r)) {
046              new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
047            }
048          } catch (Exception e) {
049            System.out.println("Error processing "+f.getAbsolutePath()+": "+e.getMessage());
050//            e.printStackTrace();
051          }
052        }
053      } else if (f.getName().endsWith(".xml")) {
054        Resource r = null;
055        try {
056          r = new XmlParser().parse(ManagedFileAccess.inStream(f));
057        } catch (Throwable e) {
058          // nothing at all
059        }
060        if (r != null) {
061          try {
062            if (visitResource(r)) {
063              new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
064            }
065          } catch (Exception e) {
066            System.out.println("Error processing "+f.getAbsolutePath()+": "+e.getMessage());
067//            e.printStackTrace();
068          }
069        }
070      }
071    } 
072  }
073
074  private boolean visitResource(Resource r) {
075    if (r.hasId()) {
076      String ref = r.fhirType()+"/"+r.getId();
077      if (refs.contains(ref)) {
078        throw new FHIRException("Duplicate resource "+ref);
079      }
080      refs.add(ref);
081    }
082    if (r instanceof CodeSystem) {
083      return visitCodeSystem((CodeSystem) r);
084    }
085    return false;
086  }
087
088  private boolean visitCodeSystem(CodeSystem cs) {
089    if (!cs.hasContent()) {
090      System.out.println("Setting content = complete for CodeSystem/"+cs.getId());      
091      cs.setContent(CodeSystemContentMode.COMPLETE);
092      return true;
093    } else if (!cs.hasHierarchyMeaning() && hasHierarchy(cs)) {      
094      System.out.println("Setting hierarchyMeaning = is-a for CodeSystem/"+cs.getId());      
095      cs.setHierarchyMeaning(CodeSystemHierarchyMeaning.ISA);
096      return true;
097    } else {      
098      return false;
099    }
100  }
101
102  private boolean hasHierarchy(CodeSystem cs) {
103    for (ConceptDefinitionComponent c : cs.getConcept()) {
104      if (c.hasConcept()) {
105        return true;
106      }
107    }
108    return false;
109  }
110
111}