View Javadoc

1   /**
2    * Copyright 2004-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.torque.mojo;
17  
18  /*
19   * Licensed to the Apache Software Foundation (ASF) under one
20   * or more contributor license agreements.  See the NOTICE file
21   * distributed with this work for additional information
22   * regarding copyright ownership.  The ASF licenses this file
23   * to you under the Apache License, Version 2.0 (the
24   * "License"); you may not use this file except in compliance
25   * with the License.  You may obtain a copy of the License at
26   *
27   *   http://www.apache.org/licenses/LICENSE-2.0
28   *
29   * Unless required by applicable law or agreed to in writing,
30   * software distributed under the License is distributed on an
31   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
32   * KIND, either express or implied.  See the License for the
33   * specific language governing permissions and limitations
34   * under the License.
35   */
36  
37  import java.io.File;
38  import java.util.Collections;
39  import java.util.Comparator;
40  import java.util.List;
41  import java.util.Vector;
42  
43  import org.apache.commons.collections.comparators.ReverseComparator;
44  import org.apache.maven.plugin.MojoExecutionException;
45  import org.apache.torque.util.SimpleScanner;
46  import org.apache.torque.util.TransactionComparator;
47  import org.kuali.db.Transaction;
48  
49  /**
50   * Executes SQL for creating tables, primary keys, constraints, indexes, relationships, and views inside a database.
51   * Also populates the tables with a default set of data.
52   * 
53   * @goal import
54   */
55  public class ImportMojo extends AbstractSQLExecutorMojo {
56  	private static final String FS = System.getProperty("file.separator");
57  
58  	public enum Order {
59  		ASCENDING, DESCENDING, NONE;
60  	}
61  
62  	/**
63  	 * This is the directory to scan for SQL files to import. ${targetDatabase} gets appended to this value
64  	 * 
65  	 * @parameter expression="${importDir}" default-value="${project.build.directory}/classes/sql"
66  	 * @required
67  	 */
68  	private File importDir;
69  
70  	/**
71  	 * @parameter expression="${importDirIncludes}" default-value="*.sql"
72  	 */
73  	private String importDirIncludes;
74  
75  	/**
76  	 * @parameter expression="${importDirExcludes}" default-value=""
77  	 */
78  	private String importDirExcludes = "";
79  
80  	/**
81  	 * Set the order in which the SQL files will be executed. Possible values are <code>ASCENDING</code> and
82  	 * <code>DESCENDING</code> and <code>NONE</code>. Default value is <code>ASCENDING</code>
83  	 * 
84  	 * @parameter expression="${order}"
85  	 */
86  	private String order = Order.ASCENDING.toString();
87  
88  	protected void updateImportDir() {
89  		String path = importDir.getAbsolutePath();
90  		if (!path.endsWith(FS)) {
91  			path += FS;
92  		}
93  		path += getTargetDatabase();
94  		importDir = new File(path);
95  	}
96  
97  	protected Vector<Transaction> getTransactions(List<File> files) {
98  		Vector<Transaction> transactions = new Vector<Transaction>();
99  		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 }