Search in sources :

Example 1 with RequiredFilesMissingStatus

use of org.kuali.kfs.gl.batch.service.impl.RequiredFilesMissingStatus in project cu-kfs by CU-CommunityApps.

the class CuFileEnterpriseFeederServiceImpl method feed.

@Override
public void feed(String processName, boolean performNotifications) {
    // to consider: maybe use java NIO classes to perform done file locking?
    synchronized (this) {
        if (StringUtils.isBlank(directoryName)) {
            throw new IllegalArgumentException("directoryName not set for FileEnterpriseFeederServiceImpl.");
        }
        FileFilter doneFileFilter = new SuffixFileFilter(DONE_FILE_SUFFIX);
        File enterpriseFeedFile = null;
        String enterpriseFeedFileName = LaborConstants.BatchFileSystem.LABOR_ENTERPRISE_FEED + LaborConstants.BatchFileSystem.EXTENSION;
        enterpriseFeedFile = new File(laborOriginEntryDirectoryName + File.separator + enterpriseFeedFileName);
        PrintStream enterpriseFeedPs = null;
        try {
            enterpriseFeedPs = new PrintStream(enterpriseFeedFile);
        } catch (FileNotFoundException e) {
            LOG.error("enterpriseFeedFile doesn't exist " + enterpriseFeedFileName);
            throw new RuntimeException("enterpriseFeedFile doesn't exist " + enterpriseFeedFileName);
        }
        if (LOG.isInfoEnabled()) {
            LOG.info("New File created for enterprise feeder service run: " + enterpriseFeedFileName);
        }
        File directory = new File(directoryName);
        if (!directory.exists() || !directory.isDirectory()) {
            LOG.error("Directory doesn't exist and or it's not really a directory " + directoryName);
            throw new RuntimeException("Directory doesn't exist and or it's not really a directory " + directoryName);
        }
        File[] doneFiles = directory.listFiles(doneFileFilter);
        reorderDoneFiles(doneFiles);
        boolean fatal = false;
        LedgerSummaryReport ledgerSummaryReport = new LedgerSummaryReport();
        // keeps track of statistics for reporting
        EnterpriseFeederReportData feederReportData = new EnterpriseFeederReportData();
        List<EnterpriseFeederStatusAndErrorMessagesWrapper> statusAndErrorsList = new ArrayList<EnterpriseFeederStatusAndErrorMessagesWrapper>();
        for (File doneFile : doneFiles) {
            File dataFile = null;
            File reconFile = null;
            EnterpriseFeederStatusAndErrorMessagesWrapper statusAndErrors = new EnterpriseFeederStatusAndErrorMessagesWrapper();
            statusAndErrors.setErrorMessages(new ArrayList<Message>());
            dataFile = getDataFile(doneFile);
            reconFile = getReconFile(doneFile);
            statusAndErrors.setFileNames(dataFile, reconFile, doneFile);
            if (dataFile == null) {
                LOG.error("Unable to find data file for done file: " + doneFile.getAbsolutePath());
                statusAndErrors.getErrorMessages().add(new Message("Unable to find data file for done file: " + doneFile.getAbsolutePath(), Message.TYPE_FATAL));
                statusAndErrors.setStatus(new RequiredFilesMissingStatus());
            }
            if (reconFile == null) {
                LOG.error("Unable to find recon file for done file: " + doneFile.getAbsolutePath());
                statusAndErrors.getErrorMessages().add(new Message("Unable to find recon file for done file: " + doneFile.getAbsolutePath(), Message.TYPE_FATAL));
                statusAndErrors.setStatus(new RequiredFilesMissingStatus());
            }
            try {
                if (dataFile != null && reconFile != null) {
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Data file: " + dataFile.getAbsolutePath());
                        LOG.info("Reconciliation File: " + reconFile.getAbsolutePath());
                    }
                    fileEnterpriseFeederHelperService.feedOnFile(doneFile, dataFile, reconFile, enterpriseFeedPs, processName, reconciliationTableId, statusAndErrors, ledgerSummaryReport, errorStatisticsReport, feederReportData);
                }
            } catch (RuntimeException e) {
                // we need to be extremely resistant to a file load failing so that it doesn't prevent other files from loading
                LOG.error("Caught exception when feeding done file: " + doneFile.getAbsolutePath());
                fatal = true;
            } finally {
                statusAndErrorsList.add(statusAndErrors);
                boolean doneFileDeleted = doneFile.delete();
                if (!doneFileDeleted) {
                    statusAndErrors.getErrorMessages().add(new Message("Unable to delete done file: " + doneFile.getAbsolutePath(), Message.TYPE_FATAL));
                }
                if (performNotifications) {
                    enterpriseFeederNotificationService.notifyFileFeedStatus(processName, statusAndErrors.getStatus(), doneFile, dataFile, reconFile, statusAndErrors.getErrorMessages());
                }
            }
        }
        enterpriseFeedPs.close();
        // if errors encountered is greater than max allowed the enterprise feed file should not be sent
        boolean enterpriseFeedFileCreated = false;
        if (feederReportData.getNumberOfErrorEncountered() > getMaximumNumberOfErrorsAllowed() || fatal) {
            enterpriseFeedFile.delete();
        } else {
            // generate done file
            String enterpriseFeedDoneFileName = enterpriseFeedFileName.replace(LaborConstants.BatchFileSystem.EXTENSION, LaborConstants.BatchFileSystem.DONE_FILE_EXTENSION);
            File enterpriseFeedDoneFile = new File(laborOriginEntryDirectoryName + File.separator + enterpriseFeedDoneFileName);
            if (!enterpriseFeedDoneFile.exists()) {
                try {
                    enterpriseFeedDoneFile.createNewFile();
                } catch (IOException e) {
                    LOG.error("Unable to create done file for enterprise feed output group.", e);
                    throw new RuntimeException("Unable to create done file for enterprise feed output group.", e);
                }
            }
            enterpriseFeedFileCreated = true;
        }
        // write out totals to log file
        if (LOG.isInfoEnabled()) {
            LOG.info("Total records read: " + feederReportData.getNumberOfRecordsRead());
            LOG.info("Total amount read: " + feederReportData.getTotalAmountRead());
            LOG.info("Total records written: " + feederReportData.getNumberOfRecordsRead());
            LOG.info("Total amount written: " + feederReportData.getTotalAmountWritten());
        }
        generateReport(enterpriseFeedFileCreated, feederReportData, statusAndErrorsList, ledgerSummaryReport, laborOriginEntryDirectoryName + File.separator + enterpriseFeedFileName);
    }
}
Also used : PrintStream(java.io.PrintStream) EnterpriseFeederStatusAndErrorMessagesWrapper(org.kuali.kfs.gl.service.impl.EnterpriseFeederStatusAndErrorMessagesWrapper) Message(org.kuali.kfs.sys.Message) EnterpriseFeederReportData(org.kuali.kfs.module.ld.report.EnterpriseFeederReportData) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RequiredFilesMissingStatus(org.kuali.kfs.gl.batch.service.impl.RequiredFilesMissingStatus) LedgerSummaryReport(org.kuali.kfs.gl.report.LedgerSummaryReport) SuffixFileFilter(org.apache.commons.io.filefilter.SuffixFileFilter) FileFilter(java.io.FileFilter) SuffixFileFilter(org.apache.commons.io.filefilter.SuffixFileFilter) File(java.io.File)

Aggregations

File (java.io.File)1 FileFilter (java.io.FileFilter)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 SuffixFileFilter (org.apache.commons.io.filefilter.SuffixFileFilter)1 RequiredFilesMissingStatus (org.kuali.kfs.gl.batch.service.impl.RequiredFilesMissingStatus)1 LedgerSummaryReport (org.kuali.kfs.gl.report.LedgerSummaryReport)1 EnterpriseFeederStatusAndErrorMessagesWrapper (org.kuali.kfs.gl.service.impl.EnterpriseFeederStatusAndErrorMessagesWrapper)1 EnterpriseFeederReportData (org.kuali.kfs.module.ld.report.EnterpriseFeederReportData)1 Message (org.kuali.kfs.sys.Message)1