private PrePopulatedValidationSupport loadIgs()

in javaHapiValidatorLambda/src/main/java/software/amazon/fwoa/Validator.java [138:203]


    private PrePopulatedValidationSupport loadIgs(final FhirContext ctx) {

        final Map<String, IBaseResource> myCodeSystems = new HashMap<>();
        final Map<String, IBaseResource> myStructureDefinitions = new HashMap<>();
        final Map<String, IBaseResource> myValueSets = new HashMap<>();

        final ImmutableSet<String> allowedResourceTypes = ImmutableSet.of("StructureDefinition", "CodeSystem", "ValueSet");

        IParser parser = ctx.newJsonParser();
        parser.setParserErrorHandler(new StrictErrorHandler());

        try (ScanResult allFiles = new ClassGraph().acceptPaths(implementationGuidesFolder).rejectPaths(implementationGuidesFolder + "/*/*").scan()) {
            ResourceList jsonResources = allFiles.getResourcesWithExtension("json");

            ResourceList indexFiles = jsonResources.filter(x -> x.getPath().endsWith(".index.json"));

            for (Resource indexFile : indexFiles) {
                IgIndex igIndex = GSON.fromJson(indexFile.getContentAsString(), IgIndex.class);
                for (IgFile file : igIndex.files) {
                    if (allowedResourceTypes.contains(file.resourceType)) {

                        String igResourcePath = indexFile.getPath().replace(".index.json", file.filename);
                        log.info("loading {}", igResourcePath);
                        ResourceList resourcesWithPath = allFiles.getResourcesWithPath(igResourcePath);
                        if (resourcesWithPath.isEmpty()) {
                            throw new RuntimeException("The following file is declared in .index.json but does not exist: " + igResourcePath);
                        }
                        Resource resource = resourcesWithPath.get(0);
                        try (InputStream inputStream = resource.open()) {
                            switch (file.resourceType) {
                                case "StructureDefinition":
                                    Class<? extends IBaseResource> structureDefinitionClass = fhirVersion.equals(FHIR_R4)
                                        ? StructureDefinition.class
                                        : org.hl7.fhir.dstu3.model.StructureDefinition.class;
                                    addStructureDefinition(parser.parseResource(structureDefinitionClass, inputStream), myStructureDefinitions);
                                    break;
                                case "CodeSystem":
                                    Class<? extends IBaseResource> codeSystemClass = fhirVersion.equals(FHIR_R4)
                                        ? CodeSystem.class
                                        : org.hl7.fhir.dstu3.model.CodeSystem.class;
                                    addCodeSystem(parser.parseResource(codeSystemClass, inputStream), myCodeSystems);
                                    break;
                                case "ValueSet":
                                    Class<? extends IBaseResource> valueSetClass = fhirVersion.equals(FHIR_R4)
                                        ? ValueSet.class
                                        : org.hl7.fhir.dstu3.model.ValueSet.class;
                                    addValueSet(parser.parseResource(valueSetClass, inputStream), myValueSets);
                                    break;
                                default:
                                    // cannot happen since we checked for allowedResourceTypes
                                    break;
                            }
                        } catch (Exception e) {
                            log.error("Failed to load Implementation guides", e);
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("Failed to load Implementation guides", e);
            throw new RuntimeException(e);
        }

        return new PrePopulatedValidationSupport(ctx, myStructureDefinitions, myValueSets, myCodeSystems);
    }