Coverage Report - org.apache.torque.mojo.ImportMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ImportMojo
0%
0/40
0%
0/8
1.333
ImportMojo$Order
0%
0/2
N/A
1.333
 
 1  
 package org.apache.torque.mojo;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.File;
 23  
 import java.util.Collections;
 24  
 import java.util.Comparator;
 25  
 import java.util.List;
 26  
 import java.util.Vector;
 27  
 
 28  
 import org.apache.commons.collections.comparators.ReverseComparator;
 29  
 import org.apache.maven.plugin.MojoExecutionException;
 30  
 import org.apache.torque.util.SimpleScanner;
 31  
 import org.apache.torque.util.TransactionComparator;
 32  
 import org.kuali.db.Transaction;
 33  
 
 34  
 /**
 35  
  * Executes SQL for creating tables, primary keys, constraints, indexes, relationships, and views inside a database.
 36  
  * Also populates the tables with a default set of data.
 37  
  * 
 38  
  * @goal import
 39  
  */
 40  0
 public class ImportMojo extends AbstractSQLExecutorMojo {
 41  0
         private static final String FS = System.getProperty("file.separator");
 42  
 
 43  0
         public enum Order {
 44  0
                 ASCENDING, DESCENDING, NONE;
 45  
         }
 46  
 
 47  
         /**
 48  
          * This is the directory to scan for SQL files to import. ${targetDatabase} gets appended to this value
 49  
          * 
 50  
          * @parameter expression="${importDir}" default-value="${project.build.directory}/classes/sql"
 51  
          * @required
 52  
          */
 53  
         private File importDir;
 54  
 
 55  
         /**
 56  
          * @parameter expression="${importDirIncludes}" default-value="*.sql"
 57  
          */
 58  
         private String importDirIncludes;
 59  
 
 60  
         /**
 61  
          * @parameter expression="${importDirExcludes}" default-value=""
 62  
          */
 63  0
         private String importDirExcludes = "";
 64  
 
 65  
         /**
 66  
          * Set the order in which the SQL files will be executed. Possible values are <code>ASCENDING</code> and
 67  
          * <code>DESCENDING</code> and <code>NONE</code>. Default value is <code>ASCENDING</code>
 68  
          * 
 69  
          * @parameter expression="${order}"
 70  
          */
 71  0
         private String order = Order.ASCENDING.toString();
 72  
 
 73  
         protected void updateImportDir() {
 74  0
                 String path = importDir.getAbsolutePath();
 75  0
                 if (!path.endsWith(FS)) {
 76  0
                         path += FS;
 77  
                 }
 78  0
                 path += getTargetDatabase();
 79  0
                 importDir = new File(path);
 80  0
         }
 81  
 
 82  
         protected Vector<Transaction> getTransactions(List<File> files) {
 83  0
                 Vector<Transaction> transactions = new Vector<Transaction>();
 84  0
                 for (File file : files) {
 85  0
                         Transaction t = new Transaction();
 86  0
                         t.setResourceLocation(file.getAbsolutePath());
 87  0
                         transactions.add(t);
 88  0
                 }
 89  0
                 return transactions;
 90  
         }
 91  
 
 92  
         @Override
 93  
         protected void configureTransactions() throws MojoExecutionException {
 94  0
                 updateImportDir();
 95  0
                 List<File> files = new SimpleScanner(importDir, importDirIncludes, importDirExcludes).getFiles();
 96  0
                 transactions = getTransactions(files);
 97  0
                 sortTransactions(transactions);
 98  0
         }
 99  
 
 100  
         /**
 101  
          * Sort the transaction list.
 102  
          */
 103  
         @SuppressWarnings("unchecked")
 104  
         protected void sortTransactions(Vector<Transaction> transactions) {
 105  0
                 Comparator<Transaction> comparator = new TransactionComparator<Transaction>(getProject().getArtifactId());
 106  0
                 if (Order.ASCENDING.toString().equals(order)) {
 107  0
                         Collections.sort(transactions, comparator);
 108  0
                 } else if (Order.DESCENDING.toString().equals(order)) {
 109  0
                         Collections.sort(transactions, new ReverseComparator(comparator));
 110  
                 }
 111  0
         }
 112  
 
 113  
         public File getImportDir() {
 114  0
                 return importDir;
 115  
         }
 116  
 
 117  
         public void setImportDir(File importDirectory) {
 118  0
                 this.importDir = importDirectory;
 119  0
         }
 120  
 
 121  
         public String getImportDirIncludes() {
 122  0
                 return importDirIncludes;
 123  
         }
 124  
 
 125  
         public void setImportDirIncludes(String importDirectoryIncludes) {
 126  0
                 this.importDirIncludes = importDirectoryIncludes;
 127  0
         }
 128  
 
 129  
         public String getImportDirExcludes() {
 130  0
                 return importDirExcludes;
 131  
         }
 132  
 
 133  
         public void setImportDirExcludes(String importDirectoryExcludes) {
 134  0
                 this.importDirExcludes = importDirectoryExcludes;
 135  0
         }
 136  
 
 137  
         public String getOrder() {
 138  0
                 return order;
 139  
         }
 140  
 
 141  
         public void setOrder(String order) {
 142  0
                 this.order = order;
 143  0
         }
 144  
 }