Search in sources :

Example 16 with BelongsToType

use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.

the class EntityTreeImplTest 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.getType()).willReturn(fieldType);
    given(fieldDefinition.getName()).willReturn("field");
    given(dataDefinition.getField("tree")).willReturn(fieldDefinition);
    given(dataDefinition.find().add(SearchRestrictions.belongsTo("field", dataDefinition, 1L)).addOrder(SearchOrders.asc("priority")).list().getEntities()).willReturn(entities);
    EntityTreeImpl tree = new EntityTreeImpl(dataDefinition, "tree", 1L);
    // then
    assertEquals(1, tree.size());
    assertEquals(entity, tree.get(0));
    assertEquals(entity, getField(tree.getRoot(), "entity"));
}
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 17 with BelongsToType

use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.

the class EntityTreeImplTest 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("tree")).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, "tree", 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)

Example 18 with BelongsToType

use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.

the class EntityServiceImpl method convertToGenericEntity.

public Entity convertToGenericEntity(final InternalDataDefinition dataDefinition, final Object databaseEntity, final Entity performer) {
    Entity genericEntity = null;
    if (databaseEntity instanceof Object[]) {
        genericEntity = dataDefinition.create();
        Object[] databaseArray = (Object[]) databaseEntity;
        List<String> fields = new ArrayList<String>(dataDefinition.getFields().keySet());
        for (int i = 0; i < fields.size(); i++) {
            if (dataDefinition.getField(fields.get(i)).getType() instanceof BelongsToType) {
                InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) ((BelongsToType) dataDefinition.getField(fields.get(i)).getType()).getDataDefinition();
                genericEntity.setField(fields.get(i), convertToGenericEntity(referencedDataDefinition, databaseArray[i]));
            } else {
                genericEntity.setField(fields.get(i), databaseArray[i]);
            }
        }
    } else if (databaseEntity.getClass().getName().startsWith("com.qcadoo.model.beans")) {
        genericEntity = dataDefinition.create(getId(databaseEntity));
        if (dataDefinition.isActivable()) {
            genericEntity.setActive(getActive(databaseEntity));
        }
        for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
            if (fieldDefinitionEntry.getValue().isPersistent() && ((InternalFieldDefinition) fieldDefinitionEntry.getValue()).isEnabled()) {
                Entity currentPerformer = performer;
                if (currentPerformer == null) {
                    currentPerformer = genericEntity;
                }
                genericEntity.setField(fieldDefinitionEntry.getKey(), getField(databaseEntity, fieldDefinitionEntry.getValue(), currentPerformer));
            }
        }
        if (dataDefinition.isPrioritizable()) {
            genericEntity.setField(dataDefinition.getPriorityField().getName(), getField(databaseEntity, dataDefinition.getPriorityField()));
        }
        for (Entry<String, FieldDefinition> fieldDefinitionEntry : dataDefinition.getFields().entrySet()) {
            if (fieldDefinitionEntry.getValue().getExpression() != null && ((InternalFieldDefinition) fieldDefinitionEntry.getValue()).isEnabled()) {
                genericEntity.setField(fieldDefinitionEntry.getKey(), expressionService.getValue(genericEntity, fieldDefinitionEntry.getValue().getExpression(), Locale.ENGLISH));
            }
        }
        dataDefinition.callViewHook(genericEntity);
    } else {
        genericEntity = new DefaultEntity(dataDefinition);
        genericEntity.setField(dataDefinition.getFields().keySet().iterator().next(), databaseEntity);
    }
    return genericEntity;
}
Also used : Entity(com.qcadoo.model.api.Entity) Entry(java.util.Map.Entry) BelongsToType(com.qcadoo.model.api.types.BelongsToType) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) ArrayList(java.util.ArrayList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 19 with BelongsToType

use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.

the class ExpressionServiceImpl method getValuesForEntity.

private Map<String, Object> getValuesForEntity(final Entity entity, final Locale locale, final int level) {
    if (entity == null) {
        return null;
    }
    Map<String, Object> values = new HashMap<>();
    values.put("id", entity.getId());
    if (level == 0) {
        values.putAll(entity.getFields());
        return values;
    }
    for (Map.Entry<String, Object> entry : entity.getFields().entrySet()) {
        if (entry.getValue() instanceof Collection) {
            values.put(entry.getKey(), entry.getValue());
        } else {
            FieldType type = entity.getDataDefinition().getField(entry.getKey()).getType();
            if (type instanceof BelongsToType) {
                Entity belongsToEntity = getBelongsToEntity(entry.getValue(), (BelongsToType) type);
                values.put(entry.getKey(), getValuesForEntity(belongsToEntity, locale, level - 1));
            } else {
                String value = null;
                if (entry.getValue() != null) {
                    value = type.toString(entry.getValue(), locale);
                }
                values.put(entry.getKey(), value);
            }
        }
    }
    return values;
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldType(com.qcadoo.model.api.types.FieldType)

Example 20 with BelongsToType

use of com.qcadoo.model.api.types.BelongsToType in project qcadoo by qcadoo.

the class ExpressionUtilTest method shouldGenerateValueOfTheBelongsToColumn.

@Test
public void shouldGenerateValueOfTheBelongsToColumn() throws Exception {
    // given
    DataDefinition dataDefinition = mock(DataDefinition.class, RETURNS_DEEP_STUBS);
    Entity product = new DefaultEntity(dataDefinition, 1L);
    product.setField("name", "P1");
    BelongsToType belongsToType = mock(BelongsToType.class);
    given(dataDefinition.getField(eq("name")).getType().toString(eq("P1"), eq(Locale.ENGLISH))).willReturn("P1");
    given(dataDefinition.getField(eq("product")).getType()).willReturn(belongsToType);
    given(dataDefinition.getField(eq("product")).getType()).willReturn(belongsToType);
    given(belongsToType.getDataDefinition()).willReturn(dataDefinition);
    Entity entity = new DefaultEntity(dataDefinition, 1L);
    entity.setField("product", product);
    // when
    String value = expressionService.getValue(entity, "#product['name']", Locale.ENGLISH);
    // then
    assertEquals("P1", value);
}
Also used : Entity(com.qcadoo.model.api.Entity) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) DefaultEntity(com.qcadoo.model.internal.DefaultEntity) DataDefinition(com.qcadoo.model.api.DataDefinition) Test(org.junit.Test)

Aggregations

BelongsToType (com.qcadoo.model.api.types.BelongsToType)26 FieldDefinition (com.qcadoo.model.api.FieldDefinition)19 Test (org.junit.Test)14 DataDefinition (com.qcadoo.model.api.DataDefinition)13 Entity (com.qcadoo.model.api.Entity)12 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)10 HasManyType (com.qcadoo.model.api.types.HasManyType)5 InternalViewDefinition (com.qcadoo.view.internal.api.InternalViewDefinition)5 TextInputComponentPattern (com.qcadoo.view.internal.components.TextInputComponentPattern)5 Before (org.junit.Before)4 EntityList (com.qcadoo.model.api.EntityList)2 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)2 FieldType (com.qcadoo.model.api.types.FieldType)2 ComponentDefinition (com.qcadoo.view.internal.ComponentDefinition)2 ComponentOption (com.qcadoo.view.internal.ComponentOption)2 GridComponentPattern (com.qcadoo.view.internal.components.grid.GridComponentPattern)2 WindowComponentPattern (com.qcadoo.view.internal.components.window.WindowComponentPattern)2 AbstractComponentPattern (com.qcadoo.view.internal.patterns.AbstractComponentPattern)2 AbstractPatternTest (com.qcadoo.view.internal.patterns.AbstractPatternTest)2 Map (java.util.Map)2