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;
}
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();
}
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();
}
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();
}
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());
}
});
}
Aggregations