View Javadoc

1   /**
2    * Copyright 2004-2013 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 for a single table. The database platform to generate SQL for
29   * is determined by ${targetDatabase}. See also <code>impex:schemasql</code>
30   * 
31   * @goal datasql
32   * @phase generate-sources
33   */
34  public class DataSqlMojo extends DataModelTaskMojo {
35  	/**
36  	 * The directory in which the SQL will be generated.
37  	 * 
38  	 * @parameter expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
39  	 * @required
40  	 */
41  	@SuppressWarnings("unused")
42  	private String dummy;
43  
44  	/**
45  	 * The location where the SQL file will be generated.
46  	 * 
47  	 * @parameter expression="${reportFile}" default-value="../../../reports/report.${project.artifactId}-data.sql"
48  	 */
49  	@SuppressWarnings("unused")
50  	private String dummy2;
51  
52  	/**
53  	 * The location where the context property file for velocity will be generated.
54  	 * 
55  	 * @parameter expression="${contextPropertiesPath}" default-value="${project.build.directory}/reports/context.datasql.properties"
56  	 */
57  	@SuppressWarnings("unused")
58  	private String dummy3;
59  
60  	/**
61  	 * Only run this mojo if the data or schema has changed since the last run
62  	 * 
63  	 * @parameter expression="${runOnlyOnChange}" default-value="true"
64  	 * @required
65  	 */
66  	private boolean runOnlyOnChange;
67  
68  	/**
69  	 * The XML file describing the database schema
70  	 * 
71  	 * @parameter expression="${schemaXMLFile}" default-value="${basedir}/src/main/impex/${project.artifactId}.xml"
72  	 * @required
73  	 */
74  	private File schemaXMLFile;
75  
76  	/**
77  	 * The directory containing data XML files
78  	 * 
79  	 * @parameter expression="${dataXMLDir}" default-value="${basedir}/src/main/impex"
80  	 * @required
81  	 */
82  	private File dataXMLDir;
83  
84  	/**
85  	 * The default set of files in that directory to include (ant style notation)
86  	 * 
87  	 * @parameter expression="${dataXMLIncludes}" default-value="*.xml"
88  	 * @required
89  	 */
90  	private String dataXMLIncludes;
91  
92  	/**
93  	 * The default set of files in that directory to exclude (ant style notation)
94  	 * 
95  	 * @parameter expression="${dataXMLExcludes}" default-value="${project.artifactId}.xml"
96  	 */
97  	private String dataXMLExcludes;
98  
99  	/**
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 }