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  import java.io.File;
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.List;
24  
25  import org.apache.commons.io.FileUtils;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  
29  /**
30   * Examine the directory that SQL files were generated to and produce a file that contains a listing of any tables that contained data at
31   * the point in time the export was run.
32   *
33   * @goal generatetablelisting
34   */
35  public class GenerateTableListingMojo extends BaseMojo {
36  
37  	/**
38  	 * The extension for SQL files
39  	 *
40  	 * @parameter expression="${impex.extension}" default-value="sql"
41  	 * @required
42  	 */
43  	private String extension;
44  
45  	/**
46  	 * The database vendor being targeted
47  	 *
48  	 * @parameter expression="${impex.targetDatabase}"
49  	 * @required
50  	 */
51  	private String targetDatabase;
52  
53  	/**
54  	 * The directory where SQL is being generated - <code>targetDatabase</code> is automatically appended to this value
55  	 *
56  	 * @parameter expression="${impex.baseSqlDir}" default-value="${project.build.directory}/impex/sql"
57  	 * @required
58  	 */
59  	private File baseSqlDir;
60  
61  	/**
62  	 * The artifactId (aka "schema") for this set of impex data
63  	 *
64  	 * @parameter expression="${impex.artifactId}" default-value="${project.artifactId}"
65  	 * @required
66  	 */
67  	private String artifactId;
68  
69  	@Override
70  	protected void executeMojo() throws MojoExecutionException, MojoFailureException {
71  		File databaseSQLDir = new File(baseSqlDir, targetDatabase);
72  		if (!databaseSQLDir.exists()) {
73  			throw new MojoExecutionException(databaseSQLDir + " does not exist");
74  		}
75  		getLog().info("Examining " + databaseSQLDir);
76  		List<File> files = getFileListing(databaseSQLDir);
77  		getLog().info("Located " + files.size() + " " + extension + " files");
78  		List<String> tableNames = getTableNames(files);
79  		getLog().info("Located " + tableNames.size() + " tables");
80  		File outputFile = new File(databaseSQLDir, artifactId + "-tables.txt");
81  		try {
82  			getLog().info("Generating " + outputFile);
83  			FileUtils.writeLines(outputFile, tableNames);
84  		} catch (IOException e) {
85  			throw new MojoExecutionException("Unexpected IO error", e);
86  		}
87  	}
88  
89  	@SuppressWarnings("unchecked")
90  	protected List<File> getFileListing(File dir) {
91  		String[] extensions = new String[] { extension };
92  		Collection<File> files = FileUtils.listFiles(dir, extensions, false);
93  		List<File> fileList = new ArrayList<File>();
94  		if (files != null) {
95  			fileList.addAll(files);
96  		}
97  		return fileList;
98  	}
99  
100 	protected List<String> getTableNames(List<File> files) {
101 		String[] skipTokens = new String[] { artifactId + "." + extension, artifactId + "-constraints." + extension };
102 		List<String> tableNames = new ArrayList<String>();
103 		for (File file : files) {
104 			if (isSkip(file, skipTokens)) {
105 				continue;
106 			}
107 			String fragment = file.getName();
108 			int beginIndex = 0;
109 			int endIndex = fragment.length() - (extension.length() + 1);
110 			String fragmentMinusExtension = fragment.substring(beginIndex, endIndex);
111 			tableNames.add(fragmentMinusExtension);
112 		}
113 		Collections.sort(tableNames);
114 		return tableNames;
115 	}
116 
117 	protected boolean isSkip(File file, String[] skipTokens) {
118 		String path = file.getAbsolutePath();
119 		for (String skipToken : skipTokens) {
120 			if (path.endsWith(skipToken)) {
121 				return true;
122 			}
123 		}
124 		return false;
125 	}
126 
127 	public String getExtension() {
128 		return extension;
129 	}
130 
131 	public void setExtension(String extension) {
132 		this.extension = extension;
133 	}
134 
135 	public String getTargetDatabase() {
136 		return targetDatabase;
137 	}
138 
139 	public void setTargetDatabase(String targetDatabase) {
140 		this.targetDatabase = targetDatabase;
141 	}
142 
143 	public String getArtifactId() {
144 		return artifactId;
145 	}
146 
147 	public void setArtifactId(String artifactId) {
148 		this.artifactId = artifactId;
149 	}
150 
151 	public File getBaseSqlDir() {
152 		return baseSqlDir;
153 	}
154 
155 	public void setBaseSqlDir(File sqlDir) {
156 		this.baseSqlDir = sqlDir;
157 	}
158 
159 }