Search in sources :

Example 1 with ManyToManyType

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

the class DataAccessServiceImpl method performCascadeStrategy.

private EntityOpResult performCascadeStrategy(final Entity entity, final FieldType fieldType, final Collection<Entity> children, final Set<EntitySignature> traversedEntities) {
    if (children == null || children.isEmpty()) {
        return EntityOpResult.successfull();
    }
    boolean isManyToManyType = fieldType instanceof ManyToManyType;
    InternalDataDefinition childDataDefinition = (InternalDataDefinition) ((DataDefinitionHolder) fieldType).getDataDefinition();
    Cascadeable.Cascade cascade = ((Cascadeable) fieldType).getCascade();
    if (Cascadeable.Cascade.NULLIFY.equals(cascade)) {
        if (!isManyToManyType) {
            return performCascadeNullification(childDataDefinition, children, entity, fieldType);
        }
        return EntityOpResult.successfull();
    } else if (Cascadeable.Cascade.DELETE.equals(cascade)) {
        return performCascadeDelete(childDataDefinition, children, isManyToManyType, traversedEntities);
    } else {
        throw new IllegalArgumentException(String.format("Unsupported cascade value '%s'", cascade));
    }
}
Also used : Cascadeable(com.qcadoo.model.api.types.Cascadeable) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 2 with ManyToManyType

use of com.qcadoo.model.api.types.ManyToManyType 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 3 with ManyToManyType

use of com.qcadoo.model.api.types.ManyToManyType 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 4 with ManyToManyType

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

the class DefaultEntityTest method init.

@Before
public final void init() {
    belongsToFieldDefinition = mock(FieldDefinition.class);
    final BelongsToType belongsToType = mock(BelongsToType.class);
    when(belongsToFieldDefinition.getType()).thenReturn(belongsToType);
    belongsToFieldDataDefinition = mock(DataDefinition.class);
    when(belongsToFieldDefinition.getDataDefinition()).thenReturn(belongsToFieldDataDefinition);
    decimalFieldDefinition = mock(FieldDefinition.class);
    final DecimalType decimalType = new DecimalType();
    when(decimalFieldDefinition.getType()).thenReturn(decimalType);
    integerFieldDefinition = mock(FieldDefinition.class);
    final IntegerType integerType = new IntegerType();
    when(integerFieldDefinition.getType()).thenReturn(integerType);
    manyToManyFieldDefinition = mock(FieldDefinition.class);
    final ManyToManyType manyToManyType = mock(ManyToManyType.class);
    when(manyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
    manyToManyDataDefinition = mock(DataDefinition.class);
    when(manyToManyFieldDefinition.getDataDefinition()).thenReturn(manyToManyDataDefinition);
    secondManyToManyFieldDefinition = mock(FieldDefinition.class);
    when(secondManyToManyFieldDefinition.getType()).thenReturn(manyToManyType);
    secondManyToManyDataDefinition = mock(DataDefinition.class);
    when(secondManyToManyFieldDefinition.getDataDefinition()).thenReturn(secondManyToManyDataDefinition);
    hasManyFieldDefinition = mock(FieldDefinition.class);
    final HasManyType hasManyType = mock(HasManyType.class);
    when(hasManyType.getJoinFieldName()).thenReturn(BELONGS_TO_FIELD_NAME);
    when(hasManyFieldDefinition.getType()).thenReturn(hasManyType);
    hasManyDataDefinition = mock(DataDefinition.class);
    when(hasManyFieldDefinition.getDataDefinition()).thenReturn(hasManyDataDefinition);
    stringFieldDefinition = mock(FieldDefinition.class);
    when(stringFieldDefinition.isPersistent()).thenReturn(false);
    dataDefinition = mock(DataDefinition.class);
    booleanFieldDefinition = mock(FieldDefinition.class);
    defaultEntity = new DefaultEntity(dataDefinition);
    final 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);
    fieldsMap.put(DECIMAL_FIELD_NAME, decimalFieldDefinition);
    fieldsMap.put(INTEGER_FIELD_NAME, integerFieldDefinition);
    fieldsMap.put(MANY_TO_MANY_FIELD_NAME, manyToManyFieldDefinition);
    fieldsMap.put(SECOND_MANY_TO_MANY_FIELD_NAME, secondManyToManyFieldDefinition);
    fieldsMap.put(HAS_MANY_FIELD_NAME, hasManyFieldDefinition);
    for (Map.Entry<String, FieldDefinition> fieldEntry : fieldsMap.entrySet()) {
        when(dataDefinition.getField(fieldEntry.getKey())).thenReturn(fieldEntry.getValue());
    }
    when(dataDefinition.getFields()).thenReturn(fieldsMap);
}
Also used : IntegerType(com.qcadoo.model.internal.types.IntegerType) HasManyType(com.qcadoo.model.api.types.HasManyType) BelongsToType(com.qcadoo.model.api.types.BelongsToType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) ManyToManyType(com.qcadoo.model.api.types.ManyToManyType) DecimalType(com.qcadoo.model.internal.types.DecimalType) DataDefinition(com.qcadoo.model.api.DataDefinition) Map(java.util.Map) Before(org.junit.Before)

Aggregations

ManyToManyType (com.qcadoo.model.api.types.ManyToManyType)4 InternalDataDefinition (com.qcadoo.model.internal.api.InternalDataDefinition)3 Entity (com.qcadoo.model.api.Entity)2 FieldDefinition (com.qcadoo.model.api.FieldDefinition)2 DataDefinition (com.qcadoo.model.api.DataDefinition)1 BelongsToType (com.qcadoo.model.api.types.BelongsToType)1 Cascadeable (com.qcadoo.model.api.types.Cascadeable)1 HasManyType (com.qcadoo.model.api.types.HasManyType)1 InternalFieldDefinition (com.qcadoo.model.internal.api.InternalFieldDefinition)1 DecimalType (com.qcadoo.model.internal.types.DecimalType)1 IntegerType (com.qcadoo.model.internal.types.IntegerType)1 Map (java.util.Map)1 Set (java.util.Set)1 Before (org.junit.Before)1