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