View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.jdbc.convert;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.kuali.common.util.Assert;
23  import org.kuali.common.util.SimpleScanner;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  public class DirectoryConverter {
28  
29      public static final String CONVERTED_EXTENSION = ".converted";
30  
31      private static final Logger logger = LoggerFactory.getLogger(DirectoryConverter.class);
32  
33      public void convert(DirectoryContext context) {
34          logger.info("Scanning directory " + context.getDirectory().getAbsolutePath());
35          logger.info("Included file pattern: " + context.getInclude());
36          logger.info("Excluded file pattern: " + context.getExclude());
37  		SimpleScanner scanner = new SimpleScanner(context.getDirectory(), context.getInclude(), context.getExclude());
38  		List<File> oldFiles = scanner.getFiles();
39          logger.info("Found " + oldFiles.size() + " files for conversion");
40  		List<File> newFiles = getNewFiles(oldFiles);
41          List<ConversionResult> results = convert(context, oldFiles, newFiles);
42  
43          logger.info("Conversion complete");
44          if(context.getPostProcessor() != null) {
45              logger.info("Initiating post conversion processor of type: " + context.getPostProcessor().getClass().getName());
46              context.getPostProcessor().process(results);
47          }
48  	}
49  
50  	protected List<ConversionResult> convert(DirectoryContext context, List<File> oldFiles, List<File> newFiles) {
51  		// The lists must be the same size
52  		Assert.isTrue(oldFiles.size() == newFiles.size());
53  
54  		SqlConverter converter = context.getConverter();
55  		List<ConversionResult> results = new ArrayList<ConversionResult>();
56  		for (int i = 0; i < oldFiles.size(); i++) {
57  			File oldFile = oldFiles.get(i);
58  			File newFile = newFiles.get(i);
59  			ConversionContext cc = new ConversionContext();
60  			cc.setNewFile(newFile);
61  			cc.setOldFile(oldFile);
62  			ConversionResult result = converter.convert(cc);
63  			results.add(result);
64  		}
65  		return results;
66  	}
67  
68  	protected List<File> getNewFiles(List<File> oldFiles) {
69  		List<File> newFiles = new ArrayList<File>();
70  		for (File oldFile : oldFiles) {
71  			File newFile = new File(oldFile.getAbsolutePath() + CONVERTED_EXTENSION);
72  			newFiles.add(newFile);
73  		}
74  		return newFiles;
75  	}
76  }