View Javadoc

1   package org.apache.torque.mojo;
2   
3   import java.io.File;
4   import java.util.List;
5   
6   import org.apache.commons.configuration.PropertiesConfiguration;
7   import org.apache.maven.plugin.MojoExecutionException;
8   import org.apache.torque.util.ChangeDetector;
9   import org.apache.torque.util.SimpleScanner;
10  import org.kuali.core.db.torque.KualiTorqueDataSQLTask;
11  
12  /**
13   * Generates platform specific SQL from database agnostic XML files. Each SQL file created by this goal contains data
14   * for a single table. The database platform to generate SQL for is determined by ${targetDatabase}. See also
15   * <code>impex:schemasql</code>
16   * 
17   * @goal datasql
18   * @phase generate-sources
19   */
20  public class DataSqlMojo extends DataModelTaskMojo {
21  	/**
22  	 * The directory in which the SQL will be generated.
23  	 * 
24  	 * @parameter property="outputDir" expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
25  	 * @required
26  	 */
27  	@SuppressWarnings("unused")
28  	private String dummy;
29  
30  	/**
31  	 * The location where the SQL file will be generated.
32  	 * 
33  	 * @parameter property="reportFile" expression="${reportFile}"
34  	 *            default-value="../../../reports/report.${project.artifactId}-data.sql"
35  	 */
36  	@SuppressWarnings("unused")
37  	private String dummy2;
38  
39  	/**
40  	 * The location where the context property file for velocity will be generated.
41  	 * 
42  	 * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}"
43  	 *            default-value="${project.build.directory}/reports/context.datasql.properties"
44  	 */
45  	@SuppressWarnings("unused")
46  	private String dummy3;
47  
48  	/**
49  	 * Only run this mojo if the data or schema has changed since the last run
50  	 * 
51  	 * @parameter expression="${runOnlyOnChange}" default-value="true"
52  	 * @required
53  	 */
54  	private boolean runOnlyOnChange;
55  
56  	/**
57  	 * The XML file describing the database schema
58  	 * 
59  	 * @parameter expression="${schemaXMLFile}" default-value="${basedir}/src/main/impex/${project.artifactId}.xml"
60  	 * @required
61  	 */
62  	private File schemaXMLFile;
63  
64  	/**
65  	 * The directory containing data XML files
66  	 * 
67  	 * @parameter expression="${dataXMLDir}" default-value="${basedir}/src/main/impex"
68  	 * @required
69  	 */
70  	private File dataXMLDir;
71  
72  	/**
73  	 * The default set of files in that directory to include (ant style notation)
74  	 * 
75  	 * @parameter expression="${dataXMLIncludes}" default-value="*.xml"
76  	 * @required
77  	 */
78  	private String dataXMLIncludes;
79  
80  	/**
81  	 * The default set of files in that directory to exclude (ant style notation)
82  	 * 
83  	 * @parameter expression="${dataXMLExcludes}" default-value="${project.artifactId}.xml"
84  	 */
85  	private String dataXMLExcludes;
86  
87  	/**
88  	 * The DTD for the data XML files
89  	 * 
90  	 * @parameter expression="${dataDTD}"
91  	 *            default-value="${project.build.directory}/generated-impex/${project.artifactId}.dtd"
92  	 * @required
93  	 */
94  	private File dataDTD;
95  
96  	@Override
97  	public void executeMojo() throws MojoExecutionException {
98  		// loadPropertiesToMojo();
99  		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 }