Search in sources :

Example 1 with FieldValue

use of org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldValue in project kie-wb-common by kiegroup.

the class FormDefinitionsProcessor method processFieldDefinition.

private void processFieldDefinition(TypeElement fieldDefinitionElement) throws Exception {
    final Messager messager = processingEnv.getMessager();
    messager.printMessage(Diagnostic.Kind.NOTE, "Discovered FieldDefinition class [" + fieldDefinitionElement.getSimpleName() + "]");
    Collection<FieldInfo> fieldInfos = extractFieldInfos(fieldDefinitionElement, null);
    String modelClassName = fieldDefinitionElement.getQualifiedName().toString();
    String fieldModifierName = fixClassName(modelClassName) + "_FieldStatusModifier";
    Map<String, String> fieldDefinition = new HashMap<>();
    fieldDefinition.put("className", modelClassName);
    fieldDefinition.put("fieldModifierName", fieldModifierName);
    Map<String, Object> templateContext = new HashMap<>();
    templateContext.put("modelClassName", modelClassName);
    templateContext.put("fieldModifierName", fieldModifierName);
    FieldDefinition fieldDefinitionAnnotation = fieldDefinitionElement.getAnnotation(FieldDefinition.class);
    for (FieldInfo fieldInfo : fieldInfos) {
        AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldValue.class.getName());
        if (annotation != null) {
            if (fieldDefinition.containsKey("value")) {
                throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldValue");
            }
            if (fieldInfo.getter == null || fieldInfo.setter == null) {
                throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldValue should have setter & getter");
            }
            fieldDefinition.put("value", fieldInfo.fieldElement.getSimpleName().toString());
        } else {
            annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldReadOnly.class.getName());
            if (annotation != null) {
                if (templateContext.containsKey("readOnly")) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldReadOnly");
                }
                if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN) && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldReadOnly must be boolean or Boolean");
                }
                if (fieldInfo.getter == null) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldReadOnly should have getter");
                }
                templateContext.put("readOnly", fieldInfo.getter);
            }
            annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldRequired.class.getName());
            if (annotation != null) {
                if (templateContext.containsKey("required")) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldRequired");
                }
                if (!fieldInfo.fieldElement.asType().getKind().equals(TypeKind.BOOLEAN) && !fieldInfo.fieldElement.asType().toString().equals(Boolean.class.getName())) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldRequired must be boolean or Boolean");
                }
                if (fieldInfo.getter == null) {
                    throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldRequired should have getter");
                }
                templateContext.put("required", fieldInfo.getter);
            }
            if (fieldDefinitionAnnotation.i18nMode().equals(I18nMode.OVERRIDE)) {
                annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldLabel.class.getName());
                if (annotation != null) {
                    if (templateContext.containsKey("label")) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it should have only one @FieldLabel");
                    }
                    if (!fieldInfo.fieldElement.asType().toString().equals(String.class.getName())) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldLabel must be a String");
                    }
                    if (fieldInfo.getter == null) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldLabel should have getter");
                    }
                    templateContext.put("label", fieldInfo.getter);
                }
                annotation = GeneratorUtils.getAnnotation(elementUtils, fieldInfo.fieldElement, FieldHelp.class.getName());
                if (annotation != null) {
                    if (templateContext.containsKey("helpMessage")) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: it has more than one field marked as @FieldHelp");
                    }
                    if (!fieldInfo.fieldElement.asType().toString().equals(String.class.getName())) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldHelp must be a String");
                    }
                    if (fieldInfo.getter == null) {
                        throw new Exception("Problem processing FieldDefinition [" + modelClassName + "]: field marked as @FieldHelp should have getter");
                    }
                    templateContext.put("helpMessage", fieldInfo.getter);
                }
            }
        }
    }
    StringBuffer source = writeTemplate("templates/FieldDefinitionModifier.ftl", templateContext);
    fieldDefinition.put("sourceCode", source.toString());
    context.getFieldDefinitions().add(fieldDefinition);
}
Also used : Messager(javax.annotation.processing.Messager) HashMap(java.util.HashMap) FieldDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldDefinition) MirroredTypeException(javax.lang.model.type.MirroredTypeException) GenerationException(org.uberfire.annotations.processors.exceptions.GenerationException) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException) AnnotationMirror(javax.lang.model.element.AnnotationMirror) FieldValue(org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldValue)

Aggregations

TemplateException (freemarker.template.TemplateException)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Messager (javax.annotation.processing.Messager)1 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 MirroredTypeException (javax.lang.model.type.MirroredTypeException)1 FieldDefinition (org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldDefinition)1 FieldValue (org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldValue)1 GenerationException (org.uberfire.annotations.processors.exceptions.GenerationException)1