From 9b44b4006e5d7e9297cfd91cbd179d4549b765c5 Mon Sep 17 00:00:00 2001 From: jensp Date: Thu, 29 Jun 2017 15:21:18 +0000 Subject: [PATCH] CCM NG/ccm-core: Some work for the Vaadin prototype git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4825 8810af33-2d31-482b-a856-94f89814c4df --- .../ui/usersgroupsroles/GroupDetails.java | 46 ++++++- .../usersgroupsroles/RoleSelectionAction.java | 34 +++++ .../ui/usersgroupsroles/RoleSelector.java | 119 +++++++++++++++++ .../RoleSelectorDataProvider.java | 125 ++++++++++++++++++ 4 files changed, 319 insertions(+), 5 deletions(-) create mode 100644 ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectionAction.java create mode 100644 ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelector.java create mode 100644 ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectorDataProvider.java diff --git a/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/GroupDetails.java b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/GroupDetails.java index af624f4dc..7e91d9c44 100644 --- a/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/GroupDetails.java +++ b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/GroupDetails.java @@ -45,6 +45,8 @@ import org.libreccm.cdi.utils.CdiUtil; import org.libreccm.security.Group; import org.libreccm.security.GroupManager; import org.libreccm.security.GroupRepository; +import org.libreccm.security.Role; +import org.libreccm.security.RoleRepository; import org.libreccm.security.User; import org.libreccm.security.UserRepository; @@ -64,6 +66,9 @@ public class GroupDetails extends Window { private static final String COL_EMAIL = "email"; private static final String COL_REMOVE = "remove"; + private static final String COL_ROLE_NAME = "role_name"; + private static final String COL_ROLE_REMOVE = "remove"; + private final UsersGroupsRoles usersGroupsRoles; private final Group group; private final GroupRepository groupRepo; @@ -152,7 +157,8 @@ public class GroupDetails extends Window { dataHasChanged = false; final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); - final GroupMembersController controller = cdiUtil + + final GroupMembersController membersController = cdiUtil .findBean(GroupMembersController.class); final Grid membersGrid = new Grid<>(); @@ -172,8 +178,8 @@ public class GroupDetails extends Window { membersGrid.addColumn(user -> bundle.getString( "ui.groups.members.remove"), new ButtonRenderer<>(event -> { - controller - .removeMemberFromGroup(event.getItem(), + membersController + .removeMemberFromGroup(event.getItem(), group); membersGrid.getDataProvider().refreshAll(); })) @@ -195,7 +201,8 @@ public class GroupDetails extends Window { userRepo.findByGroup(group), (selectedUsers -> { selectedUsers.forEach(user -> { - controller.addMembersToGroup(selectedUsers, group); + membersController + .addMembersToGroup(selectedUsers, group); membersGrid.getDataProvider().refreshAll(); }); })); @@ -217,8 +224,37 @@ public class GroupDetails extends Window { dataProvider.setGroup(group); membersGrid.setDataProvider(dataProvider); - //ToDo Add roles grid + final GroupRolesController rolesController = cdiUtil + .findBean(GroupRolesController.class); + final Grid rolesGrid = new Grid<>(); + rolesGrid + .addColumn(Role::getName) + .setId(COL_ROLE_NAME) + .setCaption("Role Name"); + rolesGrid + .addColumn(role -> bundle + .getString("ui.groups.roles.remove"), + new ButtonRenderer<>(event -> { + rolesController + .removeRoleFromGroup(event.getItem(), group); + rolesGrid.getDataProvider().refreshAll(); + })) + .setId(COL_ROLE_REMOVE); + rolesGrid.setWidth("100%"); + + final RoleRepository roleRepository = cdiUtil + .findBean(RoleRepository.class); + + final HeaderRow rolesGridHeader = rolesGrid.prependHeaderRow(); + final Button addRoleButton = new Button("Add role"); + addRoleButton.setIcon(VaadinIcons.PLUS); + addRoleButton.setStyleName(ValoTheme.BUTTON_TINY); + addRoleButton.addClickListener(event -> { + + }); + //ToDo Add roles grid + final TabSheet tabs = new TabSheet(); tabs.addTab(membersGrid, "Members"); tabs.addTab(new Label("Roles Placeholder"), "Roles"); diff --git a/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectionAction.java b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectionAction.java new file mode 100644 index 000000000..706a7d0d3 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectionAction.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.admin.ui.usersgroupsroles; + +import org.libreccm.security.Role; + +import java.util.Set; + +/** + * + * @author Jens Pelzetter + */ +@FunctionalInterface +public interface RoleSelectionAction { + + void action(Set selectedRoles); + +} diff --git a/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelector.java b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelector.java new file mode 100644 index 000000000..f36ad8f17 --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelector.java @@ -0,0 +1,119 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.admin.ui.usersgroupsroles; + +import com.arsdigita.ui.admin.AdminUiConstants; + +import com.vaadin.cdi.CDIUI; +import com.vaadin.icons.VaadinIcons; +import com.vaadin.ui.Button; +import com.vaadin.ui.Grid; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.UI; +import com.vaadin.ui.Window; +import com.vaadin.ui.components.grid.HeaderCell; +import com.vaadin.ui.components.grid.HeaderRow; +import com.vaadin.ui.themes.ValoTheme; +import org.libreccm.cdi.utils.CdiUtil; +import org.libreccm.security.Role; +import org.libreccm.security.RoleRepository; + +import java.util.List; +import java.util.ResourceBundle; + +/** + * + * @author Jens Pelzetter + */ +public class RoleSelector extends Window { + + private static final long serialVersionUID = -1437536052155383270L; + + private static final String COL_NAME = "rolename"; + + private final RoleRepository roleRepo; + + private final RoleSelectionAction roleSelectionAction; + + public RoleSelector(final String caption, + final String actionLabel, + final UsersGroupsRoles usersGroupsRoles, + final List excludedRoles, + final RoleSelectionAction action) { + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + roleRepo = cdiUtil.findBean(RoleRepository.class); + this.roleSelectionAction = action; + + addWidgets(caption, actionLabel, excludedRoles, action); + } + + private void addWidgets(final String caption, + final String actionLabel, + final List excludedRoles, + final RoleSelectionAction action) { + + setCaption(caption); + + final ResourceBundle bundle = ResourceBundle + .getBundle(AdminUiConstants.ADMIN_BUNDLE, + UI.getCurrent().getLocale()); + + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + + final Grid rolesGrid = new Grid<>(); + rolesGrid + .addColumn(Role::getName) + .setId(COL_NAME) + .setCaption("Role"); + + rolesGrid.setSelectionMode(Grid.SelectionMode.MULTI); + rolesGrid.setWidth("100%"); + + final Button actionButton = new Button(actionLabel); + actionButton.addClickListener(event -> { + action.action(rolesGrid.getSelectedItems()); + close(); + }); + + actionButton.setIcon(VaadinIcons.PLUS_CIRCLE_O); + actionButton.setStyleName(ValoTheme.BUTTON_TINY); + + final Button clearButton = new Button("Clear selection"); + clearButton.addClickListener(event -> { + rolesGrid.getSelectionModel().deselectAll(); + }); + clearButton.setIcon(VaadinIcons.BACKSPACE); + clearButton.setStyleName(ValoTheme.BUTTON_TINY); + + final HeaderRow actions = rolesGrid.prependHeaderRow(); + final HeaderCell actionsCell = actions.join(COL_NAME); + actionsCell.setComponent(new HorizontalLayout(actionButton, + clearButton)); + + final RoleSelectorDataProvider dataProvider = cdiUtil + .findBean(RoleSelectorDataProvider.class); + + dataProvider.setExcludedRoles(excludedRoles); + + rolesGrid.setDataProvider(dataProvider); + + setContent(rolesGrid); + } +} diff --git a/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectorDataProvider.java b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectorDataProvider.java new file mode 100644 index 000000000..9cb8e54fe --- /dev/null +++ b/ccm-core/src/main/java/org/libreccm/admin/ui/usersgroupsroles/RoleSelectorDataProvider.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.admin.ui.usersgroupsroles; + +import com.vaadin.cdi.ViewScoped; +import com.vaadin.data.provider.AbstractDataProvider; +import com.vaadin.data.provider.Query; +import org.libreccm.security.Role; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@ViewScoped +public class RoleSelectorDataProvider extends AbstractDataProvider { + + private static final long serialVersionUID = 3915041291561733758L; + + @Inject + private EntityManager entityManager; + + private String roleNameFilter; + + private List excludedRoles; + + @Override + public boolean isInMemory() { + return false; + } + + @Transactional(Transactional.TxType.REQUIRED) + @Override + public int size(final Query query) { + final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + final CriteriaQuery criteriaQuery = builder + .createQuery(Long.class); + final Root from = criteriaQuery.from(Role.class); + + criteriaQuery + .select(builder.count(from)) + .distinct(true); + + if (roleNameFilter != null && !roleNameFilter.trim().isEmpty()) { + criteriaQuery + .where(builder.like(builder.lower(from.get("name")), + String.format("%s%%", roleNameFilter))); + } + + if (excludedRoles != null && !excludedRoles.isEmpty()) { + criteriaQuery.where(builder.not(from.in(excludedRoles))); + } + + return entityManager + .createQuery(criteriaQuery) + .getSingleResult() + .intValue(); + } + + @Transactional(Transactional.TxType.REQUIRED) + @Override + public Stream fetch(final Query query) { + final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); + final CriteriaQuery criteriaQuery = builder + .createQuery(Role.class); + final Root from = criteriaQuery.from(Role.class); + + criteriaQuery.distinct(true); + + if (roleNameFilter != null && !roleNameFilter.trim().isEmpty()) { + criteriaQuery + .where(builder.like(builder.lower(from.get("name")), + String.format("%s%%", roleNameFilter))); + } + + if (excludedRoles != null && !excludedRoles.isEmpty()) { + criteriaQuery.where(builder.not(from.in(excludedRoles))); + } + + criteriaQuery.orderBy(builder.asc(from.get("name"))); + + return entityManager + .createQuery(criteriaQuery) + .setMaxResults(query.getLimit()) + .setFirstResult(query.getOffset()) + .getResultList() + .stream(); + } + + public void setRoleNameFilter(final String roleNameFilter) { + this.roleNameFilter = roleNameFilter; + refreshAll(); + } + + public void setExcludedRoles(final List excluededRoles) { + this.excludedRoles = new ArrayList<>(excluededRoles); + refreshAll(); + } +}