Search in sources :

Example 1 with ItemTrackingAction

use of com.haulmont.cuba.gui.components.actions.ItemTrackingAction in project cuba by cuba-platform.

the class EntityRestore method buildLayout.

protected void buildLayout() {
    Object value = entities.getValue();
    if (value != null) {
        MetaClass metaClass = (MetaClass) value;
        MetaProperty deleteTsMetaProperty = metaClass.getProperty("deleteTs");
        if (deleteTsMetaProperty != null) {
            if (entitiesTable != null) {
                tablePanel.remove(entitiesTable);
            }
            if (filter != null) {
                tablePanel.remove(filter);
            }
            ComponentsFactory componentsFactory = AppConfig.getFactory();
            entitiesTable = componentsFactory.createComponent(Table.class);
            entitiesTable.setFrame(frame);
            restoreButton = componentsFactory.createComponent(Button.class);
            restoreButton.setId("restore");
            restoreButton.setCaption(getMessage("entityRestore.restore"));
            ButtonsPanel buttonsPanel = componentsFactory.createComponent(ButtonsPanel.class);
            buttonsPanel.add(restoreButton);
            entitiesTable.setButtonsPanel(buttonsPanel);
            RowsCount rowsCount = componentsFactory.createComponent(RowsCount.class);
            entitiesTable.setRowsCount(rowsCount);
            final SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
            Formatter dateTimeFormatter = propertyValue -> {
                if (propertyValue == null) {
                    return StringUtils.EMPTY;
                }
                return dateTimeFormat.format(propertyValue);
            };
            // collect properties in order to add non-system columns first
            LinkedList<Table.Column> nonSystemPropertyColumns = new LinkedList<>();
            LinkedList<Table.Column> systemPropertyColumns = new LinkedList<>();
            List<MetaProperty> metaProperties = new ArrayList<>();
            for (MetaProperty metaProperty : metaClass.getProperties()) {
                // don't show embedded & multiple referred entities
                Range range = metaProperty.getRange();
                if (isEmbedded(metaProperty) || range.getCardinality().isMany() || metadataTools.isSystemLevel(metaProperty) || (range.isClass() && metadataTools.isSystemLevel(range.asClass()))) {
                    continue;
                }
                metaProperties.add(metaProperty);
                Table.Column column = new Table.Column(metaClass.getPropertyPath(metaProperty.getName()));
                if (!metadataTools.isSystem(metaProperty)) {
                    column.setCaption(getPropertyCaption(metaClass, metaProperty));
                    nonSystemPropertyColumns.add(column);
                } else {
                    column.setCaption(metaProperty.getName());
                    systemPropertyColumns.add(column);
                }
                if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
                    column.setFormatter(dateTimeFormatter);
                }
            }
            for (Table.Column column : nonSystemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            for (Table.Column column : systemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            DsContext dsContext = getDsContext();
            if (entitiesDs != null) {
                ((DsContextImplementation) dsContext).unregister(entitiesDs);
            }
            entitiesDs = DsBuilder.create(dsContext).setId("entitiesDs").setMetaClass(metaClass).setView(buildView(metaClass, metaProperties)).buildGroupDatasource();
            entitiesDs.setQuery("select e from " + metaClass.getName() + " e " + "where e.deleteTs is not null order by e.deleteTs");
            entitiesDs.setSoftDeletion(false);
            entitiesDs.refresh();
            entitiesTable.setDatasource(entitiesDs);
            String filterId = metaClass.getName().replace("$", "") + "GenericFilter";
            filter = componentsFactory.createComponent(Filter.class);
            filter.setId(filterId);
            filter.setFrame(getFrame());
            StringBuilder sb = new StringBuilder("");
            for (MetaProperty property : metaClass.getProperties()) {
                AnnotatedElement annotatedElement = property.getAnnotatedElement();
                if (annotatedElement.getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class) != null) {
                    sb.append(property.getName()).append("|");
                }
            }
            Element filterElement = Dom4j.readDocument(String.format("<filter id=\"%s\">\n" + "    <properties include=\".*\" exclude=\"\"/>\n" + "</filter>", filterId)).getRootElement();
            String excludedProperties = sb.toString();
            if (StringUtils.isNotEmpty(excludedProperties)) {
                Element properties = filterElement.element("properties");
                properties.attribute("exclude").setValue(excludedProperties.substring(0, excludedProperties.lastIndexOf("|")));
            }
            filter.setXmlDescriptor(filterElement);
            filter.setUseMaxResults(true);
            filter.setDatasource(entitiesDs);
            entitiesTable.setSizeFull();
            entitiesTable.setMultiSelect(true);
            Action restoreAction = new ItemTrackingAction("restore").withCaption(getMessage("entityRestore.restore")).withHandler(event -> showRestoreDialog());
            entitiesTable.addAction(restoreAction);
            restoreButton.setAction(restoreAction);
            tablePanel.add(filter);
            tablePanel.add(entitiesTable);
            tablePanel.expand(entitiesTable, "100%", "100%");
            entitiesTable.refresh();
            ((FilterImplementation) filter).loadFiltersAndApplyDefault();
        }
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Dom4j(com.haulmont.bali.util.Dom4j) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) EntityRestoreService(com.haulmont.cuba.core.app.EntityRestoreService) SimpleDateFormat(java.text.SimpleDateFormat) EnableRestore(com.haulmont.cuba.core.entity.annotation.EnableRestore) MetaClass(com.haulmont.chile.core.model.MetaClass) Inject(javax.inject.Inject) MetadataTools(com.haulmont.cuba.core.global.MetadataTools) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) GroupDatasource(com.haulmont.cuba.gui.data.GroupDatasource) ViewRepository(com.haulmont.cuba.core.global.ViewRepository) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Range(com.haulmont.chile.core.model.Range) Formatter(com.haulmont.cuba.gui.components.Formatter) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Status(com.haulmont.cuba.gui.components.Action.Status) Type(com.haulmont.cuba.gui.components.DialogAction.Type) View(com.haulmont.cuba.core.global.View) AppConfig(com.haulmont.cuba.gui.AppConfig) Element(org.dom4j.Element) Entity(com.haulmont.cuba.core.entity.Entity) MessageTools(com.haulmont.cuba.core.global.MessageTools) AnnotatedElement(java.lang.reflect.AnnotatedElement) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Formatter(com.haulmont.cuba.gui.components.Formatter) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Range(com.haulmont.chile.core.model.Range) MetaClass(com.haulmont.chile.core.model.MetaClass) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ItemTrackingAction

