Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
DataModelTaskMojo |
|
| 1.3333333333333333;1.333 |
1 | package org.apache.torque.mojo; | |
2 | ||
3 | import java.io.File; | |
4 | import java.io.IOException; | |
5 | import java.util.List; | |
6 | ||
7 | import org.apache.commons.lang.StringUtils; | |
8 | import org.apache.maven.plugin.MojoExecutionException; | |
9 | import org.apache.tools.ant.types.FileSet; | |
10 | import org.apache.torque.task.TorqueDataModelTask; | |
11 | import org.apache.torque.util.JdbcConfigurer; | |
12 | import org.apache.torque.util.SimpleScanner; | |
13 | import org.kuali.core.db.torque.PropertyHandlingException; | |
14 | import org.kuali.db.DatabaseType; | |
15 | ||
16 | /** | |
17 | * The base class for mojos that wrap DataModelTasks | |
18 | */ | |
19 | 0 | public abstract class DataModelTaskMojo extends TexenTaskMojo { |
20 | ||
21 | /** | |
22 | * The Velocity context property for the target database | |
23 | */ | |
24 | public static final String TARGET_DATABASE_CONTEXT_PROPERTY = "targetDatabase"; | |
25 | ||
26 | /** | |
27 | * Database URL. | |
28 | * | |
29 | * @parameter expression="${url}" | |
30 | */ | |
31 | String url; | |
32 | ||
33 | /** | |
34 | * The suffix of the generated sql files. | |
35 | */ | |
36 | 0 | String suffix = ""; |
37 | ||
38 | protected File getCanonicalReportFile() throws MojoExecutionException { | |
39 | try { | |
40 | 0 | String filename = getOutputDir() + FS + getReportFile(); |
41 | 0 | File file = new File(filename); |
42 | 0 | return file.getCanonicalFile(); |
43 | 0 | } catch (IOException e) { |
44 | 0 | throw new MojoExecutionException("Error with report file", e); |
45 | } | |
46 | } | |
47 | ||
48 | protected FileSet getAntFileSet(File baseDir, String includes, String excludes) { | |
49 | 0 | FileSet fileSet = new FileSet(); |
50 | 0 | fileSet.setDir(baseDir); |
51 | 0 | fileSet.setIncludes(includes); |
52 | 0 | fileSet.setExcludes(excludes); |
53 | 0 | return fileSet; |
54 | } | |
55 | ||
56 | /** | |
57 | * Validate that some essential configuration items are present | |
58 | */ | |
59 | protected void updateConfiguration() throws MojoExecutionException { | |
60 | try { | |
61 | // loadPropertiesToMojo(); | |
62 | 0 | new JdbcConfigurer().updateConfiguration(this); |
63 | 0 | } catch (PropertyHandlingException e) { |
64 | 0 | throw new MojoExecutionException("Error handling properties", e); |
65 | 0 | } |
66 | 0 | } |
67 | ||
68 | protected String getInvalidTargetDatabaseMessage() { | |
69 | 0 | StringBuffer sb = new StringBuffer(); |
70 | 0 | sb.append("\n\n"); |
71 | 0 | sb.append("Target database of '" + getTargetDatabase() + "' is invalid.\n\n"); |
72 | 0 | sb.append("Valid values are " + org.springframework.util.StringUtils.arrayToCommaDelimitedString(DatabaseType.values()) + ".\n\n"); |
73 | 0 | sb.append("Specify targetDatabase in the plugin configuration or as a system property.\n\n"); |
74 | 0 | sb.append("For example:\n-DtargetDatabase=oracle\n\n."); |
75 | 0 | return sb.toString(); |
76 | } | |
77 | ||
78 | /** | |
79 | * Validate that some essential configuration items are present | |
80 | */ | |
81 | protected void validateConfiguration() throws MojoExecutionException { | |
82 | 0 | if (StringUtils.isEmpty(getTargetDatabase())) { |
83 | 0 | throw new MojoExecutionException(getInvalidTargetDatabaseMessage()); |
84 | } | |
85 | ||
86 | try { | |
87 | 0 | DatabaseType.valueOf(getTargetDatabase().toUpperCase()); |
88 | 0 | } catch (IllegalArgumentException e) { |
89 | 0 | throw new MojoExecutionException(getInvalidTargetDatabaseMessage()); |
90 | 0 | } |
91 | 0 | } |
92 | ||
93 | /** | |
94 | * The path to the directory where the schema XML files are located | |
95 | * | |
96 | * @parameter expression="${schemaDir}" default-value="${basedir}/src/main/impex" | |
97 | * @required | |
98 | */ | |
99 | private String schemaDir; | |
100 | ||
101 | /** | |
102 | * The schema files from that directory which should be included (ant-style notation). | |
103 | * | |
104 | * @parameter expression="${schemaIncludes}" default-value="${project.artifactId}.xml" | |
105 | * @required | |
106 | */ | |
107 | private String schemaIncludes; | |
108 | ||
109 | /** | |
110 | * The schema files from that directory which should be excluded (ant-style notation). | |
111 | */ | |
112 | private String schemaExcludes; | |
113 | ||
114 | /** | |
115 | * The type of database we are targeting (eg oracle, mysql). This is optional if <code>url</code> is supplied as the | |
116 | * database type will be automatically detected based on the <code>url</code>. If targetDatabase is explicitly | |
117 | * supplied it will override the type selected by the automatic detection logic. | |
118 | * | |
119 | * @parameter expression="${targetDatabase}" | |
120 | */ | |
121 | private String targetDatabase; | |
122 | ||
123 | /** | |
124 | * The target package for the generated classes. | |
125 | * | |
126 | * @parameter expression="${targetPackage}" default-value="impex.generated" | |
127 | */ | |
128 | private String targetPackage; | |
129 | ||
130 | /** | |
131 | * The file containing the generation report, relative to <code>outputDir</code>. | |
132 | * | |
133 | * @required | |
134 | */ | |
135 | private String reportFile; | |
136 | ||
137 | /** | |
138 | * Determines if this task should run only if the schema has changed | |
139 | * | |
140 | * @parameter expression="${runOnlyOnSchemaChange}" default-value="true" | |
141 | */ | |
142 | private boolean runOnlyOnSchemaChange; | |
143 | ||
144 | /** | |
145 | * The path to the properties file containing the mapping sql file -> target database. | |
146 | * | |
147 | * @parameter expression="${sqlDbMap}" default-value="${project.build.directory}/reports/sqldbmap.properties" | |
148 | */ | |
149 | private String sqlDbMap; | |
150 | ||
151 | /** | |
152 | * Returns the path to the control template. | |
153 | * | |
154 | * @return the path to the control template. | |
155 | */ | |
156 | protected abstract String getControlTemplate(); | |
157 | ||
158 | protected void addTargetDatabaseToOutputDir() { | |
159 | 0 | TorqueDataModelTask task = (TorqueDataModelTask) super.getGeneratorTask(); |
160 | 0 | String newOutputDir = getOutputDir() + FS + getTargetDatabase(); |
161 | 0 | task.setOutputDirectory(new File(newOutputDir)); |
162 | 0 | setOutputDir(newOutputDir); |
163 | 0 | } |
164 | ||
165 | protected void addTargetDatabaseToReportFile() { | |
166 | 0 | TorqueDataModelTask task = (TorqueDataModelTask) super.getGeneratorTask(); |
167 | 0 | String newReportFile = getReportFile() + "." + getTargetDatabase(); |
168 | 0 | task.setOutputFile(newReportFile); |
169 | 0 | setReportFile(newReportFile); |
170 | 0 | } |
171 | ||
172 | /** | |
173 | * Configures the Texen task wrapped by this mojo | |
174 | */ | |
175 | protected void configureTask() throws MojoExecutionException { | |
176 | 0 | super.configureTask(); |
177 | ||
178 | 0 | TorqueDataModelTask task = (TorqueDataModelTask) super.getGeneratorTask(); |
179 | 0 | task.setControlTemplate(getControlTemplate()); |
180 | 0 | task.setOutputFile(getReportFile()); |
181 | 0 | task.setTargetDatabase(getTargetDatabase()); |
182 | 0 | task.setTargetPackage(getTargetPackage()); |
183 | 0 | if (getSqlDbMap() != null) { |
184 | 0 | task.setSqlDbMap(getSqlDbMap()); |
185 | } | |
186 | 0 | } |
187 | ||
188 | protected List<File> getSchemaFiles() { | |
189 | 0 | return new SimpleScanner(new File(getSchemaDir()), getSchemaIncludes(), getSchemaExcludes()).getFiles(); |
190 | } | |
191 | ||
192 | /** | |
193 | * Returns the directory where the schema files are located. | |
194 | * | |
195 | * @return the the directory where the schema files are located. | |
196 | */ | |
197 | public String getSchemaDir() { | |
198 | 0 | return schemaDir; |
199 | } | |
200 | ||
201 | /** | |
202 | * Sets the the directory where the schema files are located. | |
203 | * | |
204 | * @param schemaDir | |
205 | * the directory where the schema files are located. | |
206 | */ | |
207 | public void setSchemaDir(String schemaDir) { | |
208 | 0 | this.schemaDir = schemaDir; |
209 | 0 | } |
210 | ||
211 | /** | |
212 | * Returns the target database (e.g. mysql, oracle, ... ) for the generated files. | |
213 | * | |
214 | * @return the target database for the generated files. | |
215 | */ | |
216 | public String getTargetDatabase() { | |
217 | 0 | return targetDatabase; |
218 | } | |
219 | ||
220 | /** | |
221 | * Sets the target database (e.g. mysql, oracle, ... ) for the generated files. | |
222 | * | |
223 | * @param targetDatabase | |
224 | * the target database for the generated files. | |
225 | */ | |
226 | public void setTargetDatabase(String targetDatabase) { | |
227 | 0 | this.targetDatabase = targetDatabase; |
228 | 0 | } |
229 | ||
230 | /** | |
231 | * Returns the target package for the generated classes. | |
232 | * | |
233 | * @return the target package for the generated classes. | |
234 | */ | |
235 | public String getTargetPackage() { | |
236 | 0 | return targetPackage; |
237 | } | |
238 | ||
239 | /** | |
240 | * Sets the target package for the generated classes. | |
241 | * | |
242 | * param targetPackage the target package for the generated classes. | |
243 | */ | |
244 | public void setTargetPackage(String targetPackage) { | |
245 | 0 | this.targetPackage = targetPackage; |
246 | 0 | } |
247 | ||
248 | /** | |
249 | * Gets the path to the report file. The path is relative to <code>outputDir</code>. | |
250 | * | |
251 | * @return the path to the report file. | |
252 | */ | |
253 | public String getReportFile() { | |
254 | 0 | return reportFile; |
255 | } | |
256 | ||
257 | /** | |
258 | * Sets the path to the report file. The path is relative to <code>outputDir</code>. | |
259 | * | |
260 | * @param reportFile | |
261 | * the path to the report file. | |
262 | */ | |
263 | public void setReportFile(String reportFile) { | |
264 | 0 | this.reportFile = reportFile; |
265 | 0 | } |
266 | ||
267 | /** | |
268 | * Returns whether this mojo should be executed only if the schema has changed. | |
269 | * | |
270 | * @return true if the mojo only runs if the schema has changed, false otherwise. | |
271 | */ | |
272 | public boolean isRunOnlyOnSchemaChange() { | |
273 | 0 | return runOnlyOnSchemaChange; |
274 | } | |
275 | ||
276 | /** | |
277 | * Sets whether this mojo should be executed only if the schema has changed. | |
278 | * | |
279 | * @param runOnlyOnSchemaChange | |
280 | * whether the mojo only should run if the schema has changed. | |
281 | */ | |
282 | public void setRunOnlyOnSchemaChange(boolean runOnlyOnSchemaChange) { | |
283 | 0 | this.runOnlyOnSchemaChange = runOnlyOnSchemaChange; |
284 | 0 | } |
285 | ||
286 | /** | |
287 | * Returns the schema files which are excluded from generation. | |
288 | * | |
289 | * @return the pattern for the excluded files. | |
290 | */ | |
291 | public String getSchemaExcludes() { | |
292 | 0 | return schemaExcludes; |
293 | } | |
294 | ||
295 | /** | |
296 | * Sets the schema files which are excluded from generation. | |
297 | * | |
298 | * @param schemaExcludes | |
299 | * the pattern for the excluded files. | |
300 | */ | |
301 | public void setSchemaExcludes(String schemaExcludes) { | |
302 | 0 | this.schemaExcludes = schemaExcludes; |
303 | 0 | } |
304 | ||
305 | /** | |
306 | * Returns the schema files which are included in generation. | |
307 | * | |
308 | * @return the pattern for the included files. | |
309 | */ | |
310 | public String getSchemaIncludes() { | |
311 | 0 | return schemaIncludes; |
312 | } | |
313 | ||
314 | /** | |
315 | * Sets the schema files which are included in generation. | |
316 | * | |
317 | * @param schemaIncludes | |
318 | * the pattern for the included files. | |
319 | */ | |
320 | public void setSchemaIncludes(String schemaIncludes) { | |
321 | 0 | this.schemaIncludes = schemaIncludes; |
322 | 0 | } |
323 | ||
324 | /** | |
325 | * Returns the path to the mapping SQL Files -> database. | |
326 | * | |
327 | * @return the path to the mapping SQL Files -> database. | |
328 | */ | |
329 | public String getSqlDbMap() { | |
330 | 0 | return sqlDbMap; |
331 | } | |
332 | ||
333 | /** | |
334 | * Sets the path to the mapping SQL Files -> database. | |
335 | * | |
336 | * @param sqlDbMap | |
337 | * the absolute path to the mapping SQL Files -> database. | |
338 | */ | |
339 | public void setSqlDbMap(String sqlDbMap) { | |
340 | 0 | this.sqlDbMap = sqlDbMap; |
341 | 0 | } |
342 | ||
343 | public String getUrl() { | |
344 | 0 | return url; |
345 | } | |
346 | ||
347 | public void setUrl(String url) { | |
348 | 0 | this.url = url; |
349 | 0 | } |
350 | ||
351 | public String getSuffix() { | |
352 | 0 | return suffix; |
353 | } | |
354 | ||
355 | public void setSuffix(String suffix) { | |
356 | 0 | this.suffix = suffix; |
357 | 0 | } |
358 | ||
359 | } |