Beispiel #1
0
  public static Client valueOf(ClientDetailsEntity clientDetails) {
    Client client = new Client();
    if (clientDetails != null) {
      client.setClientId(Text.valueOf(clientDetails.getClientId()));
      client.setDisplayName(Text.valueOf(clientDetails.getClientName()));
      client.setShortDescription(Text.valueOf(clientDetails.getClientDescription()));
      client.setWebsite(Text.valueOf(clientDetails.getClientWebsite()));
      client.redirectUris = new ArrayList<RedirectUri>();
      if (clientDetails.getClientRegisteredRedirectUris() != null) {
        for (ClientRedirectUriEntity rUri : clientDetails.getClientRegisteredRedirectUris()) {
          client.redirectUris.add(RedirectUri.valueOf(rUri));
        }
      }

      client.persistentTokenEnabled = new Checkbox();
      client.persistentTokenEnabled.setValue(clientDetails.isPersistentTokensEnabled());
      client.setType(Text.valueOf(clientDetails.getClientType().value()));

      if (clientDetails.isScoped()) client.setScopes(clientDetails.getScope());

      client.setMemberId(Text.valueOf(clientDetails.getGroupProfileId()));
    }
    return client;
  }
  @Override
  @Transactional
  public void sendApiRecordCreationEmail(String toEmail, OrcidProfile createdProfile) {

    Source source = null;
    CustomEmailEntity customEmail = null;
    if (createdProfile.getOrcidHistory() != null
        && createdProfile.getOrcidHistory().getSource() != null) {
      if (!PojoUtil.isEmpty(createdProfile.getOrcidHistory().getSource().retrieveSourcePath())) {
        source = createdProfile.getOrcidHistory().getSource();
        customEmail =
            getCustomizedEmail(
                createdProfile.getOrcidHistory().getSource().retrieveSourcePath(), EmailType.CLAIM);
      }
    }

    String email =
        createdProfile.getOrcidBio().getContactDetails().retrievePrimaryEmail().getValue().trim();
    String emailName = deriveEmailFriendlyName(createdProfile);
    String orcid = createdProfile.getOrcidIdentifier().getPath();
    String verificationUrl = createClaimVerificationUrl(email, orcidUrlManager.getBaseUrl());

    String creatorName = "";
    if (source != null) {
      if (source.getSourceName() != null && source.getSourceName().getContent() != null) {
        creatorName = source.getSourceName().getContent();
      } else if (!PojoUtil.isEmpty(source.retrieveSourcePath())) {
        creatorName = source.retrieveSourcePath();
      }
    }

    String subject = null;
    String body = null;
    String htmlBody = null;
    String sender = null;

    if (customEmail != null) {
      // Get the customized sender if available
      sender =
          PojoUtil.isEmpty(customEmail.getSender())
              ? CLAIM_NOTIFY_ORCID_ORG
              : customEmail.getSender();
      // Get the customized subject is available
      subject =
          PojoUtil.isEmpty(customEmail.getSubject())
              ? getSubject("email.subject.api_record_creation", createdProfile)
              : customEmail.getSubject();
      // Replace the wildcards
      subject = subject.replace(WILDCARD_USER_NAME, emailName);
      subject = subject.replace(WILDCARD_MEMBER_NAME, creatorName);
      if (customEmail.isHtml()) {
        htmlBody = customEmail.getContent();
        htmlBody = htmlBody.replace(WILDCARD_USER_NAME, emailName);
        htmlBody = htmlBody.replace(WILDCARD_MEMBER_NAME, creatorName);
        htmlBody = htmlBody.replace(EmailConstants.WILDCARD_VERIFICATION_URL, verificationUrl);
        if (htmlBody.contains(WILDCARD_WEBSITE) || htmlBody.contains(WILDCARD_DESCRIPTION)) {
          ClientDetailsEntity clientDetails = customEmail.getClientDetailsEntity();
          htmlBody = htmlBody.replace(WILDCARD_WEBSITE, clientDetails.getClientWebsite());
          htmlBody = htmlBody.replace(WILDCARD_DESCRIPTION, clientDetails.getClientDescription());
        }
      } else {
        body = customEmail.getContent();
        body = body.replace(WILDCARD_USER_NAME, emailName);
        body = body.replace(WILDCARD_MEMBER_NAME, creatorName);
        body = body.replace(EmailConstants.WILDCARD_VERIFICATION_URL, verificationUrl);
        if (body.contains(WILDCARD_WEBSITE) || body.contains(WILDCARD_DESCRIPTION)) {
          ClientDetailsEntity clientDetails = customEmail.getClientDetailsEntity();
          body = body.replace(WILDCARD_WEBSITE, clientDetails.getClientWebsite());
          body = body.replace(WILDCARD_DESCRIPTION, clientDetails.getClientDescription());
        }
      }
    } else {
      subject = getSubject("email.subject.api_record_creation", createdProfile);
      // Create map of template params
      Map<String, Object> templateParams = new HashMap<String, Object>();
      templateParams.put("emailName", emailName);
      templateParams.put("orcid", orcid);
      templateParams.put("subject", subject);
      templateParams.put("creatorName", creatorName);
      templateParams.put("baseUri", orcidUrlManager.getBaseUrl());
      templateParams.put("baseUriHttp", orcidUrlManager.getBaseUriHttp());
      templateParams.put("verificationUrl", verificationUrl);

      addMessageParams(templateParams, createdProfile);
      // Generate body from template
      body = templateManager.processTemplate("api_record_creation_email.ftl", templateParams);
      htmlBody =
          templateManager.processTemplate("api_record_creation_email_html.ftl", templateParams);
    }

    // Send message
    if (apiRecordCreationEmailEnabled) {
      boolean isCustomEmail = customEmail != null ? true : false;
      // TODO: How to handle sender? we might have to register them on
      // mailgun
      if (isCustomEmail) {
        mailGunManager.sendEmail(sender, email, subject, body, htmlBody, isCustomEmail);
      } else {
        mailGunManager.sendEmail(CLAIM_NOTIFY_ORCID_ORG, email, subject, body, htmlBody);
      }
    } else {
      LOGGER.debug(
          "Not sending API record creation email, because option is disabled. Message would have been: {}",
          body);
    }
  }