Search in sources :

Example 1 with JavaFormModel

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

the class MultipleSubformBackendFormRenderingContextManagerTest method getNestedForms.

@Override
protected FormDefinition[] getNestedForms() {
    JavaFormModel model = new PortableJavaModel(Person.class.getName());
    FormDefinition creationForm = new FormDefinition(model);
    creationForm.setId("person-creation");
    FieldDefinition field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(Long.class.getName()));
    field.setName("id");
    field.setBinding("id");
    creationForm.getFields().add(field);
    field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(String.class.getName()));
    field.setName("name");
    field.setBinding("name");
    creationForm.getFields().add(field);
    field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(String.class.getName()));
    field.setName("lastName");
    field.setBinding("lastName");
    creationForm.getFields().add(field);
    field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(Date.class.getName()));
    field.setName("birthday");
    field.setBinding("birthday");
    creationForm.getFields().add(field);
    FormDefinition editionForm = new FormDefinition(model);
    editionForm.setId("person-edition");
    field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(Long.class.getName()));
    field.setName("id");
    field.setBinding("id");
    editionForm.getFields().add(field);
    field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(String.class.getName()));
    field.setName("name");
    field.setBinding("name");
    editionForm.getFields().add(field);
    field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(String.class.getName()));
    field.setName("lastName");
    field.setBinding("lastName");
    editionForm.getFields().add(field);
    field = fieldManager.getDefinitionByDataType(new TypeInfoImpl(Date.class.getName()));
    field.setName("birthday");
    field.setBinding("birthday");
    editionForm.getFields().add(field);
    return new FormDefinition[] { creationForm, editionForm };
}
Also used : FieldDefinition(org.kie.workbench.common.forms.model.FieldDefinition) MultipleSubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition) PortableJavaModel(org.kie.workbench.common.forms.model.impl.PortableJavaModel) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) TypeInfoImpl(org.kie.workbench.common.forms.model.impl.TypeInfoImpl) JavaFormModel(org.kie.workbench.common.forms.model.JavaFormModel) Person(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.model.Person) Date(java.util.Date)

Example 2 with JavaFormModel

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

the class BPMNFormGenerationTest method checkPersonForm.

private void checkPersonForm(FormDefinition form, Map<String, FormDefinition> allForms) {
    assertNotNull(form);
    assertTrue(form.getModel() instanceof JavaFormModel);
    assertEquals(4, form.getFields().size());
    FieldDefinition field = form.getFieldByBinding("name");
    assertFieldStatus(field, "name", String.class.getName());
    field = form.getFieldByBinding("type");
    assertFieldStatus(field, "type", PersonType.class.getName());
    assertTrue(field instanceof EnumListBoxFieldDefinition);
    field = form.getFieldByBinding("personalData");
    assertFieldStatus(field, "personalData", PersonalData.class.getName());
    assertTrue(field instanceof SubFormFieldDefinition);
    SubFormFieldDefinition subForm = (SubFormFieldDefinition) field;
    assertNotNull(subForm.getNestedForm());
    assertNotNull(allForms.get(subForm.getNestedForm()));
    field = form.getFieldByBinding("log");
    assertFieldStatus(field, "log", LogEntry.class.getName());
    assertTrue(field instanceof MultipleSubFormFieldDefinition);
    MultipleSubFormFieldDefinition multipleSubForm = (MultipleSubFormFieldDefinition) field;
    assertNotNull(multipleSubForm.getCreationForm());
    assertEquals(multipleSubForm.getCreationForm(), multipleSubForm.getEditionForm());
    FormDefinition nestedForm = allForms.get(multipleSubForm.getCreationForm());
    assertNotNull(nestedForm);
    assertNotNull(multipleSubForm.getColumnMetas());
    assertEquals(nestedForm.getFields().size(), multipleSubForm.getColumnMetas().size());
    multipleSubForm.getColumnMetas().forEach(columnMeta -> {
        FieldDefinition nestedField = nestedForm.getFieldByBinding(columnMeta.getProperty());
        assertNotNull(nestedField);
        assertEquals(nestedField.getLabel(), columnMeta.getLabel());
    });
}
Also used : PersonalData(org.kie.workbench.common.forms.jbpm.server.service.impl.model.PersonalData) SubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition) MultipleSubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition) FieldDefinition(org.kie.workbench.common.forms.model.FieldDefinition) EnumListBoxFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.selectors.listBox.definition.EnumListBoxFieldDefinition) SubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition) MultipleSubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition) PersonType(org.kie.workbench.common.forms.jbpm.server.service.impl.model.PersonType) Mockito.anyString(org.mockito.Mockito.anyString) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) JavaFormModel(org.kie.workbench.common.forms.model.JavaFormModel) EnumListBoxFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.selectors.listBox.definition.EnumListBoxFieldDefinition) LogEntry(org.kie.workbench.common.forms.jbpm.server.service.impl.model.LogEntry) MultipleSubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition)

Example 3 with JavaFormModel

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

the class FormDefinitionSerializerImplTest method initTest.

