Search in sources :

Example 21 with InternalDataDefinition

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

the class DataAccessServiceImpl method move.

@Transactional
@Monitorable
private void move(final InternalDataDefinition dataDefinition, final Long entityId, final int position, final int offset) {
    InternalDataDefinition dataDefinitionToMove = getDataDefinitionByMasterModel(dataDefinition);
    checkNotNull(dataDefinitionToMove, L_DATA_DEFINITION_MUST_BE_GIVEN);
    checkState(dataDefinitionToMove.isPrioritizable(), "Entity must be prioritizable");
    checkState(dataDefinitionToMove.isEnabled(), L_DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
    checkNotNull(entityId, "EntityId must be given");
    Object databaseEntity = getDatabaseEntity(dataDefinitionToMove, entityId);
    if (databaseEntity == null) {
        logEntityInfo(dataDefinitionToMove, entityId, "hasn't been prioritized, because it doesn't exist");
        return;
    }
    priorityService.move(dataDefinitionToMove, databaseEntity, position, offset);
    logEntityInfo(dataDefinitionToMove, entityId, "has been prioritized");
}
Also used : InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Monitorable(com.qcadoo.model.api.aop.Monitorable) Transactional(org.springframework.transaction.annotation.Transactional)

Example 22 with InternalDataDefinition

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

the class DataAccessServiceImpl method activate.

@Override
@Transactional
@Monitorable
public List<Entity> activate(final InternalDataDefinition dataDefinition, final Long... entityIds) {
    if (!dataDefinition.isActivable()) {
        return Collections.emptyList();
    }
    InternalDataDefinition dataDefinitionToActivate = getDataDefinitionByMasterModel(dataDefinition);
    List<Entity> activatedEntities = new ArrayList<Entity>();
    for (Long entityId : entityIds) {
        Entity entity = get(dataDefinitionToActivate, entityId);
        if (entity == null) {
            throw new IllegalStateException("Cannot activate " + entityId);
        }
        if (!entity.isActive()) {
            entity.setActive(true);
            entity = save(dataDefinitionToActivate, entity);
            if (!entity.isValid()) {
                throw new IllegalStateException("Cannot activate " + entity);
            }
            LOG.debug(entity + " has been activated");
            activatedEntities.add(entity);
        }
    }
    return activatedEntities;
}
Also used : Entity(com.qcadoo.model.api.Entity) ArrayList(java.util.ArrayList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Monitorable(com.qcadoo.model.api.aop.Monitorable) Transactional(org.springframework.transaction.annotation.Transactional)

Example 23 with InternalDataDefinition

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

the class DataAccessServiceImpl method performSave.

@SuppressWarnings("unchecked")
private Entity performSave(final InternalDataDefinition dataDefinition, final Entity genericEntity, final Set<Entity> alreadySavedEntities, final Set<Entity> newlySavedEntities, boolean fast) {
    checkNotNull(dataDefinition, L_DATA_DEFINITION_MUST_BE_GIVEN);
    checkState(dataDefinition.isEnabled(), L_DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
    checkNotNull(genericEntity, "Entity must be given");
    if (alreadySavedEntities.contains(genericEntity)) {
        return genericEntity;
    }
    Entity genericEntityToSave = genericEntity.copy();
    Object existingDatabaseEntity = getExistingDatabaseEntity(dataDefinition, genericEntity);
    Entity existingGenericEntity = null;
    if (existingDatabaseEntity != null) {
        existingGenericEntity = entityService.convertToGenericEntity(dataDefinition, existingDatabaseEntity);
    }
    if (!fast) {
        validationService.validateGenericEntity(dataDefinition, genericEntity, existingGenericEntity);
    }
    if (!genericEntity.isValid()) {
        copyValidationErrors(dataDefinition, genericEntityToSave, genericEntity);
        if (existingGenericEntity != null) {
            copyMissingFields(genericEntityToSave, existingGenericEntity);
        }
        logValidationErrors(genericEntityToSave);
        return genericEntityToSave;
    }
    Object databaseEntity = entityService.convertToDatabaseEntity(dataDefinition, genericEntity, existingDatabaseEntity);
    if (genericEntity.getId() == null) {
        priorityService.prioritizeEntity(dataDefinition, databaseEntity);
    }
    saveDatabaseEntity(dataDefinition, databaseEntity);
    if (dataDefinition.isVersionable()) {
        hibernateService.getCurrentSession().flush();
    }
    Entity savedEntity = entityService.convertToGenericEntity(dataDefinition, databaseEntity);
    copyGlobalMessages(dataDefinition, savedEntity, genericEntity);
    for (Entry<String, FieldDefinition> fieldEntry : dataDefinition.getFields().entrySet()) {
        if (fieldEntry.getValue().getType() instanceof HasManyType) {
            List<Entity> entities = (List<Entity>) genericEntity.getField(fieldEntry.getKey());
            HasManyType hasManyType = (HasManyType) fieldEntry.getValue().getType();
            if (entities == null || entities instanceof EntityListImpl) {
                savedEntity.setField(fieldEntry.getKey(), entities);
                continue;
            }
            List<Entity> savedEntities = saveHasManyEntities(alreadySavedEntities, newlySavedEntities, hasManyType.getJoinFieldName(), savedEntity, entities, (InternalDataDefinition) hasManyType.getDataDefinition());
            EntityList dbEntities = savedEntity.getHasManyField(fieldEntry.getKey());
            EntityOpResult results = removeOrphans(hasManyType, findOrphans(savedEntities, dbEntities));
            if (!results.isSuccessfull()) {
                // #TODO MAKU
                copyValidationErrors(dataDefinition, savedEntity, results.getMessagesHolder());
                savedEntity.setField(fieldEntry.getKey(), existingGenericEntity.getField(fieldEntry.getKey()));
                return savedEntity;
            }
            savedEntity.setField(fieldEntry.getKey(), savedEntities);
        } else if (fieldEntry.getValue().getType() instanceof TreeType) {
            List<Entity> entities = (List<Entity>) genericEntity.getField(fieldEntry.getKey());
            if (entities == null || entities instanceof EntityTreeImpl) {
                savedEntity.setField(fieldEntry.getKey(), entities);
                continue;
            }
            TreeType treeType = (TreeType) fieldEntry.getValue().getType();
            List<Entity> savedEntities = saveTreeEntities(alreadySavedEntities, newlySavedEntities, treeType.getJoinFieldName(), savedEntity, entities, (InternalDataDefinition) treeType.getDataDefinition(), null);
            savedEntity.setField(fieldEntry.getKey(), savedEntities);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(savedEntity + " has been saved");
    }
    alreadySavedEntities.add(savedEntity);
    if (genericEntity.getId() == null && savedEntity.getId() != null) {
        newlySavedEntities.add(savedEntity);
    }
    return savedEntity;
}
Also used : Entity(com.qcadoo.model.api.Entity) TreeType(com.qcadoo.model.api.types.TreeType) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) EntityList(com.qcadoo.model.api.EntityList) EntityOpResult(com.qcadoo.model.api.EntityOpResult) HasManyType(com.qcadoo.model.api.types.HasManyType) EntityList(com.qcadoo.model.api.EntityList) List(java.util.List) ArrayList(java.util.ArrayList) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 24 with InternalDataDefinition

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

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

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