@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);
      }
    }
  }
 public OrcidProfile getOrcidProfile(String orcid) {
   OrcidProfile orcidProfile = new OrcidProfile();
   orcidProfile.setOrcidIdentifier(orcid);
   OrcidBio orcidBio = new OrcidBio();
   orcidProfile.setOrcidBio(orcidBio);
   Delegation delegation = new Delegation();
   orcidBio.setDelegation(delegation);
   GivenPermissionTo givenPermissionTo = new GivenPermissionTo();
   delegation.setGivenPermissionTo(givenPermissionTo);
   DelegationDetails delegationDetails = new DelegationDetails();
   givenPermissionTo.getDelegationDetails().add(delegationDetails);
   DelegateSummary delegateSummary = new DelegateSummary(new OrcidIdentifier(delegateOrcid));
   delegationDetails.setDelegateSummary(delegateSummary);
   return orcidProfile;
 }
  private String extractAmenderName(OrcidProfile orcidProfile, String amenderId) {
    Delegation delegation = orcidProfile.getOrcidBio().getDelegation();
    if (delegation != null
        && delegation.getGivenPermissionTo() != null
        && !delegation.getGivenPermissionTo().getDelegationDetails().isEmpty()) {
      for (DelegationDetails delegationDetails :
          delegation.getGivenPermissionTo().getDelegationDetails()) {
        if (amenderId.equals(
            delegationDetails.getDelegateSummary().getOrcidIdentifier().getPath())) {
          return delegationDetails.getDelegateSummary().getCreditName().getContent();
        }
      }
    }

    ClientDetailsEntity clientDetailsEntity = clientDetailsEntityCacheManager.retrieve(amenderId);
    if (clientDetailsEntity != null) {
      return clientDetailsEntity.getClientName();
    }
    return "";
  }