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