class MapperUtils {

    public static String buildPrincipalName(String principal, @Nullable String userPrefix, @Nullable String userDomain)
    {
        boolean userDomainIncluded = StringUtils.substringAfterLast(principal, "@").equals(userDomain);

        if (StringUtils.isNotBlank(userPrefix) && !StringUtils.startsWith(principal, userPrefix))
        {
            principal = String.format("%s.%s", userPrefix, principal);
            if (userDomainIncluded)
            {
                String userIdDomainTruncated = StringUtils.substringBeforeLast(principal, "@");
                principal = StringUtils.left(userIdDomainTruncated, 20);
                principal = String.format("%s@%s", principal, userDomain);
            }
            else
            {
                principal = StringUtils.left(principal, 20);
            }
        }

        if (StringUtils.isNotBlank(userDomain) && !userDomainIncluded)
        {
            principal = String.format("%s@%s", principal, userDomain);
        }

        return principal;
    }

}