use of com.haulmont.cuba.gui.components.actions.ItemTrackingAction in project cuba by cuba-platform.

the class GroupBrowser method init.

@Override
public void init(final Map<String, Object> params) {
    CreateAction createAction = new CreateAction(groupsTree);
    createAction.setAfterCommitHandler(entity -> {
        groupsTree.expandTree();
    });
    groupsTree.addAction(createAction);
    createAction.setCaption(getMessage("action.create"));
    createAction.setOpenType(OpenType.DIALOG);
    EditAction groupEditAction = new EditAction(groupsTree);
    groupEditAction.setAfterCommitHandler(entity -> {
        groupsTree.expandTree();
    });
    groupEditAction.setOpenType(OpenType.DIALOG);
    groupsTree.addAction(groupEditAction);
    groupCreateButton.addAction(createAction);
    groupCreateButton.addAction(groupCopyAction);
    userCreateAction = new GroupPropertyCreateAction(usersTable);
    userCreateAction.setAfterCommitHandler(entity -> {
        usersTable.getDatasource().refresh();
    });
    groupsTree.addAction(new RemoveAction(groupsTree) {

        @Override
        protected boolean isApplicable() {
            if (target != null && target.getDatasource() != null && target.getSingleSelected() != null) {
                @SuppressWarnings("unchecked") HierarchicalDatasource<Group, UUID> ds = (HierarchicalDatasource<Group, UUID>) target.getDatasource();
                UUID selectedItemId = (UUID) target.getSingleSelected().getId();
                return ds.getChildren(selectedItemId).isEmpty();
            }
            return false;
        }
    });
    usersTable.addAction(userCreateAction);
    RemoveAction removeAction = new UserRemoveAction(usersTable, userManagementService);
    usersTable.addAction(removeAction);
    Action moveToGroupAction = new ItemTrackingAction("moveToGroup").withIcon("icons/move.png").withCaption(getMessage("moveToGroup")).withHandler(event -> {
        Set<User> selected = usersTable.getSelected();
        if (!selected.isEmpty()) {
            Map<String, Object> lookupParams = ParamsMap.of("exclude", groupsTree.getSelected().iterator().next(), "excludeChildren", false);
            Lookup lookupWindow = openLookup(Group.class, items -> {
                if (items.size() == 1) {
                    Group group = (Group) items.iterator().next();
                    List<UUID> usersForModify = new ArrayList<>();
                    for (User user : selected) {
                        usersForModify.add(user.getId());
                    }
                    userManagementService.moveUsersToGroup(usersForModify, group.getId());
                    if (selected.size() == 1) {
                        User user = selected.iterator().next();
                        showNotification(formatMessage("userMovedToGroup", user.getLogin(), group.getName()));
                    } else {
                        showNotification(formatMessage("usersMovedToGroup", group.getName()));
                    }
                    usersTable.getDatasource().refresh();
                }
            }, OpenType.DIALOG, lookupParams);
            lookupWindow.addCloseListener(actionId -> {
                usersTable.requestFocus();
            });
        }
    });
    MetaClass userMetaClass = metadata.getSession().getClass(User.class);
    moveToGroupAction.setEnabled(security.isEntityOpPermitted(userMetaClass, EntityOp.UPDATE));
    usersTable.addAction(moveToGroupAction);
    tabsheet.addListener(newTab -> {
        if ("constraintsTab".equals(newTab.getName())) {
            initConstraintsTab();
        } else if ("attributesTab".equals(newTab.getName())) {
            initAttributesTab();
        }
    });
    final boolean hasPermissionsToCreateGroup = security.isEntityOpPermitted(metadata.getSession().getClass(Group.class), EntityOp.CREATE);
    // enable actions if group is selected
    groupsDs.addItemChangeListener(e -> {
        if (userCreateAction != null) {
            userCreateAction.setEnabled(e.getItem() != null);
        }
        if (attributeCreateAction != null) {
            attributeCreateAction.setEnabled(e.getItem() != null);
        }
        if (constraintCreateAction != null) {
            constraintCreateAction.setEnabled(e.getItem() != null);
        }
        groupCopyAction.setEnabled(hasPermissionsToCreateGroup && e.getItem() != null);
        CollectionDatasource ds = usersTable.getDatasource();
        if (ds instanceof CollectionDatasource.SupportsPaging) {
            ((CollectionDatasource.SupportsPaging) ds).setFirstResult(0);
        }
    });
    groupsDs.refresh();
    groupsTree.expandTree();
    final Collection<UUID> itemIds = groupsDs.getRootItemIds();
    if (!itemIds.isEmpty()) {
        groupsTree.setSelected(groupsDs.getItem(itemIds.iterator().next()));
    }
    groupCreateButton.setEnabled(hasPermissionsToCreateGroup);
    groupCopyAction.setEnabled(hasPermissionsToCreateGroup);
    importUpload.addFileUploadSucceedListener(event -> {
        File file = fileUploadingAPI.getFile(importUpload.getFileId());
        if (file == null) {
            String errorMsg = String.format("Entities import upload error. File with id %s not found", importUpload.getFileId());
            throw new RuntimeException(errorMsg);
        }
        byte[] fileBytes;
        try (InputStream is = new FileInputStream(file)) {
            fileBytes = IOUtils.toByteArray(is);
        } catch (IOException e) {
            throw new RuntimeException("Unable to import file", e);
        }
        try {
            fileUploadingAPI.deleteFile(importUpload.getFileId());
        } catch (FileStorageException e) {
            log.error("Unable to delete temp file", e);
        }
        try {
            Collection<Entity> importedEntities;
            if ("json".equals(Files.getFileExtension(importUpload.getFileName()))) {
                importedEntities = entityImportExportService.importEntitiesFromJSON(new String(fileBytes), createGroupsImportView());
            } else {
                importedEntities = entityImportExportService.importEntitiesFromZIP(fileBytes, createGroupsImportView());
            }
            long importedGroupsCount = importedEntities.stream().filter(entity -> entity instanceof Group).count();
            showNotification(importedGroupsCount + " groups imported", NotificationType.HUMANIZED);
            groupsDs.refresh();
        } catch (Exception e) {
            showNotification(formatMessage("importError", e.getMessage()), NotificationType.ERROR);
        }
    });
    Companion companion = getCompanion();
    if (companion != null) {
        companion.initDragAndDrop(usersTable, groupsTree, (event) -> {
            if (event.getUsers().size() == 1) {
                if (moveSelectedUsersToGroup(event)) {
                    showNotification(formatMessage("userMovedToGroup", event.getUsers().get(0).getLogin(), event.getGroup().getName()));
                }
            } else {
                showOptionDialog(messages.getMainMessage("dialogs.Confirmation"), formatMessage("dialogs.moveToGroup.message", event.getGroup().getName(), event.getUsers().size()), MessageType.CONFIRMATION, new Action[] { new DialogAction(Type.OK).withHandler(dialogEvent -> {
                    if (moveSelectedUsersToGroup(event)) {
                        showNotification(formatMessage("usersMovedToGroup", event.getGroup().getName()));
                    }
                }), new DialogAction(Type.CANCEL, Action.Status.PRIMARY) });
            }
        });
    }
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) ReferenceImportBehaviour(com.haulmont.cuba.core.app.importexport.ReferenceImportBehaviour) LoggerFactory(org.slf4j.LoggerFactory) ParamsMap(com.haulmont.bali.util.ParamsMap) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) CreateAction(com.haulmont.cuba.gui.components.actions.CreateAction) Inject(javax.inject.Inject) ComponentsFactory(com.haulmont.cuba.gui.xml.layout.ComponentsFactory) Files(com.google.common.io.Files) CollectionImportPolicy(com.haulmont.cuba.core.app.importexport.CollectionImportPolicy) UserRemoveAction(com.haulmont.cuba.gui.app.security.user.browse.UserRemoveAction) ExportFormat(com.haulmont.cuba.gui.export.ExportFormat) Named(javax.inject.Named) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Logger(org.slf4j.Logger) EntityImportExportService(com.haulmont.cuba.core.app.importexport.EntityImportExportService) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) com.haulmont.cuba.security.entity(com.haulmont.cuba.security.entity) Type(com.haulmont.cuba.gui.components.DialogAction.Type) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EditAction(com.haulmont.cuba.gui.components.actions.EditAction) UserManagementService(com.haulmont.cuba.security.app.UserManagementService) Collectors(java.util.stream.Collectors) RemoveAction(com.haulmont.cuba.gui.components.actions.RemoveAction) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) EntityImportView(com.haulmont.cuba.core.app.importexport.EntityImportView) Consumer(java.util.function.Consumer) IOUtils(org.apache.commons.io.IOUtils) ByteArrayDataProvider(com.haulmont.cuba.gui.export.ByteArrayDataProvider) HierarchicalDatasource(com.haulmont.cuba.gui.data.HierarchicalDatasource) FileUploadingAPI(com.haulmont.cuba.gui.upload.FileUploadingAPI) ConstraintLocalizationService(com.haulmont.cuba.core.app.ConstraintLocalizationService) BaseUuidEntity(com.haulmont.cuba.core.entity.BaseUuidEntity) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) Entity(com.haulmont.cuba.core.entity.Entity) InputStream(java.io.InputStream) BaseUuidEntity(com.haulmont.cuba.core.entity.BaseUuidEntity) Entity(com.haulmont.cuba.core.entity.Entity) CreateAction(com.haulmont.cuba.gui.components.actions.CreateAction) UserRemoveAction(com.haulmont.cuba.gui.app.security.user.browse.UserRemoveAction) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) EditAction(com.haulmont.cuba.gui.components.actions.EditAction) RemoveAction(com.haulmont.cuba.gui.components.actions.RemoveAction) UserRemoveAction(com.haulmont.cuba.gui.app.security.user.browse.UserRemoveAction) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) EditAction(com.haulmont.cuba.gui.components.actions.EditAction) CreateAction(com.haulmont.cuba.gui.components.actions.CreateAction) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) HierarchicalDatasource(com.haulmont.cuba.gui.data.HierarchicalDatasource) MetaClass(com.haulmont.chile.core.model.MetaClass) UserRemoveAction(com.haulmont.cuba.gui.app.security.user.browse.UserRemoveAction) RemoveAction(com.haulmont.cuba.gui.components.actions.RemoveAction) File(java.io.File)

