Search in sources :

Example 1 with FieldInfo

use of org.kie.workbench.common.forms.adf.processors.util.FieldInfo in project kie-wb-common by kiegroup.

the class FieldDefinitionModifierGenerator method processFieldDefinition.

private void processFieldDefinition(TypeElement fieldDefinitionElement) throws Exception {
    final Messager messager = context.getMessager();
    messager.printMessage(Diagnostic.Kind.NOTE, "Discovered FieldDefinition class [" + fieldDefinitionElement.getSimpleName() + "]");
    Collection<FieldInfo> fieldInfos = FormGenerationUtils.extractFieldInfos(fieldDefinitionElement, null);
    String modelClassName = fieldDefinitionElement.getQualifiedName().toString();
    String fieldModifierName = FormGenerationUtils.fixClassName(modelClassName) + "_FieldStatusModifier";
    FieldDefinitionModifierData fieldDefinition = new FieldDefinitionModifierData(modelClassName, fieldModifierName);
    FieldDefinition fieldDefinitionAnnotation = fieldDefinitionElement.getAnnotation(FieldDefinition.class);
    for (FieldInfo fieldInfo : fieldInfos) {
        processField(fieldDefinition, fieldDefinitionAnnotation, fieldInfo);
    }
    context.getFieldDefinitions().add(fieldDefinition);
}
Also used : Messager(javax.annotation.processing.Messager) FieldDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldDefinition) FieldInfo(org.kie.workbench.common.forms.adf.processors.util.FieldInfo)

Example 2 with FieldInfo

use of org.kie.workbench.common.forms.adf.processors.util.FieldInfo in project kie-wb-common by kiegroup.

the class FormDefinitionGenerator method extracFormFields.

