public static String[] getUserRank(
      PortletPreferences preferences, String languageId, MBStatsUser statsUser) throws Exception {

    String[] rank = {StringPool.BLANK, StringPool.BLANK};

    int maxPosts = 0;

    Group group = GroupLocalServiceUtil.getGroup(statsUser.getGroupId());

    long companyId = group.getCompanyId();

    String[] ranks = LocalizationUtil.getPreferencesValues(preferences, "ranks", languageId);

    for (int i = 0; i < ranks.length; i++) {
      String[] kvp = StringUtil.split(ranks[i], CharPool.EQUAL);

      String curRank = kvp[0];
      String curRankValue = kvp[1];

      String[] curRankValueKvp = StringUtil.split(curRankValue, CharPool.COLON);

      if (curRankValueKvp.length <= 1) {
        int posts = GetterUtil.getInteger(curRankValue);

        if ((posts <= statsUser.getMessageCount()) && (posts >= maxPosts)) {

          rank[0] = curRank;
          maxPosts = posts;
        }

      } else {
        String entityType = curRankValueKvp[0];
        String entityValue = curRankValueKvp[1];

        try {
          if (_isEntityRank(companyId, statsUser, entityType, entityValue)) {

            rank[1] = curRank;

            break;
          }
        } catch (Exception e) {
          if (_log.isWarnEnabled()) {
            _log.warn(e);
          }
        }
      }
    }

    return rank;
  }
  private static boolean _isEntityRank(
      long companyId, MBStatsUser statsUser, String entityType, String entityValue)
      throws Exception {

    long groupId = statsUser.getGroupId();
    long userId = statsUser.getUserId();

    if (entityType.equals("organization-role") || entityType.equals("site-role")) {

      Role role = RoleLocalServiceUtil.getRole(companyId, entityValue);

      if (UserGroupRoleLocalServiceUtil.hasUserGroupRole(userId, groupId, role.getRoleId(), true)) {

        return true;
      }
    } else if (entityType.equals("organization")) {
      Organization organization =
          OrganizationLocalServiceUtil.getOrganization(companyId, entityValue);

      if (OrganizationLocalServiceUtil.hasUserOrganization(
          userId, organization.getOrganizationId(), false, false)) {

        return true;
      }
    } else if (entityType.equals("regular-role")) {
      if (RoleLocalServiceUtil.hasUserRole(userId, companyId, entityValue, true)) {

        return true;
      }
    } else if (entityType.equals("user-group")) {
      UserGroup userGroup = UserGroupLocalServiceUtil.getUserGroup(companyId, entityValue);

      if (UserLocalServiceUtil.hasUserGroupUser(userGroup.getUserGroupId(), userId)) {

        return true;
      }
    }

    return false;
  }