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