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  	private static final String FS = File.separator;
37  
38  	/**
39  	 * The extension for SQL files
40  	 *
41  	 * @parameter expression="${impex.extension}" default-value="sql"
42  	 * @required
43  	 */
44  	private String extension;
45  
46  	/**
47  	 * The database vendor being targeted
48  	 *
49  	 * @parameter expression="${impex.targetDatabase}"
50  	 * @required
51  	 */
52  	private String targetDatabase;
53  
54  	/**
55  	 * The directory where SQL is being generated - <code>targetDatabase</code> is automatically appended to this value
56  	 *
57  	 * @parameter expression="${impex.baseSqlDir}" default-value="${project.build.directory}/impex/sql"
58  	 * @required
59  	 */
60  	private File baseSqlDir;
61  
62  	/**
63  	 * The artifactId (aka "schema") for this set of impex data
64  	 *
65  	 * @parameter expression="${impex.artifactId}" default-value="${project.artifactId}"
66  	 * @required
67  	 */
68  	private String artifactId;
69  
70  	@Override
71  	protected void executeMojo() throws MojoExecutionException, MojoFailureException {
72  		File databaseSQLDir = new File(baseSqlDir, targetDatabase);
73  		if (!databaseSQLDir.exists()) {
74  			throw new MojoExecutionException(databaseSQLDir + " does not exist");
75  		}
76  		getLog().info("Examining " + databaseSQLDir);
77  		List<File> files = getFileListing(databaseSQLDir);
78  		getLog().info("Located " + files.size() + " " + extension + " files");
79  		List<String> tableNames = getTableNames(files);
80  		List<String> locations = getLocations(tableNames);
81  		getLog().info("Located " + tableNames.size() + " tables");
82  		try {
83  			String outputFilename = databaseSQLDir.getAbsolutePath() + FS + ".." + FS + ".." + FS + "META-INF" + FS + targetDatabase + FS + artifactId + ".tables";
84  			File outputFile = new File(outputFilename);
85  			getLog().info("Generating " + outputFile.getCanonicalPath());
86  			FileUtils.writeLines(outputFile, locations);
87  		} catch (IOException e) {
88  			throw new MojoExecutionException("Unexpected IO error", e);
89  		}
90  	}
91  
92  	protected List<String> getLocations(List<String> tableNames) {
93  		List<String> locations = new ArrayList<String>();
94  		for (String tableName : tableNames) {
95  			locations.add("classpath:sql/" + targetDatabase + "/" + tableName + ".sql");
96  		}
97  		return locations;
98  	}
99  
100 	@SuppressWarnings("unchecked")
101 	protected List<File> getFileListing(File dir) {
102 		String[] extensions = new String[] { extension };
103 		Collection<File> files = FileUtils.listFiles(dir, extensions, false);
104 		List<File> fileList = new ArrayList<File>();
105 		if (files != null) {
106 			fileList.addAll(files);
107 		}
108 		return fileList;
109 	}
110 
111 	protected List<String> getTableNames(List<File> files) {
112 		String[] skipTokens = new String[] { artifactId + "." + extension, artifactId + "-constraints." + extension };
113 		List<String> tableNames = new ArrayList<String>();
114 		for (File file : files) {
115 			if (isSkip(file, skipTokens)) {
116 				continue;
117 			}
118 			String fragment = file.getName();
119 			int beginIndex = 0;
120 			int endIndex = fragment.length() - (extension.length() + 1);
121 			String fragmentMinusExtension = fragment.substring(beginIndex, endIndex);
122 			tableNames.add(fragmentMinusExtension);
123 		}
124 		Collections.sort(tableNames);
125 		return tableNames;
126 	}
127 
128 	protected boolean isSkip(File file, String[] skipTokens) {
129 		String path = file.getAbsolutePath();
130 		for (String skipToken : skipTokens) {
131 			if (path.endsWith(skipToken)) {
132 				return true;
133 			}
134 		}
135 		return false;
136 	}
137 
138 	public String getExtension() {
139 		return extension;
140 	}
141 
142 	public void setExtension(String extension) {
143 		this.extension = extension;
144 	}
145 
146 	public String getTargetDatabase() {
147 		return targetDatabase;
148 	}
149 
150 	public void setTargetDatabase(String targetDatabase) {
151 		this.targetDatabase = targetDatabase;
152 	}
153 
154 	public String getArtifactId() {
155 		return artifactId;
156 	}
157 
158 	public void setArtifactId(String artifactId) {
159 		this.artifactId = artifactId;
160 	}
161 
162 	public File getBaseSqlDir() {
163 		return baseSqlDir;
164 	}
165 
166 	public void setBaseSqlDir(File sqlDir) {
167 		this.baseSqlDir = sqlDir;
168 	}
169 
170 }