Search in sources :

Example 1 with DynamicModelConstraints

use of org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints in project kie-wb-common by kiegroup.

the class NestedFormBackendFormRenderingContextManagerTest method testConstraintsExtraction.

@Test
public void testConstraintsExtraction() {
    DynamicModelConstraints constraints = context.getRenderingContext().getModelConstraints().get(Person.class.getName());
    assertNotNull("Constraints cannot be null", constraints);
    assertFalse("There should be field constraints", constraints.getFieldConstraints().isEmpty());
    assertEquals("There should be 3 constraints", 3, constraints.getFieldConstraints().size());
    testFieldAnnotation(constraints, "id", Min.class.getName(), Max.class.getName());
    testFieldAnnotation(constraints, "name", NotNull.class.getName(), NotEmpty.class.getName());
    testFieldAnnotation(constraints, "birthday", NotNull.class.getName());
}
Also used : DynamicModelConstraints(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints) Min(javax.validation.constraints.Min) Max(javax.validation.constraints.Max) NotEmpty(org.hibernate.validator.constraints.NotEmpty) Person(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.model.Person) Assert.assertNotNull(org.junit.Assert.assertNotNull) NotNull(javax.validation.constraints.NotNull) Test(org.junit.Test)

Example 2 with DynamicModelConstraints

use of org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints 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));
                    });
                });
            }
        }
    });
}
Also used : DynamicModelConstraints(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) HashMap(java.util.HashMap) BeanDescriptor(javax.validation.metadata.BeanDescriptor) JavaFormModel(org.kie.workbench.common.forms.model.JavaFormModel) FieldConstraint(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.FieldConstraint)

Example 3 with DynamicModelConstraints

use of org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints in project kie-wb-common by kiegroup.

the class DynamicFormHandlerGenerator method generateFormHandler.

@Override
public FormHandler generateFormHandler(MapModelRenderingContext context) {
    DynamicModelValidator dynamicValidator = new DynamicModelValidator(validator);
    if (context.getRootForm().getModel() instanceof JavaFormModel) {
        DynamicModelConstraints constraints = context.getModelConstraints().get(((JavaFormModel) context.getRootForm().getModel()).getType());
        if (constraints != null) {
            dynamicValidator.setModelConstraints(constraints);
        }
    }
    FormValidator formValidator = new FormValidatorImpl(dynamicValidator, fieldStateValidator);
    FormHandler handler = new FormHandlerImpl(formValidator, new FieldChangeHandlerManagerImpl());
    if (context.getParentContext() == null) {
        helper.initContext(context);
    }
    return handler;
}
Also used : DynamicModelValidator(org.kie.workbench.common.forms.dynamic.client.processing.engine.handling.DynamicModelValidator) DynamicModelConstraints(org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints) FieldChangeHandlerManagerImpl(org.kie.workbench.common.forms.processing.engine.handling.impl.FieldChangeHandlerManagerImpl) FormValidatorImpl(org.kie.workbench.common.forms.processing.engine.handling.impl.FormValidatorImpl) FormHandler(org.kie.workbench.common.forms.processing.engine.handling.FormHandler) FormHandlerImpl(org.kie.workbench.common.forms.processing.engine.handling.impl.FormHandlerImpl) JavaFormModel(org.kie.workbench.common.forms.model.JavaFormModel) FormValidator(org.kie.workbench.common.forms.processing.engine.handling.FormValidator)

Aggregations

DynamicModelConstraints (org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints)3 JavaFormModel (org.kie.workbench.common.forms.model.JavaFormModel)2 HashMap (java.util.HashMap)1 Max (javax.validation.constraints.Max)1 Min (javax.validation.constraints.Min)1 NotNull (javax.validation.constraints.NotNull)1 BeanDescriptor (javax.validation.metadata.BeanDescriptor)1 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)1 NotEmpty (org.hibernate.validator.constraints.NotEmpty)1 Assert.assertNotNull (org.junit.Assert.assertNotNull)1 Test (org.junit.Test)1 Person (org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.model.Person)1 DynamicModelValidator (org.kie.workbench.common.forms.dynamic.client.processing.engine.handling.DynamicModelValidator)1 FieldConstraint (org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.FieldConstraint)1 FormHandler (org.kie.workbench.common.forms.processing.engine.handling.FormHandler)1 FormValidator (org.kie.workbench.common.forms.processing.engine.handling.FormValidator)1 FieldChangeHandlerManagerImpl (org.kie.workbench.common.forms.processing.engine.handling.impl.FieldChangeHandlerManagerImpl)1 FormHandlerImpl (org.kie.workbench.common.forms.processing.engine.handling.impl.FormHandlerImpl)1 FormValidatorImpl (org.kie.workbench.common.forms.processing.engine.handling.impl.FormValidatorImpl)1