All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hl7.fhir.r5.utils.ResourceFixer Maven / Gradle / Ivy

package org.hl7.fhir.r5.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.formats.IParser.OutputStyle;
import org.hl7.fhir.r5.formats.JsonParser;
import org.hl7.fhir.r5.formats.XmlParser;
import org.hl7.fhir.r5.model.CodeSystem;
import org.hl7.fhir.r5.model.Enumerations.CodeSystemContentMode;
import org.hl7.fhir.r5.model.CodeSystem.CodeSystemHierarchyMeaning;
import org.hl7.fhir.r5.model.CodeSystem.ConceptDefinitionComponent;
import org.hl7.fhir.r5.model.Resource;
import org.hl7.fhir.utilities.MarkedToMoveToAdjunctPackage;
import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;

@MarkedToMoveToAdjunctPackage
public class ResourceFixer {


  public static void main(String[] args) throws IOException {
    new ResourceFixer().vistAllResources(args[0]);

  }

  private Set refs = new HashSet<>();
  
  private void vistAllResources(String folder) throws IOException {
    
    for (File f : ManagedFileAccess.file(folder).listFiles()) {
      if (f.isDirectory()) {
        vistAllResources(f.getAbsolutePath());
      } else if (f.getName().endsWith(".json")) {
        Resource r = null;
        try {
          r = new JsonParser().parse(ManagedFileAccess.inStream(f));
        } catch (Throwable e) {
          // nothing at all
        }
        if (r != null) {
          try {
            if (visitResource(r)) {
              new JsonParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
            }
          } catch (Exception e) {
            System.out.println("Error processing "+f.getAbsolutePath()+": "+e.getMessage());
//            e.printStackTrace();
          }
        }
      } else if (f.getName().endsWith(".xml")) {
        Resource r = null;
        try {
          r = new XmlParser().parse(ManagedFileAccess.inStream(f));
        } catch (Throwable e) {
          // nothing at all
        }
        if (r != null) {
          try {
            if (visitResource(r)) {
              new XmlParser().setOutputStyle(OutputStyle.PRETTY).compose(ManagedFileAccess.outStream(f), r);
            }
          } catch (Exception e) {
            System.out.println("Error processing "+f.getAbsolutePath()+": "+e.getMessage());
//            e.printStackTrace();
          }
        }
      }
    } 
  }

  private boolean visitResource(Resource r) {
    if (r.hasId()) {
      String ref = r.fhirType()+"/"+r.getId();
      if (refs.contains(ref)) {
        throw new FHIRException("Duplicate resource "+ref);
      }
      refs.add(ref);
    }
    if (r instanceof CodeSystem) {
      return visitCodeSystem((CodeSystem) r);
    }
    return false;
  }

  private boolean visitCodeSystem(CodeSystem cs) {
    if (!cs.hasContent()) {
      System.out.println("Setting content = complete for CodeSystem/"+cs.getId());      
      cs.setContent(CodeSystemContentMode.COMPLETE);
      return true;
    } else if (!cs.hasHierarchyMeaning() && hasHierarchy(cs)) {      
      System.out.println("Setting hierarchyMeaning = is-a for CodeSystem/"+cs.getId());      
      cs.setHierarchyMeaning(CodeSystemHierarchyMeaning.ISA);
      return true;
    } else {      
      return false;
    }
  }

  private boolean hasHierarchy(CodeSystem cs) {
    for (ConceptDefinitionComponent c : cs.getConcept()) {
      if (c.hasConcept()) {
        return true;
      }
    }
    return false;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy