Search in sources :

Example 1 with FieldPlaceHolderEntry

use of org.kie.workbench.common.forms.model.impl.meta.entries.FieldPlaceHolderEntry in project kie-wb-common by kiegroup.

the class DMOModelResolver method resolveModelForType.

private static DataObjectFormModel resolveModelForType(final ModuleDataModelOracle oracle, final String modelType, final Predicate<TypeSource> sourcePredicate) {
    PortablePreconditions.checkNotNull("oracle", oracle);
    PortablePreconditions.checkNotNull("modelType", modelType);
    PortablePreconditions.checkNotNull("sourcePredicate", sourcePredicate);
    if (FormModelPropertiesUtil.isBaseType(modelType)) {
        throw new IllegalArgumentException("Cannot extract FormModel for type '" + modelType + "'.");
    }
    final String modelName = modelType.substring(modelType.lastIndexOf(".") + 1);
    final DataObjectFormModel formModel = new DataObjectFormModel(modelName, modelType);
    TypeSource typeSource = oracle.getModuleTypeSources().get(modelType);
    if (typeSource == null || !sourcePredicate.test(typeSource)) {
        return null;
    }
    if (typeSource.equals(TypeSource.JAVA_DEPENDENCY)) {
        formModel.setSource(Source.EXTERNAL);
    }
    ModelField[] fields = oracle.getModuleModelFields().get(modelType);
    Map<String, Set<Annotation>> fieldAnnotations = oracle.getModuleTypeFieldsAnnotations().getOrDefault(modelType, Collections.emptyMap());
    Arrays.stream(fields).forEach(modelField -> {
        if (modelField.getName().equals("this")) {
            return;
        }
        if (!FieldAccessorsAndMutators.BOTH.equals(modelField.getAccessorsAndMutators())) {
            return;
        }
        Set<Annotation> annotations = Optional.ofNullable(fieldAnnotations.get(modelField.getName())).orElse(Collections.EMPTY_SET);
        if (!isValidModelProperty(modelField, annotations)) {
            return;
        }
        try {
            String fieldType = modelField.getClassName();
            boolean isEnum = oracle.getModuleJavaEnumDefinitions().get(modelType + "#" + modelField.getName()) != null;
            boolean isList = DataType.TYPE_COLLECTION.equals(modelField.getType());
            if (isList) {
                fieldType = oracle.getModuleFieldParametersType().get(modelType + "#" + modelField.getName());
            }
            TypeKind typeKind = isEnum ? TypeKind.ENUM : FormModelPropertiesUtil.isBaseType(fieldType) ? TypeKind.BASE : TypeKind.OBJECT;
            TypeInfo info = new TypeInfoImpl(typeKind, fieldType, isList);
            ModelProperty modelProperty = new ModelPropertyImpl(modelField.getName(), info);
            annotations.stream().filter(annotation -> annotation.getQualifiedTypeName().equalsIgnoreCase(LABEL_ANNOTATION)).findAny().ifPresent(annotation -> {
                String label = (String) annotation.getParameters().get(LABEL_ANNOTATION_VALUE_PARAM);
                modelProperty.getMetaData().addEntry(new FieldLabelEntry(label));
                modelProperty.getMetaData().addEntry(new FieldPlaceHolderEntry(label));
            });
            formModel.addProperty(modelProperty);
        } catch (Exception ex) {
            logger.warn("Error processing model '" + modelType + "' impossible generate FieldDefinition for model field '" + modelField.getName() + "' (" + modelField.getType() + ")");
        }
    });
    return formModel;
}
Also used : Set(java.util.Set) FieldLabelEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldLabelEntry) TypeKind(org.kie.workbench.common.forms.model.TypeKind) TypeSource(org.kie.soup.project.datamodel.oracle.TypeSource) ModelPropertyImpl(org.kie.workbench.common.forms.model.impl.ModelPropertyImpl) TypeInfo(org.kie.workbench.common.forms.model.TypeInfo) Annotation(org.kie.soup.project.datamodel.oracle.Annotation) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) ModelProperty(org.kie.workbench.common.forms.model.ModelProperty) FieldPlaceHolderEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldPlaceHolderEntry) TypeInfoImpl(org.kie.workbench.common.forms.model.impl.TypeInfoImpl) DataObjectFormModel(org.kie.workbench.common.forms.data.modeller.model.DataObjectFormModel)

