001    package org.apache.torque.mojo;
002    
003    import java.io.File;
004    import java.util.List;
005    
006    import org.apache.commons.configuration.PropertiesConfiguration;
007    import org.apache.maven.plugin.MojoExecutionException;
008    import org.apache.torque.util.ChangeDetector;
009    import org.apache.torque.util.SimpleScanner;
010    import org.kuali.core.db.torque.KualiTorqueDataSQLTask;
011    
012    /**
013     * Generates platform specific SQL from database agnostic XML files. Each SQL file created by this goal contains data
014     * for a single table. The database platform to generate SQL for is determined by ${targetDatabase}. See also
015     * <code>impex:schemasql</code>
016     * 
017     * @goal datasql
018     * @phase generate-sources
019     */
020    public class DataSqlMojo extends DataModelTaskMojo {
021            /**
022             * The directory in which the SQL will be generated.
023             * 
024             * @parameter property="outputDir" expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
025             * @required
026             */
027            @SuppressWarnings("unused")
028            private String dummy;
029    
030            /**
031             * The location where the SQL file will be generated.
032             * 
033             * @parameter property="reportFile" expression="${reportFile}"
034             *            default-value="../../../reports/report.${project.artifactId}-data.sql"
035             */
036            @SuppressWarnings("unused")
037            private String dummy2;
038    
039            /**
040             * The location where the context property file for velocity will be generated.
041             * 
042             * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
043             *            default-value="${project.build.directory}/reports/context.datasql.properties"
044             */
045            @SuppressWarnings("unused")
046            private String dummy3;
047    
048            /**
049             * Only run this mojo if the data or schema has changed since the last run
050             * 
051             * @parameter expression="${runOnlyOnChange}" default-value="true"
052             * @required
053             */
054            private boolean runOnlyOnChange;
055    
056            /**
057             * The XML file describing the database schema
058             * 
059             * @parameter expression="${schemaXMLFile}" default-value="${basedir}/src/main/impex/${project.artifactId}.xml"
060             * @required
061             */
062            private File schemaXMLFile;
063    
064            /**
065             * The directory containing data XML files
066             * 
067             * @parameter expression="${dataXMLDir}" default-value="${basedir}/src/main/impex"
068             * @required
069             */
070            private File dataXMLDir;
071    
072            /**
073             * The default set of files in that directory to include (ant style notation)
074             * 
075             * @parameter expression="${dataXMLIncludes}" default-value="*.xml"
076             * @required
077             */
078            private String dataXMLIncludes;
079    
080            /**
081             * The default set of files in that directory to exclude (ant style notation)
082             * 
083             * @parameter expression="${dataXMLExcludes}" default-value="${project.artifactId}.xml"
084             */
085            private String dataXMLExcludes;
086    
087            /**
088             * The DTD for the data XML files
089             * 
090             * @parameter expression="${dataDTD}"
091             *            default-value="${project.build.directory}/generated-impex/${project.artifactId}.dtd"
092             * @required
093             */
094            private File dataDTD;
095    
096            @Override
097            public void executeMojo() throws MojoExecutionException {
098                    // loadPropertiesToMojo();
099                    updateConfiguration();
100                    validateConfiguration();
101                    generateContextProperties();
102                    configureTask();
103                    addTargetDatabaseToOutputDir();
104                    addTargetDatabaseToReportFile();
105                    ChangeDetector schema = new ChangeDetector(getCanonicalReportFile(), getSchemaFiles());
106                    ChangeDetector data = new ChangeDetector(getCanonicalReportFile(), getDataFiles());
107                    if (!schema.isChanged() && !data.isChanged() && isRunOnlyOnChange()) {
108                            getLog().info("------------------------------------------------------------------------");
109                            getLog().info("Data and schema are unchanged.  Skipping generation.");
110                            getLog().info("------------------------------------------------------------------------");
111                            return;
112                    }
113                    getLog().info("------------------------------------------------------------------------");
114                    getLog().info("Generating SQL for " + getTargetDatabase() + " from data XML files");
115                    getLog().info("------------------------------------------------------------------------");
116                    getAntTask().execute();
117            }
118    
119            protected List<File> getDataFiles() {
120                    return new SimpleScanner(getDataXMLDir(), getDataXMLIncludes(), getDataXMLExcludes()).getFiles();
121            }
122    
123            /**
124             * Returns the context properties for the Texen task.
125             * 
126             * @return The PropertiesConfiguration containing all context properties, not null.
127             */
128            protected PropertiesConfiguration getMojoContextProperties() {
129                    PropertiesConfiguration configuration = new PropertiesConfiguration();
130                    configuration.addProperty(TARGET_DATABASE_CONTEXT_PROPERTY, super.getTargetDatabase());
131                    return configuration;
132            }
133    
134            /**
135             * Configures the Texen task wrapped by this mojo
136             */
137            protected void configureTask() throws MojoExecutionException {
138                    KualiTorqueDataSQLTask task = new KualiTorqueDataSQLTask();
139                    setAntTask(task);
140                    super.configureTask();
141                    task.setDataDTD(getDataDTD());
142                    task.addFileset(getAntFileSet(getDataXMLDir(), getDataXMLIncludes(), getDataXMLExcludes()));
143                    task.setXmlFile(getSchemaXMLFile().getAbsolutePath());
144                    task.setTargetDatabase(getTargetDatabase());
145            }
146    
147            /**
148             * Returns the path to the control template.
149             * 
150             * @return "sql/load/Control.vm"
151             */
152            protected String getControlTemplate() {
153                    return "sql/load/Control.vm";
154            }
155    
156            public String getDataXMLIncludes() {
157                    return dataXMLIncludes;
158            }
159    
160            public void setDataXMLIncludes(String dataXMLIncludes) {
161                    this.dataXMLIncludes = dataXMLIncludes;
162            }
163    
164            public String getDataXMLExcludes() {
165                    return dataXMLExcludes;
166            }
167    
168            public void setDataXMLExcludes(String dataXMLExcludes) {
169                    this.dataXMLExcludes = dataXMLExcludes;
170            }
171    
172            public File getDataXMLDir() {
173                    return dataXMLDir;
174            }
175    
176            public void setDataXMLDir(File dataXMLDir) {
177                    this.dataXMLDir = dataXMLDir;
178            }
179    
180            public File getSchemaXMLFile() {
181                    return schemaXMLFile;
182            }
183    
184            public void setSchemaXMLFile(File schemaXMLFile) {
185                    this.schemaXMLFile = schemaXMLFile;
186            }
187    
188            public boolean isRunOnlyOnChange() {
189                    return runOnlyOnChange;
190            }
191    
192            public void setRunOnlyOnChange(boolean runOnlyOnDataChange) {
193                    this.runOnlyOnChange = runOnlyOnDataChange;
194            }
195    
196            public File getDataDTD() {
197                    return dataDTD;
198            }
199    
200            public void setDataDTD(File dataDTD) {
201                    this.dataDTD = dataDTD;
202            }
203    
204    }