use of org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.FieldConstraint in project kie-wb-common by kiegroup.
the class ContextModelConstraintsExtractorImpl method readModelConstraints.
@Override
public void readModelConstraints(MapModelRenderingContext clientRenderingContext, ClassLoader classLoader) {
if (clientRenderingContext == null) {
throw new IllegalArgumentException("Context cannot be null");
}
if (classLoader == null) {
throw new IllegalArgumentException("ClassLoader cannot be null");
}
clientRenderingContext.getAvailableForms().values().forEach(formDefinition -> {
if (formDefinition.getModel() instanceof JavaFormModel) {
JavaFormModel javaModel = (JavaFormModel) formDefinition.getModel();
if (clientRenderingContext.getModelConstraints().containsKey(javaModel)) {
return;
}
Class clazz = null;
try {
clazz = classLoader.loadClass(javaModel.getType());
if (clazz == null) {
clazz = getClass().forName(javaModel.getType());
}
} catch (ClassNotFoundException e) {
// maybe Class is not on the project ClassLoader, let's try on the main ClassLoader
try {
clazz = getClass().forName(javaModel.getType());
} catch (ClassNotFoundException e1) {
// ops! class not available on the main classLoader
}
}
if (clazz == null) {
logger.warn("Unable to find class for type {} on any classLoader. Skipping annotation processing", javaModel.getType());
} else {
BeanDescriptor descriptor = Validation.buildDefaultValidatorFactory().getValidator().getConstraintsForClass(clazz);
Set<PropertyDescriptor> properties = descriptor.getConstrainedProperties();
DynamicModelConstraints constraints = new DynamicModelConstraints(javaModel.getType());
clientRenderingContext.getModelConstraints().put(javaModel.getType(), constraints);
properties.forEach(property -> {
property.getConstraintDescriptors().forEach(constraintDescriptor -> {
Map<String, Object> attributes = new HashMap<>();
constraintDescriptor.getAttributes().forEach((key, value) -> {
if (key.equals("payload") || key.equals("groups")) {
return;
}
Object portableValue;
if (EnvUtil.isPortableType(value.getClass())) {
portableValue = value;
} else {
portableValue = value.toString();
}
attributes.put(key, portableValue);
});
constraints.addConstraintForField(property.getPropertyName(), new FieldConstraint(constraintDescriptor.getAnnotation().annotationType().getName(), attributes));
});
});
}
}
});
}
Aggregations