Example 2 with FieldPlaceHolderEntry

use of org.kie.workbench.common.forms.model.impl.meta.entries.FieldPlaceHolderEntry in project kie-wb-common by kiegroup.

the class FieldManagerTest method testGetDefinitionByModelPropertyWithMetaData.

@Test
public void testGetDefinitionByModelPropertyWithMetaData() {
    property.getMetaData().addEntry(new FieldTypeEntry(TextAreaFieldType.NAME));
    property.getMetaData().addEntry(new FieldLabelEntry(METADATA_LABEL));
    property.getMetaData().addEntry(new FieldPlaceHolderEntry(METADATA_PLACEHOLDER));
    property.getMetaData().addEntry(new FieldReadOnlyEntry(METADATA_READONLY));
    property.getMetaData().addEntry(new FieldRequiredEntry(METADATA_REQUIRED));
    FieldDefinition fieldDefinition = fieldManager.getDefinitionByModelProperty(property);
    Assertions.assertThat(fieldDefinition).isNotNull().isInstanceOf(TextAreaFieldDefinition.class).hasFieldOrPropertyWithValue("name", PROPERTY_NAME).hasFieldOrPropertyWithValue("label", METADATA_LABEL).hasFieldOrPropertyWithValue("placeHolder", METADATA_PLACEHOLDER).hasFieldOrPropertyWithValue("required", METADATA_REQUIRED).hasFieldOrPropertyWithValue("readOnly", METADATA_READONLY).hasFieldOrPropertyWithValue("binding", PROPERTY_NAME);
}
Also used : FieldTypeEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldTypeEntry) FieldLabelEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldLabelEntry) FieldReadOnlyEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldReadOnlyEntry) FieldDefinition(org.kie.workbench.common.forms.model.FieldDefinition) TextBoxFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.textBox.definition.TextBoxFieldDefinition) TextAreaFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.textArea.definition.TextAreaFieldDefinition) FieldPlaceHolderEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldPlaceHolderEntry) FieldRequiredEntry(org.kie.workbench.common.forms.model.impl.meta.entries.FieldRequiredEntry) Test(org.junit.Test)

Aggregations

FieldLabelEntry (org.kie.workbench.common.forms.model.impl.meta.entries.FieldLabelEntry)2 FieldPlaceHolderEntry (org.kie.workbench.common.forms.model.impl.meta.entries.FieldPlaceHolderEntry)2 Set (java.util.Set)1 Test (org.junit.Test)1 Annotation (org.kie.soup.project.datamodel.oracle.Annotation)1 ModelField (org.kie.soup.project.datamodel.oracle.ModelField)1 TypeSource (org.kie.soup.project.datamodel.oracle.TypeSource)1 DataObjectFormModel (org.kie.workbench.common.forms.data.modeller.model.DataObjectFormModel)1 TextAreaFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.textArea.definition.TextAreaFieldDefinition)1 TextBoxFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.textBox.definition.TextBoxFieldDefinition)1 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)1 ModelProperty (org.kie.workbench.common.forms.model.ModelProperty)1 TypeInfo (org.kie.workbench.common.forms.model.TypeInfo)1 TypeKind (org.kie.workbench.common.forms.model.TypeKind)1 ModelPropertyImpl (org.kie.workbench.common.forms.model.impl.ModelPropertyImpl)1 TypeInfoImpl (org.kie.workbench.common.forms.model.impl.TypeInfoImpl)1 FieldReadOnlyEntry (org.kie.workbench.common.forms.model.impl.meta.entries.FieldReadOnlyEntry)1 FieldRequiredEntry (org.kie.workbench.common.forms.model.impl.meta.entries.FieldRequiredEntry)1 FieldTypeEntry (org.kie.workbench.common.forms.model.impl.meta.entries.FieldTypeEntry)1