use of org.kie.workbench.common.forms.adf.definitions.annotations.FieldParam 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;
}
use of org.kie.workbench.common.forms.adf.definitions.annotations.FieldParam in project kie-wb-common by kiegroup.
the class FormDefinitionsProcessor method extracFormFields.
private List<Map<String, String>> extracFormFields(TypeElement type, FieldPolicy policy, I18nSettings i18nSettings) throws Exception {
final Elements elementUtils = processingEnv.getElementUtils();
Collection<FieldInfo> fieldInfos = extractFieldInfos(type, fieldElement -> {
if (policy.equals(FieldPolicy.ALL)) {
AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldElement, SkipFormField.class.getName());
if (annotation != null) {
return false;
}
} else {
AnnotationMirror annotation = GeneratorUtils.getAnnotation(elementUtils, fieldElement, FormField.class.getName());
if (annotation == null) {
return false;
}
}
return true;
});
List<Map<String, String>> elementsSettings = new ArrayList<>();
for (FieldInfo fieldInfo : fieldInfos) {
if (fieldInfo.getter != null && fieldInfo.setter != null) {
String fieldName = fieldInfo.fieldElement.getSimpleName().toString();
String fieldLabel = fieldName;
String helpMessage = "";
String binding = fieldName;
String methodName = "getFormElement_" + fieldName;
Map<String, Object> elementContext = new HashMap<>();
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.fieldElement.asType();
String fieldModifier = "";
if (finalType instanceof DeclaredType) {
Element finalTypeElement = processingEnv.getTypeUtils().asElement(finalType);
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)) {
fieldLabel = finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.labelKeySuffix();
Collection<FieldInfo> labelInfos = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null);
if (labelInfos != null && labelInfos.size() == 1) {
FieldInfo labelInfo = labelInfos.iterator().next();
fieldLabel = finalType.toString() + i18nSettings.separator() + labelInfo.fieldElement.getSimpleName();
}
helpMessage = finalType.toString() + i18nSettings.separator() + fieldDefinitionAnnotation.helpMessageKeySuffix();
Collection<FieldInfo> helpMessages = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null);
if (helpMessages != null && helpMessages.size() == 1) {
FieldInfo helpInfo = helpMessages.iterator().next();
helpMessage = finalType.toString() + i18nSettings.separator() + helpInfo.fieldElement.getSimpleName();
}
}
Collection<FieldInfo> fieldValue = extractFieldInfos((TypeElement) 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();
binding += "." + valueInfo.getFieldElement().getSimpleName();
fieldModifier = fixClassName(finalType.toString()) + "_FieldStatusModifier";
finalType = valueInfo.getFieldElement().asType();
overrideI18n = !fieldDefinitionAnnotation.i18nMode().equals(I18nMode.DONT_OVERRIDE);
} else {
FormDefinition formDefinitionAnnotation = finalTypeElement.getAnnotation(FormDefinition.class);
if (formDefinitionAnnotation != null) {
fieldLabel = finalType.toString() + i18nSettings.separator() + FieldDefinition.LABEL;
Collection<FieldInfo> labelInfos = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldLabel.class) != null);
if (labelInfos != null && labelInfos.size() == 1) {
FieldInfo labelInfo = labelInfos.iterator().next();
fieldLabel = finalType.toString() + i18nSettings.separator() + labelInfo.fieldElement.getSimpleName();
overrideI18n = true;
}
helpMessage = finalType.toString() + i18nSettings.separator() + FieldDefinition.HELP_MESSAGE;
Collection<FieldInfo> helpMessages = extractFieldInfos((TypeElement) finalTypeElement, fieldElement -> fieldElement.getAnnotation(FieldHelp.class) != null);
if (helpMessages != null && helpMessages.size() == 1) {
FieldInfo helpInfo = helpMessages.iterator().next();
helpMessage = finalType.toString() + i18nSettings.separator() + helpInfo.fieldElement.getSimpleName();
overrideI18n = true;
}
typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT;
}
}
}
DeclaredType fieldType = (DeclaredType) finalType;
if (processingEnv.getTypeUtils().isAssignable(fieldType.asElement().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);
if (FormModelPropertiesUtil.isBaseType(finalType.toString())) {
typeKind = org.kie.workbench.common.forms.model.TypeKind.BASE;
} else if (elementUtils.getTypeElement(finalType.toString()).getSuperclass().toString().startsWith("java.lang.Enum")) {
typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM;
} else {
typeKind = org.kie.workbench.common.forms.model.TypeKind.OBJECT;
}
} else if (elementUtils.getTypeElement(finalType.toString()).getSuperclass().toString().startsWith("java.lang.Enum")) {
typeKind = org.kie.workbench.common.forms.model.TypeKind.ENUM;
}
}
elementContext.put("formModel", type.getQualifiedName().toString());
elementContext.put("methodName", methodName);
elementContext.put("fieldName", fieldName);
elementContext.put("binding", binding);
elementContext.put("type", typeKind.toString());
elementContext.put("className", finalType.toString());
elementContext.put("isList", String.valueOf(isList));
elementContext.put("fieldModifier", fieldModifier);
Map<String, String> params = new HashMap<>();
elementContext.put("params", params);
String afterElement = "";
FormField settings = fieldInfo.fieldElement.getAnnotation(FormField.class);
if (settings != null) {
String typeName;
try {
typeName = settings.type().getName();
} catch (MirroredTypeException exception) {
typeName = exception.getTypeMirror().toString();
}
if (StringUtils.isEmpty(typeName)) {
typeName = FieldType.class.getName();
}
afterElement = settings.afterElement();
elementContext.put("preferredType", typeName);
if (!overrideI18n && !isEmpty(settings.labelKey())) {
fieldLabel = settings.labelKey();
}
if (!overrideI18n && !isEmpty(settings.helpMessageKey())) {
helpMessage = settings.helpMessageKey();
}
elementContext.put("required", Boolean.valueOf(settings.required()).toString());
elementContext.put("readOnly", Boolean.valueOf(settings.readonly()).toString());
for (FieldParam fieldParam : settings.settings()) {
params.put(fieldParam.name(), fieldParam.value());
}
elementContext.put("wrap", Boolean.valueOf(settings.layoutSettings().wrap()).toString());
elementContext.put("horizontalSpan", String.valueOf(settings.layoutSettings().horizontalSpan()));
elementContext.put("verticalSpan", String.valueOf(settings.layoutSettings().verticalSpan()));
} else {
elementContext.put("preferredType", FieldType.class.getName());
elementContext.put("required", Boolean.FALSE.toString());
elementContext.put("readOnly", Boolean.FALSE.toString());
elementContext.put("wrap", Boolean.FALSE.toString());
elementContext.put("horizontalSpan", "1");
elementContext.put("verticalSpan", "1");
}
if (!overrideI18n) {
if (!isEmpty(i18nSettings.keyPreffix())) {
fieldLabel = i18nSettings.keyPreffix() + i18nSettings.separator() + fieldLabel;
helpMessage = i18nSettings.keyPreffix() + i18nSettings.separator() + helpMessage;
}
}
elementContext.put("labelKey", fieldLabel);
elementContext.put("helpMessageKey", helpMessage);
elementContext.put("afterElement", afterElement);
extractFieldExtraSettings(elementContext, fieldInfo.fieldElement);
StringBuffer methodCode = writeTemplate("templates/FieldElement.ftl", elementContext);
Map<String, String> fieldSettings = new HashMap<>();
fieldSettings.put("elementName", fieldName);
fieldSettings.put("afterElement", afterElement);
fieldSettings.put("methodName", methodName);
fieldSettings.put("method", methodCode.toString());
elementsSettings.add(fieldSettings);
}
}
return elementsSettings;
}
use of org.kie.workbench.common.forms.adf.definitions.annotations.FieldParam in project kie-wb-common by kiegroup.
the class FormDefinitionGenerator method processFormDefinition.
protected void processFormDefinition(TypeElement formElement) throws Exception {
final Messager messager = context.getMessager();
messager.printMessage(Diagnostic.Kind.NOTE, "Discovered FormDefintion class [" + formElement.getSimpleName() + "]");
boolean checkInheritance = false;
FormDefinition definition = formElement.getAnnotation(FormDefinition.class);
String modelClassName = formElement.getQualifiedName().toString();
String builderClassName = FormGenerationUtils.fixClassName(formElement.getQualifiedName().toString()) + FORM_BUILDER_SUFFIX;
FormDefinitionData form = new FormDefinitionData(modelClassName, builderClassName);
form.setStartElement(definition.startElement());
form.setI18nBundle(StringUtils.isEmpty(definition.i18n().bundle()) ? formElement.asType().toString() : definition.i18n().bundle());
Column[] columns = definition.layout().value();
List<String> layoutColumns = new ArrayList<>();
if (columns.length == 0) {
layoutColumns.add(ColSpan.SPAN_12.getName());
} else {
for (Column column : columns) {
layoutColumns.add(column.value().getName());
}
}
form.setLayoutColumns(layoutColumns);
checkInheritance = definition.allowInheritance();
Map<String, String> defaultFieldSettings = new HashMap<>();
for (FieldParam param : definition.defaultFieldSettings()) {
defaultFieldSettings.put(param.name(), param.value());
}
List<FormDefinitionFieldData> formElements = new ArrayList<>();
if (checkInheritance) {
TypeElement parent = getParent(formElement);
formElements.addAll(extractParentFormFields(parent, definition.policy(), definition.i18n(), defaultFieldSettings));
}
formElements.addAll(extracFormFields(formElement, definition.policy(), definition.i18n(), defaultFieldSettings));
FormGenerationUtils.sort(definition.startElement(), formElements);
messager.printMessage(Diagnostic.Kind.NOTE, "Discovered " + formElements.size() + " elements for form [" + formElement.getQualifiedName().toString() + "]");
form.getElements().addAll(formElements);
context.getForms().add(form);
}
Aggregations