RoleAccountManagementBulkAction extends AbstractBulkAction<IdmRoleDto, IdmRoleFilter> { @Override public ResultModels prevalidate() { IdmBulkActionDto action = getAction(); List<UUID> entities = getEntities(action, new StringBuilder()); ResultModels result = new ResultModels(); Map<ResultModel, Long> models = new HashMap<>(); entities.forEach(roleId -> { IdmIdentityRoleFilter identityRoleFilter = new IdmIdentityRoleFilter(); identityRoleFilter.setRoleId(roleId); IdmRoleDto role = getService().get(roleId); long count = identityRoleService.find(identityRoleFilter, PageRequest.of(0, 1)).getTotalElements(); if (count > 0) { models.put(new DefaultResultModel(AccResultCode.ROLE_ACM_BULK_ACTION_NUMBER_OF_IDENTITIES, ImmutableMap.of("role", role.getCode(), "count", count)), count); } }); boolean someIdentitiesFound = models.values() .stream() .filter(count -> count > 0) .findFirst() .isPresent(); if (!someIdentitiesFound) { result.addInfo(new DefaultResultModel(AccResultCode.ROLE_ACM_BULK_ACTION_NONE_IDENTITIES)); } else { List<Entry<ResultModel, Long>> collect = models .entrySet() .stream() .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) .limit(5) .collect(Collectors.toList()); collect.forEach(entry -> { result.addInfo(entry.getKey()); }); } return result; }  @Override ResultModels prevalidate(); @Override String getName(); @Override int getOrder(); @Override ReadWriteDtoService<IdmRoleDto, IdmRoleFilter> getService(); static final String NAME; }
@Test public void testPrevalidationBulkAction() { IdmRoleDto role = getHelper().createRole(); IdmBulkActionDto bulkAction = this.findBulkAction(IdmRole.class, RoleAccountManagementBulkAction.NAME); bulkAction.getIdentifiers().add(role.getId()); ResultModels resultModels = bulkActionManager.prevalidate(bulkAction); assertEquals(1, resultModels.getInfos().size()); assertEquals(AccResultCode.ROLE_ACM_BULK_ACTION_NONE_IDENTITIES.getCode(), resultModels.getInfos().get(0).getStatusEnum()); IdmIdentityDto identity = getHelper().createIdentity(); getHelper().createIdentityRole(identity, role); resultModels = bulkActionManager.prevalidate(bulkAction); assertEquals(1, resultModels.getInfos().size()); assertEquals(AccResultCode.ROLE_ACM_BULK_ACTION_NUMBER_OF_IDENTITIES.getCode(), resultModels.getInfos().get(0).getStatusEnum()); }