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 public class IdentifyInvalidDataFiles extends BaseMojo {
27 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 Utils utils = new Utils();
64 try {
65 getLog().info("Examining " + dataDir.getAbsolutePath());
66 Database db = utils.getDatabase(schemaXMLFile, targetDatabase);
67 DirectoryScanner ds = getDirectoryScanner();
68 Set<File> existing = getExistingFiles(ds);
69 Set<File> allowed = getDatabaseFiles(db);
70 Set<File> invalid = SetUtils.difference(existing, allowed);
71 getLog().info(existing.size() + " data files currently exist");
72 getLog().info(invalid.size() + " of those are invalid");
73 StringBuilder sb = new StringBuilder();
74 int count = 0;
75 for (File file : invalid) {
76 if (count != 0) {
77 sb.append(",");
78 }
79 sb.append("**/src/main/impex/" + file.getName());
80 getLog().info("Marked for removal: " + file.getName());
81 count++;
82 }
83 Properties properties = getProject().getProperties();
84 properties.setProperty("impex.data.invalid", sb.toString());
85 } catch (Exception e) {
86 throw new MojoExecutionException("Error executing mojo", e);
87 }
88 }
89
90 protected Set<File> getDatabaseFiles(Database db) {
91 List<?> tables = db.getTables();
92 Set<File> files = new TreeSet<File>();
93 for (Object object : tables) {
94 Table table = (Table) object;
95 String tableName = table.getName();
96 String filename = dataDir.getAbsolutePath() + FS + tableName + extension;
97 File file = new File(filename);
98 files.add(file);
99 }
100 return files;
101 }
102
103 protected Set<File> getExistingFiles(DirectoryScanner ds) {
104 ds.scan();
105 String[] relativeFilenames = ds.getIncludedFiles();
106 Set<File> files = new TreeSet<File>();
107 for (int i = 0; i < relativeFilenames.length; i++) {
108 String filename = ds.getBasedir().getAbsolutePath() + FS + relativeFilenames[i];
109 File file = new File(filename);
110 files.add(file);
111 }
112 return files;
113 }
114
115 protected DirectoryScanner getDirectoryScanner() {
116 DirectoryScanner ds = new DirectoryScanner();
117 ds.setBasedir(dataDir);
118 ds.setIncludes(new String[] { dataDirIncludes });
119 ds.setExcludes(new String[] { dataDirExcludes });
120 return ds;
121 }
122
123 protected File getFile(Table table) {
124 String tableName = table.getName();
125 String filename = dataDir.getAbsolutePath() + FS + tableName + extension;
126 File file = new File(filename);
127 return file;
128 }
129
130 public File getDataDir() {
131 return dataDir;
132 }
133
134 public void setDataDir(File dataDir) {
135 this.dataDir = dataDir;
136 }
137
138 public String getDataDirIncludes() {
139 return dataDirIncludes;
140 }
141
142 public void setDataDirIncludes(String dataDirIncludes) {
143 this.dataDirIncludes = dataDirIncludes;
144 }
145
146 public String getDataDirExcludes() {
147 return dataDirExcludes;
148 }
149
150 public void setDataDirExcludes(String dataDirExcludes) {
151 this.dataDirExcludes = dataDirExcludes;
152 }
153
154 public String getSchemaXMLFile() {
155 return schemaXMLFile;
156 }
157
158 public void setSchemaXMLFile(String schemaXMLFile) {
159 this.schemaXMLFile = schemaXMLFile;
160 }
161
162 public String getExtension() {
163 return extension;
164 }
165
166 public void setExtension(String extension) {
167 this.extension = extension;
168 }
169
170 public String getTargetDatabase() {
171 return targetDatabase;
172 }
173
174 public void setTargetDatabase(String targetDatabase) {
175 this.targetDatabase = targetDatabase;
176 }
177
178 }