Search in sources :

Example 1 with QuotaEmailTemplatesVO

use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.

the class QuotaResponseBuilderImpl method updateQuotaEmailTemplate.

@Override
public boolean updateQuotaEmailTemplate(QuotaEmailTemplateUpdateCmd cmd) {
    final String templateName = cmd.getTemplateName();
    final String templateSubject = StringEscapeUtils.escapeJavaScript(cmd.getTemplateSubject());
    final String templateBody = StringEscapeUtils.escapeJavaScript(cmd.getTemplateBody());
    final String locale = cmd.getLocale();
    final List<QuotaEmailTemplatesVO> templates = _quotaEmailTemplateDao.listAllQuotaEmailTemplates(templateName);
    if (templates.size() == 1) {
        final QuotaEmailTemplatesVO template = templates.get(0);
        template.setTemplateSubject(templateSubject);
        template.setTemplateBody(templateBody);
        if (locale != null) {
            template.setLocale(locale);
        }
        return _quotaEmailTemplateDao.updateQuotaEmailTemplate(template);
    }
    return false;
}
Also used : QuotaEmailTemplatesVO(org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO)

Example 2 with QuotaEmailTemplatesVO

use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.

the class QuotaAlertManagerImplTest method testSendQuotaAlert.

@Test
public void testSendQuotaAlert() throws UnsupportedEncodingException, MessagingException {
    Mockito.doCallRealMethod().when(quotaAlertManager).sendQuotaAlert(Mockito.any(QuotaAlertManagerImpl.DeferredQuotaEmail.class));
    AccountVO account = new AccountVO();
    account.setId(2L);
    account.setDomainId(1L);
    account.setType(Account.ACCOUNT_TYPE_NORMAL);
    account.setAccountName("admin");
    account.setUuid("uuid");
    QuotaAccountVO quotaAccount = new QuotaAccountVO(2L);
    quotaAccount.setQuotaBalance(new BigDecimal(404));
    quotaAccount.setQuotaMinBalance(new BigDecimal(100));
    quotaAccount.setQuotaBalanceDate(new Date());
    quotaAccount.setQuotaAlertDate(null);
    quotaAccount.setQuotaEnforce(0);
    QuotaAlertManagerImpl.DeferredQuotaEmail email = new QuotaAlertManagerImpl.DeferredQuotaEmail(account, quotaAccount, new BigDecimal(100), QuotaConfig.QuotaEmailTemplateTypes.QUOTA_LOW);
    QuotaEmailTemplatesVO quotaEmailTemplatesVO = new QuotaEmailTemplatesVO();
    quotaEmailTemplatesVO.setTemplateSubject("Low quota");
    quotaEmailTemplatesVO.setTemplateBody("Low quota {{accountID}}");
    List<QuotaEmailTemplatesVO> emailTemplates = new ArrayList<>();
    emailTemplates.add(quotaEmailTemplatesVO);
    Mockito.when(quotaEmailTemplateDao.listAllQuotaEmailTemplates(Mockito.anyString())).thenReturn(emailTemplates);
    DomainVO domain = new DomainVO();
    domain.setUuid("uuid");
    domain.setName("/domain");
    Mockito.when(domainDao.findByIdIncludingRemoved(Mockito.anyLong())).thenReturn(new DomainVO());
    UserVO user = new UserVO();
    user.setUsername("user1");
    user.setEmail("user1@apache.org");
    List<UserVO> users = new ArrayList<>();
    users.add(user);
    Mockito.when(userDao.listByAccount(Mockito.anyLong())).thenReturn(users);
    quotaAlertManager.sendQuotaAlert(email);
    assertTrue(email.getSendDate() != null);
    Mockito.verify(emailQuotaAlert, Mockito.times(1)).sendQuotaAlert(Mockito.anyList(), Mockito.anyString(), Mockito.anyString());
}
Also used : QuotaEmailTemplatesVO(org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO) ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) Date(java.util.Date) DomainVO(com.cloud.domain.DomainVO) UserVO(com.cloud.user.UserVO) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) Test(org.junit.Test)

Example 3 with QuotaEmailTemplatesVO

use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.

the class QuotaAlertManagerImpl method sendQuotaAlert.

