| 1 | |
package org.apache.torque.mojo; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.util.List; |
| 5 | |
import java.util.Properties; |
| 6 | |
import java.util.Set; |
| 7 | |
import java.util.TreeSet; |
| 8 | |
|
| 9 | |
import org.apache.maven.plugin.MojoExecutionException; |
| 10 | |
import org.apache.maven.plugin.MojoFailureException; |
| 11 | |
import org.apache.tools.ant.DirectoryScanner; |
| 12 | |
import org.apache.torque.engine.database.model.Database; |
| 13 | |
import org.apache.torque.engine.database.model.Table; |
| 14 | |
import org.kuali.core.db.torque.SetUtils; |
| 15 | |
import org.kuali.core.db.torque.Utils; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | 0 | public class IdentifyInvalidDataFiles extends BaseMojo { |
| 27 | 0 | private static final String FS = System.getProperty("file.separator"); |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
private String extension; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
private File dataDir; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
private String dataDirIncludes; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
private String dataDirExcludes; |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
private String schemaXMLFile; |
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
private String targetDatabase; |
| 60 | |
|
| 61 | |
@Override |
| 62 | |
protected void executeMojo() throws MojoExecutionException, MojoFailureException { |
| 63 | 0 | Utils utils = new Utils(); |
| 64 | |
try { |
| 65 | 0 | getLog().info("Examining " + dataDir.getAbsolutePath()); |
| 66 | 0 | Database db = utils.getDatabase(schemaXMLFile, targetDatabase); |
| 67 | 0 | DirectoryScanner ds = getDirectoryScanner(); |
| 68 | 0 | Set<File> existing = getExistingFiles(ds); |
| 69 | 0 | Set<File> allowed = getDatabaseFiles(db); |
| 70 | 0 | Set<File> invalid = SetUtils.difference(existing, allowed); |
| 71 | 0 | getLog().info(existing.size() + " data files currently exist"); |
| 72 | 0 | getLog().info(invalid.size() + " of those are invalid"); |
| 73 | 0 | StringBuilder sb = new StringBuilder(); |
| 74 | 0 | int count = 0; |
| 75 | 0 | for (File file : invalid) { |
| 76 | 0 | if (count != 0) { |
| 77 | 0 | sb.append(","); |
| 78 | |
} |
| 79 | 0 | sb.append("**/src/main/impex/" + file.getName()); |
| 80 | 0 | getLog().info("Marked for removal: " + file.getName()); |
| 81 | 0 | count++; |
| 82 | |
} |
| 83 | 0 | Properties properties = getProject().getProperties(); |
| 84 | 0 | properties.setProperty("impex.data.invalid", sb.toString()); |
| 85 | 0 | } catch (Exception e) { |
| 86 | 0 | throw new MojoExecutionException("Error executing mojo", e); |
| 87 | 0 | } |
| 88 | 0 | } |
| 89 | |
|
| 90 | |
protected Set<File> getDatabaseFiles(Database db) { |
| 91 | 0 | List<?> tables = db.getTables(); |
| 92 | 0 | Set<File> files = new TreeSet<File>(); |
| 93 | 0 | for (Object object : tables) { |
| 94 | 0 | Table table = (Table) object; |
| 95 | 0 | String tableName = table.getName(); |
| 96 | 0 | String filename = dataDir.getAbsolutePath() + FS + tableName + extension; |
| 97 | 0 | File file = new File(filename); |
| 98 | 0 | files.add(file); |
| 99 | 0 | } |
| 100 | 0 | return files; |
| 101 | |
} |
| 102 | |
|
| 103 | |
protected Set<File> getExistingFiles(DirectoryScanner ds) { |
| 104 | 0 | ds.scan(); |
| 105 | 0 | String[] relativeFilenames = ds.getIncludedFiles(); |
| 106 | 0 | Set<File> files = new TreeSet<File>(); |
| 107 | 0 | for (int i = 0; i < relativeFilenames.length; i++) { |
| 108 | 0 | String filename = ds.getBasedir().getAbsolutePath() + FS + relativeFilenames[i]; |
| 109 | 0 | File file = new File(filename); |
| 110 | 0 | files.add(file); |
| 111 | |
} |
| 112 | 0 | return files; |
| 113 | |
} |
| 114 | |
|
| 115 | |
protected DirectoryScanner getDirectoryScanner() { |
| 116 | 0 | DirectoryScanner ds = new DirectoryScanner(); |
| 117 | 0 | ds.setBasedir(dataDir); |
| 118 | 0 | ds.setIncludes(new String[] { dataDirIncludes }); |
| 119 | 0 | ds.setExcludes(new String[] { dataDirExcludes }); |
| 120 | 0 | return ds; |
| 121 | |
} |
| 122 | |
|
| 123 | |
protected File getFile(Table table) { |
| 124 | 0 | String tableName = table.getName(); |
| 125 | 0 | String filename = dataDir.getAbsolutePath() + FS + tableName + extension; |
| 126 | 0 | File file = new File(filename); |
| 127 | 0 | return file; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public File getDataDir() { |
| 131 | 0 | return dataDir; |
| 132 | |
} |
| 133 | |
|
| 134 | |
public void setDataDir(File dataDir) { |
| 135 | 0 | this.dataDir = dataDir; |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
public String getDataDirIncludes() { |
| 139 | 0 | return dataDirIncludes; |
| 140 | |
} |
| 141 | |
|
| 142 | |
public void setDataDirIncludes(String dataDirIncludes) { |
| 143 | 0 | this.dataDirIncludes = dataDirIncludes; |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
public String getDataDirExcludes() { |
| 147 | 0 | return dataDirExcludes; |
| 148 | |
} |
| 149 | |
|
| 150 | |
public void setDataDirExcludes(String dataDirExcludes) { |
| 151 | 0 | this.dataDirExcludes = dataDirExcludes; |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
public String getSchemaXMLFile() { |
| 155 | 0 | return schemaXMLFile; |
| 156 | |
} |
| 157 | |
|
| 158 | |
public void setSchemaXMLFile(String schemaXMLFile) { |
| 159 | 0 | this.schemaXMLFile = schemaXMLFile; |
| 160 | 0 | } |
| 161 | |
|
| 162 | |
public String getExtension() { |
| 163 | 0 | return extension; |
| 164 | |
} |
| 165 | |
|
| 166 | |
public void setExtension(String extension) { |
| 167 | 0 | this.extension = extension; |
| 168 | 0 | } |
| 169 | |
|
| 170 | |
public String getTargetDatabase() { |
| 171 | 0 | return targetDatabase; |
| 172 | |
} |
| 173 | |
|
| 174 | |
public void setTargetDatabase(String targetDatabase) { |
| 175 | 0 | this.targetDatabase = targetDatabase; |
| 176 | 0 | } |
| 177 | |
|
| 178 | |
} |