001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.fp.batch;
017    
018    import java.io.File;
019    import java.util.ArrayList;
020    import java.util.Date;
021    import java.util.List;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.fp.batch.service.ProcurementCardLoadTransactionsService;
025    import org.kuali.kfs.sys.batch.AbstractStep;
026    import org.kuali.kfs.sys.batch.BatchInputFileType;
027    import org.kuali.kfs.sys.batch.service.BatchInputFileService;
028    
029    /**
030     * This step will call a service method to load the kuali pcard xml file into the transaction table. Validates the data before the
031     * load. Functions performed by this step: 1) Lookup path and filename from APC for the pcard input file 2) Load the pcard xml file
032     * 3) Parse each transaction and validate against the data dictionary 4) Clean fp_prcrmnt_card_trn_t from the previous run 5) Load
033     * new transactions into fp_prcrmnt_card_trn_t 6) Rename input file using the current date (backup) RESTART: All functions performed
034     * withing a single transaction. Step can be restarted as needed.
035     */
036    public class ProcurementCardLoadStep extends AbstractStep {
037        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardLoadStep.class);
038    
039        private ProcurementCardLoadTransactionsService procurementCardLoadTransactionsService;
040        private BatchInputFileService batchInputFileService;
041        private BatchInputFileType procurementCardInputFileType;
042    
043        /**
044         * Controls the procurement card process.
045         */
046        public boolean execute(String jobName, Date jobRunDate) {
047            procurementCardLoadTransactionsService.cleanTransactionsTable();
048    
049            List<String> fileNamesToLoad = batchInputFileService.listInputFileNamesWithDoneFile(procurementCardInputFileType);
050    
051            boolean processSuccess = true;
052            List<String> processedFiles = new ArrayList();
053            for (String inputFileName : fileNamesToLoad) {
054                processSuccess = procurementCardLoadTransactionsService.loadProcurementCardFile(inputFileName);
055                if (processSuccess) {
056                    processedFiles.add(inputFileName);
057                }
058            }
059    
060            removeDoneFiles(processedFiles);
061    
062            return processSuccess;
063        }
064    
065        /**
066         * Clears out associated .done files for the processed data files.
067         */
068        private void removeDoneFiles(List<String> dataFileNames) {
069            for (String dataFileName : dataFileNames) {
070                File doneFile = new File(StringUtils.substringBeforeLast(dataFileName, ".") + ".done");
071                if (doneFile.exists()) {
072                    doneFile.delete();
073                }
074            }
075        }
076    
077        /**
078         * Sets the batchInputFileService attribute value.
079         */
080        public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
081            this.batchInputFileService = batchInputFileService;
082        }
083    
084        /**
085         * Sets the procurementCardInputFileType attribute value.
086         */
087        public void setProcurementCardInputFileType(BatchInputFileType procurementCardInputFileType) {
088            this.procurementCardInputFileType = procurementCardInputFileType;
089        }
090    
091        /**
092         * Sets the procurementCardLoadTransactionsService attribute value.
093         */
094        public void setProcurementCardLoadTransactionsService(ProcurementCardLoadTransactionsService procurementCardLoadTransactionsService) {
095            this.procurementCardLoadTransactionsService = procurementCardLoadTransactionsService;
096        }
097    }