Search in sources :

Example 66 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class ExcludeAction method doRemove.

@SuppressWarnings("unchecked")
@Override
protected void doRemove(Set<Entity> selected, boolean autocommit) {
    @SuppressWarnings({ "unchecked" }) CollectionDatasource ds = target.getDatasource();
    if (ds instanceof NestedDatasource) {
        // Clear reference to master entity
        Datasource masterDs = ((NestedDatasource) ds).getMaster();
        MetaProperty metaProperty = ((NestedDatasource) ds).getProperty();
        if (masterDs != null && metaProperty != null) {
            MetaProperty inverseProp = metaProperty.getInverse();
            if (inverseProp != null) {
                ExtendedEntities extendedEntities = metadata.getExtendedEntities();
                Class inversePropClass = extendedEntities.getEffectiveClass(inverseProp.getDomain());
                Class dsClass = extendedEntities.getEffectiveClass(ds.getMetaClass());
                if (inversePropClass.isAssignableFrom(dsClass)) {
                    for (Entity item : selected) {
                        item.setValue(inverseProp.getName(), null);
                    }
                }
            }
        }
    }
    for (Entity item : selected) {
        ds.modifyItem(item);
        ds.excludeItem(item);
    }
    if (autocommit && (ds.getCommitMode() != Datasource.CommitMode.PARENT)) {
        try {
            ds.commit();
        } catch (RuntimeException e) {
            ds.refresh();
            throw e;
        }
    }
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) PropertyDatasource(com.haulmont.cuba.gui.data.PropertyDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) ExtendedEntities(com.haulmont.cuba.core.global.ExtendedEntities) Entity(com.haulmont.cuba.core.entity.Entity) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) NestedDatasource(com.haulmont.cuba.gui.data.NestedDatasource) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 67 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class DeletePolicyProcessor method getReference.

