Search in sources :

Example 1 with CodebookVariableEnumGeneration

use of gov.cms.bfd.model.codegen.annotations.CodebookVariableEnumGeneration in project beneficiary-fhir-data by CMSgov.

the class CodebookVariablesEnumProcessor method process.

/**
 * @param annotatedPackage the {@link PackageElement} to process that has been annotated with
 *     {@link CodebookVariableEnumGeneration}
 * @throws IOException An {@link IOException} may be thrown if errors are encountered trying to
 *     generate source files.
 */
private void process(PackageElement annotatedPackage) throws IOException {
    CodebookVariableEnumGeneration annotation = annotatedPackage.getAnnotation(CodebookVariableEnumGeneration.class);
    logNote(annotatedPackage, "Processing package annotated with: '%s'.", annotation);
    Map<String, Variable> variablesById = CodebookVariableReader.buildVariablesMappedById();
    ClassName variableEnumName = ClassName.get(annotatedPackage.getQualifiedName().toString(), annotation.enumName());
    TypeSpec.Builder variablesEnumType = TypeSpec.enumBuilder(variableEnumName).addModifiers(Modifier.PUBLIC).addSuperinterface(CcwCodebookInterface.class);
    variablesEnumType.addJavadoc("Enumerates the known CCW {@link $T} {@link $T}s, as extracted from the codebook" + " PDFs at <a href=\"https://www.ccwdata.org/web/guest/data-dictionaries\">CCW Data" + " Dictionaries</a>.\n", Codebook.class, Variable.class);
    for (Variable variable : variablesById.values()) {
        /*
       * Adds a standard enum constant, but with a lot of JavaDoc. Pulling this info
       * into IDEs should make development a lot easier for folks. (Note: technically,
       * we should HTML-escape everything, but I only bothered with the fields that
       * were actually causing problems, such as descriptions.)
       */
        Builder variableEnumBuilder = TypeSpec.anonymousClassBuilder("");
        variableEnumBuilder.addJavadoc("<p>The {@code $L} CCW variable has the following properties (taken from its codebook PDF at" + " <a href=\"https://www.ccwdata.org/web/guest/data-dictionaries\">CCW" + " Data Dictionaries</a>):</p>\n", variable.getId());
        variableEnumBuilder.addJavadoc("<ul>\n");
        variableEnumBuilder.addJavadoc("<li><strong>Codebook:</strong> $L ($L)</li>\n", variable.getCodebook().getName(), variable.getCodebook().getVersion());
        variableEnumBuilder.addJavadoc("<li><strong>Label:</strong> $L</li>\n", variable.getLabel());
        variableEnumBuilder.addJavadoc("<li><strong>Description:</strong>\n");
        for (String paragraph : variable.getDescription().orElse(Arrays.asList(MISSING_VARIABLE_FIELD))) variableEnumBuilder.addJavadoc("<p>$L</p>\n", HtmlEscapers.htmlEscaper().escape(paragraph));
        variableEnumBuilder.addJavadoc("</li>\n");
        variableEnumBuilder.addJavadoc("<li><strong>Short Name:</strong> $L</li>\n", variable.getShortName().orElse(MISSING_VARIABLE_FIELD));
        variableEnumBuilder.addJavadoc("<li><strong>Long Name:</strong> $L</li>\n", variable.getLongName());
        variableEnumBuilder.addJavadoc("<li><strong>Type:</strong> $L</li>\n", variable.getType().isPresent() ? variable.getType().get().toString() : MISSING_VARIABLE_FIELD);
        variableEnumBuilder.addJavadoc("<li><strong>Length:</strong> $L</li>\n", variable.getLength());
        variableEnumBuilder.addJavadoc("<li><strong>Source:</strong> $L</li>\n", variable.getSource().orElse(MISSING_VARIABLE_FIELD));
        variableEnumBuilder.addJavadoc("<li><strong>Value Format:</strong> $L</li>\n", variable.getValueFormat().orElse(MISSING_VARIABLE_FIELD));
        if (variable.getValueGroups() != null)
            variableEnumBuilder.addJavadoc("<li><strong>Coded Values?:</strong> $L</li>\n", variable.getValueGroups().isPresent());
        variableEnumBuilder.addJavadoc("<li><strong>Comment:</strong>\n");
        for (String paragraph : variable.getComment().orElse(Arrays.asList(MISSING_VARIABLE_FIELD))) variableEnumBuilder.addJavadoc("<p>$L</p>\n", HtmlEscapers.htmlEscaper().escape(paragraph));
        variableEnumBuilder.addJavadoc("</li>\n");
        variableEnumBuilder.addJavadoc("</ul>\n");
        variablesEnumType.addEnumConstant(variable.getId(), variableEnumBuilder.build());
    }
    variablesEnumType.addField(FieldSpec.builder(ParameterizedTypeName.get(Map.class, String.class, Variable.class), "VARIABLES_BY_ID", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL).initializer("$T.buildVariablesMappedById()", CodebookVariableReader.class).build());
    variablesEnumType.addMethod(MethodSpec.methodBuilder("getVariable").addModifiers(Modifier.PUBLIC).addStatement("return VARIABLES_BY_ID.get(this.name())").returns(Variable.class).addJavadoc("@return the {@link $T} data (parsed from a codebook PDF) for this {@link $T} constant\n", Variable.class, variableEnumName).build());
    TypeSpec columnEnumFinal = variablesEnumType.build();
    JavaFile columnsEnumFile = JavaFile.builder(annotatedPackage.getQualifiedName().toString(), columnEnumFinal).build();
    columnsEnumFile.writeTo(processingEnv.getFiler());
}
Also used : Variable(gov.cms.bfd.model.codebook.model.Variable) CodebookVariableEnumGeneration(gov.cms.bfd.model.codegen.annotations.CodebookVariableEnumGeneration) Builder(com.squareup.javapoet.TypeSpec.Builder) Builder(com.squareup.javapoet.TypeSpec.Builder) ClassName(com.squareup.javapoet.ClassName) JavaFile(com.squareup.javapoet.JavaFile) TypeSpec(com.squareup.javapoet.TypeSpec)

Aggregations

ClassName (com.squareup.javapoet.ClassName)1 JavaFile (com.squareup.javapoet.JavaFile)1 TypeSpec (com.squareup.javapoet.TypeSpec)1 Builder (com.squareup.javapoet.TypeSpec.Builder)1 Variable (gov.cms.bfd.model.codebook.model.Variable)1 CodebookVariableEnumGeneration (gov.cms.bfd.model.codegen.annotations.CodebookVariableEnumGeneration)1