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 String artifactId;
52  
53      
54  
55  
56  
57  
58  
59      private File newDataOutputDir;
60  
61      
62  
63  
64  
65  
66  
67      private File oldDataXMLDir;
68  
69      
70  
71  
72  
73  
74  
75      private String oldDataXMLIncludes;
76  
77      
78  
79  
80  
81  
82      private String oldDataXMLExcludes;
83  
84      
85  
86  
87      protected String[] getOldFiles() throws IOException {
88          DirectoryScanner ds = new DirectoryScanner();
89          ds.setIncludes(new String[] { getOldDataXMLIncludes() });
90          if (getOldDataXMLExcludes() != null) {
91              ds.setExcludes(new String[] { getOldDataXMLExcludes() });
92          }
93          ds.setBasedir(getOldDataXMLDir());
94          ds.scan();
95          return ds.getIncludedFiles();
96      }
97  
98      protected boolean isMorphNeeded(final File oldFile, final File newFile) {
99          if (!newFile.exists()) {
100             return true;
101         }
102 
103         long lastModifiedOld = oldFile.lastModified();
104         long lastModifiedNew = newFile.lastModified();
105         return lastModifiedOld > lastModifiedNew;
106     }
107 
108     protected List<MorphRequest> getMorphRequests(final String[] oldFiles) throws IOException {
109         String inputPath = getOldDataXMLDir().getAbsolutePath();
110         String outputPath = getNewDataOutputDir().getAbsolutePath();
111         forceMkdir(getNewDataOutputDir());
112         List<MorphRequest> requests = new ArrayList<MorphRequest>();
113         for (String oldFilename : oldFiles) {
114             String oldFilePath = inputPath + FS + oldFilename;
115             String newFilePath = outputPath + FS + oldFilename;
116             File oldFile = new File(oldFilePath);
117             File newFile = new File(newFilePath);
118             if (!isMorphNeeded(oldFile, newFile)) {
119                 getLog().debug("Skipping morph on " + oldFilename);
120                 continue;
121             }
122             MorphRequest request = new MorphRequest();
123             request.setEncoding(getEncoding());
124             request.setName(oldFile.getName());
125             request.setInputStream(new FileInputStream(oldFile));
126             request.setOutputStream(new FileOutputStream(newFile));
127             requests.add(request);
128         }
129         return requests;
130     }
131 
132     protected void showConfig() {
133         getLog().info(StringUtils.repeat("-", utils.getDefaultPrintableConsoleWidth() - 7));
134         getLog().info("   From: " + oldDataXMLDir.getAbsolutePath());
135         getLog().info("     To: " + newDataOutputDir.getAbsolutePath());
136         getLog().info("Include: " + oldDataXMLIncludes);
137         getLog().info("Exclude: " + oldDataXMLExcludes);
138     }
139 
140     @Override
141     protected void executeMojo() throws MojoExecutionException {
142         try {
143             showConfig();
144             String[] oldFiles = getOldFiles();
145             PrettyPrint pp = new PrettyPrint("[INFO] Converting " + oldFiles.length + " data XML files");
146             utils.left(pp);
147             List<MorphRequest> requests = getMorphRequests(oldFiles);
148             for (MorphRequest request : requests) {
149                 Morpher morpher = new DataMorpher(request, artifactId);
150                 morpher.executeMorph();
151             }
152             utils.right(pp);
153             int skipCount = oldFiles.length - requests.size();
154             if (skipCount > 0) {
155                 getLog().info("Skipped " + skipCount + " files that were unchanged.");
156             }
157         } catch (IOException e) {
158             throw new MojoExecutionException("Unexpected error", e);
159         }
160     }
161 
162     public File getNewDataOutputDir() {
163         return newDataOutputDir;
164     }
165 
166     public void setNewDataOutputDir(final File newDataOutputDir) {
167         this.newDataOutputDir = newDataOutputDir;
168     }
169 
170     public File getOldDataXMLDir() {
171         return oldDataXMLDir;
172     }
173 
174     public void setOldDataXMLDir(final File oldDataXMLDir) {
175         this.oldDataXMLDir = oldDataXMLDir;
176     }
177 
178     public String getOldDataXMLIncludes() {
179         return oldDataXMLIncludes;
180     }
181 
182     public void setOldDataXMLIncludes(final String oldDataXMLIncludes) {
183         this.oldDataXMLIncludes = oldDataXMLIncludes;
184     }
185 
186     public String getOldDataXMLExcludes() {
187         return oldDataXMLExcludes;
188     }
189 
190     public void setOldDataXMLExcludes(final String oldDataXMLExcludes) {
191         this.oldDataXMLExcludes = oldDataXMLExcludes;
192     }
193 
194     public String getArtifactId() {
195         return artifactId;
196     }
197 
198     public void setArtifactId(String artifactId) {
199         this.artifactId = artifactId;
200     }
201 }