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    /*
019     * Licensed to the Apache Software Foundation (ASF) under one
020     * or more contributor license agreements.  See the NOTICE file
021     * distributed with this work for additional information
022     * regarding copyright ownership.  The ASF licenses this file
023     * to you under the Apache License, Version 2.0 (the
024     * "License"); you may not use this file except in compliance
025     * with the License.  You may obtain a copy of the License at
026     *
027     *   http://www.apache.org/licenses/LICENSE-2.0
028     *
029     * Unless required by applicable law or agreed to in writing,
030     * software distributed under the License is distributed on an
031     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
032     * KIND, either express or implied.  See the License for the
033     * specific language governing permissions and limitations
034     * under the License.
035     */
036    
037    import java.io.File;
038    import java.util.Collections;
039    import java.util.Comparator;
040    import java.util.List;
041    import java.util.Vector;
042    
043    import org.apache.commons.collections.comparators.ReverseComparator;
044    import org.apache.maven.plugin.MojoExecutionException;
045    import org.apache.torque.util.SimpleScanner;
046    import org.apache.torque.util.TransactionComparator;
047    import org.kuali.db.Transaction;
048    
049    /**
050     * Executes SQL for creating tables, primary keys, constraints, indexes, relationships, and views inside a database.
051     * Also populates the tables with a default set of data.
052     * 
053     * @goal import
054     */
055    public class ImportMojo extends AbstractSQLExecutorMojo {
056            private static final String FS = System.getProperty("file.separator");
057    
058            public enum Order {
059                    ASCENDING, DESCENDING, NONE;
060            }
061    
062            /**
063             * This is the directory to scan for SQL files to import. ${targetDatabase} gets appended to this value
064             * 
065             * @parameter expression="${importDir}" default-value="${project.build.directory}/classes/sql"
066             * @required
067             */
068            private File importDir;
069    
070            /**
071             * @parameter expression="${importDirIncludes}" default-value="*.sql"
072             */
073            private String importDirIncludes;
074    
075            /**
076             * @parameter expression="${importDirExcludes}" default-value=""
077             */
078            private String importDirExcludes = "";
079    
080            /**
081             * Set the order in which the SQL files will be executed. Possible values are <code>ASCENDING</code> and
082             * <code>DESCENDING</code> and <code>NONE</code>. Default value is <code>ASCENDING</code>
083             * 
084             * @parameter expression="${order}"
085             */
086            private String order = Order.ASCENDING.toString();
087    
088            protected void updateImportDir() {
089                    String path = importDir.getAbsolutePath();
090                    if (!path.endsWith(FS)) {
091                            path += FS;
092                    }
093                    path += getTargetDatabase();
094                    importDir = new File(path);
095            }
096    
097            protected Vector<Transaction> getTransactions(List<File> files) {
098                    Vector<Transaction> transactions = new Vector<Transaction>();
099                    for (File file : files) {
100                            Transaction t = new Transaction();
101                            t.setResourceLocation(file.getAbsolutePath());
102                            transactions.add(t);
103                    }
104                    return transactions;
105            }
106    
107            @Override
108            protected void configureTransactions() throws MojoExecutionException {
109                    updateImportDir();
110                    List<File> files = new SimpleScanner(importDir, importDirIncludes, importDirExcludes).getFiles();
111                    transactions = getTransactions(files);
112                    sortTransactions(transactions);
113            }
114    
115            /**
116             * Sort the transaction list.
117             */
118            @SuppressWarnings("unchecked")
119            protected void sortTransactions(Vector<Transaction> transactions) {
120                    Comparator<Transaction> comparator = new TransactionComparator<Transaction>(getProject().getArtifactId());
121                    if (Order.ASCENDING.toString().equals(order)) {
122                            Collections.sort(transactions, comparator);
123                    } else if (Order.DESCENDING.toString().equals(order)) {
124                            Collections.sort(transactions, new ReverseComparator(comparator));
125                    }
126            }
127    
128            public File getImportDir() {
129                    return importDir;
130            }
131    
132            public void setImportDir(File importDirectory) {
133                    this.importDir = importDirectory;
134            }
135    
136            public String getImportDirIncludes() {
137                    return importDirIncludes;
138            }
139    
140            public void setImportDirIncludes(String importDirectoryIncludes) {
141                    this.importDirIncludes = importDirectoryIncludes;
142            }
143    
144            public String getImportDirExcludes() {
145                    return importDirExcludes;
146            }
147    
148            public void setImportDirExcludes(String importDirectoryExcludes) {
149                    this.importDirExcludes = importDirectoryExcludes;
150            }
151    
152            public String getOrder() {
153                    return order;
154            }
155    
156            public void setOrder(String order) {
157                    this.order = order;
158            }
159    }