use of io.jmix.core.pessimisticlocking.LockInfo in project jmix by jmix-framework.
the class EntityCombinedScreen method lockIfNeeded.
/**
* Pessimistic lock before start of editing, if it is configured for the entity.
*/
protected boolean lockIfNeeded(Entity entity) {
MetaClass metaClass = getMetaClassForLocking(entity);
LockInfo lockInfo = getApplicationContext().getBean(LockService.class).lock(metaClass.getName(), EntityValues.getId(entity).toString());
if (lockInfo == null) {
justLocked = true;
} else if (!(lockInfo instanceof LockNotSupported)) {
showNotification(messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUsername(), getApplicationContext().getBean(DatatypeFormatter.class).formatDateTime(lockInfo.getSince())), NotificationType.HUMANIZED);
return false;
}
return true;
}
use of io.jmix.core.pessimisticlocking.LockInfo in project jmix by jmix-framework.
the class AbstractEditor method setItemInternal.
@SuppressWarnings("unchecked")
protected void setItemInternal(Entity item) {
Datasource ds = getDatasourceInternal();
DataSupplier dataservice = ds.getDataSupplier();
DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent();
DynamicAttributesGuiTools dynamicAttributesGuiTools = (DynamicAttributesGuiTools) getApplicationContext().getBean(DynamicAttributesGuiTools.NAME);
if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getFrame().getId())) {
ds.setLoadDynamicAttributes(true);
}
Class<? extends Entity> entityClass = item.getClass();
Object entityId = EntityValues.getId(item);
EntityStates entityStates = getApplicationContext().getBean(EntityStates.class);
if (parentDs != null) {
if (!PersistenceHelper.isNew(item) && !parentDs.getItemsToCreate().contains(item) && !parentDs.getItemsToUpdate().contains(item) && parentDs instanceof CollectionDatasource && ((CollectionDatasource) parentDs).containsItem(EntityValues.getId(item)) && !entityStates.isLoadedWithFetchPlan(item, ds.getView())) {
item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
if (parentDs instanceof CollectionPropertyDatasourceImpl) {
((CollectionPropertyDatasourceImpl) parentDs).replaceItem(item);
} else {
((CollectionDatasource) parentDs).updateItem(item);
}
}
item = EntityCopyUtils.copyCompositions(item);
handlePreviouslyDeletedCompositionItems(item, parentDs);
} else if (!PersistenceHelper.isNew(item)) {
item = dataservice.reload(item, ds.getView(), ds.getMetaClass(), ds.getLoadDynamicAttributes());
}
if (item == null) {
throw new EntityAccessException(metadata.getClassNN(entityClass), entityId);
}
if (PersistenceHelper.isNew(item) && !ds.getMetaClass().equals(metadata.getClass(item))) {
Entity newItem = ds.getDataSupplier().newInstance(ds.getMetaClass());
MetadataTools metadataTools = (MetadataTools) getApplicationContext().getBean(MetadataTools.class);
metadataTools.copy(item, newItem);
item = newItem;
}
if (ds.getLoadDynamicAttributes()) {
if (PersistenceHelper.isNew(item)) {
dynamicAttributesGuiTools.initDefaultAttributeValues(item, metadata.getClass(item));
}
if (item instanceof Categorized) {
dynamicAttributesGuiTools.listenCategoryChanges(ds);
}
}
ds.setItem(item);
if (PersistenceHelper.isNew(item)) {
// make sure that they will be saved on commit.
for (Datasource datasource : ds.getDsContext().getAll()) {
if (datasource instanceof NestedDatasource && ((NestedDatasource) datasource).getMaster() == ds) {
if (datasource.getItem() != null && PersistenceHelper.isNew(datasource.getItem()))
((DatasourceImplementation) datasource).modified(datasource.getItem());
}
}
}
((DatasourceImplementation) ds).setModified(false);
Security security = (Security) getApplicationContext().getBean(Security.NAME);
if (!PersistenceHelper.isNew(item)) {
if (security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
readOnlyDueToLock = false;
LockService lockService = (LockService) getApplicationContext().getBean(LockService.NAME);
LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), EntityValues.getId(item).toString());
if (lockInfo == null) {
justLocked = true;
addAfterDetachListener(afterCloseEvent -> {
releaseLock();
});
} else if (!(lockInfo instanceof LockNotSupported)) {
UserSessionSource userSessionSource = (UserSessionSource) getApplicationContext().getBean(UserSessionSource.NAME);
Frame frame = (Frame) getFrame();
frame.getWindowManager().showNotification(messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUsername(), Datatypes.getNN(Date.class).format(lockInfo.getSince(), userSessionSource.getLocale())), Frame.NotificationType.HUMANIZED);
readOnlyDueToLock = true;
showEnableEditingBtn = false;
setReadOnly(true);
}
} else {
showEnableEditingBtn = false;
setReadOnly(true);
}
}
}
Aggregations