Search in sources :

Example 11 with FormComponentState

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

the class ScopeEntityIdChangeListenerTest method shouldHaveScopeListeners.

@Test
public void shouldHaveScopeListeners() throws Exception {
    // given
    ComponentState component1 = createMockComponent("component1");
    ComponentState component2 = createMockComponent("component2");
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn(null);
    ApplicationContext applicationContext = mock(ApplicationContext.class);
    setField(pattern, "applicationContext", applicationContext);
    FormComponentState container = new FormComponentState(pattern);
    container.addScopeEntityIdChangeListener("component1", (ScopeEntityIdChangeListener) component1);
    container.addScopeEntityIdChangeListener("component2", (ScopeEntityIdChangeListener) component2);
    // when
    container.setFieldValue(13L);
    // then
    verify((ScopeEntityIdChangeListener) component1).onScopeEntityIdChange(13L);
    verify((ScopeEntityIdChangeListener) component2).onScopeEntityIdChange(13L);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) ScopeEntityIdChangeListener(com.qcadoo.view.internal.ScopeEntityIdChangeListener) ComponentState(com.qcadoo.view.api.ComponentState) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) Test(org.junit.Test)

Example 12 with FormComponentState

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

the class FormComponentStateTest method shouldInitialeFormWithEntityId.

@Test
public void shouldInitialeFormWithEntityId() throws Exception {
    // given
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn(null);
    setField(pattern, "applicationContext", applicationContext);
    InternalComponentState componentState = new FormComponentState(pattern);
    JSONObject json = new JSONObject();
    JSONObject jsonContent = new JSONObject();
    jsonContent.put(FormComponentState.JSON_ENTITY_ID, 13L);
    json.put(AbstractComponentState.JSON_CONTENT, jsonContent);
    JSONObject jsonChildren = new JSONObject();
    json.put(AbstractComponentState.JSON_CHILDREN, jsonChildren);
    // when
    componentState.initialize(json, Locale.ENGLISH);
    // then
    assertEquals(13L, componentState.getFieldValue());
}
Also used : FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) JSONObject(org.json.JSONObject) InternalComponentState(com.qcadoo.view.internal.api.InternalComponentState) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 13 with FormComponentState

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

the class FormComponentStateTest method init.

@Before
public void init() throws Exception {
    entity = mock(Entity.class);
    given(entity.getField("name")).willReturn("text");
    viewDefinitionState = mock(ViewDefinitionState.class);
    translationService = mock(TranslationService.class);
    fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(new StringType());
    given(fieldDefinition.getName()).willReturn("name");
    dataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
    given(dataDefinition.get(12L)).willReturn(null);
    given(dataDefinition.get(13L)).willReturn(entity);
    given(dataDefinition.getPluginIdentifier()).willReturn("plugin");
    given(dataDefinition.getName()).willReturn("name");
    given(dataDefinition.getField("name")).willReturn(fieldDefinition);
    given(dataDefinition.delete(any(Long.class))).willReturn(EntityOpResult.successfull());
    given(dataDefinition.create(anyLong())).willAnswer(new Answer<Entity>() {

        @Override
        public Entity answer(final InvocationOnMock invocation) throws Throwable {
            Long id = (Long) invocation.getArguments()[0];
            return new DefaultEntity(dataDefinition, id);
        }
    });
    FieldComponentPattern namePattern = mock(FieldComponentPattern.class);
    given(namePattern.isRequired()).willReturn(false);
    given(namePattern.isPersistent()).willReturn(true);
    name = new FieldComponentState(namePattern);
    name.setTranslationService(translationService);
    name.setName("name");
    name.initialize(new JSONObject(), Locale.ENGLISH);
    FormComponentPattern pattern = mock(FormComponentPattern.class);
    given(pattern.getExpressionNew()).willReturn(null);
    given(pattern.getExpressionEdit()).willReturn("'static expression'");
    applicationContext = mock(ApplicationContext.class);
    setField(pattern, "applicationContext", applicationContext);
    SecurityRolesService securityRolesService = mock(SecurityRolesService.class);
    given(applicationContext.getBean(SecurityRolesService.class)).willReturn(securityRolesService);
    form = new FormComponentState(pattern);
    form.setDataDefinition(dataDefinition);
    form.setTranslationService(translationService);
    form.addFieldEntityIdChangeListener("name", name);
    form.initialize(new JSONObject(ImmutableMap.of("components", new JSONObject())), Locale.ENGLISH);
    new ExpressionServiceImpl().init();
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) StringType(com.qcadoo.model.internal.types.StringType) FieldComponentPattern(com.qcadoo.view.internal.components.FieldComponentPattern) FieldDefinition(com.qcadoo.model.api.FieldDefinition) ViewDefinitionState(com.qcadoo.view.api.ViewDefinitionState) DataDefinition(com.qcadoo.model.api.DataDefinition) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) TranslationService(com.qcadoo.localization.api.TranslationService) FieldComponentState(com.qcadoo.view.internal.components.FieldComponentState) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) SecurityRolesService(com.qcadoo.security.api.SecurityRolesService) Matchers.anyLong(org.mockito.Matchers.anyLong) ExpressionServiceImpl(com.qcadoo.model.internal.ExpressionServiceImpl) Before(org.junit.Before)

Example 14 with FormComponentState

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

the class FormComponentStateTest method shouldRenderFormEntityId.

@Test
public void shouldRenderFormEntityId() throws Exception {
    // given
    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");
    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
    assertEquals(13L, json.getJSONObject(AbstractComponentState.JSON_CONTENT).getLong(FormComponentState.JSON_ENTITY_ID));
}
Also used : FormComponentPattern(com.qcadoo.view.internal.components.form.FormComponentPattern) JSONObject(org.json.JSONObject) TranslationService(com.qcadoo.localization.api.TranslationService) AbstractComponentState(com.qcadoo.view.internal.states.AbstractComponentState) DataDefinition(com.qcadoo.model.api.DataDefinition) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 15 with FormComponentState

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

the class MaintenanceEventContextService method changeContext.

private Entity changeContext(ViewDefinitionState view, Entity maintenanceEventContextEntity) {
    FormComponent formComponent = (FormComponentState) view.getComponentByReference(QcadooViewConstants.L_FORM);
    maintenanceEventContextEntity.setField(MaintenanceEventContextFields.CONFIRMED, false);
    maintenanceEventContextEntity = maintenanceEventContextEntity.getDataDefinition().save(maintenanceEventContextEntity);
    formComponent.setEntity(maintenanceEventContextEntity);
    return maintenanceEventContextEntity;
}
Also used : FormComponent(com.qcadoo.view.api.components.FormComponent) FormComponentState(com.qcadoo.view.internal.components.form.FormComponentState)

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