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 org.apache.maven.plugin.MojoExecutionException;
019    import org.apache.torque.util.ChangeDetector;
020    
021    /**
022     * Generates platform specific SQL from database agnostic XML files.<br>
023     * <br>
024     * There are two types of SQL files created by this goal:<br>
025     * <br>
026     * Type 1: DDL statements for creating tables, primary keys, indexes, and unique constraints. Does not contain DDL for enforcing relationships between tables.<br>
027     * Type 2: DDL statements for creating and enforcing relationships between tables<br>
028     * <br>
029     * This allows data to be imported into multiple tables concurrently. Running the first type of SQL file will create the empty tables without any foreign key constraints. Data can
030     * then be loaded concurrently into the tables (using optimized high speed tools if desired) without needing to worry about the order in which the tables are loaded. After data has
031     * been loaded, the second type of SQL file can be run to add the relationships between the tables. As long as the data set is consistent and correct, all the relationships will
032     * get created correctly.<br>
033     * <br>
034     * The database platform to generate SQL for is determined by ${targetDatabase}. See also <code>impex:datasql</code>
035     * 
036     * @goal schemasql
037     * @phase generate-sources
038     */
039    public class SchemaSqlMojo extends SqlMojoBase {
040    
041            /**
042             * The directory in which the SQL will be generated.
043             * 
044             * @parameter expression="${outputDir}" default-value="${project.build.directory}/classes/sql"
045             */
046            @SuppressWarnings("unused")
047            private String dummy1;
048    
049            /**
050             * The location where the report file will be generated.
051             * 
052             * @parameter expression="${reportFile}" default-value= "../../../reports/report.${project.artifactId}.sql.generation"
053             */
054            @SuppressWarnings("unused")
055            private String dummy2;
056    
057            /**
058             * The location where the context property file for velocity will be generated.
059             * 
060             * @parameter expression="${contextPropertiesPath}" default-value="${project.build.directory}/reports/context.sql.properties"
061             */
062            @SuppressWarnings("unused")
063            private String dummy3;
064    
065            /**
066             * The suffix of the generated sql files.
067             * 
068             * @parameter expression="${suffix}"
069             */
070            @SuppressWarnings("unused")
071            private String dummy4;
072    
073            protected void showConfig() {
074                    getLog().info("Schema Dir: " + getSchemaDir());
075                    getLog().info("Includes: " + getSchemaIncludes());
076                    getLog().info("Excludes: " + getSchemaExcludes());
077            }
078    
079            /**
080             * Generate SQL from schema XML files
081             */
082            @Override
083            public void executeMojo() throws MojoExecutionException {
084                    updateConfiguration();
085                    validateConfiguration();
086                    generateContextProperties();
087                    configureTask();
088                    addTargetDatabaseToOutputDir();
089                    addTargetDatabaseToReportFile();
090                    showConfig();
091                    ChangeDetector detector = new ChangeDetector(getCanonicalReportFile(), getSchemaFiles());
092                    if (!detector.isChanged() && isRunOnlyOnSchemaChange()) {
093                            getLog().info("Schema has not changed.  Skipping generation");
094                            return;
095                    }
096                    getLog().info("------------------------------------------------------------------------");
097                    getLog().info("Generating SQL for " + getTargetDatabase() + " from schema XML files");
098                    getLog().info("------------------------------------------------------------------------");
099                    getAntTask().execute();
100            }
101    }