@Before
public void initTest() {
    fieldManager = new TestFieldManager();
    definitionSerializer = new FormDefinitionSerializerImpl(new FieldSerializer(), new FormModelSerializer(), new TestMetaDataEntryManager());
    JavaFormModel model = new PortableJavaModel("org.test.MyParentModel");
    formDefinition = new FormDefinition(model);
    formDefinition.setId("testForm");
    formDefinition.setName("testForm");
    formDefinition.setLayoutTemplate(new LayoutTemplate());
    for (BasicTypeFieldProvider provider : fieldManager.getAllBasicTypeProviders()) {
        for (String type : provider.getSupportedTypes()) {
            FieldDefinition field = provider.getFieldByType(new TypeInfoImpl(type));
            assertNotNull(field);
            String fieldDescription = provider.getFieldTypeName() + "_" + type;
            field.setName(fieldDescription);
            field.setLabel(fieldDescription);
            field.setStandaloneClassName(type);
            field.setBinding(fieldDescription);
            model.getProperties().add(new ModelPropertyImpl(fieldDescription, new TypeInfoImpl(type)));
            formDefinition.getFields().add(field);
        }
    }
    SubFormFieldDefinition subForm = new SubFormFieldDefinition();
    subForm.setLabel("SubForm");
    subForm.setNestedForm("");
    subForm.setBinding("model");
    subForm.setStandaloneClassName("org.test.MyTestModel");
    subForm.setBinding("SubForm");
    formDefinition.getFields().add(subForm);
    model.getProperties().add(new ModelPropertyImpl(subForm.getBinding(), new TypeInfoImpl(TypeKind.OBJECT, subForm.getStandaloneClassName(), false)));
    MultipleSubFormFieldDefinition multipleSubForm = new MultipleSubFormFieldDefinition();
    multipleSubForm.setLabel("MultipleSubForm");
    multipleSubForm.setCreationForm("");
    multipleSubForm.setEditionForm("");
    multipleSubForm.setStandaloneClassName("org.test.MyTestModel");
    multipleSubForm.setBinding("MultipleSubForm");
    formDefinition.getFields().add(multipleSubForm);
    model.getProperties().add(new ModelPropertyImpl(multipleSubForm.getBinding(), new TypeInfoImpl(TypeKind.OBJECT, multipleSubForm.getStandaloneClassName(), true)));
    EnumListBoxFieldDefinition enumListBox = new EnumListBoxFieldDefinition();
    enumListBox.setLabel("EnumListBox");
    enumListBox.setBinding("EnumListBox");
    enumListBox.setStandaloneClassName("org.test.MyTestModel");
    formDefinition.getFields().add(enumListBox);
    model.getProperties().add(new ModelPropertyImpl(enumListBox.getBinding(), new TypeInfoImpl(TypeKind.ENUM, enumListBox.getStandaloneClassName(), false)));
}
Also used : LayoutTemplate(org.uberfire.ext.layout.editor.api.editor.LayoutTemplate) FieldDefinition(org.kie.workbench.common.forms.model.FieldDefinition) MultipleSubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition) EnumListBoxFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.selectors.listBox.definition.EnumListBoxFieldDefinition) SubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition) ModelPropertyImpl(org.kie.workbench.common.forms.model.impl.ModelPropertyImpl) TestMetaDataEntryManager(org.kie.workbench.common.forms.fields.test.TestMetaDataEntryManager) MultipleSubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition) EnumListBoxFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.selectors.listBox.definition.EnumListBoxFieldDefinition) MultipleSubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition) SubFormFieldDefinition(org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition) PortableJavaModel(org.kie.workbench.common.forms.model.impl.PortableJavaModel) TestFieldManager(org.kie.workbench.common.forms.fields.test.TestFieldManager) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) TypeInfoImpl(org.kie.workbench.common.forms.model.impl.TypeInfoImpl) JavaFormModel(org.kie.workbench.common.forms.model.JavaFormModel) BasicTypeFieldProvider(org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.BasicTypeFieldProvider) Before(org.junit.Before)

Example 4 with JavaFormModel

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

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

JavaFormModel (org.kie.workbench.common.forms.model.JavaFormModel)5 MultipleSubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.multipleSubform.definition.MultipleSubFormFieldDefinition)3 FieldDefinition (org.kie.workbench.common.forms.model.FieldDefinition)3 FormDefinition (org.kie.workbench.common.forms.model.FormDefinition)3 DynamicModelConstraints (org.kie.workbench.common.forms.dynamic.service.shared.impl.validation.DynamicModelConstraints)2 EnumListBoxFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.selectors.listBox.definition.EnumListBoxFieldDefinition)2 SubFormFieldDefinition (org.kie.workbench.common.forms.fields.shared.fieldTypes.relations.subForm.definition.SubFormFieldDefinition)2 PortableJavaModel (org.kie.workbench.common.forms.model.impl.PortableJavaModel)2 TypeInfoImpl (org.kie.workbench.common.forms.model.impl.TypeInfoImpl)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 BeanDescriptor (javax.validation.metadata.BeanDescriptor)1 PropertyDescriptor (javax.validation.metadata.PropertyDescriptor)1 Before (org.junit.Before)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 BasicTypeFieldProvider (org.kie.workbench.common.forms.fields.shared.fieldTypes.basic.BasicTypeFieldProvider)1 TestFieldManager (org.kie.workbench.common.forms.fields.test.TestFieldManager)1 TestMetaDataEntryManager (org.kie.workbench.common.forms.fields.test.TestMetaDataEntryManager)1