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 java.io.File;
19  import java.util.List;
20  
21  import org.apache.commons.configuration.PropertiesConfiguration;
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.torque.util.ChangeDetector;
24  import org.apache.torque.util.SimpleScanner;
25  import org.kuali.core.db.torque.KualiTorqueDataSQLTask;
26  
27  /**
28   * Generates platform specific SQL from database agnostic XML files. Each SQL file created by this goal contains data
29   * for a single table. The database platform to generate SQL for is determined by ${targetDatabase}. See also
30   * <code>impex:schemasql</code>
31   * 
32   * @goal datasql
33   * @phase generate-sources
34   */
35  public class DataSqlMojo extends DataModelTaskMojo {
36  	/**
37  	 * The directory in which the SQL will be generated.
38  	 * 
39  	 * @parameter property="outputDir" expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
40  	 * @required
41  	 */
42  	@SuppressWarnings("unused")
43  	private String dummy;
44  
45  	/**
46  	 * The location where the SQL file will be generated.
47  	 * 
48  	 * @parameter property="reportFile" expression="${reportFile}"
49  	 *            default-value="../../../reports/report.${project.artifactId}-data.sql"
50  	 */
51  	@SuppressWarnings("unused")
52  	private String dummy2;
53  
54  	/**
55  	 * The location where the context property file for velocity will be generated.
56  	 * 
57  	 * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
58  	 *            default-value="${project.build.directory}/reports/context.datasql.properties"
59  	 */
60  	@SuppressWarnings("unused")
61  	private String dummy3;
62  
63  	/**
64  	 * Only run this mojo if the data or schema has changed since the last run
65  	 * 
66  	 * @parameter expression="${runOnlyOnChange}" default-value="true"
67  	 * @required
68  	 */
69  	private boolean runOnlyOnChange;
70  
71  	/**
72  	 * The XML file describing the database schema
73  	 * 
74  	 * @parameter expression="${schemaXMLFile}" default-value="${basedir}/src/main/impex/${project.artifactId}.xml"
75  	 * @required
76  	 */
77  	private File schemaXMLFile;
78  
79  	/**
80  	 * The directory containing data XML files
81  	 * 
82  	 * @parameter expression="${dataXMLDir}" default-value="${basedir}/src/main/impex"
83  	 * @required
84  	 */
85  	private File dataXMLDir;
86  
87  	/**
88  	 * The default set of files in that directory to include (ant style notation)
89  	 * 
90  	 * @parameter expression="${dataXMLIncludes}" default-value="*.xml"
91  	 * @required
92  	 */
93  	private String dataXMLIncludes;
94  
95  	/**
96  	 * The default set of files in that directory to exclude (ant style notation)
97  	 * 
98  	 * @parameter expression="${dataXMLExcludes}" default-value="${project.artifactId}.xml"
99  	 */
100 	private String dataXMLExcludes;
101 
102 	/**
103 	 * The DTD for the data XML files
104 	 * 
105 	 * @parameter expression="${dataDTD}"
106 	 *            default-value="${project.build.directory}/generated-impex/${project.artifactId}.dtd"
107 	 * @required
108 	 */
109 	private File dataDTD;
110 
111 	@Override
112 	public void executeMojo() throws MojoExecutionException {
113 		// loadPropertiesToMojo();
114 		updateConfiguration();
115 		validateConfiguration();
116 		generateContextProperties();
117 		configureTask();
118 		addTargetDatabaseToOutputDir();
119 		addTargetDatabaseToReportFile();
120 		ChangeDetector schema = new ChangeDetector(getCanonicalReportFile(), getSchemaFiles());
121 		ChangeDetector data = new ChangeDetector(getCanonicalReportFile(), getDataFiles());
122 		if (!schema.isChanged() && !data.isChanged() && isRunOnlyOnChange()) {
123 			getLog().info("------------------------------------------------------------------------");
124 			getLog().info("Data and schema are unchanged.  Skipping generation.");
125 			getLog().info("------------------------------------------------------------------------");
126 			return;
127 		}
128 		getLog().info("------------------------------------------------------------------------");
129 		getLog().info("Generating SQL for " + getTargetDatabase() + " from data XML files");
130 		getLog().info("------------------------------------------------------------------------");
131 		getAntTask().execute();
132 	}
133 
134 	protected List<File> getDataFiles() {
135 		return new SimpleScanner(getDataXMLDir(), getDataXMLIncludes(), getDataXMLExcludes()).getFiles();
136 	}
137 
138 	/**
139 	 * Returns the context properties for the Texen task.
140 	 * 
141 	 * @return The PropertiesConfiguration containing all context properties, not null.
142 	 */
143 	protected PropertiesConfiguration getMojoContextProperties() {
144 		PropertiesConfiguration configuration = new PropertiesConfiguration();
145 		configuration.addProperty(TARGET_DATABASE_CONTEXT_PROPERTY, super.getTargetDatabase());
146 		return configuration;
147 	}
148 
149 	/**
150 	 * Configures the Texen task wrapped by this mojo
151 	 */
152 	protected void configureTask() throws MojoExecutionException {
153 		KualiTorqueDataSQLTask task = new KualiTorqueDataSQLTask();
154 		setAntTask(task);
155 		super.configureTask();
156 		task.setDataDTD(getDataDTD());
157 		task.addFileset(getAntFileSet(getDataXMLDir(), getDataXMLIncludes(), getDataXMLExcludes()));
158 		task.setXmlFile(getSchemaXMLFile().getAbsolutePath());
159 		task.setTargetDatabase(getTargetDatabase());
160 	}
161 
162 	/**
163 	 * Returns the path to the control template.
164 	 * 
165 	 * @return "sql/load/Control.vm"
166 	 */
167 	protected String getControlTemplate() {
168 		return "sql/load/Control.vm";
169 	}
170 
171 	public String getDataXMLIncludes() {
172 		return dataXMLIncludes;
173 	}
174 
175 	public void setDataXMLIncludes(String dataXMLIncludes) {
176 		this.dataXMLIncludes = dataXMLIncludes;
177 	}
178 
179 	public String getDataXMLExcludes() {
180 		return dataXMLExcludes;
181 	}
182 
183 	public void setDataXMLExcludes(String dataXMLExcludes) {
184 		this.dataXMLExcludes = dataXMLExcludes;
185 	}
186 
187 	public File getDataXMLDir() {
188 		return dataXMLDir;
189 	}
190 
191 	public void setDataXMLDir(File dataXMLDir) {
192 		this.dataXMLDir = dataXMLDir;
193 	}
194 
195 	public File getSchemaXMLFile() {
196 		return schemaXMLFile;
197 	}
198 
199 	public void setSchemaXMLFile(File schemaXMLFile) {
200 		this.schemaXMLFile = schemaXMLFile;
201 	}
202 
203 	public boolean isRunOnlyOnChange() {
204 		return runOnlyOnChange;
205 	}
206 
207 	public void setRunOnlyOnChange(boolean runOnlyOnDataChange) {
208 		this.runOnlyOnChange = runOnlyOnDataChange;
209 	}
210 
211 	public File getDataDTD() {
212 		return dataDTD;
213 	}
214 
215 	public void setDataDTD(File dataDTD) {
216 		this.dataDTD = dataDTD;
217 	}
218 
219 }