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  		List<String> locations = getLocations(tableNames);
80  		getLog().info("Located " + tableNames.size() + " tables");
81  		File outputFile = new File(databaseSQLDir, artifactId + "-tables.txt");
82  		try {
83  			getLog().info("Generating " + outputFile);
84  			FileUtils.writeLines(outputFile, locations);
85  		} catch (IOException e) {
86  			throw new MojoExecutionException("Unexpected IO error", e);
87  		}
88  	}
89  
90  	protected List<String> getLocations(List<String> tableNames) {
91  		List<String> locations = new ArrayList<String>();
92  		for (String tableName : tableNames) {
93  			locations.add("classpath:sql/" + targetDatabase + "/" + tableName + ".sql");
94  		}
95  		return locations;
96  	}
97  
98  	@SuppressWarnings("unchecked")
99  	protected List<File> getFileListing(File dir) {
100 		String[] extensions = new String[] { extension };
101 		Collection<File> files = FileUtils.listFiles(dir, extensions, false);
102 		List<File> fileList = new ArrayList<File>();
103 		if (files != null) {
104 			fileList.addAll(files);
105 		}
106 		return fileList;
107 	}
108 
109 	protected List<String> getTableNames(List<File> files) {
110 		String[] skipTokens = new String[] { artifactId + "." + extension, artifactId + "-constraints." + extension };
111 		List<String> tableNames = new ArrayList<String>();
112 		for (File file : files) {
113 			if (isSkip(file, skipTokens)) {
114 				continue;
115 			}
116 			String fragment = file.getName();
117 			int beginIndex = 0;
118 			int endIndex = fragment.length() - (extension.length() + 1);
119 			String fragmentMinusExtension = fragment.substring(beginIndex, endIndex);
120 			tableNames.add(fragmentMinusExtension);
121 		}
122 		Collections.sort(tableNames);
123 		return tableNames;
124 	}
125 
126 	protected boolean isSkip(File file, String[] skipTokens) {
127 		String path = file.getAbsolutePath();
128 		for (String skipToken : skipTokens) {
129 			if (path.endsWith(skipToken)) {
130 				return true;
131 			}
132 		}
133 		return false;
134 	}
135 
136 	public String getExtension() {
137 		return extension;
138 	}
139 
140 	public void setExtension(String extension) {
141 		this.extension = extension;
142 	}
143 
144 	public String getTargetDatabase() {
145 		return targetDatabase;
146 	}
147 
148 	public void setTargetDatabase(String targetDatabase) {
149 		this.targetDatabase = targetDatabase;
150 	}
151 
152 	public String getArtifactId() {
153 		return artifactId;
154 	}
155 
156 	public void setArtifactId(String artifactId) {
157 		this.artifactId = artifactId;
158 	}
159 
160 	public File getBaseSqlDir() {
161 		return baseSqlDir;
162 	}
163 
164 	public void setBaseSqlDir(File sqlDir) {
165 		this.baseSqlDir = sqlDir;
166 	}
167 
168 }