Example 3 with ItemTrackingAction

use of com.haulmont.cuba.gui.components.actions.ItemTrackingAction in project cuba by cuba-platform.

the class GroupBrowser method initConstraintsTab.

protected void initConstraintsTab() {
    if (constraintsTabInitialized) {
        return;
    }
    @SuppressWarnings("unchecked") Table<Constraint> constraintsTable = (Table) getComponentNN("constraintsTable");
    constraintCreateAction = new GroupPropertyCreateAction(constraintsTable);
    constraintsTable.addAction(constraintCreateAction);
    Action activateAction = new ItemTrackingAction("activate").withHandler(event -> {
        Constraint constraint = constraintsTable.getSingleSelected();
        if (constraint != null) {
            constraint.setIsActive(!Boolean.TRUE.equals(constraint.getIsActive()));
            constraintsDs.commit();
            constraintsDs.refresh();
        }
    });
    constraintsTable.addAction(activateAction);
    constraintsTable.addAction(new ConstraintLocalizationEditAction(constraintsTable));
    constraintsDs.addItemChangeListener(e -> {
        if (e.getItem() != null) {
            activateAction.setCaption(Boolean.TRUE.equals(e.getItem().getIsActive()) ? getMessage("deactivate") : getMessage("activate"));
        }
    });
    constraintsTable.addGeneratedColumn("entityName", constraint -> {
        if (StringUtils.isEmpty(constraint.getEntityName())) {
            return componentsFactory.createComponent(Label.class);
        }
        MetaClass metaClass = metadata.getClassNN(constraint.getEntityName());
        MetaClass effectiveMetaClass = metadata.getExtendedEntities().getEffectiveMetaClass(metaClass);
        Label label = componentsFactory.createComponent(Label.class);
        label.setValue(effectiveMetaClass.getName());
        return label;
    });
    constraintsTabInitialized = true;
    constraintsTable.refresh();
}
Also used : CreateAction(com.haulmont.cuba.gui.components.actions.CreateAction) UserRemoveAction(com.haulmont.cuba.gui.app.security.user.browse.UserRemoveAction) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) EditAction(com.haulmont.cuba.gui.components.actions.EditAction) RemoveAction(com.haulmont.cuba.gui.components.actions.RemoveAction) MetaClass(com.haulmont.chile.core.model.MetaClass) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction)

Example 4 with ItemTrackingAction

use of com.haulmont.cuba.gui.components.actions.ItemTrackingAction in project cuba by cuba-platform.

the class FileBrowser method init.

@Override
public void init(Map<String, Object> params) {
    super.init(params);
    filesTable.addAction(new ItemTrackingAction("download").withCaption(getMessage("download")).withHandler(event -> {
        FileDescriptor fileDescriptor = filesTable.getSingleSelected();
        if (fileDescriptor != null) {
            exportDisplay.show(fileDescriptor, null);
        }
    }));
    BaseAction multiUploadAction = new BaseAction("multiupload").withCaption(getMessage("multiupload")).withHandler(event -> {
        if (!security.isEntityOpPermitted(FileDescriptor.class, EntityOp.READ)) {
            throw new AccessDeniedException(PermissionType.ENTITY_OP, FileDescriptor.class.getSimpleName());
        }
        Window window = openWindow("multiuploadDialog", OpenType.DIALOG);
        window.addCloseListener(actionId -> {
            if (COMMIT_ACTION_ID.equals(actionId)) {
                Collection<FileDescriptor> items = ((MultiUploader) window).getFiles();
                for (FileDescriptor fdesc : items) {
                    boolean modified = filesDs.isModified();
                    filesDs.addItem(fdesc);
                    ((DatasourceImplementation) filesDs).setModified(modified);
                }
                filesTable.requestFocus();
            }
        });
    });
    multiUploadAction.setEnabled(security.isEntityOpPermitted(FileDescriptor.class, EntityOp.CREATE));
    multiUploadBtn.setAction(multiUploadAction);
}
Also used : Security(com.haulmont.cuba.core.global.Security) Table(com.haulmont.cuba.gui.components.Table) Collection(java.util.Collection) Button(com.haulmont.cuba.gui.components.Button) ExportDisplay(com.haulmont.cuba.gui.export.ExportDisplay) PermissionType(com.haulmont.cuba.security.entity.PermissionType) UUID(java.util.UUID) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) Inject(javax.inject.Inject) AbstractLookup(com.haulmont.cuba.gui.components.AbstractLookup) EntityOp(com.haulmont.cuba.security.entity.EntityOp) Window(com.haulmont.cuba.gui.components.Window) Map(java.util.Map) BaseAction(com.haulmont.cuba.gui.components.actions.BaseAction) AccessDeniedException(com.haulmont.cuba.core.global.AccessDeniedException) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) CollectionDatasource(com.haulmont.cuba.gui.data.CollectionDatasource) OpenType(com.haulmont.cuba.gui.WindowManager.OpenType) Window(com.haulmont.cuba.gui.components.Window) AccessDeniedException(com.haulmont.cuba.core.global.AccessDeniedException) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) BaseAction(com.haulmont.cuba.gui.components.actions.BaseAction) DatasourceImplementation(com.haulmont.cuba.gui.data.impl.DatasourceImplementation) FileDescriptor(com.haulmont.cuba.core.entity.FileDescriptor)

