class GroupCreationWorkflow implements IsWidget {

    protected void checkCreate() {
        final String identifier = createEntity.getEntityIdentifier();
        if (identifier != null) {
            loadingBox.show();

            // Check constrained groups, they cannot be created (such as registered roles).
            final Collection<String> constrainedGroups = userSystemManager.getConstrainedGroups();
            if (null != constrainedGroups && constrainedGroups.contains(identifier)) {

                loadingBox.hide();

                // Registered role found with this identifier, so name is not valid.
                showErrorMessage(UsersManagementClientConstants.INSTANCE.roleAlreadyExists());
                createEntity.setErrorState();
            } else {

                userSystemManager.groups(o -> {
                                             loadingBox.hide();

                                             // Group found, so name is not valid.
                                             showErrorMessage(UsersManagementClientConstants.INSTANCE.groupAlreadyExists());
                                             createEntity.setErrorState();
                                         },
                                         new ErrorCallback<Message>() {
                                             @Override
                                             public boolean error(final Message o,
                                                                  final Throwable throwable) {
                                                 loadingBox.hide();
                                                 Throwable error = throwable;
                                                 if (throwable instanceof GroupNotFoundException) {
                                                     // Group not found, so name is valid.
                                                     createGroup(identifier);
                                                     error = null;
                                                 } else if (throwable instanceof InvalidEntityIdentifierException) {
                                                     error = new SecurityManagementException(getGroupIdentifierNotValidMessage((InvalidEntityIdentifierException) throwable),
                                                                                             throwable);
                                                 }
                                                 // On error,
                                                 if (null != error) {
                                                     showError(throwable);
                                                     create();
                                                 }
                                                 return false;
                                             }
                                         }).get(identifier);
            }
        }
    }

    @Inject public  GroupCreationWorkflow(final ClientUserSystemManager userSystemManager,
                                 final Caller<AuthorizationService> authorizationService,
                                 final PermissionManager permissionManager,
                                 final Event<OnErrorEvent> errorEvent,
                                 final ConfirmBox confirmBox,
                                 final LoadingBox loadingBox,
                                 final Event<NotificationEvent> workbenchNotification,
                                 final CreateEntity createEntity,
                                 final GroupUsersAssignment groupUsersAssignment,
                                 final Event<CreateGroupEvent> onCreateGroupEvent,
                                 final EntityWorkflowView view);

}