Search in sources :

Example 1 with CustomEmailEntity

use of org.orcid.persistence.jpa.entities.CustomEmailEntity in project ORCID-Source by ORCID.

the class NotificationManagerImpl method sendApiRecordCreationEmail.

@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;
        // 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);
    }
}
Also used : ClientDetailsEntity(org.orcid.persistence.jpa.entities.ClientDetailsEntity) HashMap(java.util.HashMap) CustomEmailEntity(org.orcid.persistence.jpa.entities.CustomEmailEntity) Source(org.orcid.jaxb.model.message.Source) MessageSource(org.springframework.context.MessageSource) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with CustomEmailEntity

use of org.orcid.persistence.jpa.entities.CustomEmailEntity in project ORCID-Source by ORCID.

the class CustomEmailManagerTest method testUpdateCustomEmail.

@Rollback
public void testUpdateCustomEmail() {
    // Check old values
    List<CustomEmailEntity> customEmails = customEmailManager.getCustomEmails("4444-4444-4444-4441");
    assertNotNull(customEmails);
    assertEquals(1, customEmails.size());
    CustomEmailEntity customEmail = customEmails.get(0);
    assertNotNull(customEmail);
    assertEquals("This is the content", customEmail.getContent());
    assertEquals(EmailType.CLAIM, customEmail.getEmailType());
    assertEquals("angel.montenegro.jimenez@gmail.com", customEmail.getSender());
    assertEquals("This is the subject", customEmail.getSubject());
    assertTrue(customEmail.isHtml());
    // Update
    customEmailManager.updateCustomEmail("4444-4444-4444-4441", EmailType.CLAIM, "updated@sender.com", "updated subject", "updated content", false);
    // Check new values
    customEmails = customEmailManager.getCustomEmails("4444-4444-4444-4441");
    assertNotNull(customEmails);
    assertEquals(1, customEmails.size());
    customEmail = customEmails.get(0);
    assertNotNull(customEmail);
    assertEquals("updated content", customEmail.getContent());
    assertEquals(EmailType.CLAIM, customEmail.getEmailType());
    assertEquals("updated@sender.com", customEmail.getSender());
    assertEquals("updated subject", customEmail.getSubject());
    assertFalse(customEmail.isHtml());
}
Also used : CustomEmailEntity(org.orcid.persistence.jpa.entities.CustomEmailEntity) Rollback(org.springframework.test.annotation.Rollback)

Example 3 with CustomEmailEntity

use of org.orcid.persistence.jpa.entities.CustomEmailEntity in project ORCID-Source by ORCID.

the class CustomEmailManagerTest method testGetCustomEmails.

@Test
public void testGetCustomEmails() {
    List<CustomEmailEntity> customEmails = customEmailManager.getCustomEmails("4444-4444-4444-4441");
    assertNotNull(customEmails);
    assertEquals(1, customEmails.size());
    CustomEmailEntity customEmail = customEmails.get(0);
    assertNotNull(customEmail);
    assertEquals("This is the content", customEmail.getContent());
    assertEquals(EmailType.CLAIM, customEmail.getEmailType());
    assertEquals("angel.montenegro.jimenez@gmail.com", customEmail.getSender());
    assertEquals("This is the subject", customEmail.getSubject());
    assertTrue(customEmail.isHtml());
}
Also used : CustomEmailEntity(org.orcid.persistence.jpa.entities.CustomEmailEntity) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest)

Example 4 with CustomEmailEntity

use of org.orcid.persistence.jpa.entities.CustomEmailEntity in project ORCID-Source by ORCID.

the class CustomEmailManagerTest method testAddAmendCustomEmail.

@Test
@Transactional
@Rollback
public void testAddAmendCustomEmail() {
    assertTrue(customEmailManager.createCustomEmail("4444-4444-4444-4441", EmailType.AMEND, "angel.montenegro.jimenez@gmail.com", "Amend subject", "Amend content", false));
    List<CustomEmailEntity> customEmails = customEmailManager.getCustomEmails("4444-4444-4444-4441");
    assertNotNull(customEmails);
    assertEquals(2, customEmails.size());
    boolean amend = false, claim = false;
    for (CustomEmailEntity customEmail : customEmails) {
        if (EmailType.AMEND.equals(customEmail.getEmailType())) {
            assertEquals("Amend subject", customEmail.getSubject());
            assertEquals("angel.montenegro.jimenez@gmail.com", customEmail.getSender());
            assertEquals("Amend content", customEmail.getContent());
            assertFalse(customEmail.isHtml());
            amend = true;
        } else {
            assertEquals("This is the content", customEmail.getContent());
            assertEquals(EmailType.CLAIM, customEmail.getEmailType());
            assertEquals("angel.montenegro.jimenez@gmail.com", customEmail.getSender());
            assertEquals("This is the subject", customEmail.getSubject());
            claim = true;
        }
    }
    assertTrue(amend);
    assertTrue(claim);
}
Also used : CustomEmailEntity(org.orcid.persistence.jpa.entities.CustomEmailEntity) Test(org.junit.Test) BaseTest(org.orcid.core.BaseTest) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with CustomEmailEntity

use of org.orcid.persistence.jpa.entities.CustomEmailEntity in project ORCID-Source by ORCID.

the class CustomEmailController method getCustomEmails.

@RequestMapping(value = "/get.json", method = RequestMethod.GET)
@ResponseBody
public List<CustomEmailForm> getCustomEmails(@RequestParam("clientId") String clientId) throws IllegalArgumentException {
    List<CustomEmailForm> result = new ArrayList<CustomEmailForm>();
    boolean haveErrors = false;
    String groupId = getEffectiveUserOrcid();
    MemberType groupType = profileEntityManager.getGroupType(groupId);
    if (!(MemberType.PREMIUM_INSTITUTION.equals(groupType) || MemberType.BASIC_INSTITUTION.equals(groupType))) {
        haveErrors = true;
    } else if (!clientDetailsManager.exists(clientId)) {
        haveErrors = true;
    } else if (!clientDetailsManager.belongsTo(clientId, groupId)) {
        haveErrors = true;
    }
    if (!haveErrors) {
        List<CustomEmailEntity> customEmails = customEmailManager.getCustomEmails(clientId);
        for (CustomEmailEntity entity : customEmails) {
            CustomEmailForm form = CustomEmailForm.valueOf(entity);
            result.add(form);
        }
    }
    return result;
}
Also used : MemberType(org.orcid.jaxb.model.clientgroup.MemberType) CustomEmailEntity(org.orcid.persistence.jpa.entities.CustomEmailEntity) ArrayList(java.util.ArrayList) CustomEmailForm(org.orcid.pojo.ajaxForm.CustomEmailForm) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

CustomEmailEntity (org.orcid.persistence.jpa.entities.CustomEmailEntity)5 Test (org.junit.Test)2 BaseTest (org.orcid.core.BaseTest)2 Rollback (org.springframework.test.annotation.Rollback)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 MemberType (org.orcid.jaxb.model.clientgroup.MemberType)1 Source (org.orcid.jaxb.model.message.Source)1 ClientDetailsEntity (org.orcid.persistence.jpa.entities.ClientDetailsEntity)1 CustomEmailForm (org.orcid.pojo.ajaxForm.CustomEmailForm)1 MessageSource (org.springframework.context.MessageSource)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1