use of com.qcadoo.model.api.types.FieldType in project qcadoo by qcadoo.
the class ModelXmlToDefinitionConverterImpl method getFieldDefinition.
private FieldDefinition getFieldDefinition(final XMLStreamReader reader, final DataDefinitionImpl dataDefinition, final FieldsTag fieldTag) throws XMLStreamException, HookInitializationException, ModelXmlParsingException {
String fieldType = reader.getLocalName();
String name = getStringAttribute(reader, "name");
FieldDefinitionImpl fieldDefinition = new FieldDefinitionImpl(dataDefinition, name);
fieldDefinition.withReadOnly(getBooleanAttribute(reader, "readonly", false));
fieldDefinition.withDefaultValue(getStringAttribute(reader, "default"));
fieldDefinition.setPersistent(getBooleanAttribute(reader, "persistent", true));
fieldDefinition.setExpression(getStringAttribute(reader, "expression"));
FieldType type = getFieldType(reader, dataDefinition, name, fieldTag, fieldType);
fieldDefinition.withType(type);
if (getBooleanAttribute(reader, "required", false)) {
fieldDefinition.withValidator(getValidatorDefinition(reader, new RequiredValidator()));
}
if (getBooleanAttribute(reader, "unique", false)) {
if (type.isCopyable() && !fieldDefinition.canBeBothCopyableAndUnique()) {
String message = String.format("Unique field can not have the copyable attribute set to true. Add 'copyable=\"false\"' to #%s_%s.%s to fix it.", dataDefinition.getPluginIdentifier(), dataDefinition.getName(), name);
throw new IllegalStateException(message);
}
fieldDefinition.withValidator(getValidatorDefinition(reader, new UniqueValidator()));
}
parseFieldValidators(reader, fieldType, fieldDefinition).forEach(fieldDefinition::withValidator);
fieldDefinition.withMissingDefaultValidators();
return fieldDefinition;
}
use of com.qcadoo.model.api.types.FieldType in project qcadoo by qcadoo.
the class GridComponentStateTest method shouldGetValueUsingExpression.
@Test
public void shouldGetValueUsingExpression() throws Exception {
// given
FieldDefinition nameFieldDefinition = mock(FieldDefinition.class);
given(productDataDefinition.getField("name")).willReturn(nameFieldDefinition);
FieldType nameFieldType = mock(FieldType.class);
given(nameFieldDefinition.getType()).willReturn(nameFieldType);
given(nameFieldType.toString(anyString(), any(Locale.class))).willAnswer(invocation -> Objects.toString(invocation.getArguments()[0]));
GridComponentColumn column = new GridComponentColumn("name");
column.setExpression("#name + ' ' + #id");
Entity entity = new DefaultEntity(productDataDefinition, 13L, ImmutableMap.of("name", (Object) "John"));
// when
String value = column.getValue(entity, Locale.ENGLISH);
// then
assertEquals("John 13", value);
}
use of com.qcadoo.model.api.types.FieldType in project qcadoo by qcadoo.
the class FieldTypeFactoryTest method shouldReturnBelongToType.
@Test
public void shouldReturnBelongToType() throws Exception {
// when
FieldType fieldType = new BelongsToEntityType("parent", "entity", dataDefinitionService, false, true);
// then
assertEquals(Object.class, fieldType.getType());
assertTrue(fieldType.toObject(fieldDefinition, new DefaultEntity(dataDefinition)).isValid());
}
use of com.qcadoo.model.api.types.FieldType in project qcadoo by qcadoo.
the class FieldTypeFactoryTest method shouldReturnIntegerType.
@Test
public void shouldReturnIntegerType() throws Exception {
// when
FieldType fieldType = new IntegerType();
// then
assertEquals(Integer.class, fieldType.getType());
assertTrue(fieldType.toObject(fieldDefinition, 1).isValid());
assertTrue(fieldType.toObject(fieldDefinition, 1234567890).isValid());
}
use of com.qcadoo.model.api.types.FieldType in project qcadoo by qcadoo.
the class FieldTypeFactoryTest method shouldReturnTextType.
@Test
public void shouldReturnTextType() throws Exception {
// when
FieldType fieldType = new TextType();
// then
assertEquals(String.class, fieldType.getType());
assertTrue(fieldType.toObject(fieldDefinition, "test").isValid());
assertTrue(fieldType.toObject(fieldDefinition, StringUtils.repeat("a", 2048)).isValid());
assertTrue(fieldType.toObject(fieldDefinition, StringUtils.repeat("a", 2049)).isValid());
}
Aggregations