Search in sources :

Example 1 with FormComponentState

use of com.qcadoo.view.internal.components.form.FormComponentState in project qcadoo by qcadoo.

the class AwesomeDynamicListState method setFieldValue.

@SuppressWarnings("unchecked")
@Override
public void setFieldValue(final Object value) {
    requestRender();
    forms = new LinkedList<>();
    if (value instanceof List) {
        List<Entity> entities = (List<Entity>) value;
        for (Entity entity : entities) {
            InternalViewDefinitionState innerFormState = new ViewDefinitionStateImpl();
            FormComponentState formState = (FormComponentState) innerFormPattern.createComponentState(innerFormState);
            innerFormPattern.updateComponentStateListeners(innerFormState);
            try {
                formState.initialize(new JSONObject(), getLocale());
            } catch (JSONException e) {
                throw new IllegalStateException(e);
            }
            formState.setEntity(entity);
            forms.add(formState);
        }
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) JSONObject(org.json.JSONObject) InternalViewDefinitionState(com.qcadoo.view.internal.api.InternalViewDefinitionState) JSONException(org.json.JSONException) List(java.util.List) LinkedList(java.util.LinkedList) ViewDefinitionStateImpl(com.qcadoo.view.internal.internal.ViewDefinitionStateImpl) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState)

Example 2 with FormComponentState

use of com.qcadoo.view.internal.components.form.FormComponentState in project qcadoo by qcadoo.

the class AwesomeDynamicListState method initializeContent.

@Override
protected void initializeContent(final JSONObject json) throws JSONException {
    if (json.has(JSON_FORM_VALUES)) {
        forms = new LinkedList<>();
        JSONArray formValues = json.getJSONArray(JSON_FORM_VALUES);
        for (int i = 0; i < formValues.length(); i++) {
            JSONObject value = formValues.getJSONObject(i);
            String formName = value.getString("name");
            JSONObject formValue = value.getJSONObject("value");
            InternalViewDefinitionState innerFormState = new ViewDefinitionStateImpl();
            FormComponentState formState = (FormComponentState) innerFormPattern.createComponentState(innerFormState);
            formState.setName(formName);
            innerFormPattern.updateComponentStateListeners(innerFormState);
            formState.initialize(formValue, getLocale());
            forms.add(formState);
        }
    }
}
Also used : JSONObject(org.json.JSONObject) InternalViewDefinitionState(com.qcadoo.view.internal.api.InternalViewDefinitionState) JSONArray(org.json.JSONArray) ViewDefinitionStateImpl(com.qcadoo.view.internal.internal.ViewDefinitionStateImpl) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState)

Example 3 with FormComponentState

use of com.qcadoo.view.internal.components.form.FormComponentState in project qcadoo by qcadoo.

the class ComponentStateTest method shouldHaveRequestUpdateStateFlag.

@Test
public void shouldHaveRequestUpdateStateFlag() throws Exception {
    // given
    new ExpressionServiceImpl().init();
    TranslationService translationService = mock(TranslationService.class);
    DataDefinition dataDefinition = mock(DataDefinition.class);
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn("2");
    ApplicationContext applicationContext = mock(ApplicationContext.class);
    setField(pattern, "applicationContext", applicationContext);
    AbstractComponentState componentState = new FormComponentState(pattern);
    componentState.setTranslationService(translationService);
    componentState.setDataDefinition(dataDefinition);
    componentState.setFieldValue(13L);
    componentState.initialize(new JSONObject(ImmutableMap.of("components", new JSONObject())), Locale.ENGLISH);
    // when
    JSONObject json = componentState.render();
    // then
    assertTrue(json.getBoolean(AbstractComponentState.JSON_UPDATE_STATE));
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) JSONObject(org.json.JSONObject) TranslationService(com.qcadoo.localization.api.TranslationService) ExpressionServiceImpl(com.qcadoo.model.internal.ExpressionServiceImpl) DataDefinition(com.qcadoo.model.api.DataDefinition) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) Test(org.junit.Test)

Example 4 with FormComponentState

use of com.qcadoo.view.internal.components.form.FormComponentState in project qcadoo by qcadoo.

the class ContainerStateTest method shouldReturnNullIfChildNotExist.

@Test
public void shouldReturnNullIfChildNotExist() throws Exception {
    // given
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn(null);
    setField(pattern, "applicationContext", applicationContext);
    FormComponentState container = new FormComponentState(pattern);
    // when
    ComponentState child = container.getChild("component");
    // then
    assertNull(child);
}
Also used : FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) ComponentState(com.qcadoo.view.api.ComponentState) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) Test(org.junit.Test)

Example 5 with FormComponentState

use of com.qcadoo.view.internal.components.form.FormComponentState in project qcadoo by qcadoo.

the class ContainerStateTest method shouldHaveNoChildren.

@Test
public void shouldHaveNoChildren() throws Exception {
    // given
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn(null);
    setField(pattern, "applicationContext", applicationContext);
    FormComponentState container = new FormComponentState(pattern);
    // when
    Map<String, InternalComponentState> children = container.getChildren();
    // then
    assertNotNull(children);
    assertEquals(0, children.size());
}
Also used : FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) Test(org.junit.Test)

Aggregations

FormComponentState (com.qcadoo.view.internal.components.form.FormComponentState)24 FormComponentPattern (com.qcadoo.view.internal.components.form.FormComponentPattern)18 Test (org.junit.Test)17 JSONObject (org.json.JSONObject)12 InternalComponentState (com.qcadoo.view.internal.api.InternalComponentState)8 ViewDefinitionState (com.qcadoo.view.api.ViewDefinitionState)5 ApplicationContext (org.springframework.context.ApplicationContext)5 TranslationService (com.qcadoo.localization.api.TranslationService)4 ComponentState (com.qcadoo.view.api.ComponentState)4 DataDefinition (com.qcadoo.model.api.DataDefinition)3 Entity (com.qcadoo.model.api.Entity)3 AbstractStateTest (com.qcadoo.view.internal.states.AbstractStateTest)3 ExpressionServiceImpl (com.qcadoo.model.internal.ExpressionServiceImpl)2 FormComponent (com.qcadoo.view.api.components.FormComponent)2 InternalViewDefinitionState (com.qcadoo.view.internal.api.InternalViewDefinitionState)2 ViewEventListenerHook (com.qcadoo.view.internal.hooks.ViewEventListenerHook)2 ViewDefinitionStateImpl (com.qcadoo.view.internal.internal.ViewDefinitionStateImpl)2 JSONArray (org.json.JSONArray)2 FieldDefinition (com.qcadoo.model.api.FieldDefinition)1 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)1