Search in sources :

Example 1 with LockService

use of com.haulmont.cuba.core.app.LockService in project cuba by cuba-platform.

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 = getBeanLocator().get(DynamicAttributesGuiTools.NAME);
    if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getFrame().getId())) {
        ds.setLoadDynamicAttributes(true);
    }
    Class<? extends Entity> entityClass = item.getClass();
    Object entityId = item.getId();
    EntityStates entityStates = getBeanLocator().get(EntityStates.class);
    if (parentDs != null) {
        if (!PersistenceHelper.isNew(item) && !parentDs.getItemsToCreate().contains(item) && !parentDs.getItemsToUpdate().contains(item) && parentDs instanceof CollectionDatasource && ((CollectionDatasource) parentDs).containsItem(item.getId()) && !entityStates.isLoadedWithView(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(entityClass, entityId);
    }
    if (PersistenceHelper.isNew(item) && !ds.getMetaClass().equals(item.getMetaClass())) {
        Entity newItem = ds.getDataSupplier().newInstance(ds.getMetaClass());
        MetadataTools metadataTools = getBeanLocator().get(MetadataTools.NAME);
        metadataTools.copy(item, newItem);
        item = newItem;
    }
    if (ds.getLoadDynamicAttributes() && item instanceof BaseGenericIdEntity) {
        if (PersistenceHelper.isNew(item)) {
            dynamicAttributesGuiTools.initDefaultAttributeValues((BaseGenericIdEntity) item, item.getMetaClass());
        }
        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 = getBeanLocator().get(Security.NAME);
    if (!PersistenceHelper.isNew(item)) {
        if (security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
            readOnlyDueToLock = false;
            LockService lockService = getBeanLocator().get(LockService.NAME);
            LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), item.getId().toString());
            if (lockInfo == null) {
                justLocked = true;
                addAfterDetachListener(afterCloseEvent -> {
                    releaseLock();
                });
            } else if (!(lockInfo instanceof LockNotSupported)) {
                UserSessionSource userSessionSource = getBeanLocator().get(UserSessionSource.NAME);
                getFrame().getWindowManager().showNotification(messages.getMainMessage("entityLocked.msg"), String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), Datatypes.getNN(Date.class).format(lockInfo.getSince(), userSessionSource.getLocale())), Frame.NotificationType.HUMANIZED);
                readOnlyDueToLock = true;
                showEnableEditingBtn = false;
                setReadOnly(true);
            }
        } else {
            showEnableEditingBtn = false;
            setReadOnly(true);
        }
    }
}
Also used : Categorized(com.haulmont.cuba.core.entity.Categorized) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) LockService(com.haulmont.cuba.core.app.LockService) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) DynamicAttributesGuiTools(com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools) CollectionPropertyDatasourceImpl(com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)

Example 2 with LockService

use of com.haulmont.cuba.core.app.LockService in project cuba by cuba-platform.

the class StandardEditor method setupLock.

protected void setupLock() {
    InstanceContainer<T> container = getEditedEntityContainer();
    Security security = getBeanLocator().get(Security.class);
    if (!getEntityStates().isNew(entityToEdit)) {
        if (security.isEntityOpPermitted(container.getEntityMetaClass(), EntityOp.UPDATE)) {
            this.readOnlyDueToLock = false;
            LockService lockService = getBeanLocator().get(LockService.class);
            LockInfo lockInfo = lockService.lock(getLockName(), entityToEdit.getId().toString());
            if (lockInfo == null) {
                this.justLocked = true;
                addAfterDetachListener(afterDetachEvent -> releaseLock());
            } else if (!(lockInfo instanceof LockNotSupported)) {
                Messages messages = getBeanLocator().get(Messages.class);
                DatatypeFormatter datatypeFormatter = getBeanLocator().get(DatatypeFormatter.class);
                getScreenContext().getNotifications().create(NotificationType.HUMANIZED).withCaption(messages.getMainMessage("entityLocked.msg")).withDescription(messages.formatMainMessage("entityLocked.desc", lockInfo.getUser().getLogin(), datatypeFormatter.formatDateTime(lockInfo.getSince()))).show();
                this.readOnlyDueToLock = true;
                this.showEnableEditingBtn = false;
                setReadOnly(true);
            }
        } else {
            this.showEnableEditingBtn = false;
            setReadOnly(true);
        }
    }
}
Also used : LockService(com.haulmont.cuba.core.app.LockService)

Example 3 with LockService

use of com.haulmont.cuba.core.app.LockService in project cuba by cuba-platform.

the class MasterDetailScreen method lockIfNeeded.

/**
 * Pessimistic lock before start of editing, if it is configured for the entity.
 */
protected boolean lockIfNeeded(Entity entity) {
    LockService lockService = getBeanLocator().get(LockService.class);
    LockInfo lockInfo = lockService.lock(getLockName(), entity.getId().toString());
    if (lockInfo == null) {
        justLocked = true;
    } else if (!(lockInfo instanceof LockNotSupported)) {
        Messages messages = getBeanLocator().get(Messages.class);
        DatatypeFormatter datatypeFormatter = getBeanLocator().get(DatatypeFormatter.class);
        Notifications notifications = getScreenContext().getNotifications();
        notifications.create(NotificationType.HUMANIZED).withCaption(messages.getMainMessage("entityLocked.msg")).withDescription(String.format(messages.getMainMessage("entityLocked.desc"), lockInfo.getUser().getLogin(), datatypeFormatter.formatDateTime(lockInfo.getSince()))).show();
        return false;
    }
    return true;
}
Also used : LockService(com.haulmont.cuba.core.app.LockService) Notifications(com.haulmont.cuba.gui.Notifications)

Aggregations

LockService (com.haulmont.cuba.core.app.LockService)3 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)1 Categorized (com.haulmont.cuba.core.entity.Categorized)1 Entity (com.haulmont.cuba.core.entity.Entity)1 Notifications (com.haulmont.cuba.gui.Notifications)1 CollectionPropertyDatasourceImpl (com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)1 DatasourceImplementation (com.haulmont.cuba.gui.data.impl.DatasourceImplementation)1 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)1