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.gl.batch;
017    
018    import java.io.BufferedReader;
019    import java.io.FileNotFoundException;
020    import java.io.FileReader;
021    import java.io.IOException;
022    import java.io.PrintStream;
023    import java.util.ArrayList;
024    import java.util.Collections;
025    import java.util.Comparator;
026    import java.util.List;
027    
028    import org.kuali.kfs.gl.batch.service.BatchSortService;
029    
030    /**
031     * This class...
032     */
033    public class BatchSortUtil {
034        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(BatchSortUtil.class);
035        
036        static public void sortTextFileWithFields(String inputFileName, String outputFileName, Comparator comparator){
037            FileReader inputFile = null;
038            PrintStream outputFileStream = null;
039            try {
040                inputFile = new FileReader(inputFileName);
041                outputFileStream = new PrintStream(outputFileName);
042            }
043            catch (FileNotFoundException e) {
044                throw new RuntimeException(e);
045            }
046            catch (IOException e) {
047                throw new RuntimeException(e);
048            }
049    
050    //        String lineLengh = GeneralLedgerConstants.getSpaceAllOriginEntryFields();
051    //        File inputFile = new File(inputFileName);
052    //        Long lineNumber = inputFile.length() / (lineLengh.length()+1);
053    
054            
055            List<String> lineList = new ArrayList();
056            BufferedReader inputBufferedReader = new BufferedReader(inputFile);
057            
058            try {
059                String currentLine = inputBufferedReader.readLine();
060                while (currentLine != null) {
061                    lineList.add(currentLine);
062                    currentLine = inputBufferedReader.readLine();
063                }
064                inputBufferedReader.close();    
065                
066            } catch (IOException e) {
067                LOG.error("sortTextFileWithFields() Stopped: " + e.getMessage());
068                throw new RuntimeException("sortTextFileWithFields() Stopped: " + e.getMessage(), e);
069            }
070            Collections.sort(lineList, comparator);
071    
072            for (String line: lineList){
073                outputFileStream.printf("%s\n", line);
074            }
075            outputFileStream.close();
076                 
077        }
078        
079        
080    }