1 package org.apache.torque.task;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.util.ArrayList;
24 import java.util.Date;
25 import java.util.Hashtable;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.DirectoryScanner;
33 import org.apache.tools.ant.types.FileSet;
34 import org.apache.torque.engine.EngineException;
35 import org.apache.torque.engine.database.model.Database;
36 import org.apache.velocity.VelocityContext;
37 import org.apache.velocity.context.Context;
38 import org.apache.texen.ant.TexenTask;
39 import org.kuali.core.db.torque.DatabaseParser;
40 import org.kuali.core.db.torque.KualiXmlToAppData;
41
42
43
44
45
46
47
48
49
50 public class TorqueDataModelTask extends TexenTask {
51
52
53
54 protected String xmlFile;
55
56
57 protected List<FileSet> filesets = new ArrayList<FileSet>();
58
59
60 protected List<Database> dataModels = new ArrayList<Database>();
61
62
63 protected Context context;
64
65
66
67
68
69 protected Hashtable<String, String> dataModelDbMap;
70
71
72
73
74 protected Hashtable<String, String> databaseNames;
75
76
77
78
79
80
81
82
83
84 protected String sqldbmap;
85
86
87 private String targetDatabase;
88
89
90 private String targetPackage;
91
92
93
94
95
96
97
98 public void setSqlDbMap(String sqldbmap) {
99
100 this.sqldbmap = getProject().resolveFile(sqldbmap).toString();
101 }
102
103
104
105
106
107
108 public String getSqlDbMap() {
109 return sqldbmap;
110 }
111
112
113
114
115
116
117 public List<Database> getDataModels() {
118 return dataModels;
119 }
120
121
122
123
124
125
126 public Hashtable<String, String> getDataModelDbMap() {
127 return dataModelDbMap;
128 }
129
130
131
132
133
134
135 public String getXmlFile() {
136 return xmlFile;
137 }
138
139
140
141
142
143
144
145 public void setXmlFile(String xmlFile) {
146 this.xmlFile = xmlFile;
147 }
148
149
150
151
152
153
154
155 public void addFileset(FileSet set) {
156 filesets.add(set);
157 }
158
159
160
161
162
163
164 public String getTargetDatabase() {
165 return targetDatabase;
166 }
167
168
169
170
171 public void setTargetDatabase(String targetDatabase) {
172 this.targetDatabase = targetDatabase;
173 }
174
175
176
177
178
179
180 public String getTargetPackage() {
181 return targetPackage;
182 }
183
184
185
186
187 public void setTargetPackage(String targetPackage) {
188 this.targetPackage = targetPackage;
189 }
190
191
192
193
194 protected DatabaseParser getDatabaseParser() {
195 return new KualiXmlToAppData(getTargetDatabase(), getTargetPackage());
196 }
197
198
199
200
201 protected Database getDataModel(File file) throws EngineException {
202
203 DatabaseParser databaseParser = getDatabaseParser();
204
205
206 Database database = databaseParser.parseResource(file.toString());
207
208
209 database.setFileName(grokName(file.toString()));
210
211
212 return database;
213 }
214
215
216
217
218 protected List<File> getDataModelFiles() {
219
220 List<File> dataModelFiles = new ArrayList<File>();
221
222
223 for (int i = 0; i < getFilesets().size(); i++) {
224
225 FileSet fs = getFilesets().get(i);
226
227
228 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
229
230
231 File srcDir = fs.getDir(getProject());
232
233
234 String[] dataModelFilesArray = ds.getIncludedFiles();
235
236
237 for (int j = 0; j < dataModelFilesArray.length; j++) {
238 File file = new File(srcDir, dataModelFilesArray[j]);
239 dataModelFiles.add(file);
240 }
241 }
242
243
244 return dataModelFiles;
245 }
246
247
248
249
250 protected List<Database> getPopulatedDataModels() throws EngineException {
251
252 List<Database> databases = new ArrayList<Database>();
253
254
255 if (getXmlFile() != null) {
256
257 Database database = getDataModel(new File(getXmlFile()));
258
259 databases.add(database);
260
261 return databases;
262 }
263
264
265 List<File> dataModelFiles = getDataModelFiles();
266
267 for (File dataModelFile : dataModelFiles) {
268
269 Database database = getDataModel(dataModelFile);
270
271 databases.add(database);
272 }
273
274 return databases;
275 }
276
277
278
279
280
281
282
283 public Context initControlContext() throws Exception {
284 if (xmlFile == null && filesets.isEmpty()) {
285 throw new BuildException("You must specify an XML schema or fileset of XML schemas!");
286 }
287
288 try {
289 dataModels = getPopulatedDataModels();
290 } catch (EngineException ee) {
291 throw new BuildException(ee);
292 }
293
294 Iterator<Database> i = dataModels.iterator();
295 databaseNames = new Hashtable<String, String>();
296 dataModelDbMap = new Hashtable<String, String>();
297
298
299
300 while (i.hasNext()) {
301 Database database = i.next();
302 databaseNames.put(database.getName(), database.getName());
303 dataModelDbMap.put(database.getFileName(), database.getName());
304 }
305
306 context = new VelocityContext();
307
308
309
310 context.put("dataModels", dataModels);
311 context.put("databaseNames", databaseNames);
312 context.put("targetDatabase", targetDatabase);
313 context.put("targetPackage", targetPackage);
314
315 return context;
316 }
317
318
319
320
321
322
323 protected void populateInitialContext(Context context) throws Exception {
324 super.populateInitialContext(context);
325 context.put("now", new Date());
326 }
327
328
329
330
331
332
333
334
335 private String grokName(String xmlFile) {
336
337
338
339
340 String name = "data-model";
341 int i = xmlFile.lastIndexOf(System.getProperty("file.separator"));
342 if (i != -1) {
343
344 i++;
345
346 int j = xmlFile.lastIndexOf('.');
347 if (i < j) {
348 name = xmlFile.substring(i, j);
349 } else {
350
351 name = xmlFile.substring(i);
352 }
353 }
354 return name;
355 }
356
357
358
359
360
361
362
363
364
365
366
367
368 public void setContextProperties(String file) {
369 super.setContextProperties(file);
370
371
372 Hashtable<?, ?> env = super.getProject().getProperties();
373 for (Iterator<?> i = env.entrySet().iterator(); i.hasNext();) {
374 Map.Entry<?, ?> entry = (Map.Entry<?, ?>) i.next();
375 String key = (String) entry.getKey();
376 if (key.startsWith("torque.")) {
377 String newKey = key.substring("torque.".length());
378 int j = newKey.indexOf(".");
379 while (j != -1) {
380 newKey = newKey.substring(0, j) + StringUtils.capitalize(newKey.substring(j + 1));
381 j = newKey.indexOf(".");
382 }
383
384 contextProperties.setProperty(newKey, entry.getValue());
385 }
386 }
387 }
388
389 public List<FileSet> getFilesets() {
390 return filesets;
391 }
392
393 public void setFilesets(List<FileSet> filesets) {
394 this.filesets = filesets;
395 }
396 }