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 | |
|
23 | |
|
24 | |
|
25 | 0 | public class KualiTorqueDataSQLTask extends TorqueDataModelTask { |
26 | |
PrettyPrint prettyPrint; |
27 | 0 | Utils utils = new Utils(); |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
protected File dataDTD; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
protected Database database; |
38 | |
|
39 | |
|
40 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
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 | |
|
99 | 0 | KualiXmlToAppData xmlParser = new KualiXmlToAppData(getTargetDatabase(), ""); |
100 | |
|
101 | |
|
102 | 0 | Database database = xmlParser.parseResource(getXmlFile()); |
103 | 0 | setDatabase(database); |
104 | |
|
105 | |
|
106 | 0 | if (!getDataDTD().exists()) { |
107 | 0 | throw new BuildException("Could not find the DTD for " + database.getName()); |
108 | |
} |
109 | |
|
110 | |
|
111 | 0 | List<File> files = getFiles(getFilesets(), getProject()); |
112 | |
|
113 | |
|
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(), Project.MSG_WARN); |
125 | |
} |
126 | |
|
127 | |
|
128 | 0 | VelocityContext context = new VelocityContext(); |
129 | 0 | context.put("xmlfiles", files); |
130 | 0 | context.put("task", this); |
131 | 0 | context.put("targetDatabase", getTargetDatabase()); |
132 | 0 | return context; |
133 | |
} |
134 | |
|
135 | |
public void onBeforeGenerate(File file) { |
136 | 0 | prettyPrint = new PrettyPrint("[INFO] Generating: " + getTargetDatabase() + "/" + StringUtils.replace(file.getName(), ".xml", ".sql")); |
137 | 0 | utils.left(prettyPrint); |
138 | 0 | } |
139 | |
|
140 | |
public void onAfterGenerate(File file) { |
141 | 0 | utils.right(prettyPrint); |
142 | 0 | prettyPrint = null; |
143 | 0 | } |
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
public List<?> getData(File file) { |
149 | |
try { |
150 | 0 | XmlToData dataXmlParser = new XmlToData(getDatabase(), getDataDTD().getAbsolutePath()); |
151 | 0 | List<?> newData = dataXmlParser.parseFile(file.getAbsolutePath()); |
152 | 0 | return newData; |
153 | 0 | } catch (Exception e) { |
154 | 0 | throw new BuildException(e); |
155 | |
} |
156 | |
} |
157 | |
|
158 | |
public Database getDatabase() { |
159 | 0 | return database; |
160 | |
} |
161 | |
|
162 | |
public void setDatabase(Database database) { |
163 | 0 | this.database = database; |
164 | 0 | } |
165 | |
|
166 | |
public File getDataDTD() { |
167 | 0 | return dataDTD; |
168 | |
} |
169 | |
|
170 | |
public void setDataDTD(File dataDTD) { |
171 | 0 | this.dataDTD = dataDTD; |
172 | 0 | } |
173 | |
|
174 | |
} |