Search in sources :

Example 6 with Entity

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

the class HierarchicalDatasourceImpl method getChildren.

@Override
public Collection<K> getChildren(K itemId) {
    if (hierarchyPropertyName != null) {
        final Entity currentItem = getItem(itemId);
        if (currentItem == null)
            return Collections.emptyList();
        List<K> res = new ArrayList<>();
        Collection<K> ids = getItemIds();
        for (K id : ids) {
            Entity item = getItemNN(id);
            Entity parentItem = EntityValues.getValue(item, hierarchyPropertyName);
            if (parentItem != null && EntityValues.getId(parentItem).equals(itemId))
                res.add((K) EntityValues.getId(item));
        }
        return res;
    }
    return Collections.emptyList();
}
Also used : Entity(io.jmix.core.Entity)

Example 7 with Entity

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

the class HierarchicalDatasourceImpl method hasChildren.

@Override
public boolean hasChildren(K itemId) {
    final Entity currentItem = getItem(itemId);
    if (currentItem == null)
        return false;
    if (hierarchyPropertyName != null) {
        Collection<K> ids = getItemIds();
        for (K id : ids) {
            Entity item = getItemNN(id);
            Entity parentItem = EntityValues.getValue(item, hierarchyPropertyName);
            if (parentItem != null && EntityValues.getId(parentItem).equals(itemId))
                return true;
        }
    }
    return false;
}
Also used : Entity(io.jmix.core.Entity)

Example 8 with Entity

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

the class HierarchicalPropertyDatasourceImpl method getRootItemIds.

@SuppressWarnings("unchecked")
@Override
public Collection<K> getRootItemIds() {
    Collection<K> ids = getItemIds();
    if (hierarchyPropertyName != null) {
        Set<K> result = new LinkedHashSet<>();
        for (K id : ids) {
            Entity item = getItemNN(id);
            Object value = EntityValues.getValue(item, hierarchyPropertyName);
            if (value == null || !containsItem((K) EntityValues.getId(((T) value))))
                result.add((K) EntityValues.getId(item));
        }
        return result;
    } else {
        return new LinkedHashSet<>(ids);
    }
}
Also used : Entity(io.jmix.core.Entity)

Example 9 with Entity

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

the class PropertyDatasourceImpl method commit.

@SuppressWarnings("unchecked")
@Override
public void commit() {
    if (!allowCommit) {
        return;
    }
    if (getCommitMode() == CommitMode.PARENT) {
        if (parentDs == null) {
            throw new IllegalStateException("parentDs is null while commitMode=PARENT");
        }
        if (parentDs instanceof CollectionDatasource) {
            CollectionDatasource parentCollectionDs = (CollectionDatasource) parentDs;
            for (Entity item : itemsToCreate) {
                if (parentCollectionDs.containsItem(EntityValues.getId(item))) {
                    parentCollectionDs.modifyItem(item);
                } else {
                    parentCollectionDs.addItem(item);
                }
            }
            for (Entity item : itemsToUpdate) {
                parentCollectionDs.modifyItem(item);
            }
            for (Entity item : itemsToDelete) {
                parentCollectionDs.removeItem(item);
            }
            // after repeated edit of new items the parent datasource can contain items-to-create which are deleted
            // in this datasource, so we need to delete them
            Collection<Entity> parentItemsToCreate = ((DatasourceImplementation) parentCollectionDs).getItemsToCreate();
            for (Entity createdItem : new ArrayList<>(parentItemsToCreate)) {
                if (!this.itemsToCreate.contains(createdItem)) {
                    MetaProperty inverseProp = metaProperty.getInverse();
                    // delete only if they have the same master item
                    if (inverseProp != null && PersistenceHelper.isLoaded(createdItem, inverseProp.getName()) && Objects.equals(EntityValues.getValue(createdItem, inverseProp.getName()), masterDs.getItem())) {
                        parentCollectionDs.removeItem(createdItem);
                    }
                }
            }
        } else {
            Entity item = null;
            if (!itemsToCreate.isEmpty()) {
                item = itemsToCreate.iterator().next();
            } else if (!itemsToUpdate.isEmpty()) {
                item = itemsToUpdate.iterator().next();
            } else if (!itemsToDelete.isEmpty()) {
                item = itemsToDelete.iterator().next();
            }
            if (item != null) {
                parentDs.setItem(item);
            }
        }
        clearCommitLists();
        modified = false;
    }
}
Also used : Entity(io.jmix.core.Entity) MetaProperty(io.jmix.core.metamodel.model.MetaProperty)

Example 10 with Entity

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

the class EmbeddedDatasourceImpl method initParentDsListeners.

protected void initParentDsListeners() {
    // noinspection unchecked
    masterDs.addItemChangeListener(e -> {
        Entity prevValue = getItem(e.getPrevItem());
        Entity newValue = getItem(e.getItem());
        reattachListeners(prevValue, newValue);
        fireItemChanged((T) prevValue);
    });
    // noinspection unchecked
    masterDs.addStateChangeListener(e -> fireStateChanged(e.getPrevState()));
    // noinspection unchecked
    masterDs.addItemPropertyChangeListener(e -> {
        if (e.getProperty().equals(metaProperty.getName()) && !Objects.equals(e.getPrevValue(), e.getValue())) {
            reattachListeners((Entity) e.getPrevValue(), (Entity) e.getValue());
            fireItemChanged((T) e.getPrevValue());
        }
    });
}
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