protected void notifyCommunityAdministrators(MembershipRequest membershipRequest)
      throws IOException, PortalException, SystemException {

    List<UserGroupRole> admins = new UniqueList<UserGroupRole>();

    Role communityAdminRole =
        roleLocalService.getRole(
            membershipRequest.getCompanyId(), RoleConstants.COMMUNITY_ADMINISTRATOR);

    List<UserGroupRole> communityAdmins =
        userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
            membershipRequest.getGroupId(), communityAdminRole.getRoleId());

    admins.addAll(communityAdmins);

    Role communityOwnerRole =
        rolePersistence.findByC_N(membershipRequest.getCompanyId(), RoleConstants.COMMUNITY_OWNER);

    List<UserGroupRole> communityOwners =
        userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
            membershipRequest.getGroupId(), communityOwnerRole.getRoleId());

    admins.addAll(communityOwners);

    for (UserGroupRole userGroupRole : admins) {
      notify(
          userGroupRole.getUserId(),
          membershipRequest,
          PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_SUBJECT,
          PropsKeys.COMMUNITIES_EMAIL_MEMBERSHIP_REQUEST_BODY);
    }
  }
  protected void notify(
      long userId, MembershipRequest membershipRequest, String subjectProperty, String bodyProperty)
      throws IOException, PortalException, SystemException {

    Company company = companyPersistence.findByPrimaryKey(membershipRequest.getCompanyId());

    Group group = groupPersistence.findByPrimaryKey(membershipRequest.getGroupId());

    User user = userPersistence.findByPrimaryKey(userId);
    User requestUser = userPersistence.findByPrimaryKey(membershipRequest.getUserId());

    String fromName =
        PrefsPropsUtil.getString(
            membershipRequest.getCompanyId(), PropsKeys.COMMUNITIES_EMAIL_FROM_NAME);

    String fromAddress =
        PrefsPropsUtil.getString(
            membershipRequest.getCompanyId(), PropsKeys.COMMUNITIES_EMAIL_FROM_ADDRESS);

    String toName = user.getFullName();
    String toAddress = user.getEmailAddress();

    String subject = PrefsPropsUtil.getContent(membershipRequest.getCompanyId(), subjectProperty);

    String body = PrefsPropsUtil.getContent(membershipRequest.getCompanyId(), bodyProperty);

    String statusKey = null;

    if (membershipRequest.getStatusId() == MembershipRequestConstants.STATUS_APPROVED) {

      statusKey = "approved";
    } else if (membershipRequest.getStatusId() == MembershipRequestConstants.STATUS_DENIED) {

      statusKey = "denied";
    } else {
      statusKey = "pending";
    }

    subject =
        StringUtil.replace(
            subject,
            new String[] {
              "[$COMMUNITY_NAME$]",
              "[$COMPANY_ID$]",
              "[$COMPANY_MX$]",
              "[$COMPANY_NAME$]",
              "[$FROM_ADDRESS$]",
              "[$FROM_NAME$]",
              "[$PORTAL_URL$]",
              "[$REQUEST_USER_ADDRESS$]",
              "[$REQUEST_USER_NAME$]",
              "[$STATUS$]",
              "[$TO_NAME$]",
              "[$USER_ADDRESS$]",
              "[$USER_NAME$]",
            },
            new String[] {
              group.getName(),
              String.valueOf(company.getCompanyId()),
              company.getMx(),
              company.getName(),
              fromAddress,
              fromName,
              company.getVirtualHost(),
              requestUser.getEmailAddress(),
              requestUser.getFullName(),
              LanguageUtil.get(user.getLocale(), statusKey),
              toName,
              user.getEmailAddress(),
              user.getFullName()
            });

    body =
        StringUtil.replace(
            body,
            new String[] {
              "[$COMMENTS$]",
              "[$COMMUNITY_NAME$]",
              "[$COMPANY_ID$]",
              "[$COMPANY_MX$]",
              "[$COMPANY_NAME$]",
              "[$FROM_ADDRESS$]",
              "[$FROM_NAME$]",
              "[$PORTAL_URL$]",
              "[$REPLY_COMMENTS$]",
              "[$REQUEST_USER_NAME$]",
              "[$REQUEST_USER_ADDRESS$]",
              "[$STATUS$]",
              "[$TO_NAME$]",
              "[$USER_ADDRESS$]",
              "[$USER_NAME$]",
            },
            new String[] {
              membershipRequest.getComments(),
              group.getName(),
              String.valueOf(company.getCompanyId()),
              company.getMx(),
              company.getName(),
              fromAddress,
              fromName,
              company.getVirtualHost(),
              membershipRequest.getReplyComments(),
              requestUser.getFullName(),
              requestUser.getEmailAddress(),
              LanguageUtil.get(user.getLocale(), statusKey),
              toName,
              user.getEmailAddress(),
              user.getFullName()
            });

    InternetAddress from = new InternetAddress(fromAddress, fromName);

    InternetAddress to = new InternetAddress(toAddress, toName);

    MailMessage message = new MailMessage(from, to, subject, body, true);

    mailService.sendEmail(message);
  }