Coverage Report - org.kuali.core.db.torque.KualiTorqueDataSQLTask
 
Classes in this File Line Coverage Branch Coverage Complexity
KualiTorqueDataSQLTask
0%
0/68
0%
0/14
2
 
 1  
 package org.kuali.core.db.torque;
 2  
 
 3  
 import java.io.File;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 import java.util.Set;
 7  
 import java.util.TreeSet;
 8  
 
 9  
 import org.apache.tools.ant.BuildException;
 10  
 import org.apache.tools.ant.DirectoryScanner;
 11  
 import org.apache.tools.ant.Project;
 12  
 import org.apache.tools.ant.types.FileSet;
 13  
 import org.apache.torque.engine.database.model.Database;
 14  
 import org.apache.torque.engine.database.model.Table;
 15  
 import org.apache.torque.engine.database.transform.XmlToData;
 16  
 import org.apache.torque.task.TorqueDataModelTask;
 17  
 import org.apache.velocity.VelocityContext;
 18  
 import org.apache.velocity.context.Context;
 19  
 import org.springframework.util.StringUtils;
 20  
 
 21  
 /**
 22  
  * This task converts data XML files into SQL files. It uses schema.xml to analyze which tables have a corresponding
 23  
  * data XML file
 24  
  */
 25  0
 public class KualiTorqueDataSQLTask extends TorqueDataModelTask {
 26  
     PrettyPrint prettyPrint;
 27  0
     Utils utils = new Utils();
 28  
 
 29  
     /**
 30  
      * The DTD itself.
 31  
      */
 32  
     protected File dataDTD;
 33  
 
 34  
     /**
 35  
      * Database object representing the information from schema.xml
 36  
      */
 37  
     protected Database database;
 38  
 
 39  
     /**
 40  
      * Get a list of File objects from the directory and filenames passed in
 41  
      */
 42  
     protected List<File> getFiles(File srcDir, String[] filenames) {
 43  0
         List<File> files = new ArrayList<File>();
 44  0
         for (int i = 0; i < filenames.length; i++) {
 45  0
             files.add(new File(srcDir, filenames[i]));
 46  
         }
 47  0
         return files;
 48  
     }
 49  
 
 50  
     /**
 51  
      * Cycle through the filesets to generate a list of file objects
 52  
      */
 53  
     protected List<File> getFiles(List<FileSet> fileSets, Project project) {
 54  0
         List<File> files = new ArrayList<File>();
 55  0
         for (FileSet fileSet : fileSets) {
 56  0
             DirectoryScanner ds = fileSet.getDirectoryScanner(project);
 57  0
             File srcDir = fileSet.getDir(project);
 58  0
             files.addAll(getFiles(srcDir, ds.getIncludedFiles()));
 59  0
         }
 60  0
         return files;
 61  
     }
 62  
 
 63  
     /**
 64  
      * Extract table names from schema.xml
 65  
      */
 66  
     protected Set<String> getTableNamesFromSchemaXML(Database database) {
 67  0
         List<?> tables = database.getTables();
 68  0
         Set<String> tablenames = new TreeSet<String>();
 69  0
         for (Object object : tables) {
 70  0
             Table table = (Table) object;
 71  0
             tablenames.add(table.getName());
 72  0
         }
 73  0
         return tablenames;
 74  
     }
 75  
 
 76  
     /**
 77  
      * Extract table names from files on the file system
 78  
      */
 79  
     protected Set<String> getTableNamesFromFiles(List<File> files) {
 80  0
         Set<String> tablenames = new TreeSet<String>();
 81  0
         for (File file : files) {
 82  0
             String filename = file.getName();
 83  0
             int pos = filename.indexOf(".xml");
 84  0
             String tablename = filename.substring(0, pos);
 85  0
             tablenames.add(tablename);
 86  0
         }
 87  0
         return tablenames;
 88  
     }
 89  
 
 90  
     /**
 91  
      * Initialize a Velocity context that can generate SQL from XML data files
 92  
      */
 93  
     public Context initControlContext() throws Exception {
 94  0
         if (getFilesets().isEmpty()) {
 95  0
             throw new BuildException("You must specify a fileset of XML data files!");
 96  
         }
 97  
 
 98  
         // Get an xml parser for schema.xml
 99  0
         KualiXmlToAppData xmlParser = new KualiXmlToAppData(getTargetDatabase(), "");
 100  
 
 101  
         // Parse schema.xml into a database object
 102  0
         Database database = xmlParser.parseResource(getXmlFile());
 103  0
         setDatabase(database);
 104  
 
 105  
         // Locate the DTD
 106  0
         if (!getDataDTD().exists()) {
 107  0
             throw new BuildException("Could not find the DTD for " + database.getName());
 108  
         }
 109  
 
 110  
         // These are the XML data files
 111  0
         List<File> files = getFiles(getFilesets(), getProject());
 112  
 
 113  
         // Resolve table information from the data XML files with table information from schema.xml
 114  0
         Set<String> schemaTables = getTableNamesFromSchemaXML(database);
 115  0
         Set<String> fileTables = getTableNamesFromFiles(files);
 116  0
         Set<String> missingFiles = SetUtils.difference(schemaTables, fileTables);
 117  0
         Set<String> intersection = SetUtils.intersection(schemaTables, fileTables);
 118  0
         Set<String> extraFiles = SetUtils.difference(fileTables, schemaTables);
 119  
 
 120  0
         log("Total tables: " + database.getTables().size());
 121  0
         log("Tables with data XML files: " + intersection.size());
 122  0
         log("Tables without data XML files: " + missingFiles.size());
 123  0
         if (extraFiles.size() > 0) {
 124  0
             log("There are files that have no corresponding entry in schema.xml: " + extraFiles.size(),
 125  
                     Project.MSG_WARN);
 126  
         }
 127  
 
 128  
         // Setup the Velocity context
 129  0
         VelocityContext context = new VelocityContext();
 130  0
         context.put("xmlfiles", files);
 131  0
         context.put("task", this);
 132  0
         context.put("targetDatabase", getTargetDatabase());
 133  0
         return context;
 134  
     }
 135  
 
 136  
     public void onBeforeGenerate(File file) {
 137  0
         prettyPrint = new PrettyPrint("[INFO] Generating: " + getTargetDatabase() + "/"
 138  
                 + StringUtils.replace(file.getName(), ".xml", ".sql"));
 139  0
         utils.left(prettyPrint);
 140  0
     }
 141  
 
 142  
     public void onAfterGenerate(File file) {
 143  0
         utils.right(prettyPrint);
 144  0
         prettyPrint = null;
 145  0
     }
 146  
 
 147  
     /**
 148  
      * Parse a data XML file. This method gets invoked from the Velocity template - sql/load/Control.vm
 149  
      */
 150  
     public List<?> getData(File file) {
 151  
         try {
 152  0
             XmlToData dataXmlParser = new XmlToData(getDatabase(), getDataDTD().getAbsolutePath());
 153  0
             List<?> newData = dataXmlParser.parseFile(file.getAbsolutePath());
 154  0
             return newData;
 155  0
         } catch (Exception e) {
 156  0
             throw new BuildException(e);
 157  
         }
 158  
     }
 159  
 
 160  
     public Database getDatabase() {
 161  0
         return database;
 162  
     }
 163  
 
 164  
     public void setDatabase(Database database) {
 165  0
         this.database = database;
 166  0
     }
 167  
 
 168  
     public File getDataDTD() {
 169  0
         return dataDTD;
 170  
     }
 171  
 
 172  
     public void setDataDTD(File dataDTD) {
 173  0
         this.dataDTD = dataDTD;
 174  0
     }
 175  
 
 176  
 }