Search in sources :

Example 1 with SupportedCodebook

use of gov.cms.bfd.model.codebook.extractor.SupportedCodebook in project beneficiary-fhir-data by CMSgov.

the class CodebookVariableReader method buildVariablesMultimappedById.

/**
 * @return A multimap of the known Variables. Why a multimap? Because some {@link Variable}s
 *     appear in more than one {@link Codebook}, and we need to cope with that.
 */
private static Map<String, List<Variable>> buildVariablesMultimappedById() {
    /*
     * Build a multimap of the known Variables. Why a multimap? Because some
     * Variables appear in more than one Codebook, and we need to cope with that.
     */
    Map<String, List<Variable>> variablesById = new LinkedHashMap<>();
    for (SupportedCodebook supportedCodebook : SupportedCodebook.values()) {
        /*
       * Find the Codebook XML file on the classpath. Note that this code will be used
       * inside an annotation processor, and those have odd context classloaders. So
       * instead of the context classloader, we use the classloader used to load one
       * of the types from the same JAR.
       */
        ClassLoader contextClassLoader = Codebook.class.getClassLoader();
        URL supportedCodebookUrl = contextClassLoader.getResource(supportedCodebook.getCodebookXmlResourceName());
        if (supportedCodebookUrl == null) {
            throw new IllegalStateException(String.format("Unable to locate classpath resource: '%s'." + " JVM Classpath: '%s'. Classloader: '%s'. Classloader URLs: '%s'.", supportedCodebook.getCodebookXmlResourceName(), System.getProperty("java.class.path"), contextClassLoader, contextClassLoader instanceof URLClassLoader ? Arrays.toString(((URLClassLoader) contextClassLoader).getURLs()) : "N/A"));
        }
        // Unmarshall the Codebook XML and pull its Variables.
        try (InputStream codebookXmlStream = supportedCodebookUrl.openStream()) {
            Codebook codebook = unmarshallCodebookXml(codebookXmlStream);
            for (Variable variable : codebook.getVariables()) {
                if (!variablesById.containsKey(variable.getId()))
                    variablesById.put(variable.getId(), new ArrayList<>());
                variablesById.get(variable.getId()).add(variable);
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }
    return variablesById;
}
Also used : Variable(gov.cms.bfd.model.codebook.model.Variable) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) SupportedCodebook(gov.cms.bfd.model.codebook.extractor.SupportedCodebook) URL(java.net.URL) LinkedHashMap(java.util.LinkedHashMap) Codebook(gov.cms.bfd.model.codebook.model.Codebook) SupportedCodebook(gov.cms.bfd.model.codebook.extractor.SupportedCodebook) URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

SupportedCodebook (gov.cms.bfd.model.codebook.extractor.SupportedCodebook)1 Codebook (gov.cms.bfd.model.codebook.model.Codebook)1 Variable (gov.cms.bfd.model.codebook.model.Variable)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1