1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.impex.schema;
17
18 import java.io.IOException;
19 import java.io.Writer;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.commons.io.IOUtils;
24 import org.kuali.common.util.LocationUtils;
25 import org.kuali.common.util.execute.Executable;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class ProduceSchemaExecutable implements Executable {
30
31 private static final Logger log = LoggerFactory.getLogger(ProduceSchemaExecutable.class.getSimpleName());
32
33 protected static final String LF = "\n";
34
35 public static final boolean DEFAULT_SKIP_EXECUTION = false;
36
37 Map<String, List<String>> fileNamesToSqls;
38
39 boolean skip = DEFAULT_SKIP_EXECUTION;
40
41 @Override
42 public void execute() {
43 if (skip) {
44 return;
45 }
46
47 for (String fileName : fileNamesToSqls.keySet()) {
48 List<String> sqls = fileNamesToSqls.get(fileName);
49 long start = System.currentTimeMillis();
50
51 log.info("Writing " + sqls.size() + " sql statments to file " + fileName);
52
53 Writer writer = null;
54 try {
55 writer = LocationUtils.openWriter(fileName);
56
57 for (String s : sqls) {
58 writer.write(s);
59 writer.write(LF);
60 writer.write(LF);
61 }
62
63 } catch (IOException e) {
64 throw new IllegalStateException("Could not write to file " + fileName + ", IOException was thrown: " + e.getMessage(), e);
65 } finally {
66 IOUtils.closeQuietly(writer);
67 }
68
69 log.info("File output complete, took: " + (System.currentTimeMillis() - start) / 1000l + " seconds");
70 }
71 }
72
73 public Map<String, List<String>> getFileNamesToSqls() {
74 return fileNamesToSqls;
75 }
76
77 public void setFileNamesToSqls(Map<String, List<String>> fileNamesToSqls) {
78 this.fileNamesToSqls = fileNamesToSqls;
79 }
80
81 public boolean isSkip() {
82 return skip;
83 }
84
85 public void setSkip(boolean skip) {
86 this.skip = skip;
87 }
88 }