private List<FormDefinitionFieldData> extracFormFields(TypeElement type, FieldPolicy policy, I18nSettings i18nSettings, Map<String, String> defaultParams) throws Exception {
    final Types typeUtils = context.getProcessingEnvironment().getTypeUtils();
    Collection<FieldInfo> fieldInfos = FormGenerationUtils.extractFieldInfos(type, fieldElement -> filter(fieldElement, policy));
    List<FormDefinitionFieldData> fieldSettings = new ArrayList<>();
    for (FieldInfo fieldInfo : fieldInfos) {
        if (fieldInfo.getSetter() != null && fieldInfo.getGetter() != null) {
            String fieldName = fieldInfo.getFieldElement().getSimpleName().toString();
            FormDefinitionFieldData fieldData = new FormDefinitionFieldData(type.getQualifiedName().toString(), fieldName);
            fieldData.setLabel(fieldName);
            fieldData.setBinding(fieldName);
            fieldData.setMethodName("getFormElement_" + fieldName);
            boolean isList = false;
            org.kie.workbench.common.forms.model.TypeKind typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE;
            boolean overrideI18n = false;
            TypeMirror finalType = fieldInfo.getFieldElement().asType();
            TypeElement finalTypeElement = (TypeElement) typeUtils.asElement(finalType);
            String fieldModifier = "";
            if (finalTypeElement.getKind().equals(ElementKind.CLASS)) {
                FieldDefinition fieldDefinitionAnnotation = finalTypeElement.getAnnotation(FieldDefinition.class);
                if (fieldDefinitionAnnotation != null) {
                    // Override the using the i18n mechanism
                    if (fieldDefinitionAnnotation.i18nMode().equals(I18nMode.OVERRIDE_I18N_KEY)) {
                        fieldData.setLabel(finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.labelKeySuffix());
                        Collection<FieldInfo> labelInfos = FormGenerationUtils.extractFieldInfos(finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null);
                        if (labelInfos != null && labelInfos.size() == 1) {
                            FieldInfo labelInfo = labelInfos.iterator().next();
                            fieldData.setLabel(finalType.toString() + i18nSettings.separator() + labelInfo.getFieldElement().getSimpleName());
                        }
                        fieldData.setHelpMessage(finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.helpMessageKeySuffix());
                        Collection<FieldInfo> helpMessages = FormGenerationUtils.extractFieldInfos(finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null);
                        if (helpMessages != null && helpMessages.size() == 1) {
                            FieldInfo helpInfo = helpMessages.iterator().next();
                            fieldData.setHelpMessage(finalType.toString() + i18nSettings.separator() + helpInfo.getFieldElement().getSimpleName());
                        }
                    }
                    Collection<FieldInfo> fieldValue = FormGenerationUtils.extractFieldInfos(finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldValue.class) != null);
                    if (fieldValue == null || fieldValue.size() != 1) {
                        throw new Exception("Problem processing FieldDefinition [" + finalType + "]: it should have one field marked as @FieldValue");
                    }
                    FieldInfo valueInfo = fieldValue.iterator().next();
                    fieldData.setBinding(fieldData.getBinding() + "." + valueInfo.getFieldElement().getSimpleName());
                    fieldModifier = FormGenerationUtils.fixClassName(finalType.toString()) + "_FieldStatusModifier";
                    finalType = valueInfo.getFieldElement().asType();
                    finalTypeElement = (TypeElement) typeUtils.asElement(finalType);
                    overrideI18n = !fieldDefinitionAnnotation.i18nMode().equals(I18nMode.DONT_OVERRIDE);
                } else {
                    FormDefinition formDefinitionAnnotation = finalTypeElement.getAnnotation(FormDefinition.class);
                    if (formDefinitionAnnotation != null) {
                        fieldData.setLabel(finalType.toString() + i18nSettings.separator() + FieldDefinition.LABEL);
                        Collection<FieldInfo> labelInfos = FormGenerationUtils.extractFieldInfos(finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null);
                        if (labelInfos != null && labelInfos.size() == 1) {
                            FieldInfo labelInfo = labelInfos.iterator().next();
                            fieldData.setLabel(finalType.toString() + i18nSettings.separator() + labelInfo.getFieldElement().getSimpleName());
                            overrideI18n = true;
                        }
                        fieldData.setHelpMessage(finalType.toString() + i18nSettings.separator() + FieldDefinition.HELP_MESSAGE);
                        Collection<FieldInfo> helpMessages = FormGenerationUtils.extractFieldInfos(finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null);
                        if (helpMessages != null && helpMessages.size() == 1) {
                            FieldInfo helpInfo = helpMessages.iterator().next();
                            fieldData.setHelpMessage(finalType.toString() + i18nSettings.separator() + helpInfo.getFieldElement().getSimpleName());
                            overrideI18n = true;
                        }
                        typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT;
                    }
                }
            }
            DeclaredType fieldType = (DeclaredType) finalType;
            if (typeUtils.isAssignable(finalTypeElement.asType(), listType)) {
                if (fieldType.getTypeArguments().size() != 1) {
                    throw new IllegalArgumentException("Impossible to generate a field for type " + fieldType.toString() + ". Type should have one and only one Type arguments.");
                }
                isList = true;
                finalType = fieldType.getTypeArguments().get(0);
                finalTypeElement = (TypeElement) typeUtils.asElement(finalType);
                if (FormModelPropertiesUtil.isBaseType(finalTypeElement.getQualifiedName().toString())) {
                    typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE;
                } else if (typeUtils.isAssignable(finalTypeElement.asType(), enumType)) {
                    typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM;
                } else {
                    typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT;
                }
            } else if (typeUtils.isAssignable(finalTypeElement.asType(), enumType)) {
                typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM;
            }
            fieldData.setType(typeKind.toString());
            fieldData.setClassName(finalTypeElement.getQualifiedName().toString());
            fieldData.setList(String.valueOf(isList));
            fieldData.setFieldModifier(fieldModifier);
            fieldData.getParams().putAll(defaultParams);
            FormField settings = fieldInfo.getFieldElement().getAnnotation(FormField.class);
            if (settings != null) {
                try {
                    fieldData.setPreferredType(settings.type().getName());
                } catch (MirroredTypeException exception) {
                    fieldData.setPreferredType(exception.getTypeMirror().toString());
                }
                fieldData.setAfterElement(settings.afterElement());
                if (!overrideI18n && !isEmpty(settings.labelKey())) {
                    fieldData.setLabel(settings.labelKey());
                }
                if (!overrideI18n && !isEmpty(settings.helpMessageKey())) {
                    fieldData.setHelpMessage(settings.helpMessageKey());
                }
                fieldData.setRequired(Boolean.valueOf(settings.required()).toString());
                fieldData.setReadOnly(Boolean.valueOf(settings.readonly()).toString());
                for (FieldParam fieldParam : settings.settings()) {
                    fieldData.getParams().put(fieldParam.name(), fieldParam.value());
                }
                fieldData.setWrap(Boolean.valueOf(settings.layoutSettings().wrap()).toString());
                fieldData.setHorizontalSpan(String.valueOf(settings.layoutSettings().horizontalSpan()));
                fieldData.setVerticalSpan(String.valueOf(settings.layoutSettings().verticalSpan()));
            }
            if (!overrideI18n) {
                if (!isEmpty(i18nSettings.keyPreffix())) {
                    fieldData.setLabel(i18nSettings.keyPreffix() + i18nSettings.separator() + fieldData.getLabel());
                    if (!isEmpty(fieldData.getHelpMessage())) {
                        fieldData.setHelpMessage(i18nSettings.keyPreffix() + i18nSettings.separator() + fieldData.getHelpMessage());
                    }
                }
            }
            extractFieldExtraSettings(fieldData, fieldInfo.getFieldElement());
            fieldSettings.add(fieldData);
        }
    }
    return fieldSettings;
}
Also used : Types(javax.lang.model.util.Types) FieldParam(org.kie.workbench.common.forms.adf.definitions.annotations.FieldParam) TypeElement(javax.lang.model.element.TypeElement) FieldDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldDefinition) ArrayList(java.util.ArrayList) MirroredTypeException(javax.lang.model.type.MirroredTypeException) MirroredTypeException(javax.lang.model.type.MirroredTypeException) TypeMirror(javax.lang.model.type.TypeMirror) FormDefinition(org.kie.workbench.common.forms.adf.definitions.annotations.FormDefinition) FormField(org.kie.workbench.common.forms.adf.definitions.annotations.FormField) SkipFormField(org.kie.workbench.common.forms.adf.definitions.annotations.SkipFormField) FieldInfo(org.kie.workbench.common.forms.adf.processors.util.FieldInfo) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

FieldDefinition (org.kie.workbench.common.forms.adf.definitions.annotations.metaModel.FieldDefinition)2 FieldInfo (org.kie.workbench.common.forms.adf.processors.util.FieldInfo)2 ArrayList (java.util.ArrayList)1 Messager (javax.annotation.processing.Messager)1 TypeElement (javax.lang.model.element.TypeElement)1 DeclaredType (javax.lang.model.type.DeclaredType)1 MirroredTypeException (javax.lang.model.type.MirroredTypeException)1 TypeMirror (javax.lang.model.type.TypeMirror)1 Types (javax.lang.model.util.Types)1 FieldParam (org.kie.workbench.common.forms.adf.definitions.annotations.FieldParam)1 FormDefinition (org.kie.workbench.common.forms.adf.definitions.annotations.FormDefinition)1 FormField (org.kie.workbench.common.forms.adf.definitions.annotations.FormField)1 SkipFormField (org.kie.workbench.common.forms.adf.definitions.annotations.SkipFormField)1