@Override
  @Transactional
  public void sendNotificationToAddedDelegate(
      OrcidProfile orcidUserGrantingPermission, List<DelegationDetails> delegatesGrantedByUser) {
    // Create map of template params
    Map<String, Object> templateParams = new HashMap<String, Object>();
    String subject = getSubject("email.subject.added_as_delegate", orcidUserGrantingPermission);

    for (DelegationDetails newDelegation : delegatesGrantedByUser) {
      ProfileEntity delegateProfileEntity =
          profileDao.find(newDelegation.getDelegateSummary().getOrcidIdentifier().getPath());
      Boolean sendAdministrativeChangeNotifications =
          delegateProfileEntity.getSendAdministrativeChangeNotifications();
      if (sendAdministrativeChangeNotifications == null || !sendAdministrativeChangeNotifications) {
        LOGGER.debug(
            "Not sending added delegate email, because option to send administrative change notifications not set to true for delegate: {}",
            delegateProfileEntity.getId());
        return;
      }

      String grantingOrcidEmail =
          orcidUserGrantingPermission
              .getOrcidBio()
              .getContactDetails()
              .retrievePrimaryEmail()
              .getValue();
      String emailNameForDelegate = deriveEmailFriendlyName(delegateProfileEntity);
      String email = delegateProfileEntity.getPrimaryEmail().getId();

      templateParams.put("emailNameForDelegate", emailNameForDelegate);
      templateParams.put(
          "grantingOrcidValue", orcidUserGrantingPermission.getOrcidIdentifier().getPath());
      templateParams.put("grantingOrcidName", deriveEmailFriendlyName(orcidUserGrantingPermission));
      templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
      templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
      templateParams.put("grantingOrcidEmail", grantingOrcidEmail);
      templateParams.put("subject", subject);

      addMessageParams(templateParams, orcidUserGrantingPermission);

      // Generate body from template
      String body = templateManager.processTemplate("added_as_delegate_email.ftl", templateParams);
      // Generate html from template
      String html =
          templateManager.processTemplate("added_as_delegate_email_html.ftl", templateParams);

      boolean notificationsEnabled = delegateProfileEntity.getEnableNotifications();
      if (notificationsEnabled) {
        NotificationCustom notification = new NotificationCustom();
        notification.setNotificationType(NotificationType.CUSTOM);
        notification.setSubject(subject);
        notification.setBodyHtml(html);
        createNotification(
            newDelegation.getDelegateSummary().getOrcidIdentifier().getPath(), notification);
      } else {
        mailGunManager.sendEmail(DELEGATE_NOTIFY_ORCID_ORG, email, subject, body, html);
      }
    }
  }
 @Override
 public ProfileEntity retrieveSourceProfileEntity() {
   String sourceOrcid = retrieveSourceOrcid();
   if (sourceOrcid == null) {
     return null;
   }
   return profileDao.find(sourceOrcid);
 }
  @Override
  public void sendDelegationRequestEmail(OrcidProfile managed, OrcidProfile trusted, String link) {
    // Create map of template params
    String orcid = managed.getOrcidIdentifier().getPath();
    Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put("link", link);

    String trustedOrcidValue = trusted.retrieveOrcidPath();
    String managedOrcidValue = managed.retrieveOrcidPath();
    String emailNameForDelegate = deriveEmailFriendlyName(managed);
    String trustedOrcidName = deriveEmailFriendlyName(trusted);
    templateParams.put("emailNameForDelegate", emailNameForDelegate);
    templateParams.put("trustedOrcidName", trustedOrcidName);
    templateParams.put("trustedOrcidValue", trustedOrcidValue);
    templateParams.put("managedOrcidValue", managedOrcidValue);

    Email primaryEmail = managed.getOrcidBio().getContactDetails().retrievePrimaryEmail();
    if (primaryEmail == null) {
      LOGGER.info("Cant send admin delegate email if primary email is null: {}", orcid);
      return;
    }

    addMessageParams(templateParams, managed);

    String htmlBody =
        templateManager.processTemplate("admin_delegate_request_html.ftl", templateParams);

    // Send message
    if (apiRecordCreationEmailEnabled) {
      String subject = getSubject("email.subject.admin_as_delegate", managed, trustedOrcidName);
      ProfileEntity trustedProfileEntity = profileDao.find(trusted.getOrcidIdentifier().getPath());
      boolean notificationsEnabled =
          trustedProfileEntity != null ? trustedProfileEntity.getEnableNotifications() : false;
      if (notificationsEnabled) {
        NotificationCustom notification = new NotificationCustom();
        notification.setNotificationType(NotificationType.CUSTOM);
        notification.setSubject(subject);
        notification.setBodyHtml(htmlBody);
        createNotification(managed.getOrcidIdentifier().getPath(), notification);
      } else {
        mailGunManager.sendEmail(
            DELEGATE_NOTIFY_ORCID_ORG, primaryEmail.getValue(), subject, null, htmlBody);
      }
      profileEventDao.persist(
          new ProfileEventEntity(orcid, ProfileEventType.ADMIN_PROFILE_DELEGATION_REQUEST));
    } else {
      LOGGER.debug(
          "Not sending admin delegate email, because API record creation email option is disabled. Message would have been: {}",
          htmlBody);
    }
  }
  @Override
  public void sendAmendEmail(
      OrcidProfile amendedProfile, AmendedSection amendedSection, Collection<Item> items) {
    String amenderOrcid = sourceManager.retrieveSourceOrcid();
    if (amenderOrcid == null) {
      LOGGER.debug("Not sending amend email, because amender is null: {}", amendedProfile);
      return;
    }
    if (amenderOrcid.equals(amendedProfile.getOrcidIdentifier().getPath())) {
      LOGGER.debug("Not sending amend email, because self edited: {}", amendedProfile);
      return;
    }
    SendChangeNotifications sendChangeNotifications =
        amendedProfile.getOrcidInternal().getPreferences().getSendChangeNotifications();
    if (sendChangeNotifications == null || !sendChangeNotifications.isValue()) {
      LOGGER.debug(
          "Not sending amend email, because option to send change notifications not set to true: {}",
          amendedProfile);
      return;
    }
    if (OrcidType.ADMIN.equals(profileDao.retrieveOrcidType(amenderOrcid))) {
      LOGGER.debug(
          "Not sending amend email, because modified by admin ({}): {}",
          amenderOrcid,
          amendedProfile);
      return;
    }

    String subject = getSubject("email.subject.amend", amendedProfile);

    // Create map of template params
    Map<String, Object> templateParams = new HashMap<String, Object>();
    templateParams.put("emailName", deriveEmailFriendlyName(amendedProfile));
    templateParams.put("orcid", amendedProfile.getOrcidIdentifier().getPath());
    templateParams.put("amenderName", extractAmenderName(amendedProfile, amenderOrcid));
    templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
    templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
    templateParams.put("subject", subject);

    addMessageParams(templateParams, amendedProfile);

    // Generate body from template
    String body = templateManager.processTemplate("amend_email.ftl", templateParams);
    // Generate html from template
    String html = templateManager.processTemplate("amend_email_html.ftl", templateParams);

    boolean notificationsEnabled =
        profileDao.find(amendedProfile.getOrcidIdentifier().getPath()).getEnableNotifications();
    if (notificationsEnabled) {
      NotificationAmended notification = new NotificationAmended();
      notification.setNotificationType(NotificationType.AMENDED);
      notification.setAmendedSection(amendedSection);
      if (items != null) {
        notification.setItems(new Items(new ArrayList<>(items)));
      }
      createNotification(amendedProfile.getOrcidIdentifier().getPath(), notification);
    } else {
      String email =
          amendedProfile.getOrcidBio().getContactDetails().retrievePrimaryEmail().getValue();
      mailGunManager.sendEmail(AMEND_NOTIFY_ORCID_ORG, email, subject, body, html);
    }
  }
 @Override
 public Long getCount() {
   return profileDao.getConfirmedProfileCount();
 }