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