001/*
002 * Copyright 2011 The Regents of the University of California.
003 */
004package org.kuali.ole.sys.batch;
005
006import java.io.File;
007import java.util.Comparator;
008import java.util.Date;
009
010import org.kuali.ole.gl.batch.BatchSortUtil;
011import org.kuali.ole.sys.batch.AbstractStep;
012
013/**
014 * General purpose sort step which can be configured with any Comparator class.
015 * 
016 * @author jonathan
017 */
018public class FlatFileSortStep extends AbstractStep {
019        private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FlatFileSortStep.class);
020        protected String inputFileName;
021        protected String outputFileName;
022        protected Class<? extends Comparator<String>> lineComparatorClass;
023        
024        public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
025        try {
026                if ( new File(inputFileName).canRead() ) {
027                        BatchSortUtil.sortTextFileWithFields(inputFileName, outputFileName, lineComparatorClass.newInstance());
028                } else {
029                        LOG.warn( "Input file: " + inputFileName + " does not exist.");
030                }
031                } catch (Exception ex) {
032                        throw new RuntimeException( "Unable to instantiate comparator class for sort: " + lineComparatorClass, ex );
033                }
034                return true;
035        }
036
037        public void setInputFileName(String inputFileName) {
038                this.inputFileName = inputFileName;
039        }
040
041        public void setOutputFileName(String outputFileName) {
042                this.outputFileName = outputFileName;
043        }
044
045        public void setLineComparatorClass( Class<? extends Comparator<String>> lineComparatorClass) {
046                this.lineComparatorClass = lineComparatorClass;
047        }
048}