Search in sources :

Example 46 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class EntityServiceImpl method convertToDatabaseEntity.

@SuppressWarnings("unchecked")
@Override
public Object convertToDatabaseEntity(final InternalDataDefinition dataDefinition, final Entity genericEntity, final Object existingDatabaseEntity) {
    Object databaseEntity = getDatabaseEntity(dataDefinition, genericEntity, existingDatabaseEntity);
    for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
        FieldDefinition fieldDefinition = fieldDefinitionEntry.getValue();
        if (fieldDefinition.isPersistent() && ((InternalFieldDefinition) fieldDefinition).isEnabled()) {
            Object fieldValue = genericEntity.getField(fieldDefinitionEntry.getKey());
            if (fieldDefinition.getType() instanceof ManyToManyType && fieldValue instanceof Iterable) {
                Set<Object> innerDatabaseEntities = Sets.newHashSet();
                for (Entity innerGenericEntity : (Iterable<Entity>) fieldValue) {
                    innerDatabaseEntities.add(getDatabaseEntity((InternalDataDefinition) innerGenericEntity.getDataDefinition(), innerGenericEntity, null));
                }
                setField(databaseEntity, fieldDefinitionEntry.getValue(), innerDatabaseEntities);
            } else {
                setField(databaseEntity, fieldDefinitionEntry.getValue(), fieldValue);
            }
        }
    }
    if (dataDefinition.isPrioritizable() && genericEntity.getField(dataDefinition.getPriorityField().getName()) != null) {
        setField(databaseEntity, dataDefinition.getPriorityField(), genericEntity.getField(dataDefinition.getPriorityField().getName()));
    }
    if (dataDefinition.isActivable()) {
        setActive(databaseEntity, genericEntity.isActive());
    }
    return databaseEntity;
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 47 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class ExpressionServiceImpl method getValue.

@Override
public String getValue(final Entity entity, final List<FieldDefinition> fieldDefinitions, final Locale locale) {
    String value = null;
    if (fieldDefinitions.size() == 1) {
        FieldDefinition field = fieldDefinitions.get(0);
        value = field.getValue(entity.getField(field.getName()), locale);
    } else {
        List<String> values = new ArrayList<String>();
        for (FieldDefinition fieldDefinition : fieldDefinitions) {
            values.add(fieldDefinition.getValue(entity.getField(fieldDefinition.getName()), locale));
        }
        value = StringUtils.join(values, ", ");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Calculating value of fields " + fieldDefinitions + " for " + entity + " : " + value);
    }
    if (StringUtils.isEmpty(value) || "null".equals(value)) {
        return null;
    } else {
        return value;
    }
}
Also used : FieldDefinition(com.qcadoo.model.api.FieldDefinition)

Example 48 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class AbstractEntityWrapperTest method superInit.

@Before
public final void superInit() {
    belongsToFieldDefinition = mock(FieldDefinition.class);
    BelongsToType belongsToType = mock(BelongsToType.class);
    when(belongsToFieldDefinition.getType()).thenReturn(belongsToType);
    belongsToFieldDataDefinition = mock(DataDefinition.class);
    when(belongsToFieldDefinition.getDataDefinition()).thenReturn(belongsToFieldDataDefinition);
    FieldDefinition stringFieldDefinition = mock(FieldDefinition.class);
    when(stringFieldDefinition.isPersistent()).thenReturn(false);
    dataDefinition = mock(DataDefinition.class);
    FieldDefinition booleanFieldDefinition = mock(FieldDefinition.class);
    Map<String, FieldDefinition> fieldsMap = Maps.newHashMap();
    fieldsMap.put(BELONGS_TO_FIELD_NAME, belongsToFieldDefinition);
    fieldsMap.put(STRING_FIELD_NAME, stringFieldDefinition);
    fieldsMap.put(BOOLEAN_FIELD_NAME, booleanFieldDefinition);
    for (Map.Entry<String, FieldDefinition> fieldEntry : fieldsMap.entrySet()) {
        when(dataDefinition.getField(fieldEntry.getKey())).thenReturn(fieldEntry.getValue());
    }
    when(dataDefinition.getFields()).thenReturn(fieldsMap);
}
Also used : BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) DataDefinition(com.qcadoo.model.api.DataDefinition) Map(java.util.Map) Before(org.junit.Before)

Example 49 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class EntityListImplTest method shouldLoadEntities.

@Test
public void shouldLoadEntities() throws Exception {
    // given
    Entity entity = mock(Entity.class);
    List<Entity> entities = Collections.singletonList(entity);
    BelongsToType fieldType = mock(BelongsToType.class);
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getName()).willReturn("field");
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(dataDefinition.getField("hasMany")).willReturn(fieldDefinition);
    given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L)).list().getEntities()).willReturn(entities);
    given(dataAccessService.get(dataDefinition, 1L)).willReturn(entity);
    EntityListImpl list = new EntityListImpl(dataDefinition, "hasMany", 1L);
    // then
    assertEquals(1, list.size());
    assertEquals(entity, list.get(0));
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Example 50 with FieldDefinition

use of com.qcadoo.model.api.FieldDefinition in project qcadoo by qcadoo.

the class EntityListImplTest method shouldReturnCriteriaBuilder.

@Test
public void shouldReturnCriteriaBuilder() throws Exception {
    // given
    BelongsToType fieldType = mock(BelongsToType.class);
    InternalDataDefinition dataDefinition = mock(InternalDataDefinition.class, RETURNS_DEEP_STUBS);
    given(fieldType.getDataDefinition()).willReturn(dataDefinition);
    FieldDefinition fieldDefinition = mock(FieldDefinition.class);
    given(fieldDefinition.getType()).willReturn(fieldType);
    given(fieldDefinition.getName()).willReturn("field");
    given(dataDefinition.getField("hasMany")).willReturn(fieldDefinition);
    SearchCriteriaBuilder searchCriteriaBuilder = mock(SearchCriteriaBuilder.class);
    given(dataDefinition.find().createAlias(fieldDefinition.getName(), fieldDefinition.getName()).add(SearchRestrictions.eq(fieldDefinition.getName() + ".id", 1L))).willReturn(searchCriteriaBuilder);
    EntityList list = new EntityListImpl(dataDefinition, "hasMany", 1L);
    // then
    assertEquals(searchCriteriaBuilder, list.find());
}
Also used : SearchCriteriaBuilder(com.qcadoo.model.api.search.SearchCriteriaBuilder) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) EntityList(com.qcadoo.model.api.EntityList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Test(org.junit.Test)

Aggregations

FieldDefinition (com.qcadoo.model.api.FieldDefinition)142 Test (org.junit.Test)92 DataDefinition (com.qcadoo.model.api.DataDefinition)49 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)48 Entity (com.qcadoo.model.api.Entity)32 BelongsToType (com.qcadoo.model.api.types.BelongsToType)19 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)15 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)13 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)12 TextInputComponentPattern (com.qcadoo.view.internal.components.TextInputComponentPattern)11 HasManyType (com.qcadoo.model.api.types.HasManyType)9 StringType (com.qcadoo.model.internal.types.StringType)9 JSONObject (org.json.JSONObject)8 Map (java.util.Map)7 Matchers.anyString (org.mockito.Matchers.anyString)7 SearchCriterion (com.qcadoo.model.api.search.SearchCriterion)6 DefaultEntity (com.qcadoo.model.internal.DefaultEntity)6 WindowComponentPattern (com.qcadoo.view.internal.components.window.WindowComponentPattern)6 Before (org.junit.Before)6 EntityList (com.qcadoo.model.api.EntityList)5