protected Entity getReference(Entity entity, MetaProperty property) {
    if (PersistenceHelper.isLoaded(entity, property.getName()))
        return entity.getValue(property.getName());
    else {
        Query query = entityManager.createQuery("select e." + property.getName() + " from " + entity.getMetaClass().getName() + " e where e." + primaryKeyName + " = ?1");
        query.setParameter(1, entity.getId());
        Object refEntity = query.getFirstResult();
        return (Entity) refEntity;
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query)

Example 68 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class DeletePolicyProcessor method cascade.

protected void cascade(String entityName, MetaProperty property) {
    String template = property.getRange().getCardinality().isMany() ? "select e from %s e join e.%s c where c." + primaryKeyName + " = ?1" : "select e from %s e where e.%s." + primaryKeyName + " = ?1";
    String qstr = String.format(template, entityName, property.getName());
    Query query = entityManager.createQuery(qstr);
    query.setParameter(1, entity.getId());
    List<Entity> list = query.getResultList();
    for (Entity e : list) {
        entityManager.remove(e);
    }
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query)

Example 69 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class DeletePolicyProcessor method processOnDelete.

protected void processOnDelete(List<MetaProperty> properties) {
    for (MetaProperty property : properties) {
        MetaClass metaClass = property.getRange().asClass();
        OnDelete annotation = property.getAnnotatedElement().getAnnotation(OnDelete.class);
        DeletePolicy deletePolicy = annotation.value();
        switch(deletePolicy) {
            case DENY:
                if (property.getRange().getCardinality().isMany()) {
                    if (!isCollectionEmpty(property))
                        throw new DeletePolicyException(this.metaClass.getName(), metaClass.getName());
                } else {
                    Object value = getReference(entity, property);
                    if (value != null)
                        throw new DeletePolicyException(this.metaClass.getName(), metaClass.getName());
                }
                break;
            case CASCADE:
                if (property.getRange().getCardinality().isMany()) {
                    Collection<Entity> value = getCollection(property);
                    if (value != null && !value.isEmpty()) {
                        for (Entity e : value) {
                            entityManager.remove(e);
                        }
                    }
                } else {
                    Entity value = getReference(entity, property);
                    if (value != null && checkIfEntityBelongsToMaster(property, value)) {
                        if (!(value instanceof SoftDelete)) {
                            if (PersistenceHelper.isLoaded(entity, property.getName())) {
                                entity.setValue(property.getName(), null);
                                entityManager.remove(value);
                            } else {
                                hardDeleteNotLoadedReference(entity, property, value);
                            }
                        } else {
                            entityManager.remove(value);
                        }
                    }
                }
                break;
            case UNLINK:
                if (property.getRange().getCardinality().isMany()) {
                    if (metadata.getTools().isOwningSide(property)) {
                        Collection<Entity> value = entity.getValue(property.getName());
                        if (value != null) {
                            value.clear();
                        }
                    } else if (property.getInverse() != null) {
                        Collection<Entity> value = getCollection(property);
                        if (value != null) {
                            value.forEach(e -> setReferenceNull(e, property.getInverse()));
                        }
                    } else {
                        throw new UnsupportedOperationException("Unable to unlink nested collection items");
                    }
                } else {
                    if (metadata.getTools().isOwningSide(property)) {
                        setReferenceNull(entity, property);
                    } else {
                        Entity value = getReference(entity, property);
                        if (value != null && property.getInverse() != null) {
                            setReferenceNull(value, property.getInverse());
                        }
                    }
                }
                break;
        }
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) Query(com.haulmont.cuba.core.Query) java.util(java.util) Logger(org.slf4j.Logger) Range(com.haulmont.chile.core.model.Range) EntityManager(com.haulmont.cuba.core.EntityManager) Persistence(com.haulmont.cuba.core.Persistence) OnDelete(com.haulmont.cuba.core.entity.annotation.OnDelete) MetaProperty(com.haulmont.chile.core.model.MetaProperty) LoggerFactory(org.slf4j.LoggerFactory) OnDeleteInverse(com.haulmont.cuba.core.entity.annotation.OnDeleteInverse) MetaClass(com.haulmont.chile.core.model.MetaClass) PersistenceImpl(com.haulmont.cuba.core.sys.PersistenceImpl) Scope(org.springframework.context.annotation.Scope) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Component(org.springframework.stereotype.Component) SQLException(java.sql.SQLException) QueryRunner(com.haulmont.bali.db.QueryRunner) Entity(com.haulmont.cuba.core.entity.Entity) Entity(com.haulmont.cuba.core.entity.Entity) MetaClass(com.haulmont.chile.core.model.MetaClass) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) MetaProperty(com.haulmont.chile.core.model.MetaProperty) OnDelete(com.haulmont.cuba.core.entity.annotation.OnDelete)

Example 70 with Entity

use of com.haulmont.cuba.core.entity.Entity in project cuba by cuba-platform.

the class DeletePolicyProcessor method isCollectionEmpty.

protected boolean isCollectionEmpty(MetaProperty property) {
    MetaProperty inverseProperty = property.getInverse();
    if (inverseProperty == null) {
        log.warn("Inverse property not found for property {}", property);
        Collection<Entity> value = entity.getValue(property.getName());
        return value == null || value.isEmpty();
    }
    String invPropName = inverseProperty.getName();
    String collectionPkName = metadata.getTools().getPrimaryKeyName(property.getRange().asClass());
    String qlStr = "select e." + collectionPkName + " from " + property.getRange().asClass().getName() + " e where e." + invPropName + "." + primaryKeyName + " = ?1";
    Query query = entityManager.createQuery(qlStr);
    query.setParameter(1, entity.getId());
    query.setMaxResults(1);
    List<Entity> list = query.getResultList();
    return list.isEmpty();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity) Query(com.haulmont.cuba.core.Query) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

Entity (com.haulmont.cuba.core.entity.Entity)203 MetaClass (com.haulmont.chile.core.model.MetaClass)51 MetaProperty (com.haulmont.chile.core.model.MetaProperty)44 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)40 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)18 Test (org.junit.Test)15 Inject (javax.inject.Inject)14 java.util (java.util)12 EntityManager (com.haulmont.cuba.core.EntityManager)11 ParseException (java.text.ParseException)11 Element (org.dom4j.Element)11 Logger (org.slf4j.Logger)11 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 LoggerFactory (org.slf4j.LoggerFactory)9 Query (com.haulmont.cuba.core.Query)8 Server (com.haulmont.cuba.core.entity.Server)8 Transaction (com.haulmont.cuba.core.Transaction)7 Datasource (com.haulmont.cuba.gui.data.Datasource)7 Collectors (java.util.stream.Collectors)7