Untitled

mail@pastecode.io avatar
unknown
java
a month ago
3.5 kB
1
Indexable
Never
public void importAffiliateOrdersRecordToTable() {
    if (!commonUtils.isServerConfiguredForScheduler()) {
        LOGGER.error("[AffiliateOrdersRecordProcessScheduler]importCSVRecordToTable] Server not mapped for scheduler");
        return;
    }

    String mailReceivers = null;
    DataRecords dataRecords = new DataRecords();

    try {
        mailReceivers = configParamRepository.findValueByName(KeyConstants.ORDER_REPORT_NOTIFICATION_MAIL_RECEIVER);
        LOGGER.info("[AffiliateOrdersRecordProcessScheduler]importRedemptionRecordToTable] Scheduler started");

        String today = new SimpleDateFormat("ddMMyyyy").format(new Date());
        String sftpPassword = configParamRepository.findValueByNameIgnoreCase(KeyConstants.SFTP_PASSWORD);
        hostPassword = StringUtils.isNotBlank(sftpPassword) ? sftpPassword : hostPassword;

        boolean file1ExistInServer = checkFileExistence("000001");
        boolean file2ExistInServer = checkFileExistence("000002");

        if (file1ExistInServer || file2ExistInServer) {
            LOGGER.info("[AffiliateOrdersRecordProcessScheduler]importCSVRecordToTable] CSV file exists in SFTP server");

            dataRecords = readCSVFile(today, file1ExistInServer ? "000001" : "000002");
            processAndSendEmail(dataRecords, mailReceivers);

        } else {
            LOGGER.info("[AffiliateOrdersRecordProcessScheduler]importCSVRecordToTable] CSV file does not exist in SFTP server");
            emailSender.sendMailWithoutAttachment(senderEmailId, mailReceivers, emailSubject, noFileExistEmailBody);
        }

    } catch (Exception e) {
        handleException(e, mailReceivers);

    } finally {
        LOGGER.info("[AffiliateOrdersRecordProcessScheduler]importCSVRecordToTable] Scheduler Completed");
    }
}

private boolean checkFileExistence(String fileSuffix) {
    return SFTPFileReading.readingFile(hostUserName, hostPassword, hostPort, hostName, filePath,
            hostRemoteFilePath + hostRemoteFileName + today + "_" + fileSuffix + ".csv");
}

private DataRecords readCSVFile(String today, String fileSuffix) {
    String localFileNameWithSuffix = localFileName + today + "_" + fileSuffix + ".csv";
    return FileReaderAndWriter.readCSVfile(filePath, localFileNameWithSuffix);
}

private void processAndSendEmail(DataRecords dataRecords, String mailReceivers) {
    if (dataRecords != null && !dataRecords.getRecords().isEmpty()) {
        Integer count = processStatusRecords(dataRecords.getRecords());
        String emailBodyText = count > 0 ? MessageFormat.format(emailBody, count, (dataRecords.getCount() - 1)) : failedEmailBody;
        emailSender.sendMailWithoutAttachment(senderEmailId, mailReceivers, emailSubject, emailBodyText);

        LOGGER.info("[ManualServiceImpl]importAffiliateOrdersRecordToTable Records count greater than zero {}", count);

    } else {
        emailSender.sendMailWithoutAttachment(senderEmailId, mailReceivers, emailSubject,
                dataRecords != null ? noDataEmailBody : noDataEmailBody);
        LOGGER.info("[AffiliateOrdersRecordProcessScheduler]importAffiliateOrdersRecordToTable] Records not found in file");
    }
}

private void handleException(Exception e, String mailReceivers) {
    LOGGER.error(String.format("[AffiliateOrdersRecordProcessScheduler]importCSVRecordToTable] Exception Occurred: %s", e.getMessage()));
    emailSender.sendMailWithoutAttachment(senderEmailId, mailReceivers, emailSubject, errorEmailBody);
}
Leave a Comment