Search in sources :

Example 41 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class CollectionDatasourceImpl method committed.

@SuppressWarnings("unchecked")
@Override
public void committed(Set<Entity> entities) {
    if (!State.VALID.equals(state)) {
        return;
    }
    for (Entity entity : entities) {
        if (!metaClass.getJavaClass().isAssignableFrom(entity.getClass()))
            continue;
        if (entity.equals(item)) {
            item = (T) entity;
        }
        updateItem((T) entity);
    }
    modified = false;
    clearCommitLists();
}
Also used : Entity(io.jmix.core.Entity)

Example 42 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class CollectionPropertyDatasourceImpl method getCollection.

protected Collection<T> getCollection() {
    Security security = AppBeans.get(Security.NAME);
    MetaClass parentMetaClass = masterDs.getMetaClass();
    MetaClass propertyMetaClass = metaProperty.getRange().asClass();
    if (!security.isEntityOpPermitted(propertyMetaClass, EntityOp.READ) || !security.isEntityAttrPermitted(parentMetaClass, metaProperty.getName(), EntityAttrAccess.VIEW)) {
        // Don't use Collections.emptyList() to avoid confusing UnsupportedOperationExceptions
        return new ArrayList<>();
    } else {
        final Entity master = masterDs.getItem();
        // noinspection unchecked
        return master == null ? null : (Collection<T>) EntityValues.getValue(master, metaProperty.getName());
    }
}
Also used : Entity(io.jmix.core.Entity) MetaClass(io.jmix.core.metamodel.model.MetaClass) Security(com.haulmont.cuba.core.global.Security)

Example 43 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class CollectionPropertyDatasourceImpl method internalAddItem.

@SuppressWarnings("unchecked")
protected void internalAddItem(T item, Runnable addToCollection) {
    backgroundWorker.checkUIAccess();
    checkState();
    checkPermission();
    if (getCollection() == null) {
        if (masterDs.getItem() == null) {
            // Last chance to find and set a master item
            MetaProperty inverseProp = metaProperty.getInverse();
            if (inverseProp != null) {
                Entity probableMasterItem = EntityValues.getValue(item, inverseProp.getName());
                if (probableMasterItem != null) {
                    Collection<Entity> masterCollection = ((CollectionPropertyDatasourceImpl) masterDs).getCollection();
                    for (Entity masterCollectionItem : masterCollection) {
                        if (masterCollectionItem.equals(probableMasterItem)) {
                            masterDs.setItem(masterCollectionItem);
                            break;
                        }
                    }
                }
            }
            if (masterDs.getItem() == null) {
                throw new IllegalStateException("Master datasource item is null");
            }
        } else {
            initCollection();
        }
    }
    // Don't add the same object instance twice (this is possible when committing nested datasources)
    if (!containsObjectInstance(item))
        addToCollection.run();
    attachListener(item);
    if (Objects.equals(this.item, item)) {
        this.item = item;
    }
    modified = true;
    if (cascadeProperty) {
        final Entity parentItem = masterDs.getItem();
        ((DatasourceImplementation) masterDs).modified(parentItem);
    }
    if (metaProperty != null && metaProperty.getRange() != null && metaProperty.getRange().getCardinality() != null && metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_MANY && !PersistenceHelper.isNew(item)) {
    // do not mark for update existing many-to-many item;
    // item is not updated here, but many-to-many table entry is added
    } else {
        modified(item);
    }
    fireCollectionChanged(Operation.ADD, Collections.singletonList(item));
}
Also used : Entity(io.jmix.core.Entity) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 44 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class CollectionPropertyDatasourceImpl method committed.

@SuppressWarnings("unchecked")
@Override
public void committed(Set<Entity> entities) {
    if (!State.VALID.equals(masterDs.getState()))
        return;
    Collection<T> collection = getCollection();
    if (collection != null) {
        for (T item : new ArrayList<>(collection)) {
            for (Entity entity : entities) {
                if (entity.equals(item)) {
                    if (collection instanceof List) {
                        List list = (List) collection;
                        list.set(list.indexOf(item), entity);
                    } else if (collection instanceof Set) {
                        Set set = (Set) collection;
                        set.remove(item);
                        set.add(entity);
                    }
                    attachListener(entity);
                    fireCollectionChanged(Operation.UPDATE, Collections.singletonList(item));
                }
            }
            // to avoid duplication in any case
            detachListener(item);
            attachListener(item);
        }
    }
    for (Entity entity : entities) {
        if (entity.equals(item)) {
            item = (T) entity;
        }
    }
    modified = false;
    clearCommitLists();
}
Also used : Entity(io.jmix.core.Entity)

Example 45 with Entity

use of io.jmix.core.Entity in project jmix by jmix-framework.

the class CollectionPropertyDatasourceImpl method removeItem.

@Override
public void removeItem(T item) {
    checkNotNullArgument(item, "item is null");
    checkState();
    checkPermission();
    Collection<T> collection = getCollection();
    if (collection != null) {
        if (this.item != null && this.item.equals(item)) {
            setItem(null);
        }
        // In case of 2nd-level composition and using List as attribute type, there may be duplicated instances
        // after repeated editing of newly added item. So remove them all.
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
            T entity = iterator.next();
            if (entity.equals(item)) {
                detachListener(entity);
                iterator.remove();
            }
        }
        modified = true;
        if (cascadeProperty) {
            final Entity parentItem = masterDs.getItem();
            // noinspection unchecked
            ((DatasourceImplementation) masterDs).modified(parentItem);
        } else {
            deleted(item);
        }
        fireCollectionChanged(Operation.REMOVE, Collections.singletonList(item));
    }
}
Also used : Entity(io.jmix.core.Entity)

Aggregations

Entity (io.jmix.core.Entity)94 MetaClass (io.jmix.core.metamodel.model.MetaClass)20 MetaProperty (io.jmix.core.metamodel.model.MetaProperty)18 CommitContext (com.haulmont.cuba.core.global.CommitContext)10 CoreTest (com.haulmont.cuba.core.testsupport.CoreTest)10 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)10 Test (org.junit.jupiter.api.Test)10 Server (com.haulmont.cuba.core.model.common.Server)8 Datasource (com.haulmont.cuba.gui.data.Datasource)8 Collectors (java.util.stream.Collectors)8 Autowired (org.springframework.beans.factory.annotation.Autowired)8 FetchPlan (io.jmix.core.FetchPlan)7 Logger (org.slf4j.Logger)7 Metadata (io.jmix.core.Metadata)6 MetadataTools (io.jmix.core.MetadataTools)6 EntityValues (io.jmix.core.entity.EntityValues)6 java.util (java.util)6 ArrayList (java.util.ArrayList)6 Nullable (javax.annotation.Nullable)6 LoggerFactory (org.slf4j.LoggerFactory)6