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 */
016package org.kuali.ole.select.batch;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.ole.select.batch.service.RequisitionLoadTransactionsService;
020import org.kuali.ole.sys.batch.AbstractStep;
021import org.kuali.ole.sys.batch.BatchInputFileType;
022import org.kuali.ole.sys.batch.service.BatchInputFileService;
023
024import java.io.File;
025import java.util.ArrayList;
026import java.util.Date;
027import java.util.List;
028
029/**
030 * This step will call a service method to load the vendor  xml file into the transaction table. Validates the data before the
031 * load.
032 */
033public class RequisitionLoadStep extends AbstractStep {
034    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(RequisitionLoadStep.class);
035
036    private BatchInputFileService batchInputFileService;
037    private RequisitionLoadTransactionsService requisitionLoadTransactionsService;
038    private BatchInputFileType requisitionInputFileType;
039
040    /**
041     * Controls the procurement card process.
042     */
043    public boolean execute(String jobName, Date jobRunDate) {
044
045        List<String> fileNamesToLoad = batchInputFileService.listInputFileNamesWithDoneFile(requisitionInputFileType);
046        boolean processSuccess = true;
047        List<String> processedFiles = new ArrayList();
048        for (String inputFileName : fileNamesToLoad) {
049            processSuccess = requisitionLoadTransactionsService.loadRequisitionFile(inputFileName);
050            if (processSuccess) {
051                processedFiles.add(inputFileName);
052            }
053        }
054
055        removeDoneFiles(processedFiles);
056
057        return processSuccess;
058    }
059
060    /**
061     * Clears out associated .done files for the processed data files.
062     */
063    private void removeDoneFiles(List<String> dataFileNames) {
064
065        for (String dataFileName : dataFileNames) {
066            File doneFile = new File(StringUtils.substringBeforeLast(dataFileName, ".") + ".done");
067            if (doneFile.exists()) {
068                doneFile.delete();
069            }
070        }
071
072    }
073
074    public BatchInputFileService getBatchInputFileService() {
075        return batchInputFileService;
076    }
077
078    public void setBatchInputFileService(BatchInputFileService batchInputFileService) {
079        this.batchInputFileService = batchInputFileService;
080    }
081
082    public RequisitionLoadTransactionsService getRequisitionLoadTransactionsService() {
083        return requisitionLoadTransactionsService;
084    }
085
086    public void setRequisitionLoadTransactionsService(RequisitionLoadTransactionsService requisitionLoadTransactionsService) {
087        this.requisitionLoadTransactionsService = requisitionLoadTransactionsService;
088    }
089
090    public BatchInputFileType getRequisitionInputFileType() {
091        return requisitionInputFileType;
092    }
093
094    public void setRequisitionInputFileType(BatchInputFileType requisitionInputFileType) {
095        this.requisitionInputFileType = requisitionInputFileType;
096    }
097
098}