class UserNamespaceAuthorizationHelper {

    public void buildNamespaceAuthorizations(ApplicationUser applicationUser)
    {
        // Get the user id from the application user.
        String userId = applicationUser.getUserId();

        // Check if user namespace authorization is not enabled or this user is a namespace authorization administrator.
        if (BooleanUtils.isNotTrue(configurationHelper.getBooleanProperty(ConfigurationValue.USER_NAMESPACE_AUTHORIZATION_ENABLED)) ||
            isNamespaceAuthorizationAdmin(userId))
        {
            // Assign all permissions for all namespaces configured in the system.
            applicationUser.setNamespaceAuthorizations(getAllNamespaceAuthorizations());
        }
        else
        {
            // Assign a set of namespace authorizations per specified user.
            Set<NamespaceAuthorization> namespaceAuthorizations = new HashSet<>();
            applicationUser.setNamespaceAuthorizations(namespaceAuthorizations);
            for (UserNamespaceAuthorizationEntity userNamespaceAuthorizationEntity : userNamespaceAuthorizationDao
                .getUserNamespaceAuthorizationsByUserId(userId))
            {
                namespaceAuthorizations.add(toNamespaceAuthorization(userNamespaceAuthorizationEntity));
            }

            // Search authorizations by wildcard token
            for (UserNamespaceAuthorizationEntity wildcardEntity : userNamespaceAuthorizationDao
                .getUserNamespaceAuthorizationsByUserIdStartsWith(WildcardHelper.WILDCARD_TOKEN))
            {
                if (wildcardHelper.matches(userId.toUpperCase(), wildcardEntity.getUserId().toUpperCase()))
                {
                    namespaceAuthorizations.add(toNamespaceAuthorization(wildcardEntity));
                }
            }
        }
    }

}