use of io.jmix.core.Entity in project jmix by jmix-framework.
the class CollectionDatasourceImpl method committed.
@SuppressWarnings("unchecked")
@Override
public void committed(Set<Entity> entities) {
if (!State.VALID.equals(state)) {
return;
}
for (Entity entity : entities) {
if (!metaClass.getJavaClass().isAssignableFrom(entity.getClass()))
continue;
if (entity.equals(item)) {
item = (T) entity;
}
updateItem((T) entity);
}
modified = false;
clearCommitLists();
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class CollectionPropertyDatasourceImpl method getCollection.
protected Collection<T> getCollection() {
Security security = AppBeans.get(Security.NAME);
MetaClass parentMetaClass = masterDs.getMetaClass();
MetaClass propertyMetaClass = metaProperty.getRange().asClass();
if (!security.isEntityOpPermitted(propertyMetaClass, EntityOp.READ) || !security.isEntityAttrPermitted(parentMetaClass, metaProperty.getName(), EntityAttrAccess.VIEW)) {
// Don't use Collections.emptyList() to avoid confusing UnsupportedOperationExceptions
return new ArrayList<>();
} else {
final Entity master = masterDs.getItem();
// noinspection unchecked
return master == null ? null : (Collection<T>) EntityValues.getValue(master, metaProperty.getName());
}
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class CollectionPropertyDatasourceImpl method internalAddItem.
@SuppressWarnings("unchecked")
protected void internalAddItem(T item, Runnable addToCollection) {
backgroundWorker.checkUIAccess();
checkState();
checkPermission();
if (getCollection() == null) {
if (masterDs.getItem() == null) {
// Last chance to find and set a master item
MetaProperty inverseProp = metaProperty.getInverse();
if (inverseProp != null) {
Entity probableMasterItem = EntityValues.getValue(item, inverseProp.getName());
if (probableMasterItem != null) {
Collection<Entity> masterCollection = ((CollectionPropertyDatasourceImpl) masterDs).getCollection();
for (Entity masterCollectionItem : masterCollection) {
if (masterCollectionItem.equals(probableMasterItem)) {
masterDs.setItem(masterCollectionItem);
break;
}
}
}
}
if (masterDs.getItem() == null) {
throw new IllegalStateException("Master datasource item is null");
}
} else {
initCollection();
}
}
// Don't add the same object instance twice (this is possible when committing nested datasources)
if (!containsObjectInstance(item))
addToCollection.run();
attachListener(item);
if (Objects.equals(this.item, item)) {
this.item = item;
}
modified = true;
if (cascadeProperty) {
final Entity parentItem = masterDs.getItem();
((DatasourceImplementation) masterDs).modified(parentItem);
}
if (metaProperty != null && metaProperty.getRange() != null && metaProperty.getRange().getCardinality() != null && metaProperty.getRange().getCardinality() == Range.Cardinality.MANY_TO_MANY && !PersistenceHelper.isNew(item)) {
// do not mark for update existing many-to-many item;
// item is not updated here, but many-to-many table entry is added
} else {
modified(item);
}
fireCollectionChanged(Operation.ADD, Collections.singletonList(item));
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class CollectionPropertyDatasourceImpl method committed.
@SuppressWarnings("unchecked")
@Override
public void committed(Set<Entity> entities) {
if (!State.VALID.equals(masterDs.getState()))
return;
Collection<T> collection = getCollection();
if (collection != null) {
for (T item : new ArrayList<>(collection)) {
for (Entity entity : entities) {
if (entity.equals(item)) {
if (collection instanceof List) {
List list = (List) collection;
list.set(list.indexOf(item), entity);
} else if (collection instanceof Set) {
Set set = (Set) collection;
set.remove(item);
set.add(entity);
}
attachListener(entity);
fireCollectionChanged(Operation.UPDATE, Collections.singletonList(item));
}
}
// to avoid duplication in any case
detachListener(item);
attachListener(item);
}
}
for (Entity entity : entities) {
if (entity.equals(item)) {
item = (T) entity;
}
}
modified = false;
clearCommitLists();
}
use of io.jmix.core.Entity in project jmix by jmix-framework.
the class CollectionPropertyDatasourceImpl method removeItem.
@Override
public void removeItem(T item) {
checkNotNullArgument(item, "item is null");
checkState();
checkPermission();
Collection<T> collection = getCollection();
if (collection != null) {
if (this.item != null && this.item.equals(item)) {
setItem(null);
}
// In case of 2nd-level composition and using List as attribute type, there may be duplicated instances
// after repeated editing of newly added item. So remove them all.
Iterator<T> iterator = collection.iterator();
while (iterator.hasNext()) {
T entity = iterator.next();
if (entity.equals(item)) {
detachListener(entity);
iterator.remove();
}
}
modified = true;
if (cascadeProperty) {
final Entity parentItem = masterDs.getItem();
// noinspection unchecked
((DatasourceImplementation) masterDs).modified(parentItem);
} else {
deleted(item);
}
fireCollectionChanged(Operation.REMOVE, Collections.singletonList(item));
}
}
Aggregations