Search in sources :

Example 61 with Entity

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

the class HierarchicalPropertyDatasourceImpl method getChildren.

@SuppressWarnings("unchecked")
@Override
public Collection<K> getChildren(K itemId) {
    if (hierarchyPropertyName != null) {
        Entity item = getItem(itemId);
        if (item == null) {
            return Collections.emptyList();
        }
        List<K> res = new ArrayList<>();
        Collection<K> ids = getItemIds();
        for (K id : ids) {
            Entity currentItem = getItemNN(id);
            Object parentItem = EntityValues.getValue(currentItem, hierarchyPropertyName);
            if (parentItem != null && parentItem.equals(item))
                res.add((K) EntityValues.getId(currentItem));
        }
        if (StringUtils.isNotBlank(sortPropertyName)) {
            res.sort((o1, o2) -> {
                Entity item1 = getItemNN(o1);
                Entity item2 = getItemNN(o2);
                Object value1 = EntityValues.getValue(item1, sortPropertyName);
                Object value2 = EntityValues.getValue(item2, sortPropertyName);
                if ((value1 instanceof Comparable) && (value2 instanceof Comparable)) {
                    return ((Comparable) value1).compareTo(value2);
                }
                return 0;
            });
        }
        return res;
    }
    return Collections.emptyList();
}
Also used : Entity(io.jmix.core.Entity)

Example 62 with Entity

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

the class PropertyDatasourceImpl method committed.

@Override
public void committed(Set<Entity> entities) {
    Entity parentItem = masterDs.getItem();
    T prevItem = getItem();
    T newItem = null;
    for (Entity entity : entities) {
        if (entity.equals(prevItem)) {
            // noinspection unchecked
            newItem = (T) entity;
            break;
        }
    }
    // If committed set contains previousItem
    if ((parentItem != null) && newItem != null) {
        // Value changed
        boolean isModified = masterDs.isModified();
        EntityValues.setValue(parentItem, metaProperty.getName(), newItem, false);
        detachListener(prevItem);
        attachListener(newItem);
        fireItemChanged(prevItem);
        ((DatasourceImplementation) masterDs).setModified(isModified);
    } else {
        if (parentItem != null) {
            Entity newParentItem = null;
            Entity previousParentItem = null;
            // Find previous and new parent items
            Iterator<Entity> commitIter = entities.iterator();
            while (commitIter.hasNext() && (previousParentItem == null) && (newParentItem == null)) {
                Entity commitItem = commitIter.next();
                if (commitItem.equals(parentItem)) {
                    previousParentItem = parentItem;
                    newParentItem = commitItem;
                }
            }
            if (previousParentItem != null) {
                detachListener(getItem(previousParentItem));
            }
            if (newParentItem != null) {
                attachListener(getItem(newParentItem));
            }
        }
    }
    modified = false;
    clearCommitLists();
}
Also used : Entity(io.jmix.core.Entity)

Example 63 with Entity

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

the class PropertyDatasourceImpl method initParentDsListeners.

@SuppressWarnings("unchecked")
protected void initParentDsListeners() {
    masterDs.addItemChangeListener(e -> {
        Entity prevValue = getItem(e.getPrevItem());
        Entity newValue = getItem(e.getItem());
        reattachListeners(prevValue, newValue);
        fireItemChanged((T) prevValue);
    });
    masterDs.addStateChangeListener(e -> fireStateChanged(e.getPrevState()));
    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)

Example 64 with Entity

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

the class PropertyDatasourceImpl method setItem.

@Override
public void setItem(T item) {
    if (getItem() != null) {
        metadata.getTools().copy(item, getItem());
        itemsToUpdate.add(item);
    } else {
        final Entity parentItem = masterDs.getItem();
        EntityValues.setValue(parentItem, metaProperty.getName(), item);
    }
    setModified(true);
}
Also used : Entity(io.jmix.core.Entity)

Example 65 with Entity

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

the class RuntimePropsDatasourceImpl method committed.

@Override
public void committed(Set<Entity> entities) {
    if (!State.VALID.equals(state)) {
        return;
    }
    for (Entity entity : entities) {
        if (entity.equals(mainDs.getItem())) {
            initMetaClass(entity);
        }
    }
    modified = false;
    clearCommitLists();
}
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