Search in sources :

Example 1 with EmailMessage

use of org.orcid.core.manager.EmailMessage in project ORCID-Source by ORCID.

the class EmailMessageSenderImpl method createDigest.

@Override
public EmailMessage createDigest(OrcidProfile orcidProfile, Collection<Notification> notifications, Locale locale) {
    int totalMessageCount = 0;
    int orcidMessageCount = 0;
    int addActivitiesMessageCount = 0;
    int amendedMessageCount = 0;
    int activityCount = 0;
    Set<String> memberIds = new HashSet<>();
    DigestEmail digestEmail = new DigestEmail();
    for (Notification notification : notifications) {
        digestEmail.addNotification(notification);
        totalMessageCount++;
        if (notification.getSource() == null) {
            orcidMessageCount++;
        } else {
            SourceClientId clientId = notification.getSource().getSourceClientId();
            if (clientId != null) {
                memberIds.add(clientId.getPath());
            }
        }
        if (notification instanceof NotificationPermission) {
            addActivitiesMessageCount++;
            NotificationPermission permissionNotification = (NotificationPermission) notification;
            activityCount += permissionNotification.getItems().getItems().size();
            permissionNotification.setEncryptedPutCode(encryptAndEncodePutCode(permissionNotification.getPutCode()));
        } else if (notification instanceof NotificationInstitutionalConnection) {
            notification.setEncryptedPutCode(encryptAndEncodePutCode(notification.getPutCode()));
        } else if (notification instanceof NotificationAmended) {
            amendedMessageCount++;
        }
    }
    String emailName = notificationManager.deriveEmailFriendlyName(orcidProfile);
    String subject = messages.getMessage("email.subject.digest", new String[] { emailName, String.valueOf(totalMessageCount) }, locale);
    Map<String, Object> params = new HashMap<>();
    params.put("locale", locale);
    params.put("messages", messages);
    params.put("messageArgs", new Object[0]);
    params.put("orcidProfile", orcidProfile);
    params.put("emailName", emailName);
    params.put("digestEmail", digestEmail);
    params.put("frequency", orcidProfile.getOrcidInternal().getPreferences().getSendEmailFrequencyDays());
    params.put("totalMessageCount", String.valueOf(totalMessageCount));
    params.put("orcidMessageCount", orcidMessageCount);
    params.put("addActivitiesMessageCount", addActivitiesMessageCount);
    params.put("activityCount", activityCount);
    params.put("amendedMessageCount", amendedMessageCount);
    params.put("memberIdsCount", memberIds.size());
    params.put("baseUri", orcidUrlManager.getBaseUrl());
    params.put("subject", subject);
    String bodyText = templateManager.processTemplate("digest_email.ftl", params, locale);
    String bodyHtml = templateManager.processTemplate("digest_email_html.ftl", params, locale);
    EmailMessage emailMessage = new EmailMessage();
    emailMessage.setSubject(subject);
    emailMessage.setBodyText(bodyText);
    emailMessage.setBodyHtml(bodyHtml);
    return emailMessage;
}
Also used : NotificationInstitutionalConnection(org.orcid.model.notification.institutional_sign_in_v2.NotificationInstitutionalConnection) EmailMessage(org.orcid.core.manager.EmailMessage) DigestEmail(org.orcid.pojo.DigestEmail) HashMap(java.util.HashMap) SourceClientId(org.orcid.jaxb.model.common_v2.SourceClientId) Notification(org.orcid.jaxb.model.notification_v2.Notification) NotificationPermission(org.orcid.jaxb.model.notification.permission_v2.NotificationPermission) HashSet(java.util.HashSet) NotificationAmended(org.orcid.jaxb.model.notification.amended_v2.NotificationAmended)

Example 2 with EmailMessage

use of org.orcid.core.manager.EmailMessage in project ORCID-Source by ORCID.

the class EmailMessageSenderImpl method sendEmailMessages.

@Override
public void sendEmailMessages() {
    LOGGER.info("About to send email messages");
    List<String> orcidsWithMessagesToSend = notificationDaoReadOnly.findOrcidsWithNotificationsToSend();
    for (final String orcid : orcidsWithMessagesToSend) {
        try {
            LOGGER.info("Sending messages for orcid: {}", orcid);
            List<Notification> notifications = notificationManager.findUnsentByOrcid(orcid);
            LOGGER.info("Found {} messages to send for orcid: {}", notifications.size(), orcid);
            EmailMessage digestMessage = createDigest(orcid, notifications);
            digestMessage.setFrom(DIGEST_FROM_ADDRESS);
            EmailEntity primaryEmail = emailDao.findPrimaryEmail(orcid);
            if (primaryEmail == null) {
                LOGGER.info("No primary email for orcid: " + orcid);
                return;
            }
            digestMessage.setTo(primaryEmail.getId());
            boolean successfullySent = mailGunManager.sendEmail(digestMessage.getFrom(), digestMessage.getTo(), digestMessage.getSubject(), digestMessage.getBodyText(), digestMessage.getBodyHtml());
            if (successfullySent) {
                flagAsSent(notifications);
            }
        } catch (RuntimeException e) {
            LOGGER.warn("Problem sending email message to user: " + orcid, e);
        }
    }
    LOGGER.info("Finished sending email messages");
}
Also used : EmailMessage(org.orcid.core.manager.EmailMessage) EmailEntity(org.orcid.persistence.jpa.entities.EmailEntity) Notification(org.orcid.jaxb.model.notification_v2.Notification)

Aggregations

EmailMessage (org.orcid.core.manager.EmailMessage)2 Notification (org.orcid.jaxb.model.notification_v2.Notification)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 SourceClientId (org.orcid.jaxb.model.common_v2.SourceClientId)1 NotificationAmended (org.orcid.jaxb.model.notification.amended_v2.NotificationAmended)1 NotificationPermission (org.orcid.jaxb.model.notification.permission_v2.NotificationPermission)1 NotificationInstitutionalConnection (org.orcid.model.notification.institutional_sign_in_v2.NotificationInstitutionalConnection)1 EmailEntity (org.orcid.persistence.jpa.entities.EmailEntity)1 DigestEmail (org.orcid.pojo.DigestEmail)1