Search in sources :

Example 26 with Entity

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

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 = item.getValue(hierarchyPropertyName);
            if (parentItem != null && parentItem.getId().equals(itemId))
                return true;
        }
    }
    return false;
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity)

Example 27 with Entity

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

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<K> item = getItemNN(id);
            Entity<K> parentItem = item.getValue(hierarchyPropertyName);
            if (parentItem != null && parentItem.getId().equals(itemId))
                res.add(item.getId());
        }
        return res;
    }
    return Collections.emptyList();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity)

Example 28 with Entity

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

the class HierarchicalPropertyDatasourceImpl method getChildren.

@Override
public Collection<K> getChildren(K itemId) {
    if (hierarchyPropertyName != null) {
        final Entity item = getItem(itemId);
        if (item == null)
            return Collections.emptyList();
        List<K> res = new ArrayList<>();
        Collection<K> ids = getItemIds();
        for (K id : ids) {
            Entity<K> currentItem = getItem(id);
            Object parentItem = currentItem.getValue(hierarchyPropertyName);
            if (parentItem != null && parentItem.equals(item))
                res.add(currentItem.getId());
        }
        if (StringUtils.isNotBlank(sortPropertyName)) {
            Collections.sort(res, new Comparator<K>() {

                @Override
                public int compare(K o1, K o2) {
                    Entity item1 = getItem(o1);
                    Entity item2 = getItem(o2);
                    Object value1 = item1.getValue(sortPropertyName);
                    Object value2 = item2.getValue(sortPropertyName);
                    if ((value1 instanceof Comparable) && (value2 instanceof Comparable))
                        return ((Comparable) value1).compareTo(value2);
                    return 0;
                }
            });
        }
        return res;
    }
    return Collections.emptyList();
}
Also used : Entity(com.haulmont.cuba.core.entity.Entity)

Example 29 with Entity

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

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();
        AbstractInstance parentInstance = (AbstractInstance) parentItem;
        parentInstance.setValue(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(com.haulmont.cuba.core.entity.Entity) AbstractInstance(com.haulmont.chile.core.model.impl.AbstractInstance)

Example 30 with Entity

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

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(com.haulmont.cuba.core.entity.Entity)

Aggregations

Entity (com.haulmont.cuba.core.entity.Entity)203 MetaClass (com.haulmont.chile.core.model.MetaClass)51 MetaProperty (com.haulmont.chile.core.model.MetaProperty)44 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)40 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)18 Test (org.junit.Test)15 Inject (javax.inject.Inject)14 java.util (java.util)12 EntityManager (com.haulmont.cuba.core.EntityManager)11 ParseException (java.text.ParseException)11 Element (org.dom4j.Element)11 Logger (org.slf4j.Logger)11 com.haulmont.cuba.core.global (com.haulmont.cuba.core.global)10 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)9 LoggerFactory (org.slf4j.LoggerFactory)9 Query (com.haulmont.cuba.core.Query)8 Server (com.haulmont.cuba.core.entity.Server)8 Transaction (com.haulmont.cuba.core.Transaction)7 Datasource (com.haulmont.cuba.gui.data.Datasource)7 Collectors (java.util.stream.Collectors)7