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