1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  package org.apache.torque.mojo;
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
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  
51  
52  
53  
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  
64  
65  
66  
67  
68  	private File importDir;
69  
70  	
71  
72  
73  	private String importDirIncludes;
74  
75  	
76  
77  
78  	private String importDirExcludes = "";
79  
80  	
81  
82  
83  
84  
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 
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 }