1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }