001    /**
002     * Copyright 2010-2013 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.common.jdbc.convert;
017    
018    import java.io.File;
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import org.kuali.common.util.Assert;
023    import org.kuali.common.util.SimpleScanner;
024    import org.slf4j.Logger;
025    import org.slf4j.LoggerFactory;
026    
027    public class DirectoryConverter {
028    
029        public static final String CONVERTED_EXTENSION = ".converted";
030    
031        private static final Logger logger = LoggerFactory.getLogger(DirectoryConverter.class);
032    
033        public void convert(DirectoryContext context) {
034            logger.info("Scanning directory " + context.getDirectory().getAbsolutePath());
035            logger.info("Included file pattern: " + context.getInclude());
036            logger.info("Excluded file pattern: " + context.getExclude());
037                    SimpleScanner scanner = new SimpleScanner(context.getDirectory(), context.getInclude(), context.getExclude());
038                    List<File> oldFiles = scanner.getFiles();
039            logger.info("Found " + oldFiles.size() + " files for conversion");
040                    List<File> newFiles = getNewFiles(oldFiles);
041            List<ConversionResult> results = convert(context, oldFiles, newFiles);
042    
043            logger.info("Conversion complete");
044            if(context.getPostProcessor() != null) {
045                logger.info("Initiating post conversion processor of type: " + context.getPostProcessor().getClass().getName());
046                context.getPostProcessor().process(results);
047            }
048            }
049    
050            protected List<ConversionResult> convert(DirectoryContext context, List<File> oldFiles, List<File> newFiles) {
051                    // The lists must be the same size
052                    Assert.isTrue(oldFiles.size() == newFiles.size());
053    
054                    SqlConverter converter = context.getConverter();
055                    List<ConversionResult> results = new ArrayList<ConversionResult>();
056                    for (int i = 0; i < oldFiles.size(); i++) {
057                            File oldFile = oldFiles.get(i);
058                            File newFile = newFiles.get(i);
059                            ConversionContext cc = new ConversionContext();
060                            cc.setNewFile(newFile);
061                            cc.setOldFile(oldFile);
062                            ConversionResult result = converter.convert(cc);
063                            results.add(result);
064                    }
065                    return results;
066            }
067    
068            protected List<File> getNewFiles(List<File> oldFiles) {
069                    List<File> newFiles = new ArrayList<File>();
070                    for (File oldFile : oldFiles) {
071                            File newFile = new File(oldFile.getAbsolutePath() + CONVERTED_EXTENSION);
072                            newFiles.add(newFile);
073                    }
074                    return newFiles;
075            }
076    }