Example 5 with ItemTrackingAction

use of com.haulmont.cuba.gui.components.actions.ItemTrackingAction in project cuba by cuba-platform.

the class RoleBrowser method init.

@Override
public void init(Map<String, Object> params) {
    super.init(params);
    Action copyRoles = new ItemTrackingAction("copy").withCaption(getMessage("actions.Copy")).withHandler(event -> {
        userManagementService.copyRole(rolesTable.getSingleSelected().getId());
        rolesDs.refresh();
    });
    boolean hasPermissionsToCreateRole = security.isEntityOpPermitted(Role.class, EntityOp.CREATE);
    copyRoles.setEnabled(hasPermissionsToCreateRole);
    rolesTable.addAction(copyRoles);
    Action assignToUsersAction = new ItemTrackingAction(rolesTable, "assignToUsers").withCaption(getMessage("assignToUsers")).withHandler(event -> {
        Set<Role> selected = rolesTable.getSelected();
        if (selected.isEmpty()) {
            showNotification(getMessage("selectRole.msg"), NotificationType.HUMANIZED);
            return;
        }
        Role role = selected.iterator().next();
        Map<String, Object> userLookupParams = new HashMap<>();
        WindowParams.MULTI_SELECT.set(userLookupParams, true);
        openLookup(User.class, items -> {
            assignRoleUsers(role, items);
        }, OpenType.THIS_TAB, userLookupParams);
    });
    rolesTable.addAction(assignToUsersAction);
    boolean hasPermissionsToCreateUserRole = security.isEntityOpPermitted(UserRole.class, EntityOp.CREATE);
    Action copy = rolesTable.getAction("assignToUsers");
    if (copy != null) {
        copy.setEnabled(hasPermissionsToCreateUserRole);
    }
    String windowOpener = (String) params.get("param$windowOpener");
    if ("sec$User.edit".equals(windowOpener)) {
        rolesTable.setMultiSelect(true);
    }
    rolesDs.addItemPropertyChangeListener(e -> {
        if (DEFAULT_ROLE_PROPERTY.equals(e.getProperty())) {
            Role reloadedRole = dataManager.reload(e.getItem(), View.LOCAL);
            reloadedRole.setDefaultRole(e.getItem().getDefaultRole());
            rolesDs.updateItem(reloadedRole);
            rolesDs.modifyItem(reloadedRole);
            rolesDs.commit();
        }
    });
    importRolesUpload.addFileUploadSucceedListener(event -> {
        importRoles();
    });
    importRolesUpload.setCaption(null);
    importRolesUpload.setUploadButtonCaption(null);
}
Also used : ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction)

