Search in sources :

Example 11 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityServiceImpl method getTreeField.

private Object getTreeField(final Object databaseEntity, final FieldDefinition fieldDefinition) {
    Long parentId = getId(databaseEntity);
    TreeType treeFieldType = (TreeType) fieldDefinition.getType();
    InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) treeFieldType.getDataDefinition();
    return new EntityTreeImpl(referencedDataDefinition, treeFieldType.getJoinFieldName(), parentId);
}
Also used : TreeType(com.qcadoo.model.api.types.TreeType) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 12 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityServiceImpl method getManyToManyField.

private Object getManyToManyField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Entity performer) {
    ManyToManyType manyToManyType = (ManyToManyType) fieldDefinition.getType();
    InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) manyToManyType.getDataDefinition();
    if (manyToManyType.isLazyLoading()) {
        return new ProxyList(fieldDefinition, getId(databaseEntity), performer);
    } else {
        @SuppressWarnings("unchecked") Set<Object> databaseEntities = (Set<Object>) getPrimitiveField(databaseEntity, fieldDefinition);
        if (databaseEntities == null) {
            return null;
        }
        List<Entity> genericEntities = Lists.newArrayList();
        @SuppressWarnings("unchecked") final Iterable<Object> fieldValues = (Iterable<Object>) getField(databaseEntity, fieldDefinition.getName());
        for (Object innerDatabaseEntity : fieldValues) {
            Entity innerEntity = null;
            Long id = getId(innerDatabaseEntity);
            if (id == null) {
                innerEntity = convertToGenericEntity(referencedDataDefinition, innerDatabaseEntity);
            } else {
                innerEntity = new ProxyEntity(referencedDataDefinition, id);
            }
            genericEntities.add(innerEntity);
        }
        return genericEntities;
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) Set(java.util.Set) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 13 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition in project qcadoo by qcadoo.

the class EntityServiceImpl method getBelongsToField.

private Object getBelongsToField(final Object databaseEntity, final FieldDefinition fieldDefinition, final Entity performer) {
    BelongsToType belongsToFieldType = (BelongsToType) fieldDefinition.getType();
    InternalDataDefinition referencedDataDefinition = (InternalDataDefinition) belongsToFieldType.getDataDefinition();
    Object value = getField(databaseEntity, fieldDefinition.getName());
    if (value == null) {
        return null;
    }
    if (performer != null && referencedDataDefinition.equals(performer.getDataDefinition()) && performer.getId() != null && performer.getId().equals(getId(value))) {
        return performer;
    }
    if (belongsToFieldType.isLazyLoading()) {
        Long id = null;
        if (value instanceof HibernateProxy) {
            id = (Long) ((HibernateProxy) value).getHibernateLazyInitializer().getIdentifier();
        } else {
            id = getId(getField(databaseEntity, fieldDefinition.getName()));
        }
        if (id == null) {
            return null;
        }
        return new ProxyEntity(referencedDataDefinition, id);
    } else {
        Entity currentPerformer = performer;
        if (performer == null || performer.getId() == null && referencedDataDefinition.equals(performer.getDataDefinition())) {
            currentPerformer = new ProxyEntity(referencedDataDefinition, getId(value));
        }
        return convertToGenericEntity(referencedDataDefinition, value, currentPerformer);
    }
}
Also used : Entity(com.qcadoo.model.api.Entity) BelongsToType(com.qcadoo.model.api.types.BelongsToType) HibernateProxy(org.hibernate.proxy.HibernateProxy) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 14 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition 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 15 with InternalDataDefinition

use of com.qcadoo.model.internal.api.InternalDataDefinition 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)

Aggregations

InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)28 Entity (com.qcadoo.model.api.Entity)16 FieldDefinition (com.qcadoo.model.api.FieldDefinition)12 BelongsToType (com.qcadoo.model.api.types.BelongsToType)10 Test (org.junit.Test)8 Monitorable (com.qcadoo.model.api.aop.Monitorable)7 Transactional (org.springframework.transaction.annotation.Transactional)7 ArrayList (java.util.ArrayList)6 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)5 EntityList (com.qcadoo.model.api.EntityList)3 ManyToManyType (com.qcadoo.model.api.types.ManyToManyType)3 TreeType (com.qcadoo.model.api.types.TreeType)3 EntityOpResult (com.qcadoo.model.api.EntityOpResult)2 SearchCriteriaBuilder (com.qcadoo.model.api.search.SearchCriteriaBuilder)2 HasManyType (com.qcadoo.model.api.types.HasManyType)2 TranslationService (com.qcadoo.localization.api.TranslationService)1 CustomHook (com.qcadoo.model.CustomHook)1 CopyException (com.qcadoo.model.api.CopyException)1 DataDefinition (com.qcadoo.model.api.DataDefinition)1 Cascadeable (com.qcadoo.model.api.types.Cascadeable)1