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