Search in sources :

Example 1 with FieldType

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;
}
Also used : FieldDefinitionImpl(com.qcadoo.model.internal.FieldDefinitionImpl) FieldType(com.qcadoo.model.api.types.FieldType)

Example 2 with FieldType

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);
}
Also used : Locale(java.util.Locale) Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) GridComponentColumn(com.qcadoo.view.internal.components.grid.GridComponentColumn) JSONObject(org.json.JSONObject) Matchers.anyString(org.mockito.Matchers.anyString) FieldType(com.qcadoo.model.api.types.FieldType) AbstractStateTest(com.qcadoo.view.internal.states.AbstractStateTest) Test(org.junit.Test)

Example 3 with FieldType

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());
}
Also used : DefaultEntity(com.qcadoo.model.internal.DefaultEntity) BelongsToEntityType(com.qcadoo.model.internal.types.BelongsToEntityType) FieldType(com.qcadoo.model.api.types.FieldType) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 4 with FieldType

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());
}
Also used : IntegerType(com.qcadoo.model.internal.types.IntegerType) FieldType(com.qcadoo.model.api.types.FieldType) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Example 5 with FieldType

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());
}
Also used : FieldType(com.qcadoo.model.api.types.FieldType) TextType(com.qcadoo.model.internal.types.TextType) DataAccessTest(com.qcadoo.model.internal.DataAccessTest) Test(org.junit.Test)

Aggregations

FieldType (com.qcadoo.model.api.types.FieldType)19 Test (org.junit.Test)15 DataAccessTest (com.qcadoo.model.internal.DataAccessTest)11 FieldDefinition (com.qcadoo.model.api.FieldDefinition)4 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)3 DataDefinition (com.qcadoo.model.api.DataDefinition)2 Entity (com.qcadoo.model.api.Entity)2 BelongsToType (com.qcadoo.model.api.types.BelongsToType)2 FieldDefinitionImpl (com.qcadoo.model.internal.FieldDefinitionImpl)2 Date (java.util.Date)2 HasManyType (com.qcadoo.model.api.types.HasManyType)1 BelongsToEntityType (com.qcadoo.model.internal.types.BelongsToEntityType)1 BooleanType (com.qcadoo.model.internal.types.BooleanType)1 DateTimeType (com.qcadoo.model.internal.types.DateTimeType)1 DateType (com.qcadoo.model.internal.types.DateType)1 DecimalType (com.qcadoo.model.internal.types.DecimalType)1 IntegerType (com.qcadoo.model.internal.types.IntegerType)1 ManyToManyEntitiesType (com.qcadoo.model.internal.types.ManyToManyEntitiesType)1 PasswordType (com.qcadoo.model.internal.types.PasswordType)1 PriorityType (com.qcadoo.model.internal.types.PriorityType)1