001 /** 002 * Copyright 2004-2012 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 029 * for a single table. The database platform to generate SQL for is determined by ${targetDatabase}. See also 030 * <code>impex:schemasql</code> 031 * 032 * @goal datasql 033 * @phase generate-sources 034 */ 035 public class DataSqlMojo extends DataModelTaskMojo { 036 /** 037 * The directory in which the SQL will be generated. 038 * 039 * @parameter property="outputDir" expression="${outputDir}" default-value="${project.build.directory}/classes/sql" 040 * @required 041 */ 042 @SuppressWarnings("unused") 043 private String dummy; 044 045 /** 046 * The location where the SQL file will be generated. 047 * 048 * @parameter property="reportFile" expression="${reportFile}" 049 * default-value="../../../reports/report.${project.artifactId}-data.sql" 050 */ 051 @SuppressWarnings("unused") 052 private String dummy2; 053 054 /** 055 * The location where the context property file for velocity will be generated. 056 * 057 * @parameter property="contextPropertiesPath" expression="${contextPropertiesPath}" 058 * default-value="${project.build.directory}/reports/context.datasql.properties" 059 */ 060 @SuppressWarnings("unused") 061 private String dummy3; 062 063 /** 064 * Only run this mojo if the data or schema has changed since the last run 065 * 066 * @parameter expression="${runOnlyOnChange}" default-value="true" 067 * @required 068 */ 069 private boolean runOnlyOnChange; 070 071 /** 072 * The XML file describing the database schema 073 * 074 * @parameter expression="${schemaXMLFile}" default-value="${basedir}/src/main/impex/${project.artifactId}.xml" 075 * @required 076 */ 077 private File schemaXMLFile; 078 079 /** 080 * The directory containing data XML files 081 * 082 * @parameter expression="${dataXMLDir}" default-value="${basedir}/src/main/impex" 083 * @required 084 */ 085 private File dataXMLDir; 086 087 /** 088 * The default set of files in that directory to include (ant style notation) 089 * 090 * @parameter expression="${dataXMLIncludes}" default-value="*.xml" 091 * @required 092 */ 093 private String dataXMLIncludes; 094 095 /** 096 * The default set of files in that directory to exclude (ant style notation) 097 * 098 * @parameter expression="${dataXMLExcludes}" default-value="${project.artifactId}.xml" 099 */ 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 }