View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Converts Ant impex data XML files into maven-impex-plugin data XML files
38   *
39   * @goal morphdata
40   * @phase generate-sources
41   */
42  public class MorphDataMojo extends BaseMojo {
43      Utils utils = new Utils();
44  
45      /**
46       * The artifactId (aka database schema)
47       *
48       * @parameter expression="${impex.artifactId}" default-value="${project.artifactId}"
49       * @required
50       */
51      private String artifactId;
52  
53      /**
54       * The directory in which the morphed XML will be generated.
55       *
56       * @parameter expression="${newDataOutputDir}" default-value="${project.build.directory}/generated-impex/xml"
57       * @required
58       */
59      private File newDataOutputDir;
60  
61      /**
62       * The directory containing the source (non-morphed) data XML files
63       *
64       * @parameter expression="${oldDataXMLDir}" default-value="${basedir}/src/main/impex"
65       * @required
66       */
67      private File oldDataXMLDir;
68  
69      /**
70       * The default set of files in that directory to include (ant style notation)
71       *
72       * @parameter expression="${oldDataXMLIncludes}" default-value="*.xml"
73       * @required
74       */
75      private String oldDataXMLIncludes;
76  
77      /**
78       * The default set of files in that directory to exclude (ant style notation)
79       *
80       * @parameter expression="${oldDataXMLExcludes}" default-value="schema.xml"
81       */
82      private String oldDataXMLExcludes;
83  
84      /**
85       * Use our configuration to determine the list of files we need to convert
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 }