Search in sources :

Example 1 with EmbeddableEntity

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

the class EntityFetcher method fetch.

@SuppressWarnings("unchecked")
protected void fetch(Entity entity, View view, Map<Instance, Set<View>> visited, boolean optimizeForDetached) {
    Set<View> views = visited.get(entity);
    if (views == null) {
        views = new HashSet<>();
        visited.put(entity, views);
    } else if (views.contains(view)) {
        return;
    }
    views.add(view);
    if (log.isTraceEnabled())
        log.trace("Fetching instance " + entity);
    MetaClass metaClass = metadata.getClassNN(entity.getClass());
    for (ViewProperty property : view.getProperties()) {
        MetaProperty metaProperty = metaClass.getPropertyNN(property.getName());
        if (!metaProperty.getRange().isClass() && !metadata.getTools().isLazyFetchedLocalAttribute(metaProperty) || metadata.getTools().isNotPersistent(metaClass, metaProperty))
            continue;
        if (log.isTraceEnabled())
            log.trace("Fetching property " + property.getName());
        Object value = entity.getValue(property.getName());
        View propertyView = property.getView();
        if (value != null && propertyView != null) {
            if (value instanceof Collection) {
                for (Object item : new ArrayList(((Collection) value))) {
                    if (item instanceof Entity) {
                        Entity e = (Entity) item;
                        if (entityStates.isDetached(e)) {
                            fetchReloaded(e, propertyView, visited, optimizeForDetached, managed -> {
                                if (value instanceof List) {
                                    List list = (List) value;
                                    list.set(list.indexOf(e), managed);
                                } else {
                                    Collection collection = (Collection) value;
                                    collection.remove(e);
                                    collection.add(managed);
                                }
                            });
                        } else {
                            fetch((Entity) item, propertyView, visited, optimizeForDetached);
                        }
                    }
                }
            } else if (value instanceof Entity) {
                Entity e = (Entity) value;
                if (!metaProperty.isReadOnly() && entityStates.isDetached(e) && !(e instanceof EmbeddableEntity)) {
                    fetchReloaded(e, propertyView, visited, optimizeForDetached, managed -> {
                        entity.setValue(property.getName(), managed);
                    });
                } else {
                    fetch(e, propertyView, visited, optimizeForDetached);
                }
            }
        }
    }
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) EntityManager(com.haulmont.cuba.core.EntityManager) Persistence(com.haulmont.cuba.core.Persistence) MetaProperty(com.haulmont.chile.core.model.MetaProperty) LoggerFactory(org.slf4j.LoggerFactory) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) Consumer(java.util.function.Consumer) Component(org.springframework.stereotype.Component) Instance(com.haulmont.chile.core.model.Instance) Transaction(com.haulmont.cuba.core.Transaction) Entity(com.haulmont.cuba.core.entity.Entity) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) Entity(com.haulmont.cuba.core.entity.Entity) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) MetaClass(com.haulmont.chile.core.model.MetaClass) MetaProperty(com.haulmont.chile.core.model.MetaProperty)

Example 2 with EmbeddableEntity

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

the class AttributeAccessSupportTest method getEmbeddableEntitySecurityStateTest.

@Test
public void getEmbeddableEntitySecurityStateTest() {
    EmbeddableEntity entity = new TestEmbeddableEntity();
    SecurityState securityState = BaseEntityInternalAccess.getOrCreateSecurityState(entity);
    BaseEntityInternalAccess.setSecurityState(entity, securityState);
    AttributeAccessSupport attributeAccessSupport = new AttributeAccessSupport();
    Assertions.assertNotNull(attributeAccessSupport.getSecurityState(entity), "com.haulmont.cuba.gui.AttributeAccessSupport#getSecurityState returns null");
}
Also used : TestEmbeddableEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestEmbeddableEntity) EmbeddableEntity(com.haulmont.cuba.core.entity.EmbeddableEntity) TestEmbeddableEntity(com.haulmont.cuba.gui.data.impl.testmodel1.TestEmbeddableEntity) SecurityState(com.haulmont.cuba.core.entity.SecurityState) Test(org.junit.jupiter.api.Test)

Aggregations

EmbeddableEntity (com.haulmont.cuba.core.entity.EmbeddableEntity)2 Instance (com.haulmont.chile.core.model.Instance)1 MetaClass (com.haulmont.chile.core.model.MetaClass)1 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 EntityManager (com.haulmont.cuba.core.EntityManager)1 Persistence (com.haulmont.cuba.core.Persistence)1 Transaction (com.haulmont.cuba.core.Transaction)1 Entity (com.haulmont.cuba.core.entity.Entity)1 SecurityState (com.haulmont.cuba.core.entity.SecurityState)1 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)1 TestEmbeddableEntity (com.haulmont.cuba.gui.data.impl.testmodel1.TestEmbeddableEntity)1 java.util (java.util)1 Consumer (java.util.function.Consumer)1 Inject (javax.inject.Inject)1 Test (org.junit.jupiter.api.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Component (org.springframework.stereotype.Component)1