Search in sources :

Example 6 with InternalDataDefinition

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

the class DataAccessServiceImpl method find.

@Override
@Transactional(readOnly = true)
@Monitorable
public SearchResult find(final SearchQuery searchQuery) {
    checkArgument(searchQuery != null, "SearchCriteria must be given");
    Query query = searchQuery.createQuery(hibernateService.getCurrentSession());
    searchQuery.addParameters(query);
    searchQuery.addCacheable(query);
    int totalNumberOfEntities = -1;
    if (searchQuery.hasFirstAndMaxResults()) {
        totalNumberOfEntities = hibernateService.list(query).size();
        searchQuery.addFirstAndMaxResults(query);
    }
    if (totalNumberOfEntities == 0) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("There is no entity matching criteria " + searchQuery);
        }
        return getResultSet(null, totalNumberOfEntities, Collections.emptyList());
    }
    List<?> results = hibernateService.list(query);
    if (totalNumberOfEntities == -1) {
        totalNumberOfEntities = results.size();
        if (totalNumberOfEntities == 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("There is no entity matching criteria " + searchQuery);
            }
            return getResultSet(null, totalNumberOfEntities, Collections.emptyList());
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("There are " + totalNumberOfEntities + " entities matching criteria " + searchQuery);
    }
    InternalDataDefinition searchQueryDataDefinition = (InternalDataDefinition) searchQuery.getDataDefinition();
    if (searchQueryDataDefinition == null) {
        searchQueryDataDefinition = hibernateService.resolveDataDefinition(query);
    }
    return getResultSet(searchQueryDataDefinition, totalNumberOfEntities, results);
}
Also used : Query(org.hibernate.Query) SearchQuery(com.qcadoo.model.internal.search.SearchQuery) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Monitorable(com.qcadoo.model.api.aop.Monitorable) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with InternalDataDefinition

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

the class DataAccessServiceImpl method copy.

public Entity copy(final InternalDataDefinition dataDefinition, final Entity sourceEntity) {
    InternalDataDefinition dataDefinitionToCopy = getDataDefinitionByMasterModel(dataDefinition);
    Entity targetEntity = dataDefinitionToCopy.create();
    for (Entry<String, FieldDefinition> fieldEntry : dataDefinitionToCopy.getFields().entrySet()) {
        FieldDefinition fieldDefinition = fieldEntry.getValue();
        String fieldName = fieldEntry.getKey();
        boolean copy = fieldDefinition.getType().isCopyable();
        if (copy) {
            targetEntity.setField(fieldName, getCopyValueOfSimpleField(sourceEntity, dataDefinitionToCopy, fieldName));
        }
    }
    if (!dataDefinitionToCopy.callCopyHook(targetEntity)) {
        return null;
    }
    for (String fieldName : dataDefinitionToCopy.getFields().keySet()) {
        copyHasManyField(sourceEntity, targetEntity, dataDefinitionToCopy, fieldName);
    }
    for (String fieldName : dataDefinitionToCopy.getFields().keySet()) {
        copyTreeField(sourceEntity, targetEntity, dataDefinitionToCopy, fieldName);
    }
    for (String fieldName : dataDefinitionToCopy.getFields().keySet()) {
        copyManyToManyField(sourceEntity, targetEntity, dataDefinitionToCopy, fieldName);
    }
    return targetEntity;
}
Also used : Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 8 with InternalDataDefinition

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

the class DataAccessServiceImpl method delete.

@Override
@Transactional
@Monitorable
public EntityOpResult delete(final InternalDataDefinition dataDefinition, final Long... entityIds) {
    InternalDataDefinition dataDefinitionToDelete = getDataDefinitionByMasterModel(dataDefinition);
    checkState(dataDefinitionToDelete.isDeletable(), "Entity must be deletable");
    checkState(dataDefinitionToDelete.isEnabled(), L_DATA_DEFINITION_BELONGS_TO_DISABLED_PLUGIN);
    checkState(entityIds.length > 0, "EntityIds must be given");
    for (Long entityId : entityIds) {
        EntityOpResult result = deleteEntity(dataDefinitionToDelete, entityId);
        if (!result.isSuccessfull()) {
            return result;
        }
    }
    return EntityOpResult.successfull();
}
Also used : EntityOpResult(com.qcadoo.model.api.EntityOpResult) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Monitorable(com.qcadoo.model.api.aop.Monitorable) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with InternalDataDefinition

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

the class DataAccessServiceImpl method getDataDefinitionByMasterModel.

private InternalDataDefinition getDataDefinitionByMasterModel(InternalDataDefinition dataDefinition) {
    InternalDataDefinition masterDataDefinition;
    if (dataDefinition.getMasterModel() == null) {
        masterDataDefinition = dataDefinition;
    } else {
        masterDataDefinition = (InternalDataDefinition) dataDefinitionService.get(dataDefinition.getMasterModel().getPluginIdentifier(), dataDefinition.getMasterModel().getName());
    }
    checkNotNull(masterDataDefinition, L_DATA_DEFINITION_MUST_BE_GIVEN);
    return masterDataDefinition;
}
Also used : InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 10 with InternalDataDefinition

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

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