Search in sources :

Example 1 with BackendFormRenderingContext

use of org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContext in project kie-wb-common by kiegroup.

the class BackendFormRenderingContextManagerImpl method registerContext.

@Override
public BackendFormRenderingContext registerContext(FormDefinition rootForm, Map<String, Object> formData, ClassLoader classLoader, Map<String, String> params, FormDefinition... nestedForms) {
    MapModelRenderingContext clientRenderingContext = new MapModelRenderingContext(String.valueOf(System.currentTimeMillis()));
    clientRenderingContext.setRootForm(rootForm);
    Arrays.stream(nestedForms).forEach(form -> clientRenderingContext.getAvailableForms().put(form.getId(), form));
    BackendFormRenderingContextImpl context = new BackendFormRenderingContextImpl(System.currentTimeMillis(), clientRenderingContext, formData, classLoader, params);
    Map<String, Object> clientFormData = new HashMap<>();
    rootForm.getFields().stream().filter(fieldDefinition -> !StringUtils.isEmpty(fieldDefinition.getBinding())).forEach(fieldDefinition -> {
        Object value = formData.get(fieldDefinition.getBinding());
        FieldValueMarshaller marshaller = registry.getMarshaller(fieldDefinition);
        if (marshaller != null) {
            marshaller.init(value, fieldDefinition, rootForm, context);
            context.getRootFormMarshallers().put(fieldDefinition.getBinding(), marshaller);
            value = marshaller.toFlatValue();
        }
        clientFormData.put(fieldDefinition.getBinding(), value);
    });
    constraintsExtractor.readModelConstraints(clientRenderingContext, classLoader);
    clientRenderingContext.setModel(clientFormData);
    contexts.put(context.getTimestamp(), context);
    return context;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) FieldValueMarshallerRegistry(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshallerRegistry) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SessionScoped(javax.enterprise.context.SessionScoped) StringUtils(org.apache.commons.lang3.StringUtils) FieldValueMarshaller(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshaller) Inject(javax.inject.Inject) MapModelRenderingContext(org.kie.workbench.common.forms.dynamic.service.shared.impl.MapModelRenderingContext) ContextModelConstraintsExtractor(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.validation.ContextModelConstraintsExtractor) Map(java.util.Map) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) BackendFormRenderingContextManager(org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContextManager) BackendFormRenderingContext(org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContext) HashMap(java.util.HashMap) MapModelRenderingContext(org.kie.workbench.common.forms.dynamic.service.shared.impl.MapModelRenderingContext) FieldValueMarshaller(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshaller)

Example 2 with BackendFormRenderingContext

use of org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContext in project kie-wb-common by kiegroup.

the class BackendFormRenderingContextManagerImpl method updateContextData.

@Override
public BackendFormRenderingContext updateContextData(long timestamp, Map<String, Object> formValues) {
    BackendFormRenderingContextImpl context = contexts.get(timestamp);
    if (context == null) {
        throw new IllegalArgumentException("Unable to find context with id '" + timestamp + "'");
    }
    FormDefinition rootForm = context.getRenderingContext().getRootForm();
    Map<String, Object> contextData = new HashMap<>();
    rootForm.getFields().stream().filter(fieldDefinition -> !StringUtils.isEmpty(fieldDefinition.getBinding())).forEach(fieldDefinition -> {
        Object value = formValues.get(fieldDefinition.getBinding());
        FieldValueMarshaller marshaller = context.getRootFormMarshallers().get(fieldDefinition.getBinding());
        if (marshaller != null) {
            value = marshaller.toRawValue(value);
        }
        contextData.put(fieldDefinition.getBinding(), value);
    });
    context.setFormData(contextData);
    return context;
}
Also used : Arrays(java.util.Arrays) Logger(org.slf4j.Logger) FieldValueMarshallerRegistry(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshallerRegistry) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) SessionScoped(javax.enterprise.context.SessionScoped) StringUtils(org.apache.commons.lang3.StringUtils) FieldValueMarshaller(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshaller) Inject(javax.inject.Inject) MapModelRenderingContext(org.kie.workbench.common.forms.dynamic.service.shared.impl.MapModelRenderingContext) ContextModelConstraintsExtractor(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.validation.ContextModelConstraintsExtractor) Map(java.util.Map) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) BackendFormRenderingContextManager(org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContextManager) BackendFormRenderingContext(org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContext) HashMap(java.util.HashMap) FormDefinition(org.kie.workbench.common.forms.model.FormDefinition) FieldValueMarshaller(org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshaller)

Aggregations

Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 SessionScoped (javax.enterprise.context.SessionScoped)2 Inject (javax.inject.Inject)2 StringUtils (org.apache.commons.lang3.StringUtils)2 FieldValueMarshaller (org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshaller)2 FieldValueMarshallerRegistry (org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.impl.marshalling.FieldValueMarshallerRegistry)2 ContextModelConstraintsExtractor (org.kie.workbench.common.forms.dynamic.backend.server.context.generation.dynamic.validation.ContextModelConstraintsExtractor)2 BackendFormRenderingContext (org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContext)2 BackendFormRenderingContextManager (org.kie.workbench.common.forms.dynamic.service.context.generation.dynamic.BackendFormRenderingContextManager)2 MapModelRenderingContext (org.kie.workbench.common.forms.dynamic.service.shared.impl.MapModelRenderingContext)2 FormDefinition (org.kie.workbench.common.forms.model.FormDefinition)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2