001/*
002 * Copyright 2007 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 */
016package org.kuali.ole.fp.batch;
017
018import java.io.File;
019import java.util.ArrayList;
020import java.util.Date;
021import java.util.List;
022
023import org.apache.commons.lang.StringUtils;
024import org.kuali.ole.fp.batch.service.ProcurementCardLoadTransactionsService;
025import org.kuali.ole.sys.batch.AbstractStep;
026import org.kuali.ole.sys.batch.BatchInputFileType;
027import org.kuali.ole.sys.batch.service.BatchInputFileService;
028import org.kuali.ole.sys.batch.service.WrappingBatchService;
029import org.kuali.ole.sys.service.ReportWriterService;
030
031/**
032 * This step will call a service method to load the kuali pcard xml file into the transaction table. Validates the data before the
033 * load. Functions performed by this step: 1) Lookup path and filename from APC for the pcard input file 2) Load the pcard xml file
034 * 3) Parse each transaction and validate against the data dictionary 4) Clean fp_prcrmnt_card_trn_mt from the previous run 5) Load
035 * new transactions into fp_prcrmnt_card_trn_mt 6) Rename input file using the current date (backup) RESTART: All functions performed
036 * withing a single transaction. Step can be restarted as needed.
037 */
038public class ProcurementCardLoadStep extends AbstractStep {
039    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(ProcurementCardLoadStep.class);
040
041    private ProcurementCardLoadTransactionsService procurementCardLoadTransactionsService;
042    private BatchInputFileService batchInputFileService;
043    private BatchInputFileType procurementCardInputFileType;
044    private ReportWriterService reportWriterService;
045
046    /**
047     * custom get requiredDirectoryNames step- assign the passed in procurementInputFileType to the batchInputFileType
048     * 
049     * @see org.kuali.ole.sys.batch.AbstractStep#prepareStepDirectory()
050     */
051    @Override
052    public List<String> getRequiredDirectoryNames() {
053        setBatchInputFileType(procurementCardInputFileType);
054        return super.getRequiredDirectoryNames();
055    }
056    
057    /**
058     * Controls the procurement card process.
059     */
060    public boolean execute(String jobName, Date jobRunDate) {
061        procurementCardLoadTransactionsService.cleanTransactionsTable();
062
063        List<String> fileNamesToLoad = batchInputFileService.listInputFileNamesWithDoneFile(procurementCardInputFileType);
064        ((WrappingBatchService) reportWriterService).initialize();
065
066        boolean processSuccess = true;
067        List<String> processedFiles = new ArrayList();
068        for (String inputFileName : fileNamesToLoad) {
069            processSuccess = procurementCardLoadTransactionsService.loadProcurementCardFile(inputFileName, reportWriterService);
070            if (processSuccess) {
071                processedFiles.add(inputFileName);
072            }
073        }
074        ((WrappingBatchService) reportWriterService).destroy();
075
076        removeDoneFiles(fileNamesToLoad);
077
078        return processSuccess;
079    }
080
081    /**
082     * Clears out associated .done files for the processed data files.
083     */
084    private void removeDoneFiles(List<String> dataFileNames) {
085        for (String dataFileName : dataFileNames) {
086            File doneFile = new File(StringUtils.substringBeforeLast(dataFileName, ".") + ".done");
087            if (doneFile.exists()) {
088                doneFile.delete();
089            }
090        }
091    }
092
093    /**
094     * Sets the batchInputFileService attribute value.
095     */
096    public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
097        this.batchInputFileService = batchInputFileService;
098    }
099
100    /**
101     * Sets the procurementCardInputFileType attribute value.
102     */
103    public void setProcurementCardInputFileType(BatchInputFileType procurementCardInputFileType) {
104        this.procurementCardInputFileType = procurementCardInputFileType;
105    }
106
107    /**
108     * Sets the procurementCardLoadTransactionsService attribute value.
109     */
110    public void setProcurementCardLoadTransactionsService(ProcurementCardLoadTransactionsService procurementCardLoadTransactionsService) {
111        this.procurementCardLoadTransactionsService = procurementCardLoadTransactionsService;
112    }
113
114    /**
115     * 
116     * @return
117     */
118    public ReportWriterService getReportWriterService() {
119        return reportWriterService;
120    }
121
122    /**
123     * 
124     * @param reportWriterService
125     */
126    public void setReportWriterService(ReportWriterService reportWriterService) {
127        this.reportWriterService = reportWriterService;
128    }
129}