Search in sources :

Example 56 with MetaClass

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

the class PersistenceTools method getOldEnumValue.

/**
 * Returns an old value of an enum attribute changed in the current transaction. The entity must be in the Managed state.
 * <p>
 * Unlike {@link #getOldValue(Entity, String)}, returns enum value and not its id.
 *
 * @param entity    entity instance
 * @param attribute attribute name
 * @return  an old value stored in the database. For a new entity returns null.
 * @throws IllegalArgumentException if the entity is not persistent or not in the Managed state
 */
@Nullable
public EnumClass getOldEnumValue(Entity entity, String attribute) {
    Object value = getOldValue(entity, attribute);
    if (value == null)
        return null;
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    MetaProperty metaProperty = metaClass.getPropertyNN(attribute);
    if (metaProperty.getRange().isEnum()) {
        for (Object o : metaProperty.getRange().asEnumeration().getValues()) {
            EnumClass enumValue = (EnumClass) o;
            if (value.equals(enumValue.getId()))
                return enumValue;
        }
    }
    return null;
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) EnumClass(com.haulmont.chile.core.datatypes.impl.EnumClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Nullable(javax.annotation.Nullable)

Example 57 with MetaClass

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

the class AttributeSecuritySupport method copyViewConsideringPermissions.

private void copyViewConsideringPermissions(View srcView, View dstView) {
    MetaClass metaClass = metadata.getClassNN(srcView.getEntityClass());
    for (ViewProperty property : srcView.getProperties()) {
        if (security.isEntityAttrReadPermitted(metaClass, property.getName())) {
            View viewCopy = null;
            if (property.getView() != null) {
                viewCopy = new View(property.getView().getEntityClass(), property.getView().getName() + "(restricted)", false);
                copyViewConsideringPermissions(property.getView(), viewCopy);
            }
            dstView.addProperty(property.getName(), viewCopy, property.getFetchMode());
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass)

Example 58 with MetaClass

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

the class AttributeSecuritySupport method setupAttributeAccess.

@SuppressWarnings("unchecked")
public <T extends Entity> void setupAttributeAccess(T entity) {
    if (entity instanceof BaseGenericIdEntity || entity instanceof EmbeddableEntity) {
        SetupAttributeAccessEvent<T> event = new SetupAttributeAccessEvent<>(entity);
        boolean handled = false;
        Map<String, SetupAttributeAccessHandler> handlers = AppBeans.getAll(SetupAttributeAccessHandler.class);
        if (handlers != null) {
            for (SetupAttributeAccessHandler handler : handlers.values()) {
                MetaClass metaClass = metadata.getExtendedEntities().getOriginalOrThisMetaClass(entity.getMetaClass());
                if (handler.supports(metaClass.getJavaClass())) {
                    handled = true;
                    handler.setupAccess(event);
                }
            }
        }
        if (event.getReadonlyAttributes() != null) {
            Set<String> attributes = event.getReadonlyAttributes();
            SecurityState state = getOrCreateSecurityState(entity);
            addReadonlyAttributes(state, attributes.toArray(new String[attributes.size()]));
        }
        if (event.getRequiredAttributes() != null) {
            Set<String> attributes = event.getRequiredAttributes();
            SecurityState state = getOrCreateSecurityState(entity);
            addRequiredAttributes(state, attributes.toArray(new String[attributes.size()]));
        }
        if (event.getHiddenAttributes() != null) {
            Set<String> attributes = event.getHiddenAttributes();
            SecurityState state = getOrCreateSecurityState(entity);
            addHiddenAttributes(state, attributes.toArray(new String[attributes.size()]));
        }
        if (handled) {
            securityTokenManager.writeSecurityToken(entity);
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) SetupAttributeAccessEvent(com.haulmont.cuba.core.app.events.SetupAttributeAccessEvent)

Example 59 with MetaClass

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

the class AttributeSecuritySupport method applySecurityToFetchGroup.

protected void applySecurityToFetchGroup(Entity entity) {
    if (entity == null) {
        return;
    }
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    FetchGroupTracker fetchGroupTracker = (FetchGroupTracker) entity;
    FetchGroup fetchGroup = fetchGroupTracker._persistence_getFetchGroup();
    SecurityState securityState = getSecurityState(entity);
    if (fetchGroup != null) {
        List<String> attributesToRemove = new ArrayList<>();
        for (String attrName : fetchGroup.getAttributeNames()) {
            String[] parts = attrName.split("\\.");
            if (parts.length > 0 && BaseEntityInternalAccess.isHiddenOrReadOnly(securityState, parts[0])) {
                attributesToRemove.add(attrName);
            } else {
                MetaClass currentMetaClass = metaClass;
                for (String part : parts) {
                    if (!security.isEntityAttrUpdatePermitted(currentMetaClass, part)) {
                        attributesToRemove.add(attrName);
                        break;
                    }
                    MetaProperty metaProperty = currentMetaClass.getPropertyNN(part);
                    if (metaProperty.getRange().isClass()) {
                        currentMetaClass = metaProperty.getRange().asClass();
                    }
                }
            }
        }
        if (!attributesToRemove.isEmpty()) {
            List<String> attributeNames = new ArrayList<>(fetchGroup.getAttributeNames());
            attributeNames.removeAll(attributesToRemove);
            fetchGroupTracker._persistence_setFetchGroup(new CubaEntityFetchGroup(attributeNames));
        }
    } else {
        List<String> attributeNames = new ArrayList<>();
        for (MetaProperty metaProperty : metaClass.getProperties()) {
            String propertyName = metaProperty.getName();
            if (metadataTools.isSystem(metaProperty)) {
                attributeNames.add(propertyName);
            }
            if (security.isEntityAttrUpdatePermitted(metaClass, propertyName) && !BaseEntityInternalAccess.isHiddenOrReadOnly(securityState, propertyName)) {
                attributeNames.add(metaProperty.getName());
            }
        }
        fetchGroupTracker._persistence_setFetchGroup(new CubaEntityFetchGroup(attributeNames));
    }
}
Also used : FetchGroupTracker(org.eclipse.persistence.queries.FetchGroupTracker) CubaEntityFetchGroup(com.haulmont.cuba.core.sys.persistence.CubaEntityFetchGroup) MetaClass(com.haulmont.chile.core.model.MetaClass) FetchGroup(org.eclipse.persistence.queries.FetchGroup) CubaEntityFetchGroup(com.haulmont.cuba.core.sys.persistence.CubaEntityFetchGroup) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 60 with MetaClass

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

the class CrossDataStoreReferenceLoader method traverseView.

private void traverseView(View view, Map<Class<? extends Entity>, List<CrossDataStoreProperty>> crossPropertiesMap, Set<View> visited) {
    if (visited.contains(view))
        return;
    visited.add(view);
    String storeName = metadataTools.getStoreName(metaClass);
    Class<? extends Entity> entityClass = view.getEntityClass();
    for (ViewProperty viewProperty : view.getProperties()) {
        MetaProperty metaProperty = metadata.getClassNN(entityClass).getPropertyNN(viewProperty.getName());
        if (metaProperty.getRange().isClass()) {
            MetaClass propertyMetaClass = metaProperty.getRange().asClass();
            if (!Objects.equals(metadataTools.getStoreName(propertyMetaClass), storeName)) {
                List<String> relatedProperties = metadataTools.getRelatedProperties(metaProperty);
                if (relatedProperties.size() == 0) {
                    continue;
                }
                if (relatedProperties.size() > 1) {
                    log.warn("More than 1 related property is defined for attribute {}, skip handling cross-datastore reference", metaProperty);
                    continue;
                }
                List<CrossDataStoreProperty> crossProperties = crossPropertiesMap.computeIfAbsent(entityClass, k -> new ArrayList<>());
                if (crossProperties.stream().noneMatch(aProp -> aProp.property == metaProperty))
                    crossProperties.add(new CrossDataStoreProperty(metaProperty, viewProperty));
            }
            View propertyView = viewProperty.getView();
            if (propertyView != null) {
                traverseView(propertyView, crossPropertiesMap, visited);
            }
        }
    }
}
Also used : MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Aggregations

MetaClass (com.haulmont.chile.core.model.MetaClass)302 MetaProperty (com.haulmont.chile.core.model.MetaProperty)103 Entity (com.haulmont.cuba.core.entity.Entity)54 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)36 Nullable (javax.annotation.Nullable)25 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)24 Element (org.dom4j.Element)21 java.util (java.util)18 Inject (javax.inject.Inject)17 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)16 Test (org.junit.Test)15 EntityManager (com.haulmont.cuba.core.EntityManager)14 CategoryAttribute (com.haulmont.cuba.core.entity.CategoryAttribute)14 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)13 Metadata (com.haulmont.cuba.core.global.Metadata)13 Logger (org.slf4j.Logger)13 LoggerFactory (org.slf4j.LoggerFactory)13 MetadataTools (com.haulmont.cuba.core.global.MetadataTools)12 Collectors (java.util.stream.Collectors)11 Range (com.haulmont.chile.core.model.Range)10