Search in sources :

Example 1 with InternalDataDefinition

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

the class SearchQueryImpl method prepareDataDefinitions.

private String prepareDataDefinitions(final String queryString) {
    boolean hasSelectSection = !queryString.startsWith("from");
    Matcher matcher = pattern.matcher(queryString);
    String newQueryString = queryString;
    while (matcher.find()) {
        InternalDataDefinition dataDefinition = dataAccessService.getDataDefinition(matcher.group(1), matcher.group(2));
        newQueryString = newQueryString.replaceAll("#" + dataDefinition.getPluginIdentifier() + "_" + dataDefinition.getName(), dataDefinition.getFullyQualifiedClassName());
        if (!hasSelectSection && mainDataDefinition == null) {
            mainDataDefinition = dataDefinition;
        }
    }
    return newQueryString;
}
Also used : Matcher(java.util.regex.Matcher) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition)

Example 2 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 SearchCriteria searchCriteria) {
    checkArgument(searchCriteria != null, "SearchCriteria must be given");
    Criteria criteria = searchCriteria.createCriteria(hibernateService.getCurrentSession());
    int totalNumberOfEntities = hibernateService.getTotalNumberOfEntities(criteria);
    if (totalNumberOfEntities == 0) {
        LOG.debug("There is no entity matching criteria " + searchCriteria);
        return getResultSet(null, totalNumberOfEntities, Collections.emptyList());
    }
    searchCriteria.addFirstAndMaxResults(criteria);
    searchCriteria.addOrders(criteria);
    searchCriteria.addCacheable(criteria);
    List<?> results = hibernateService.list(criteria);
    if (LOG.isDebugEnabled()) {
        LOG.debug("There are " + totalNumberOfEntities + " entities matching criteria " + searchCriteria);
    }
    InternalDataDefinition searchQueryDataDefinition = (InternalDataDefinition) searchCriteria.getDataDefinition();
    if (searchQueryDataDefinition == null) {
        searchQueryDataDefinition = hibernateService.resolveDataDefinition(criteria);
    }
    return getResultSet(searchQueryDataDefinition, totalNumberOfEntities, results);
}
Also used : SearchCriteria(com.qcadoo.model.internal.search.SearchCriteria) Criteria(org.hibernate.Criteria) InternalDataDefinition(com.qcadoo.model.internal.api.InternalDataDefinition) Monitorable(com.qcadoo.model.api.aop.Monitorable) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with InternalDataDefinition

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

the class DataAccessServiceImpl method deactivate.

@Override
@Transactional
@Monitorable
public List<Entity> deactivate(final InternalDataDefinition dataDefinition, final Long... entityIds) {
    if (!dataDefinition.isActivable()) {
        return Collections.emptyList();
    }
    InternalDataDefinition dataDefinitionToDeactivate = getDataDefinitionByMasterModel(dataDefinition);
    List<Entity> deactivatedEntities = new ArrayList<Entity>();
    for (Long entityId : entityIds) {
        Entity entity = get(dataDefinitionToDeactivate, entityId);
        if (entity == null) {
            throw new IllegalStateException("Cannot deactivate " + entityId + " (entity not found)");
        }
        if (entity.isActive()) {
            entity.setActive(false);
            entity = save(dataDefinitionToDeactivate, entity);
            if (!entity.isValid()) {
                throw new IllegalStateException("Cannot deactivate " + entity + " because of validation errors");
            }
            LOG.debug(entity + " has been deactivated");
            deactivatedEntities.add(entity);
        }
    }
    return deactivatedEntities;
}
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 4 with InternalDataDefinition

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

the class DataAccessServiceImpl method copy.

@Override
@Transactional
@Monitorable
public List<Entity> copy(final InternalDataDefinition dataDefinition, final Long... entityIds) {
    InternalDataDefinition dataDefinitionToCopy = getDataDefinitionByMasterModel(dataDefinition);
    List<Entity> copiedEntities = new ArrayList<Entity>();
    for (Long entityId : entityIds) {
        Entity sourceEntity = get(dataDefinitionToCopy, entityId);
        Entity targetEntity = copy(dataDefinitionToCopy, sourceEntity);
        if (targetEntity == null) {
            throw new IllegalStateException("Cannot copy " + sourceEntity);
        }
        LOG.debug(sourceEntity + " has been copied to " + targetEntity);
        targetEntity = save(dataDefinitionToCopy, targetEntity);
        if (!targetEntity.isValid()) {
            throw new CopyException(targetEntity);
        }
        copiedEntities.add(targetEntity);
    }
    return copiedEntities;
}
Also used : Entity(com.qcadoo.model.api.Entity) CopyException(com.qcadoo.model.api.CopyException) 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 5 with InternalDataDefinition

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

the class DataAccessServiceImpl method copyTreeField.

private void copyTreeField(final Entity sourceEntity, final Entity targetEntity, final DataDefinition dataDefinition, final String fieldName) {
    FieldDefinition fieldDefinition = dataDefinition.getField(fieldName);
    if (!isFieldCopyable(TreeType.class, fieldDefinition, dataDefinition)) {
        return;
    }
    TreeType treeType = ((TreeType) fieldDefinition.getType());
    List<Entity> entities = new ArrayList<Entity>();
    Entity root = sourceEntity.getTreeField(fieldName).getRoot();
    if (root != null) {
        root.setField(treeType.getJoinFieldName(), null);
        root = copy((InternalDataDefinition) treeType.getDataDefinition(), root);
        if (root != null) {
            entities.add(root);
        }
    }
    targetEntity.setField(fieldName, entities);
}
Also used : TreeType(com.qcadoo.model.api.types.TreeType) Entity(com.qcadoo.model.api.Entity) FieldDefinition(com.qcadoo.model.api.FieldDefinition) InternalFieldDefinition(com.qcadoo.model.internal.api.InternalFieldDefinition) ArrayList(java.util.ArrayList) 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