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