Aggregations

ItemTrackingAction (com.haulmont.cuba.gui.components.actions.ItemTrackingAction)7 Inject (javax.inject.Inject)4 MetaClass (com.haulmont.chile.core.model.MetaClass)3 OpenType (com.haulmont.cuba.gui.WindowManager.OpenType)3 com.haulmont.cuba.gui.components (com.haulmont.cuba.gui.components)3 RemoveAction (com.haulmont.cuba.gui.components.actions.RemoveAction)3 CollectionDatasource (com.haulmont.cuba.gui.data.CollectionDatasource)3 UUID (java.util.UUID)3 StringUtils (org.apache.commons.lang.StringUtils)3 ParamsMap (com.haulmont.bali.util.ParamsMap)2 Entity (com.haulmont.cuba.core.entity.Entity)2 UserRemoveAction (com.haulmont.cuba.gui.app.security.user.browse.UserRemoveAction)2 Type (com.haulmont.cuba.gui.components.DialogAction.Type)2 BaseAction (com.haulmont.cuba.gui.components.actions.BaseAction)2 CreateAction (com.haulmont.cuba.gui.components.actions.CreateAction)2 EditAction (com.haulmont.cuba.gui.components.actions.EditAction)2 RefreshAction (com.haulmont.cuba.gui.components.actions.RefreshAction)2 ComponentsFactory (com.haulmont.cuba.gui.xml.layout.ComponentsFactory)2 java.util (java.util)2 Map (java.util.Map)2