Search in sources :

Example 56 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class ExcludeAction method checkRemovePermission.

@Override
protected boolean checkRemovePermission() {
    CollectionDatasource ds = target.getDatasource();
    if (ds instanceof PropertyDatasource) {
        PropertyDatasource propertyDatasource = (PropertyDatasource) ds;
        MetaClass parentMetaClass = propertyDatasource.getMaster().getMetaClass();
        MetaProperty metaProperty = propertyDatasource.getProperty();
        boolean attrPermitted = security.isEntityAttrPermitted(parentMetaClass, metaProperty.getName(), EntityAttrAccess.MODIFY);
        if (!attrPermitted) {
            return false;
        }
    }
    return true;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) PropertyDatasource(com.haulmont.cuba.gui.data.PropertyDatasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 57 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty 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 58 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class DeletePolicyProcessor method processOnDeleteInverse.

protected void processOnDeleteInverse(List<MetaProperty> properties) {
    for (MetaProperty property : properties) {
        MetaClass metaClass = property.getDomain();
        List<MetaClass> persistentEntities = new ArrayList<>();
        if (isPersistent(metaClass))
            persistentEntities.add(metaClass);
        for (MetaClass descendant : metaClass.getDescendants()) {
            if (isPersistent(descendant))
                persistentEntities.add(descendant);
        }
        for (MetaClass persistentEntity : persistentEntities) {
            OnDeleteInverse annotation = property.getAnnotatedElement().getAnnotation(OnDeleteInverse.class);
            DeletePolicy deletePolicy = annotation.value();
            switch(deletePolicy) {
                case DENY:
                    if (referenceExists(persistentEntity.getName(), property))
                        throw new DeletePolicyException(this.metaClass.getName(), persistentEntity.getName());
                    break;
                case CASCADE:
                    cascade(persistentEntity.getName(), property);
                    break;
                case UNLINK:
                    unlink(persistentEntity.getName(), property);
                    break;
            }
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) OnDeleteInverse(com.haulmont.cuba.core.entity.annotation.OnDeleteInverse) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 59 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty 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)

Example 60 with MetaProperty

use of com.haulmont.chile.core.model.MetaProperty in project cuba by cuba-platform.

the class EclipseLinkSessionEventListener method preLogin.

@Override
public void preLogin(SessionEvent event) {
    Session session = event.getSession();
    setPrintInnerJoinOnClause(session);
    Map<Class, ClassDescriptor> descriptorMap = session.getDescriptors();
    for (Map.Entry<Class, ClassDescriptor> entry : descriptorMap.entrySet()) {
        MetaClass metaClass = metadata.getSession().getClassNN(entry.getKey());
        ClassDescriptor desc = entry.getValue();
        setCacheable(metaClass, desc, session);
        if (Entity.class.isAssignableFrom(desc.getJavaClass())) {
            // set DescriptorEventManager that doesn't invoke listeners for base classes
            desc.setEventManager(new DescriptorEventManager() {

                @Override
                public void notifyListeners(DescriptorEvent event) {
                    if (hasAnyListeners()) {
                        for (int index = 0; index < getEventListeners().size(); index++) {
                            DescriptorEventListener listener = (DescriptorEventListener) getEventListeners().get(index);
                            notifyListener(listener, event);
                        }
                    }
                }
            });
            desc.getEventManager().addListener(descriptorEventListener);
        }
        if (SoftDelete.class.isAssignableFrom(desc.getJavaClass())) {
            desc.getQueryManager().setAdditionalCriteria("this.deleteTs is null");
            desc.setDeletePredicate(entity -> entity instanceof SoftDelete && PersistenceHelper.isLoaded(entity, "deleteTs") && ((SoftDelete) entity).isDeleted());
        }
        List<DatabaseMapping> mappings = desc.getMappings();
        for (DatabaseMapping mapping : mappings) {
            // support UUID
            String attributeName = mapping.getAttributeName();
            MetaProperty metaProperty = metaClass.getPropertyNN(attributeName);
            if (metaProperty.getRange().isDatatype()) {
                if (metaProperty.getJavaType().equals(UUID.class)) {
                    ((DirectToFieldMapping) mapping).setConverter(UuidConverter.getInstance());
                    setDatabaseFieldParameters(session, mapping.getField());
                }
            } else if (metaProperty.getRange().isClass() && !metaProperty.getRange().getCardinality().isMany()) {
                MetaClass refMetaClass = metaProperty.getRange().asClass();
                MetaProperty refPkProperty = metadata.getTools().getPrimaryKeyProperty(refMetaClass);
                if (refPkProperty != null && refPkProperty.getJavaType().equals(UUID.class)) {
                    for (DatabaseField field : ((OneToOneMapping) mapping).getForeignKeyFields()) {
                        setDatabaseFieldParameters(session, field);
                    }
                }
            }
            // embedded attributes
            if (mapping instanceof AggregateObjectMapping) {
                EmbeddedParameters embeddedParameters = metaProperty.getAnnotatedElement().getAnnotation(EmbeddedParameters.class);
                if (embeddedParameters != null && !embeddedParameters.nullAllowed())
                    ((AggregateObjectMapping) mapping).setIsNullAllowed(false);
            }
            if (mapping.isOneToManyMapping()) {
                OneToManyMapping oneToManyMapping = (OneToManyMapping) mapping;
                if (SoftDelete.class.isAssignableFrom(oneToManyMapping.getReferenceClass())) {
                    oneToManyMapping.setAdditionalJoinCriteria(new ExpressionBuilder().get("deleteTs").isNull());
                }
            }
            if (mapping.isOneToOneMapping()) {
                OneToOneMapping oneToOneMapping = (OneToOneMapping) mapping;
                if (SoftDelete.class.isAssignableFrom(oneToOneMapping.getReferenceClass())) {
                    if (mapping.isManyToOneMapping()) {
                        oneToOneMapping.setSoftDeletionForBatch(false);
                        oneToOneMapping.setSoftDeletionForValueHolder(false);
                    } else {
                        OneToOne oneToOne = metaProperty.getAnnotatedElement().getAnnotation(OneToOne.class);
                        if (oneToOne != null) {
                            if (Strings.isNullOrEmpty(oneToOne.mappedBy())) {
                                oneToOneMapping.setSoftDeletionForBatch(false);
                                oneToOneMapping.setSoftDeletionForValueHolder(false);
                            } else {
                                oneToOneMapping.setAdditionalJoinCriteria(new ExpressionBuilder().get("deleteTs").isNull());
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : EmbeddedParameters(com.haulmont.cuba.core.entity.annotation.EmbeddedParameters) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) DescriptorEventListener(org.eclipse.persistence.descriptors.DescriptorEventListener) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) ExpressionBuilder(org.eclipse.persistence.expressions.ExpressionBuilder) DescriptorEventManager(org.eclipse.persistence.descriptors.DescriptorEventManager) OneToOne(javax.persistence.OneToOne) MetaClass(com.haulmont.chile.core.model.MetaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Map(java.util.Map) Session(org.eclipse.persistence.sessions.Session)

Aggregations

MetaProperty (com.haulmont.chile.core.model.MetaProperty)157 MetaClass (com.haulmont.chile.core.model.MetaClass)102 Entity (com.haulmont.cuba.core.entity.Entity)44 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)26 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)21 Range (com.haulmont.chile.core.model.Range)13 Nullable (javax.annotation.Nullable)11 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)9 java.util (java.util)9 Element (org.dom4j.Element)9 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)8 Collectors (java.util.stream.Collectors)6 Inject (javax.inject.Inject)6 Query (com.haulmont.cuba.core.Query)5 SoftDelete (com.haulmont.cuba.core.entity.SoftDelete)5 Collection (java.util.Collection)5 MessageTools (com.haulmont.cuba.core.global.MessageTools)4 PropertyDatasource (com.haulmont.cuba.gui.data.PropertyDatasource)4 Logger (org.slf4j.Logger)4