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;
}
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;
}
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;
}
Aggregations