1 package org.apache.torque.mojo;
2
3 import static org.apache.commons.io.FileUtils.forceMkdir;
4
5 import java.io.File;
6 import java.io.FileInputStream;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.List;
11
12 import org.apache.maven.plugin.MojoExecutionException;
13 import org.apache.torque.mojo.morph.DataMorpher;
14 import org.apache.torque.mojo.morph.MorphRequest;
15 import org.apache.torque.mojo.morph.Morpher;
16 import org.codehaus.plexus.util.DirectoryScanner;
17 import org.codehaus.plexus.util.StringUtils;
18 import org.kuali.core.db.torque.PrettyPrint;
19 import org.kuali.core.db.torque.Utils;
20
21
22
23
24
25
26
27 public class MorphDataMojo extends BaseMojo {
28 Utils utils = new Utils();
29
30
31
32
33
34
35
36 private File newDataOutputDir;
37
38
39
40
41
42
43
44 private File oldDataXMLDir;
45
46
47
48
49
50
51
52 private String oldDataXMLIncludes;
53
54
55
56
57
58
59 private String oldDataXMLExcludes;
60
61
62
63
64 protected String[] getOldFiles() throws IOException {
65 DirectoryScanner ds = new DirectoryScanner();
66 ds.setIncludes(new String[] { getOldDataXMLIncludes() });
67 if (getOldDataXMLExcludes() != null) {
68 ds.setExcludes(new String[] { getOldDataXMLExcludes() });
69 }
70 ds.setBasedir(getOldDataXMLDir());
71 ds.scan();
72 return ds.getIncludedFiles();
73 }
74
75 protected boolean isMorphNeeded(final File oldFile, final File newFile) {
76 if (!newFile.exists()) {
77 return true;
78 }
79
80 long lastModifiedOld = oldFile.lastModified();
81 long lastModifiedNew = newFile.lastModified();
82 return lastModifiedOld > lastModifiedNew;
83 }
84
85 protected List<MorphRequest> getMorphRequests(final String[] oldFiles) throws IOException {
86 String inputPath = getOldDataXMLDir().getAbsolutePath();
87 String outputPath = getNewDataOutputDir().getAbsolutePath();
88 forceMkdir(getNewDataOutputDir());
89 List<MorphRequest> requests = new ArrayList<MorphRequest>();
90 for (String oldFilename : oldFiles) {
91 String oldFilePath = inputPath + FS + oldFilename;
92 String newFilePath = outputPath + FS + oldFilename;
93 File oldFile = new File(oldFilePath);
94 File newFile = new File(newFilePath);
95 if (!isMorphNeeded(oldFile, newFile)) {
96 getLog().debug("Skipping morph on " + oldFilename);
97 continue;
98 }
99 MorphRequest request = new MorphRequest();
100 request.setEncoding(getEncoding());
101 request.setName(oldFile.getName());
102 request.setInputStream(new FileInputStream(oldFile));
103 request.setOutputStream(new FileOutputStream(newFile));
104 requests.add(request);
105 }
106 return requests;
107 }
108
109 protected void showConfig() {
110 getLog().info(StringUtils.repeat("-", utils.getDefaultPrintableConsoleWidth() - 7));
111 getLog().info(" From: " + oldDataXMLDir.getAbsolutePath());
112 getLog().info(" To: " + newDataOutputDir.getAbsolutePath());
113 getLog().info("Include: " + oldDataXMLIncludes);
114 getLog().info("Exclude: " + oldDataXMLExcludes);
115 }
116
117 @Override
118 protected void executeMojo() throws MojoExecutionException {
119 try {
120 showConfig();
121 String[] oldFiles = getOldFiles();
122 PrettyPrint pp = new PrettyPrint("[INFO] Converting " + oldFiles.length + " data XML files");
123 utils.left(pp);
124 List<MorphRequest> requests = getMorphRequests(oldFiles);
125 for (MorphRequest request : requests) {
126 Morpher morpher = new DataMorpher(request, getProject().getArtifactId());
127 morpher.executeMorph();
128 }
129 utils.right(pp);
130 int skipCount = oldFiles.length - requests.size();
131 if (skipCount > 0) {
132 getLog().info("Skipped " + skipCount + " files that were unchanged.");
133 }
134 } catch (IOException e) {
135 throw new MojoExecutionException("Unexpected error", e);
136 }
137 }
138
139 public File getNewDataOutputDir() {
140 return newDataOutputDir;
141 }
142
143 public void setNewDataOutputDir(final File newDataOutputDir) {
144 this.newDataOutputDir = newDataOutputDir;
145 }
146
147 public File getOldDataXMLDir() {
148 return oldDataXMLDir;
149 }
150
151 public void setOldDataXMLDir(final File oldDataXMLDir) {
152 this.oldDataXMLDir = oldDataXMLDir;
153 }
154
155 public String getOldDataXMLIncludes() {
156 return oldDataXMLIncludes;
157 }
158
159 public void setOldDataXMLIncludes(final String oldDataXMLIncludes) {
160 this.oldDataXMLIncludes = oldDataXMLIncludes;
161 }
162
163 public String getOldDataXMLExcludes() {
164 return oldDataXMLExcludes;
165 }
166
167 public void setOldDataXMLExcludes(final String oldDataXMLExcludes) {
168 this.oldDataXMLExcludes = oldDataXMLExcludes;
169 }
170 }