Search in sources :

Example 1 with DatasourceImplementation

use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.

the class Param method createOptionsDataSource.

protected CollectionDatasource<Entity<Object>, Object> createOptionsDataSource(MetaClass metaClass) {
    CollectionDatasource ds = DsBuilder.create(datasource.getDsContext()).setMetaClass(metaClass).setViewName(entityView).buildCollectionDatasource();
    ds.setRefreshOnComponentValueChange(true);
    ((DatasourceImplementation) ds).initialized();
    if (!StringUtils.isBlank(entityWhere)) {
        QueryTransformer transformer = QueryTransformerFactory.createTransformer("select e from " + metaClass.getName() + " e");
        transformer.addWhere(entityWhere);
        String q = transformer.getResult();
        ds.setQuery(q);
    }
    if (WindowParams.DISABLE_AUTO_REFRESH.getBool(datasource.getDsContext().getFrameContext())) {
        if (ds instanceof CollectionDatasource.Suspendable)
            ((CollectionDatasource.Suspendable) ds).refreshIfNotSuspended();
        else
            ds.refresh();
    }
    return ds;
}
Also used : CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation)

Example 2 with DatasourceImplementation

use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation 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 3 with DatasourceImplementation

use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.

the class UserEditor method preCommit.

@Override
protected boolean preCommit() {
    User user = getItem();
    if (PersistenceHelper.isNew(user)) {
        String password = passwField.getValue();
        String passwordConfirmation = confirmPasswField.getValue();
        if (passwField.isRequired() && (StringUtils.isBlank(password) || StringUtils.isBlank(passwordConfirmation))) {
            showNotification(getMessage("emptyPassword"), NotificationType.WARNING);
            return false;
        } else {
            if (Objects.equals(password, passwordConfirmation)) {
                if (StringUtils.isNotEmpty(password)) {
                    ClientConfig passwordPolicyConfig = configuration.getConfig(ClientConfig.class);
                    if (passwordPolicyConfig.getPasswordPolicyEnabled()) {
                        String regExp = passwordPolicyConfig.getPasswordPolicyRegExp();
                        if (!password.matches(regExp)) {
                            showNotification(getMessage("simplePassword"), NotificationType.WARNING);
                            return false;
                        }
                    }
                    String passwordHash = passwordEncryption.getPasswordHash(user.getId(), password);
                    user.setPassword(passwordHash);
                }
            } else {
                showNotification(getMessage("passwordsDoNotMatch"), NotificationType.WARNING);
                return false;
            }
        }
    }
    boolean isDsModified = rolesDs.isModified();
    Collection<UserRole> userRoles = new ArrayList<>(rolesDs.getItems());
    for (UserRole userRole : userRoles) {
        if (userRole.getRole() != null && userRole.getRole().isPredefined()) {
            if (userRole.getRoleName() == null) {
                userRole.setRoleName(userRole.getRole().getName());
            }
            userRole.setRole(null);
            rolesDs.modifyItem(userRole);
        }
    }
    for (Object itemToDelete : ((AbstractDatasource) rolesDs).getItemsToDelete()) {
        if (itemToDelete instanceof UserRole && ((UserRole) itemToDelete).getRoleName() != null) {
            ((UserRole) itemToDelete).setRole(null);
        }
    }
    ((AbstractDatasource) rolesDs).setModified(isDsModified);
    if (rolesDs.isModified()) {
        @SuppressWarnings("unchecked") DatasourceImplementation<UserRole> rolesDsImpl = (DatasourceImplementation) rolesDs;
        CommitContext ctx = new CommitContext(Collections.emptyList(), rolesDsImpl.getItemsToDelete());
        dataSupplier.commit(ctx);
        List<UserRole> modifiedRoles = new ArrayList<>(rolesDsImpl.getItemsToCreate());
        modifiedRoles.addAll(rolesDsImpl.getItemsToUpdate());
        rolesDsImpl.committed(Collections.emptySet());
        for (UserRole userRole : modifiedRoles) {
            rolesDsImpl.modified(userRole);
        }
    }
    return true;
}
Also used : AbstractDatasource(com.haulmont.cuba.gui.data.impl.AbstractDatasource) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) ClientConfig(com.haulmont.cuba.client.ClientConfig)

Example 4 with DatasourceImplementation

use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.

the class WebAbstractTable method getItemDatasource.

