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