public void sendQuotaAlert(DeferredQuotaEmail emailToBeSent) {
    final AccountVO account = emailToBeSent.getAccount();
    final BigDecimal balance = emailToBeSent.getQuotaBalance();
    final BigDecimal usage = emailToBeSent.getQuotaUsage();
    final QuotaConfig.QuotaEmailTemplateTypes emailType = emailToBeSent.getEmailTemplateType();
    final List<QuotaEmailTemplatesVO> emailTemplates = _quotaEmailTemplateDao.listAllQuotaEmailTemplates(emailType.toString());
    if (emailTemplates != null && emailTemplates.get(0) != null) {
        final QuotaEmailTemplatesVO emailTemplate = emailTemplates.get(0);
        final DomainVO accountDomain = _domainDao.findByIdIncludingRemoved(account.getDomainId());
        final List<UserVO> usersInAccount = _userDao.listByAccount(account.getId());
        String userNames = "";
        final List<String> emailRecipients = new ArrayList<String>();
        for (UserVO user : usersInAccount) {
            userNames += String.format("%s <%s>,", user.getUsername(), user.getEmail());
            emailRecipients.add(user.getEmail());
        }
        if (userNames.endsWith(",")) {
            userNames = userNames.substring(0, userNames.length() - 1);
        }
        final Map<String, String> optionMap = new HashMap<String, String>();
        optionMap.put("accountName", account.getAccountName());
        optionMap.put("accountID", account.getUuid());
        optionMap.put("accountUsers", userNames);
        optionMap.put("domainName", accountDomain.getName());
        optionMap.put("domainID", accountDomain.getUuid());
        optionMap.put("quotaBalance", QuotaConfig.QuotaCurrencySymbol.value() + " " + balance.toString());
        if (emailType == QuotaEmailTemplateTypes.QUOTA_STATEMENT) {
            optionMap.put("quotaUsage", QuotaConfig.QuotaCurrencySymbol.value() + " " + usage.toString());
        }
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("accountName" + account.getAccountName() + "accountID" + account.getUuid() + "accountUsers" + userNames + "domainName" + accountDomain.getName() + "domainID" + accountDomain.getUuid());
        }
        final StrSubstitutor templateEngine = new StrSubstitutor(optionMap);
        final String subject = templateEngine.replace(emailTemplate.getTemplateSubject());
        final String body = templateEngine.replace(emailTemplate.getTemplateBody());
        try {
            _emailQuotaAlert.sendQuotaAlert(emailRecipients, subject, body);
            emailToBeSent.sentSuccessfully(_quotaAcc);
        } catch (Exception e) {
            s_logger.error(String.format("Unable to send quota alert email (subject=%s; body=%s) to account %s (%s) recipients (%s) due to error (%s)", subject, body, account.getAccountName(), account.getUuid(), emailRecipients, e));
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Exception", e);
            }
        }
    } else {
        s_logger.error(String.format("No quota email template found for type %s, cannot send quota alert email to account %s(%s)", emailType, account.getAccountName(), account.getUuid()));
    }
}
Also used : QuotaEmailTemplatesVO(org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) QuotaEmailTemplateTypes(org.apache.cloudstack.quota.constant.QuotaConfig.QuotaEmailTemplateTypes) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) BigDecimal(java.math.BigDecimal) MessagingException(javax.mail.MessagingException) ConfigurationException(javax.naming.ConfigurationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DomainVO(com.cloud.domain.DomainVO) StrSubstitutor(org.apache.commons.lang3.text.StrSubstitutor) UserVO(com.cloud.user.UserVO) QuotaConfig(org.apache.cloudstack.quota.constant.QuotaConfig)

Example 4 with QuotaEmailTemplatesVO

use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.

the class QuotaResponseBuilderImplTest method testListQuotaEmailTemplates.

@Test
public void testListQuotaEmailTemplates() {
    QuotaEmailTemplateListCmd cmd = new QuotaEmailTemplateListCmd();
    cmd.setTemplateName("some name");
    List<QuotaEmailTemplatesVO> templates = new ArrayList<>();
    QuotaEmailTemplatesVO template = new QuotaEmailTemplatesVO();
    template.setTemplateName("template");
    templates.add(template);
    Mockito.when(quotaEmailTemplateDao.listAllQuotaEmailTemplates(Mockito.anyString())).thenReturn(templates);
    assertTrue(quotaResponseBuilder.listQuotaEmailTemplates(cmd).size() == 1);
}
Also used : QuotaEmailTemplateListCmd(org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd) QuotaEmailTemplatesVO(org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with QuotaEmailTemplatesVO

use of org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO in project cloudstack by apache.

the class QuotaResponseBuilderImplTest method testUpdateQuotaEmailTemplate.

@Test
public void testUpdateQuotaEmailTemplate() {
    QuotaEmailTemplateUpdateCmd cmd = new QuotaEmailTemplateUpdateCmd();
    cmd.setTemplateBody("some body");
    cmd.setTemplateName("some name");
    cmd.setTemplateSubject("some subject");
    List<QuotaEmailTemplatesVO> templates = new ArrayList<>();
    Mockito.when(quotaEmailTemplateDao.listAllQuotaEmailTemplates(Mockito.anyString())).thenReturn(templates);
    Mockito.when(quotaEmailTemplateDao.updateQuotaEmailTemplate(Mockito.any(QuotaEmailTemplatesVO.class))).thenReturn(true);
    // invalid template test
    assertFalse(quotaResponseBuilder.updateQuotaEmailTemplate(cmd));
    // valid template test
    QuotaEmailTemplatesVO template = new QuotaEmailTemplatesVO();
    template.setTemplateName("template");
    templates.add(template);
    assertTrue(quotaResponseBuilder.updateQuotaEmailTemplate(cmd));
}
Also used : QuotaEmailTemplatesVO(org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO) ArrayList(java.util.ArrayList) QuotaEmailTemplateUpdateCmd(org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd) Test(org.junit.Test)

Aggregations

QuotaEmailTemplatesVO (org.apache.cloudstack.quota.vo.QuotaEmailTemplatesVO)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)3 DomainVO (com.cloud.domain.DomainVO)2 AccountVO (com.cloud.user.AccountVO)2 UserVO (com.cloud.user.UserVO)2 BigDecimal (java.math.BigDecimal)2 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 MessagingException (javax.mail.MessagingException)1 ConfigurationException (javax.naming.ConfigurationException)1 QuotaEmailTemplateListCmd (org.apache.cloudstack.api.command.QuotaEmailTemplateListCmd)1 QuotaEmailTemplateUpdateCmd (org.apache.cloudstack.api.command.QuotaEmailTemplateUpdateCmd)1 QuotaConfig (org.apache.cloudstack.quota.constant.QuotaConfig)1 QuotaEmailTemplateTypes (org.apache.cloudstack.quota.constant.QuotaConfig.QuotaEmailTemplateTypes)1 StrSubstitutor (org.apache.commons.lang3.text.StrSubstitutor)1