View Javadoc

1   /**
2    * Copyright 2004-2013 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  	/**
71  	 * The suffix to append to the end of the file stored in META-INF
72  	 *
73  	 * @parameter expression="${impex.metaFileSuffix}" default-value="tables"
74  	 * @required
75  	 */
76  	private String metaFileSuffix;
77  
78  	@Override
79  	protected void executeMojo() throws MojoExecutionException, MojoFailureException {
80  		File databaseSQLDir = new File(baseSqlDir, targetDatabase);
81  		if (!databaseSQLDir.exists()) {
82  			throw new MojoExecutionException(databaseSQLDir + " does not exist");
83  		}
84  		try {
85  			getLog().info("Examining " + databaseSQLDir.getCanonicalPath());
86  			List<File> files = getFileListing(databaseSQLDir);
87  			getLog().info("Located " + files.size() + " " + extension + " files");
88  			List<String> tableNames = getTableNames(files);
89  			List<String> locations = getLocations(tableNames);
90  			getLog().info("Located " + tableNames.size() + " tables");
91  			String outputFilename = databaseSQLDir.getAbsolutePath() + FS + ".." + FS + ".." + FS + "META-INF" + FS + targetDatabase + FS + artifactId + "." + metaFileSuffix;
92  			File outputFile = new File(outputFilename);
93  			getLog().info("Generating " + outputFile.getCanonicalPath());
94  			FileUtils.writeLines(outputFile, locations);
95  		} catch (IOException e) {
96  			throw new MojoExecutionException("Unexpected IO error", e);
97  		}
98  	}
99  
100 	protected List<String> getLocations(List<String> tableNames) {
101 		List<String> locations = new ArrayList<String>();
102 		for (String tableName : tableNames) {
103 			locations.add("classpath:sql/" + targetDatabase + "/" + tableName + ".sql");
104 		}
105 		return locations;
106 	}
107 
108 	@SuppressWarnings("unchecked")
109 	protected List<File> getFileListing(File dir) {
110 		String[] extensions = new String[] { extension };
111 		Collection<File> files = FileUtils.listFiles(dir, extensions, false);
112 		List<File> fileList = new ArrayList<File>();
113 		if (files != null) {
114 			fileList.addAll(files);
115 		}
116 		return fileList;
117 	}
118 
119 	protected List<String> getTableNames(List<File> files) {
120 		String[] skipTokens = new String[] { artifactId + "." + extension, artifactId + "-constraints." + extension };
121 		List<String> tableNames = new ArrayList<String>();
122 		for (File file : files) {
123 			if (isSkip(file, skipTokens)) {
124 				continue;
125 			}
126 			String fragment = file.getName();
127 			int beginIndex = 0;
128 			int endIndex = fragment.length() - (extension.length() + 1);
129 			String fragmentMinusExtension = fragment.substring(beginIndex, endIndex);
130 			tableNames.add(fragmentMinusExtension);
131 		}
132 		Collections.sort(tableNames);
133 		return tableNames;
134 	}
135 
136 	protected boolean isSkip(File file, String[] skipTokens) {
137 		String path = file.getAbsolutePath();
138 		for (String skipToken : skipTokens) {
139 			if (path.endsWith(skipToken)) {
140 				return true;
141 			}
142 		}
143 		return false;
144 	}
145 
146 	public String getExtension() {
147 		return extension;
148 	}
149 
150 	public void setExtension(String extension) {
151 		this.extension = extension;
152 	}
153 
154 	public String getTargetDatabase() {
155 		return targetDatabase;
156 	}
157 
158 	public void setTargetDatabase(String targetDatabase) {
159 		this.targetDatabase = targetDatabase;
160 	}
161 
162 	public String getArtifactId() {
163 		return artifactId;
164 	}
165 
166 	public void setArtifactId(String artifactId) {
167 		this.artifactId = artifactId;
168 	}
169 
170 	public File getBaseSqlDir() {
171 		return baseSqlDir;
172 	}
173 
174 	public void setBaseSqlDir(File sqlDir) {
175 		this.baseSqlDir = sqlDir;
176 	}
177 
178 	public String getMetaFileSuffix() {
179 		return metaFileSuffix;
180 	}
181 
182 	public void setMetaFileSuffix(String metaFileSuffix) {
183 		this.metaFileSuffix = metaFileSuffix;
184 	}
185 
186 }