| 1 |  | package org.apache.torque.mojo; | 
            
  
    | 2 |  |  | 
            
  
    | 3 |  | import java.io.File; | 
            
  
    | 4 |  | import java.io.FileInputStream; | 
            
  
    | 5 |  | import java.io.IOException; | 
            
  
    | 6 |  | import java.io.InputStream; | 
            
  
    | 7 |  | import java.io.OutputStream; | 
            
  
    | 8 |  | import java.util.HashSet; | 
            
  
    | 9 |  | import java.util.List; | 
            
  
    | 10 |  | import java.util.Properties; | 
            
  
    | 11 |  | import java.util.Set; | 
            
  
    | 12 |  | import java.util.TreeSet; | 
            
  
    | 13 |  |  | 
            
  
    | 14 |  | import org.apache.commons.io.FileUtils; | 
            
  
    | 15 |  | import org.apache.commons.io.IOUtils; | 
            
  
    | 16 |  | import org.apache.maven.plugin.MojoExecutionException; | 
            
  
    | 17 |  | import org.apache.maven.plugin.MojoFailureException; | 
            
  
    | 18 |  | import org.apache.tools.ant.DirectoryScanner; | 
            
  
    | 19 |  | import org.apache.torque.engine.database.model.Database; | 
            
  
    | 20 |  | import org.apache.torque.engine.database.model.Table; | 
            
  
    | 21 |  | import org.kuali.core.db.torque.SetUtils; | 
            
  
    | 22 |  | import org.kuali.core.db.torque.Utils; | 
            
  
    | 23 |  |  | 
            
  
    | 24 |  |  | 
            
  
    | 25 |  |  | 
            
  
    | 26 |  |  | 
            
  
    | 27 |  |  | 
            
  
    | 28 |  |  | 
            
  
    | 29 |  |  | 
            
  
    | 30 |  |  | 
            
  
    | 31 |  |  | 
            
  
    | 32 |  |  | 
            
  
    | 33 |  |  | 
            
  
    | 34 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 126 (126) | Complexity: 30 | Complexity Density: 0.32 |  | 
  
  
    | 35 |  | public class IdentifyInvalidDataFiles extends BaseMojo { | 
            
  
    | 36 |  | private static final String FS = System.getProperty("file.separator"); | 
            
  
    | 37 |  |  | 
            
  
    | 38 |  |  | 
            
  
    | 39 |  |  | 
            
  
    | 40 |  |  | 
            
  
    | 41 |  |  | 
            
  
    | 42 |  | private String extension; | 
            
  
    | 43 |  |  | 
            
  
    | 44 |  |  | 
            
  
    | 45 |  |  | 
            
  
    | 46 |  |  | 
            
  
    | 47 |  |  | 
            
  
    | 48 |  | private File dataDir; | 
            
  
    | 49 |  |  | 
            
  
    | 50 |  |  | 
            
  
    | 51 |  |  | 
            
  
    | 52 |  |  | 
            
  
    | 53 |  | private String dataDirIncludes; | 
            
  
    | 54 |  |  | 
            
  
    | 55 |  |  | 
            
  
    | 56 |  |  | 
            
  
    | 57 |  |  | 
            
  
    | 58 |  | private String dataDirExcludes; | 
            
  
    | 59 |  |  | 
            
  
    | 60 |  |  | 
            
  
    | 61 |  |  | 
            
  
    | 62 |  |  | 
            
  
    | 63 |  | private String schemaXMLFile; | 
            
  
    | 64 |  |  | 
            
  
    | 65 |  |  | 
            
  
    | 66 |  |  | 
            
  
    | 67 |  |  | 
            
  
    | 68 |  | private String targetDatabase; | 
            
  
    | 69 |  |  | 
            
  
    | 70 |  |  | 
            
  
    | 71 |  |  | 
            
  
    | 72 |  |  | 
            
  
    | 73 |  |  | 
            
  
    | 74 |  |  | 
            
  
    | 75 |  | private File markedForRemoval; | 
            
  
    | 76 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 9 (9) | Complexity: 2 | Complexity Density: 0.29 |  | 
  
  
    | 77 | 0 |  protected Set<File> getExportedFiles() throws MojoExecutionException {... | 
            
  
    | 78 | 0 | String filename = getProject().getBuild().getDirectory() + "/impex/exported-tables.txt"; | 
            
  
    | 79 | 0 | File file = new File(filename); | 
            
  
    | 80 | 0 | if (!file.exists()) { | 
            
  
    | 81 | 0 | getLog().warn("Could not locate " + file.getAbsolutePath()); | 
            
  
    | 82 | 0 | return new HashSet<File>(); | 
            
  
    | 83 |  | } | 
            
  
    | 84 | 0 | List<String> tablenames = getContents(file); | 
            
  
    | 85 | 0 | return getExportedFiles(tablenames); | 
            
  
    | 86 |  | } | 
            
  
    | 87 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 6 (6) | Complexity: 1 | Complexity Density: 0.17 |  | 
  
  
    | 88 | 0 |  protected Set<File> getExportedFiles(List<String> tablenames) {... | 
            
  
    | 89 | 0 | Set<File> files = new TreeSet<File>(); | 
            
  
    | 90 | 0 | for (String tablename : tablenames) { | 
            
  
    | 91 | 0 | String filename = dataDir.getAbsolutePath() + FS + tablename + extension; | 
            
  
    | 92 | 0 | File file = new File(filename); | 
            
  
    | 93 | 0 | files.add(file); | 
            
  
    | 94 |  | } | 
            
  
    | 95 | 0 | return files; | 
            
  
    | 96 |  | } | 
            
  
    | 97 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 6 (6) | Complexity: 2 | Complexity Density: 0.33 |  | 
  
  
    | 98 | 0 |  @SuppressWarnings("unchecked")... | 
            
  
    | 99 |  | protected List<String> getContents(File file) throws MojoExecutionException { | 
            
  
    | 100 | 0 | InputStream in = null; | 
            
  
    | 101 | 0 | try { | 
            
  
    | 102 | 0 | in = new FileInputStream(file); | 
            
  
    | 103 | 0 | return IOUtils.readLines(in); | 
            
  
    | 104 |  | } catch (IOException e) { | 
            
  
    | 105 | 0 | throw new MojoExecutionException("Unexpected error", e); | 
            
  
    | 106 |  | } finally { | 
            
  
    | 107 | 0 | IOUtils.closeQuietly(in); | 
            
  
    | 108 |  | } | 
            
  
    | 109 |  | } | 
            
  
    | 110 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 35 (35) | Complexity: 5 | Complexity Density: 0.17 |  | 
  
  
    | 111 | 0 |  @Override... | 
            
  
    | 112 |  | protected void executeMojo() throws MojoExecutionException, MojoFailureException { | 
            
  
    | 113 | 0 | Utils utils = new Utils(); | 
            
  
    | 114 | 0 | try { | 
            
  
    | 115 | 0 | getLog().info("Examining " + dataDir.getAbsolutePath()); | 
            
  
    | 116 | 0 | Database db = utils.getDatabase(schemaXMLFile, targetDatabase); | 
            
  
    | 117 | 0 | DirectoryScanner ds = getDirectoryScanner(); | 
            
  
    | 118 | 0 | Set<File> existing = getExistingFiles(ds); | 
            
  
    | 119 | 0 | Set<File> exported = getExportedFiles(); | 
            
  
    | 120 | 0 | Set<File> allowed = getDatabaseFiles(db); | 
            
  
    | 121 | 0 | if (exported.size() > 0) { | 
            
  
    | 122 | 0 | allowed = exported; | 
            
  
    | 123 |  | } | 
            
  
    | 124 | 0 | Set<File> invalid = SetUtils.difference(existing, allowed); | 
            
  
    | 125 | 0 | getLog().info(existing.size() + " data files currently exist"); | 
            
  
    | 126 | 0 | getLog().info(invalid.size() + " of those are invalid"); | 
            
  
    | 127 | 0 | StringBuilder sb = new StringBuilder(); | 
            
  
    | 128 | 0 | int count = 0; | 
            
  
    | 129 | 0 | StringBuilder invalidFiles = new StringBuilder(); | 
            
  
    | 130 | 0 | for (File file : invalid) { | 
            
  
    | 131 | 0 | if (count != 0) { | 
            
  
    | 132 | 0 | sb.append(","); | 
            
  
    | 133 |  | } | 
            
  
    | 134 | 0 | sb.append("**/src/main/impex/" + file.getName()); | 
            
  
    | 135 | 0 | invalidFiles.append(file.getCanonicalPath() + "\n"); | 
            
  
    | 136 | 0 | getLog().info("Marked for removal: " + file.getName()); | 
            
  
    | 137 | 0 | count++; | 
            
  
    | 138 |  | } | 
            
  
    | 139 | 0 | Properties properties = getProject().getProperties(); | 
            
  
    | 140 | 0 | properties.setProperty("impex.data.invalid", sb.toString()); | 
            
  
    | 141 | 0 | if (count > 0) { | 
            
  
    | 142 | 0 | properties.setProperty("impex.found.invalid", Boolean.TRUE.toString()); | 
            
  
    | 143 | 0 | createFile(markedForRemoval, invalidFiles.toString()); | 
            
  
    | 144 |  | } | 
            
  
    | 145 |  | } catch (Exception e) { | 
            
  
    | 146 | 0 | throw new MojoExecutionException("Error executing mojo", e); | 
            
  
    | 147 |  | } | 
            
  
    | 148 |  | } | 
            
  
    | 149 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 5 (5) | Complexity: 1 | Complexity Density: 0.2 |  | 
  
  
    | 150 | 0 |  protected void createFile(File file, String contents) throws IOException {... | 
            
  
    | 151 | 0 | OutputStream out = null; | 
            
  
    | 152 | 0 | try { | 
            
  
    | 153 | 0 | out = FileUtils.openOutputStream(file); | 
            
  
    | 154 | 0 | IOUtils.write(contents, out); | 
            
  
    | 155 |  | } finally { | 
            
  
    | 156 | 0 | IOUtils.closeQuietly(out); | 
            
  
    | 157 |  | } | 
            
  
    | 158 |  | } | 
            
  
    | 159 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 9 (9) | Complexity: 1 | Complexity Density: 0.11 |  | 
  
  
    | 160 | 0 |  protected Set<File> getDatabaseFiles(Database db) {... | 
            
  
    | 161 | 0 | List<?> tables = db.getTables(); | 
            
  
    | 162 | 0 | Set<File> files = new TreeSet<File>(); | 
            
  
    | 163 | 0 | for (Object object : tables) { | 
            
  
    | 164 | 0 | Table table = (Table) object; | 
            
  
    | 165 | 0 | String tableName = table.getName(); | 
            
  
    | 166 | 0 | String filename = dataDir.getAbsolutePath() + FS + tableName + extension; | 
            
  
    | 167 | 0 | File file = new File(filename); | 
            
  
    | 168 | 0 | files.add(file); | 
            
  
    | 169 |  | } | 
            
  
    | 170 | 0 | return files; | 
            
  
    | 171 |  | } | 
            
  
    | 172 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 10 (10) | Complexity: 2 | Complexity Density: 0.25 |  | 
  
  
    | 173 | 0 |  protected Set<File> getExistingFiles(DirectoryScanner ds) {... | 
            
  
    | 174 | 0 | ds.scan(); | 
            
  
    | 175 | 0 | String[] relativeFilenames = ds.getIncludedFiles(); | 
            
  
    | 176 | 0 | Set<File> files = new TreeSet<File>(); | 
            
  
    | 177 | 0 | for (int i = 0; i < relativeFilenames.length; i++) { | 
            
  
    | 178 | 0 | String filename = ds.getBasedir().getAbsolutePath() + FS + relativeFilenames[i]; | 
            
  
    | 179 | 0 | File file = new File(filename); | 
            
  
    | 180 | 0 | files.add(file); | 
            
  
    | 181 |  | } | 
            
  
    | 182 | 0 | return files; | 
            
  
    | 183 |  | } | 
            
  
    | 184 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 5 (5) | Complexity: 1 | Complexity Density: 0.2 |  | 
  
  
    | 185 | 0 |  protected DirectoryScanner getDirectoryScanner() {... | 
            
  
    | 186 | 0 | DirectoryScanner ds = new DirectoryScanner(); | 
            
  
    | 187 | 0 | ds.setBasedir(dataDir); | 
            
  
    | 188 | 0 | ds.setIncludes(new String[] { dataDirIncludes }); | 
            
  
    | 189 | 0 | ds.setExcludes(new String[] { dataDirExcludes }); | 
            
  
    | 190 | 0 | return ds; | 
            
  
    | 191 |  | } | 
            
  
    | 192 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 4 (4) | Complexity: 1 | Complexity Density: 0.25 |  | 
  
  
    | 193 | 0 |  protected File getFile(Table table) {... | 
            
  
    | 194 | 0 | String tableName = table.getName(); | 
            
  
    | 195 | 0 | String filename = dataDir.getAbsolutePath() + FS + tableName + extension; | 
            
  
    | 196 | 0 | File file = new File(filename); | 
            
  
    | 197 | 0 | return file; | 
            
  
    | 198 |  | } | 
            
  
    | 199 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 200 | 0 |  public File getDataDir() {... | 
            
  
    | 201 | 0 | return dataDir; | 
            
  
    | 202 |  | } | 
            
  
    | 203 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 204 | 0 |  public void setDataDir(File dataDir) {... | 
            
  
    | 205 | 0 | this.dataDir = dataDir; | 
            
  
    | 206 |  | } | 
            
  
    | 207 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 208 | 0 |  public String getDataDirIncludes() {... | 
            
  
    | 209 | 0 | return dataDirIncludes; | 
            
  
    | 210 |  | } | 
            
  
    | 211 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 212 | 0 |  public void setDataDirIncludes(String dataDirIncludes) {... | 
            
  
    | 213 | 0 | this.dataDirIncludes = dataDirIncludes; | 
            
  
    | 214 |  | } | 
            
  
    | 215 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 216 | 0 |  public String getDataDirExcludes() {... | 
            
  
    | 217 | 0 | return dataDirExcludes; | 
            
  
    | 218 |  | } | 
            
  
    | 219 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 220 | 0 |  public void setDataDirExcludes(String dataDirExcludes) {... | 
            
  
    | 221 | 0 | this.dataDirExcludes = dataDirExcludes; | 
            
  
    | 222 |  | } | 
            
  
    | 223 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 224 | 0 |  public String getSchemaXMLFile() {... | 
            
  
    | 225 | 0 | return schemaXMLFile; | 
            
  
    | 226 |  | } | 
            
  
    | 227 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 228 | 0 |  public void setSchemaXMLFile(String schemaXMLFile) {... | 
            
  
    | 229 | 0 | this.schemaXMLFile = schemaXMLFile; | 
            
  
    | 230 |  | } | 
            
  
    | 231 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 232 | 0 |  public String getExtension() {... | 
            
  
    | 233 | 0 | return extension; | 
            
  
    | 234 |  | } | 
            
  
    | 235 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 236 | 0 |  public void setExtension(String extension) {... | 
            
  
    | 237 | 0 | this.extension = extension; | 
            
  
    | 238 |  | } | 
            
  
    | 239 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 240 | 0 |  public String getTargetDatabase() {... | 
            
  
    | 241 | 0 | return targetDatabase; | 
            
  
    | 242 |  | } | 
            
  
    | 243 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 244 | 0 |  public void setTargetDatabase(String targetDatabase) {... | 
            
  
    | 245 | 0 | this.targetDatabase = targetDatabase; | 
            
  
    | 246 |  | } | 
            
  
    | 247 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 248 | 0 |  public File getMarkedForRemoval() {... | 
            
  
    | 249 | 0 | return markedForRemoval; | 
            
  
    | 250 |  | } | 
            
  
    | 251 |  |  | 
               
        |  |  | 
           
           |  | 0% | Uncovered Elements: 1 (1) | Complexity: 1 | Complexity Density: 1 |  | 
  
  
    | 252 | 0 |  public void setMarkedForRemoval(File markedForRemoval) {... | 
            
  
    | 253 | 0 | this.markedForRemoval = markedForRemoval; | 
            
  
    | 254 |  | } | 
            
  
    | 255 |  |  | 
            
  
    | 256 |  | } |