1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.torque.mojo;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.List;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.apache.maven.plugin.MojoExecutionException;
24 import org.apache.tools.ant.types.FileSet;
25 import org.apache.torque.task.TorqueDataModelTask;
26 import org.apache.torque.util.JdbcConfigurer;
27 import org.apache.torque.util.SimpleScanner;
28 import org.kuali.core.db.torque.PropertyHandlingException;
29 import org.kuali.db.DatabaseType;
30
31
32
33
34 public abstract class DataModelTaskMojo extends TexenTaskMojo {
35
36
37
38
39 public static final String TARGET_DATABASE_CONTEXT_PROPERTY = "targetDatabase";
40
41
42
43
44
45
46 String url;
47
48
49
50
51 String suffix = "";
52
53 protected File getCanonicalReportFile() throws MojoExecutionException {
54 try {
55 String filename = getOutputDir() + FS + getReportFile();
56 File file = new File(filename);
57 return file.getCanonicalFile();
58 } catch (IOException e) {
59 throw new MojoExecutionException("Error with report file", e);
60 }
61 }
62
63 protected FileSet getAntFileSet(File baseDir, String includes, String excludes) {
64 FileSet fileSet = new FileSet();
65 fileSet.setDir(baseDir);
66 fileSet.setIncludes(includes);
67 fileSet.setExcludes(excludes);
68 return fileSet;
69 }
70
71
72
73
74 protected void updateConfiguration() throws MojoExecutionException {
75 try {
76
77 new JdbcConfigurer().updateConfiguration(this);
78 } catch (PropertyHandlingException e) {
79 throw new MojoExecutionException("Error handling properties", e);
80 }
81 }
82
83 protected String getInvalidTargetDatabaseMessage() {
84 StringBuffer sb = new StringBuffer();
85 sb.append("\n\n");
86 sb.append("Target database of '" + getTargetDatabase() + "' is invalid.\n\n");
87 sb.append("Valid values are " + org.springframework.util.StringUtils.arrayToCommaDelimitedString(DatabaseType.values()) + ".\n\n");
88 sb.append("Specify targetDatabase in the plugin configuration or as a system property.\n\n");
89 sb.append("For example:\n-DtargetDatabase=oracle\n\n.");
90 return sb.toString();
91 }
92
93
94
95
96 protected void validateConfiguration() throws MojoExecutionException {
97 if (StringUtils.isEmpty(getTargetDatabase())) {
98 throw new MojoExecutionException(getInvalidTargetDatabaseMessage());
99 }
100
101 try {
102 DatabaseType.valueOf(getTargetDatabase().toUpperCase());
103 } catch (IllegalArgumentException e) {
104 throw new MojoExecutionException(getInvalidTargetDatabaseMessage());
105 }
106 }
107
108
109
110
111
112
113
114 private String schemaDir;
115
116
117
118
119
120
121
122 private String schemaIncludes;
123
124
125
126
127 private String schemaExcludes;
128
129
130
131
132
133
134
135
136 private String targetDatabase;
137
138
139
140
141
142
143 private String targetPackage;
144
145
146
147
148
149
150 private String reportFile;
151
152
153
154
155
156
157 private boolean runOnlyOnSchemaChange;
158
159
160
161
162
163
164 private String sqlDbMap;
165
166
167
168
169
170
171 protected abstract String getControlTemplate();
172
173 protected void addTargetDatabaseToOutputDir() {
174 TorqueDataModelTask task = (TorqueDataModelTask) super.getGeneratorTask();
175 String newOutputDir = getOutputDir() + FS + getTargetDatabase();
176 task.setOutputDirectory(new File(newOutputDir));
177 setOutputDir(newOutputDir);
178 }
179
180 protected void addTargetDatabaseToReportFile() {
181 TorqueDataModelTask task = (TorqueDataModelTask) super.getGeneratorTask();
182 String newReportFile = getReportFile() + "." + getTargetDatabase();
183 task.setOutputFile(newReportFile);
184 setReportFile(newReportFile);
185 }
186
187
188
189
190 protected void configureTask() throws MojoExecutionException {
191 super.configureTask();
192
193 TorqueDataModelTask task = (TorqueDataModelTask) super.getGeneratorTask();
194 task.setControlTemplate(getControlTemplate());
195 task.setOutputFile(getReportFile());
196 task.setTargetDatabase(getTargetDatabase());
197 task.setTargetPackage(getTargetPackage());
198 if (getSqlDbMap() != null) {
199 task.setSqlDbMap(getSqlDbMap());
200 }
201 }
202
203 protected List<File> getSchemaFiles() {
204 return new SimpleScanner(new File(getSchemaDir()), getSchemaIncludes(), getSchemaExcludes()).getFiles();
205 }
206
207
208
209
210
211
212 public String getSchemaDir() {
213 return schemaDir;
214 }
215
216
217
218
219
220
221
222 public void setSchemaDir(String schemaDir) {
223 this.schemaDir = schemaDir;
224 }
225
226
227
228
229
230
231 public String getTargetDatabase() {
232 return targetDatabase;
233 }
234
235
236
237
238
239
240
241 public void setTargetDatabase(String targetDatabase) {
242 this.targetDatabase = targetDatabase;
243 }
244
245
246
247
248
249
250 public String getTargetPackage() {
251 return targetPackage;
252 }
253
254
255
256
257
258
259 public void setTargetPackage(String targetPackage) {
260 this.targetPackage = targetPackage;
261 }
262
263
264
265
266
267
268 public String getReportFile() {
269 return reportFile;
270 }
271
272
273
274
275
276
277
278 public void setReportFile(String reportFile) {
279 this.reportFile = reportFile;
280 }
281
282
283
284
285
286
287 public boolean isRunOnlyOnSchemaChange() {
288 return runOnlyOnSchemaChange;
289 }
290
291
292
293
294
295
296
297 public void setRunOnlyOnSchemaChange(boolean runOnlyOnSchemaChange) {
298 this.runOnlyOnSchemaChange = runOnlyOnSchemaChange;
299 }
300
301
302
303
304
305
306 public String getSchemaExcludes() {
307 return schemaExcludes;
308 }
309
310
311
312
313
314
315
316 public void setSchemaExcludes(String schemaExcludes) {
317 this.schemaExcludes = schemaExcludes;
318 }
319
320
321
322
323
324
325 public String getSchemaIncludes() {
326 return schemaIncludes;
327 }
328
329
330
331
332
333
334
335 public void setSchemaIncludes(String schemaIncludes) {
336 this.schemaIncludes = schemaIncludes;
337 }
338
339
340
341
342
343
344 public String getSqlDbMap() {
345 return sqlDbMap;
346 }
347
348
349
350
351
352
353
354 public void setSqlDbMap(String sqlDbMap) {
355 this.sqlDbMap = sqlDbMap;
356 }
357
358 public String getUrl() {
359 return url;
360 }
361
362 public void setUrl(String url) {
363 this.url = url;
364 }
365
366 public String getSuffix() {
367 return suffix;
368 }
369
370 public void setSuffix(String suffix) {
371 this.suffix = suffix;
372 }
373
374 }