private void commitTransactions() { for (Field<?> f : fieldToPropertyId.keySet()) { ((Property.Transactional<?>) f.getPropertyDataSource()).commit(); } }
private void rollbackTransactions() { for (Field<?> f : fieldToPropertyId.keySet()) { try { ((Property.Transactional<?>) f.getPropertyDataSource()) .rollback(); } catch (Exception rollbackException) { // FIXME: What to do ? } } }
private void startTransactions() throws CommitException { for (Field<?> f : fieldToPropertyId.keySet()) { Property.Transactional<?> property = (Property.Transactional<?>) f .getPropertyDataSource(); if (property == null) { throw new CommitException( "Property \"" + fieldToPropertyId.get(f) + "\" not bound to datasource."); } property.startTransaction(); } }
/** * Sets the read only state to the given value for all fields with writable * data source. Fields with read only data source will always be set to read * only. * * @param fieldsReadOnly * true to set the fields with writable data source to read only, * false to set them to read write */ public void setReadOnly(boolean fieldsReadOnly) { readOnly = fieldsReadOnly; for (Field<?> field : getFields()) { if (field.getPropertyDataSource() == null || !field.getPropertyDataSource().isReadOnly()) { field.setReadOnly(fieldsReadOnly); } else { field.setReadOnly(true); } } }
/** * Configures a field with the settings set for this FieldBinder. * <p> * By default this updates the buffered, read only and enabled state of the * field. Also adds validators when applicable. Fields with read only data * source are always configured as read only. * <p> * Unlike the default implementation in FieldGroup, MBeanFieldGroup only * makes field read only based on the property's hint, not the opposite. * This way developer can in form code choose to make some fields read only. * * @param field The field to update */ @Override protected void configureField(Field<?> field) { boolean readOnlyStatus = isReadOnly() || field.getPropertyDataSource().isReadOnly(); super.configureField(field); // reset the badly set readOnlyStatus field.setReadOnly(readOnlyStatus); }
/** * Configures a field with the settings set for this FieldBinder. * <p> * By default this updates the buffered, read only and enabled state of the * field. Also adds validators when applicable. Fields with read only data * source are always configured as read only. * * @param field * The field to update */ protected void configureField(Field<?> field) { field.setBuffered(isBuffered()); field.setEnabled(isEnabled()); if (field.getPropertyDataSource().isReadOnly()) { field.setReadOnly(true); } else { field.setReadOnly(isReadOnly()); } }
@Test public void simpleInitializationTest() { // GIVEN fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field<Object> field = fieldFactory.createField(); // THEN assertTrue(TextField.class.isAssignableFrom(field.getClass())); assertEquals(definition, fieldFactory.getFieldDefinition()); assertEquals(false, field.isRequired()); assertEquals("label", field.getCaption()); assertEquals(false, field.getPropertyDataSource().isReadOnly()); assertEquals(true, field.getPropertyDataSource() instanceof TransformedProperty); }
@Test public void changePropertyValueTest() throws Exception { // GIVEN fieldFactory = new TestTextFieldFactory(definition, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); Field<Object> field = fieldFactory.createField(); // WHEN field.setValue("new Value"); // THEN Node res = ((JcrNodeAdapter) baseItem).applyChanges(); assertEquals(true, res.hasProperty(propertyName)); assertEquals("new Value", res.getProperty(propertyName).getString()); assertEquals(PropertyType.STRING, res.getProperty(propertyName).getType()); Property p = baseItem.getItemProperty(propertyName); assertEquals(field.getPropertyDataSource().getValue(), p.getValue()); assertEquals("new Value", p.getValue()); assertEquals(String.class, p.getValue().getClass()); }
@Test public void preselectsFirstSelectedOptionWithConverter() throws Exception { // GIVEN a factory/field backed by a Float property, with a converter explicitly configured baseItem = new PropertysetItem(); ObjectProperty<Float> preTypedProperty = new ObjectProperty<>(null, Float.class); baseItem.addItemProperty("floaty", preTypedProperty); // and some float options (with 2.5 and 3.5 marked selected) for (SelectFieldOptionDefinition optionDefinition : definition.getOptions()) { optionDefinition.setValue(optionDefinition.getValue() + ".5"); } definition.getOptions().get(1).setSelected(true); definition.getOptions().get(2).setSelected(true); definition.setType(null); definition.setConverterClass(StringToFloatConverter.class); initializeSelectFieldFactory(); // WHEN Field field = dialogSelect.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertThat(p.getValue(), equalTo((Object) 2.5f)); }
@Test public void testDefaultValue() throws Exception { // GIVEN baseItem = new JcrNewNodeAdapter(baseNode, baseNode.getPrimaryNodeType().getName()); checkBoxField = new CheckBoxFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); checkBoxField.setComponentProvider(componentProvider); definition.setDefaultValue("true"); // WHEN Field<Boolean> field = checkBoxField.createField(); // THEN assertEquals(true, field.getPropertyDataSource().getValue()); }
@Test public void testGetHiddenFieldPropertyDataSourceWhenItemNodeDoesNotIncludeHiddenProperty() throws Exception { // GIVEN definition.setDefaultValue("test"); factory = new HiddenFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); factory.setComponentProvider(componentProvider); // WHEN Field<?> field = factory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertNotNull(p); assertEquals("test", p.getValue().toString()); assertEquals("test", baseItem.getItemProperty("hiddenProperty").getValue()); assertEquals("test", field.getValue()); }
@Test public void supportsPropertysetItemWithNonExistingProperty() throws Exception { // GIVEN baseItem = new PropertysetItem(); baseItem.addItemProperty("foo", new ObjectProperty<>("fooValue")); ConfiguredFieldDefinition def = createConfiguredFieldDefinition(new ConfiguredFieldDefinition(), "bar"); fieldFactory = new TestTextFieldFactory(def, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field<?> field = fieldFactory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertNotNull(p); }
@Test public void preselectsFirstOptionIfNoneSelectedWithConverter() throws Exception { // GIVEN a factory/field backed by a Float property, with a converter explicitly configured baseItem = new PropertysetItem(); ObjectProperty<Float> preTypedProperty = new ObjectProperty<>(null, Float.class); baseItem.addItemProperty("floaty", preTypedProperty); // and some float options (with none selected) for (SelectFieldOptionDefinition optionDefinition : definition.getOptions()) { optionDefinition.setValue(optionDefinition.getValue() + ".5"); } definition.setType(null); definition.setConverterClass(StringToFloatConverter.class); initializeSelectFieldFactory(); // WHEN Field field = dialogSelect.createField(); // THEN first option is selected Property<?> p = field.getPropertyDataSource(); assertThat(p.getValue(), equalTo((Object) 1.5f)); }
@Test public void supportsBeanItem() throws Exception { // GIVEN baseItem = new BeanItem<>(new TestBean("bar")); ConfiguredFieldDefinition def = createConfiguredFieldDefinition(new ConfiguredFieldDefinition(), "foo"); fieldFactory = new TestTextFieldFactory(def, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field<?> field = fieldFactory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertEquals("bar", p.getValue()); }
@Test public void supportsPropertysetItem() throws Exception { // GIVEN baseItem = new PropertysetItem(); baseItem.addItemProperty("foo", new ObjectProperty<>("fooValue")); ConfiguredFieldDefinition def = createConfiguredFieldDefinition(new ConfiguredFieldDefinition(), "foo"); fieldFactory = new TestTextFieldFactory(def, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field<?> field = fieldFactory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertEquals("fooValue", p.getValue()); }
@Test public void testGetHiddenFieldPropertyDataSourceWhenItemNodeIncludesHiddenProperty() throws Exception { // GIVEN definition.setDefaultValue("test2"); baseItem.addItemProperty("hiddenProperty", new ObjectProperty<>("test1")); factory = new HiddenFieldFactory(definition, baseItem, uiContext, i18NAuthoringSupport); factory.setComponentProvider(componentProvider); // WHEN Field<?> field = factory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertNotNull(p); assertEquals("test1", p.getValue().toString()); assertEquals("test1", baseItem.getItemProperty("hiddenProperty").getValue()); assertEquals("test1", field.getValue()); }
@Test public void supportsBeanItemWithEnumMemberAndDefaultValue() throws Exception { // GIVEN baseItem = new BeanItem<>(new TestBean(null)); ConfiguredFieldDefinition def = createConfiguredFieldDefinition(new ConfiguredFieldDefinition(), "breakfast"); def.setType("info.magnolia.ui.form.field.factory.AbstractFieldFactoryTest$Breakfast"); def.setDefaultValue(Breakfast.BAKED_BEANS.name()); fieldFactory = new TestTextFieldFactory(def, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field field = fieldFactory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertEquals(Breakfast.BAKED_BEANS, p.getValue()); }
@Test public void supportsBeanItemWithEnumMemberViaListToSetTransformer() throws Exception { // GIVEN baseItem = new BeanItem<>(new TestBean(null)); ConfiguredFieldDefinition def = createConfiguredFieldDefinition(new ConfiguredFieldDefinition(), "breakfast"); def.setType("info.magnolia.ui.form.field.factory.AbstractFieldFactoryTest$Breakfast"); def.setDefaultValue(Breakfast.BAKED_BEANS.name()); def.setTransformerClass((Class<? extends Transformer<?>>) (Object) ListToSetTransformer.class); fieldFactory = new TestTextFieldFactory(def, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field field = fieldFactory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertEquals(Breakfast.BAKED_BEANS, p.getValue()); }
@Test public void supportsDefaultValueWithConfiguredConverter() throws Exception { // GIVEN a factory/field backed by a Float property, with a converter explicitly configured baseItem = new PropertysetItem(); ObjectProperty<Float> preTypedProperty = new ObjectProperty<>(null, Float.class); baseItem.addItemProperty("floaty", preTypedProperty); ConfiguredFieldDefinition def = createConfiguredFieldDefinition(new ConfiguredFieldDefinition(), "floaty"); def.setType(null); def.setDefaultValue("0.86"); def.setConverterClass(StringToFloatConverter.class); fieldFactory = new TestTextFieldFactory(def, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field<?> field = fieldFactory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertEquals(0.86f, p.getValue()); }
@Test public void supportsEnumPropertyAndDefaultValue() throws Exception { // GIVEN baseItem = new PropertysetItem(); ObjectProperty<Breakfast> preTypedProperty = new ObjectProperty<>(Breakfast.EGGS_AND_BACON); preTypedProperty.setValue(null); // resetting actual value, so that defaultValue mechanism kicks in baseItem.addItemProperty("breakfast", preTypedProperty); ConfiguredFieldDefinition def = createConfiguredFieldDefinition(new ConfiguredFieldDefinition(), "breakfast"); def.setType("info.magnolia.ui.form.field.factory.AbstractFieldFactoryTest$Breakfast"); def.setDefaultValue(Breakfast.BAKED_BEANS.name()); fieldFactory = new TestTextFieldFactory(def, baseItem, null, i18NAuthoringSupport); fieldFactory.setComponentProvider(this.componentProvider); // WHEN Field field = fieldFactory.createField(); // THEN Property<?> p = field.getPropertyDataSource(); assertEquals(Breakfast.BAKED_BEANS, p.getValue()); }