Search in sources :

Example 1 with TypeKind

use of org.kie.workbench.common.forms.model.TypeKind 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 TypeKind

use of org.kie.workbench.common.forms.model.TypeKind in project kie-wb-common by kiegroup.

the class BPMNRuntimeFormGeneratorService method extractModelFields.

@Override
protected List<FieldDefinition> extractModelFields(JavaFormModel formModel, GenerationContext<ClassLoader> context) {
    Class clazz;
    String modelType = formModel.getType();
    try {
        clazz = context.getSource().loadClass(modelType);
        if (clazz == null) {
            clazz = getClass().forName(modelType);
        }
    } catch (ClassNotFoundException e) {
        throw new IllegalArgumentException("Unable to extract Form Fields for class '" + modelType + "'");
    }
    if (clazz == null) {
        throw new IllegalArgumentException("Unable to extract Form Fields for class '" + modelType + "'");
    }
    ModuleDataModelOracle oracle = getModuleOracle(clazz);
    if (oracle != null) {
        List<FieldDefinition> formFields = new ArrayList<>();
        ModelField[] fields = oracle.getModuleModelFields().get(modelType);
        Arrays.stream(fields).forEach(modelField -> {
            if (modelField.getName().equals("this")) {
                return;
            }
            String fieldType = modelField.getClassName();
            boolean isEnunm = 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 = isEnunm ? TypeKind.ENUM : FormModelPropertiesUtil.isBaseType(fieldType) ? TypeKind.BASE : TypeKind.OBJECT;
            TypeInfo info = new TypeInfoImpl(typeKind, fieldType, isList);
            ModelProperty modelProperty = new ModelPropertyImpl(modelField.getName(), info);
            formModel.getProperties().add(modelProperty);
            FieldDefinition field = generateFieldDefinition(modelProperty, context);
            if (field != null) {
                formFields.add(field);
            }
        });
        return formFields;
    }
    return null;
}
Also used : ModuleDataModelOracle(org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle) FieldDefinition(org.kie.workbench.common.forms.model.FieldDefinition) ArrayList(java.util.ArrayList) TypeKind(org.kie.workbench.common.forms.model.TypeKind) ModelPropertyImpl(org.kie.workbench.common.forms.model.impl.ModelPropertyImpl) TypeInfo(org.kie.workbench.common.forms.model.TypeInfo) ModelField(org.kie.soup.project.datamodel.oracle.ModelField) ModelProperty(org.kie.workbench.common.forms.model.ModelProperty) TypeInfoImpl(org.kie.workbench.common.forms.model.impl.TypeInfoImpl)

Example 3 with TypeKind

use of org.kie.workbench.common.forms.model.TypeKind in project kie-wb-common by kiegroup.

the class ModelPropertiesGenerator method createModelProperty.

public static ModelProperty createModelProperty(String name, String className, boolean isMultiple, ClassLoader classLoader) {
    if (FormModelPropertiesUtil.isBaseType(className)) {
        // Dealing with basic type properties (String, Integer...)
        return new ModelPropertyImpl(name, new TypeInfoImpl(className, isMultiple));
    } else {
        // Dealing with complex types.
        if (FormModelPropertiesUtil.isListType(className)) {
            // If className is a List let's create a model for Object...
            return createModelProperty(name, Object.class.getName(), true);
        }
        try {
            Class clazz = classLoader.loadClass(className);
            TypeKind typeKind = clazz.isEnum() ? TypeKind.ENUM : TypeKind.OBJECT;
            return new ModelPropertyImpl(name, new TypeInfoImpl(typeKind, className, isMultiple));
        } catch (ClassNotFoundException e) {
            logger.warn("Unable to create property '" + name + "' for class '" + className + "':", e);
        }
    }
    return null;
}
Also used : TypeKind(org.kie.workbench.common.forms.model.TypeKind) ModelPropertyImpl(org.kie.workbench.common.forms.model.impl.ModelPropertyImpl) TypeInfoImpl(org.kie.workbench.common.forms.model.impl.TypeInfoImpl)

Aggregations

TypeKind (org.kie.workbench.common.forms.model.TypeKind)3 ModelPropertyImpl (org.kie.workbench.common.forms.model.impl.ModelPropertyImpl)3 TypeInfoImpl (org.kie.workbench.common.forms.model.impl.TypeInfoImpl)3 ModelField (org.kie.soup.project.datamodel.oracle.ModelField)2 ModelProperty (org.kie.workbench.common.forms.model.ModelProperty)2 TypeInfo (org.kie.workbench.common.forms.model.TypeInfo)2 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 Annotation (org.kie.soup.project.datamodel.oracle.Annotation)1 ModuleDataModelOracle (org.kie.soup.project.datamodel.oracle.ModuleDataModelOracle)1 TypeSource (org.kie.soup.project.datamodel.oracle.TypeSource)1 DataObjectFormModel (org.kie.workbench.common.forms.data.modeller.model.DataObjectFormModel)1 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)1 FieldLabelEntry (org.kie.workbench.common.forms.model.impl.meta.entries.FieldLabelEntry)1 FieldPlaceHolderEntry (org.kie.workbench.common.forms.model.impl.meta.entries.FieldPlaceHolderEntry)1