Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
1.7 kB
1
Indexable
Never
@Scheduled(cron = "${sftp.password.expiration.cron}")
public void passwordExpirationMailSend() {
    LOGGER.info("[SchedulerService]passwordExpirationMailSend] Scheduler started");

    LocalDateTime localDate = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

    try {
        List<ConfigParam> configObjectValues = configParamRepository.findByNameIgnoreCaseOrderByName(Arrays.asList(KeyConstants.SFTP_PASSWORD, KeyConstants.SFTP_PASSWORD_RECEIVERS));

        if (configObjectValues.isEmpty()) {
            LOGGER.error("[passwordExpirationMailSend] config data missing ");
            return;
        }

        LocalDateTime lastUpdatedDateTime = configObjectValues.get(0).getUpdatedAt();

        // local data = today and lastUpdatedDate = Dec-19
        boolean conditionsMet = (localDate.isAfter(lastUpdatedDateTime.plusDays(40)))
                && (senderEmailId != null && StringUtils.isNotEmpty(senderEmailId)
                && StringUtils.isNotEmpty(configObjectValues.get(1).getValue()));

        if (conditionsMet) {
            emailSender.sendMailWithoutAttachment(
                    senderEmailId, configObjectValues.get(1).getValue(), emailSubjectPasswordExpiration,
                    MessageFormat.format(emailBodyPasswordExpiration, lastUpdatedDateTime.plusDays(42).format(formatter)));
        } else {
            LOGGER.error("[passwordExpirationMailSend] conditions fail in Email {}", configObjectValues.get(1).getValue());
        }
    } catch (Exception e) {
        LOGGER.error("[passwordExpirationMailSend] Exception occurred in Email {}, {}", e.getMessage(), e);
    }
}
Leave a Comment