Search in sources :

Example 1 with DeferredQuotaEmail

use of org.apache.cloudstack.quota.QuotaAlertManagerImpl.DeferredQuotaEmail in project cloudstack by apache.

the class QuotaStatementImpl method sendStatement.

@Override
public void sendStatement() {
    List<DeferredQuotaEmail> deferredQuotaEmailList = new ArrayList<DeferredQuotaEmail>();
    for (final QuotaAccountVO quotaAccount : _quotaAcc.listAllQuotaAccount()) {
        if (quotaAccount.getQuotaBalance() == null) {
            // no quota usage for this account ever, ignore
            continue;
        }
        //check if it is statement time
        Calendar[] interval = statementTime(Calendar.getInstance(), _period);
        Date lastStatementDate = quotaAccount.getLastStatementDate();
        if (interval != null) {
            AccountVO account = _accountDao.findById(quotaAccount.getId());
            if (account != null) {
                if (lastStatementDate == null || getDifferenceDays(lastStatementDate, new Date()) >= s_LAST_STATEMENT_SENT_DAYS + 1) {
                    BigDecimal quotaUsage = _quotaUsage.findTotalQuotaUsage(account.getAccountId(), account.getDomainId(), null, interval[0].getTime(), interval[1].getTime());
                    s_logger.info("For account=" + quotaAccount.getId() + ", quota used = " + quotaUsage);
                    // send statement
                    deferredQuotaEmailList.add(new DeferredQuotaEmail(account, quotaAccount, quotaUsage, QuotaConfig.QuotaEmailTemplateTypes.QUOTA_STATEMENT));
                } else {
                    if (s_logger.isDebugEnabled()) {
                        s_logger.debug("For " + quotaAccount.getId() + " the statement has been sent recently");
                    }
                }
            }
        } else if (lastStatementDate != null) {
            s_logger.info("For " + quotaAccount.getId() + " it is already more than " + getDifferenceDays(lastStatementDate, new Date()) + " days, will send statement in next cycle");
        }
    }
    for (DeferredQuotaEmail emailToBeSent : deferredQuotaEmailList) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Attempting to send quota STATEMENT email to users of account: " + emailToBeSent.getAccount().getAccountName());
        }
        _quotaAlert.sendQuotaAlert(emailToBeSent);
    }
}
Also used : Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) DeferredQuotaEmail(org.apache.cloudstack.quota.QuotaAlertManagerImpl.DeferredQuotaEmail) QuotaAccountVO(org.apache.cloudstack.quota.vo.QuotaAccountVO) AccountVO(com.cloud.user.AccountVO) Date(java.util.Date) BigDecimal(java.math.BigDecimal)

Aggregations

AccountVO (com.cloud.user.AccountVO)1 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 DeferredQuotaEmail (org.apache.cloudstack.quota.QuotaAlertManagerImpl.DeferredQuotaEmail)1 QuotaAccountVO (org.apache.cloudstack.quota.vo.QuotaAccountVO)1