@SuppressWarnings("unchecked")
@Override
public Datasource getItemDatasource(Entity item) {
    if (fieldDatasources == null) {
        fieldDatasources = new WeakHashMap<>();
    }
    Object fieldDatasource = fieldDatasources.get(item);
    if (fieldDatasource instanceof Datasource) {
        return (Datasource) fieldDatasource;
    }
    EntityTableItems containerTableItems = (EntityTableItems) getItems();
    Datasource datasource = DsBuilder.create().setAllowCommit(false).setMetaClass(containerTableItems.getEntityMetaClass()).setRefreshMode(CollectionDatasource.RefreshMode.NEVER).setViewName(View.LOCAL).buildDatasource();
    ((DatasourceImplementation) datasource).valid();
    datasource.setItem(item);
    fieldDatasources.put(item, datasource);
    return datasource;
}
Also used : Datasource(com.haulmont.cuba.gui.data.Datasource) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) EntityTableItems(com.haulmont.cuba.gui.components.data.meta.EntityTableItems) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation)

Example 5 with DatasourceImplementation

use of com.haulmont.cuba.gui.data.impl.DatasourceImplementation in project cuba by cuba-platform.

the class EditorWindowDelegate method setItem.

@SuppressWarnings("unchecked")
public void setItem(Entity item) {
    Datasource ds = getDatasource();
    DataSupplier dataservice = ds.getDataSupplier();
    DatasourceImplementation parentDs = (DatasourceImplementation) ((DatasourceImplementation) ds).getParent();
    DynamicAttributesGuiTools dynamicAttributesGuiTools = AppBeans.get(DynamicAttributesGuiTools.NAME);
    if (dynamicAttributesGuiTools.screenContainsDynamicAttributes(ds.getView(), getWrapper().getId())) {
        ds.setLoadDynamicAttributes(true);
    }
    Class<? extends Entity> entityClass = item.getClass();
    Object entityId = item.getId();
    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());
        metadata.getTools().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 = AppBeans.get(Security.NAME);
    if (!PersistenceHelper.isNew(item) && security.isEntityOpPermitted(ds.getMetaClass(), EntityOp.UPDATE)) {
        readOnly = false;
        LockInfo lockInfo = lockService.lock(getMetaClassForLocking(ds).getName(), item.getId().toString());
        if (lockInfo == null) {
            justLocked = true;
        } else if (!(lockInfo instanceof LockNotSupported)) {
            window.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);
            Action action = window.getAction(Window.Editor.WINDOW_COMMIT);
            if (action != null)
                action.setEnabled(false);
            action = window.getAction(Window.Editor.WINDOW_COMMIT_AND_CLOSE);
            if (action != null)
                action.setEnabled(false);
            readOnly = true;
        }
    }
}
Also used : Categorized(com.haulmont.cuba.core.entity.Categorized) BaseGenericIdEntity(com.haulmont.cuba.core.entity.BaseGenericIdEntity) Entity(com.haulmont.cuba.core.entity.Entity) 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)

Aggregations

DatasourceImplementation (com.haulmont.cuba.gui.data.impl.DatasourceImplementation)13 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)4 Datasource (com.haulmont.cuba.gui.data.Datasource)4 BaseGenericIdEntity (com.haulmont.cuba.core.entity.BaseGenericIdEntity)2 Categorized (com.haulmont.cuba.core.entity.Categorized)2 Entity (com.haulmont.cuba.core.entity.Entity)2 DsContext (com.haulmont.cuba.gui.data.DsContext)2 CollectionPropertyDatasourceImpl (com.haulmont.cuba.gui.data.impl.CollectionPropertyDatasourceImpl)2 DynamicAttributesGuiTools (com.haulmont.cuba.gui.dynamicattributes.DynamicAttributesGuiTools)2 DsContextLoader (com.haulmont.cuba.gui.xml.data.DsContextLoader)2 MetaProperty (com.haulmont.chile.core.model.MetaProperty)1 MetaPropertyPath (com.haulmont.chile.core.model.MetaPropertyPath)1 ClientConfig (com.haulmont.cuba.client.ClientConfig)1 LockService (com.haulmont.cuba.core.app.LockService)1 FileDescriptor (com.haulmont.cuba.core.entity.FileDescriptor)1 AccessDeniedException (com.haulmont.cuba.core.global.AccessDeniedException)1 Security (com.haulmont.cuba.core.global.Security)1 OpenType (com.haulmont.cuba.gui.WindowManager.OpenType)1 AbstractLookup (com.haulmont.cuba.gui.components.AbstractLookup)1 Button (com.haulmont.cuba.gui.components.Button)1