001    package org.apache.torque.mojo;
002    
003    import java.util.Properties;
004    
005    import org.apache.cxf.common.util.StringUtils;
006    import org.apache.maven.plugin.MojoExecutionException;
007    import org.apache.maven.plugin.descriptor.PluginDescriptor;
008    import org.apache.torque.util.JdbcConfigurer;
009    import org.kuali.core.db.torque.DumpTask;
010    import org.kuali.core.db.torque.PropertyHandlingException;
011    import org.kuali.core.db.torque.StringFilter;
012    
013    /**
014     * Base Mojo for export related functionality
015     */
016    public abstract class ExportMojo extends AntTaskMojo {
017    
018        /**
019         * Additional properties for the JDBC driver
020         *
021         * @parameter
022         */
023        private Properties driverProperties;
024    
025        /**
026         * If true, files will be exported in a format that the Ant impex tasks can understand
027         *
028         * @parameter expression="${antCompatibilityMode}" default-value="false"
029         */
030        private boolean antCompatibilityMode;
031    
032        /**
033         * The Maven artifactId. Included here as a simple property of the mojo itself to facilitate usage of
034         * <code>BeanUtils.copyProperties()</code> to copy properties between Mojo's and Ant tasks
035         *
036         * @parameter expression="${artifactId}" default-value="${project.artifactId}"
037         * @required
038         */
039        private String artifactId;
040    
041        /**
042         * By default the version of the maven plugin being used during the build will be included as part of a comment in
043         * the XML. Set this to false to prevent this. Useful if you are committing files to SCM and don't want a change in
044         * the version of the plugin to show up as a change to the data.
045         *
046         * @parameter expression="${includeVersionInComment}" default-value="true"
047         * @required
048         */
049        private boolean includeVersionInComment;
050    
051        /**
052         * Additional comment that gets placed into the generated XML document. Anything included here gets appended to the
053         * default value <br>
054         * <b>Default value is:</b> Auto-generated by the maven-impex-plugin v1.0.2<br>
055         *
056         * @parameter expression="${comment}"
057         */
058        private String comment;
059    
060        /**
061         * Comma separated list of regular expressions for tables to include in the export
062         *
063         * @parameter expression="${includes}"
064         */
065        private String includes;
066    
067        /**
068         * Comma separated list of regular expressions for tables to exclude from the export
069         *
070         * @parameter expression="${excludes}"
071         */
072        private String excludes;
073    
074        /**
075         * The type of database we are targeting (eg oracle, mysql). This is optional if <code>url</code> is supplied as the
076         * database type will be automatically detected based on the <code>url</code>. If targetDatabase is explicitly
077         * supplied it will override the type selected by the automatic detection logic.
078         *
079         * @parameter expression="${targetDatabase}"
080         */
081        private String targetDatabase;
082    
083        /**
084         * The schema to export. This parameter is optional, as the schema to export is automatically derived from platform
085         * specific logic that converts the artifactId.<br>
086         * <br>
087         * For example:<br>
088         * ks-embedded-db becomes KSEMBEDDED for Oracle, and ksembedded for MySQL If <code>schema</code> is supplied, the
089         * supplied value will be used instead of the value derived from the artifactId
090         *
091         * @parameter expression="${schema}"
092         */
093        private String schema;
094    
095        /**
096         * Database driver classname. This parameter is optional, as the correct JDBC driver to use is detected from the
097         * <code>url</code> in almost all cases (works for Oracle, MySQL, Derby, PostGresSQL, DB2, H2, HSQL, SQL Server). If
098         * a driver is explicitly supplied, it will be used in place of the JDBC driver the automatic detection logic would
099         * have chosen.
100         *
101         * @parameter expression="${driver}"
102         */
103        private String driver;
104    
105        /**
106         * The connect URL of the database.
107         *
108         * @parameter expression="${url}"
109         * @required
110         */
111        private String url;
112    
113        /**
114         * The user name to connect to the database. If not specified this is automatically generated by platform specific
115         * logic for converting the artifact id.<br>
116         * <br>
117         * For example:<br>
118         * ks-embedded-db becomes KSEMBEDDED for Oracle and ksembedded for MySQL
119         *
120         * @parameter expression="${username}"
121         */
122        private String username;
123    
124        /**
125         * The password for the database user. If not specified this is automatically generated by platform specific logic
126         * for converting the artifact id.<br>
127         * <br>
128         * For example:<br>
129         * ks-embedded-db becomes KSEMBEDDED for Oracle and ksembedded for MySQL
130         *
131         * @parameter expression="${password}"
132         */
133        private String password;
134    
135        /**
136         * Returns the fully qualified class name of the database driver.
137         */
138        public String getDriver() {
139            return driver;
140        }
141    
142        /**
143         * Sets the fully qualified class name of the database driver.
144         *
145         * @param driver
146         *            the fully qualified class name of the database driver.
147         */
148        public void setDriver(final String driver) {
149            this.driver = driver;
150        }
151    
152        /**
153         * Returns the password of the database user.
154         *
155         * @return the password of the database user.
156         */
157        public String getPassword() {
158            return password;
159        }
160    
161        /**
162         * Sets the password of the database user.
163         *
164         * @param password
165         *            the password of the database user.
166         */
167        public void setPassword(final String password) {
168            this.password = password;
169        }
170    
171        /**
172         * Returns the connect URL to the database.
173         *
174         * @return the connect URL to the database.
175         */
176        public String getUrl() {
177            return url;
178        }
179    
180        /**
181         * Sets the connect URL to the database.
182         *
183         * @param url
184         *            the connect URL to the database.
185         */
186        public void setUrl(final String url) {
187            this.url = url;
188        }
189    
190        public String getUsername() {
191            return username;
192        }
193    
194        public void setUsername(final String username) {
195            this.username = username;
196        }
197    
198        public String getSchema() {
199            return schema;
200        }
201    
202        public void setSchema(final String schema) {
203            this.schema = schema;
204        }
205    
206        public String getTargetDatabase() {
207            return targetDatabase;
208        }
209    
210        public void setTargetDatabase(final String targetDatabase) {
211            this.targetDatabase = targetDatabase;
212        }
213    
214        public String getIncludes() {
215            return includes;
216        }
217    
218        public void setIncludes(final String includePatterns) {
219            this.includes = includePatterns;
220        }
221    
222        public String getExcludes() {
223            return excludes;
224        }
225    
226        public void setExcludes(final String excludePatterns) {
227            this.excludes = excludePatterns;
228        }
229    
230        public String getComment() {
231            return comment;
232        }
233    
234        public void setComment(final String comment) {
235            this.comment = comment;
236        }
237    
238        protected String getUpdatedComment() {
239            PluginDescriptor descriptor = (PluginDescriptor) this.getPluginContext().get("pluginDescriptor");
240            if (descriptor == null) {
241                // This is null for Maven 2.x
242                return " Auto-generated by the maven-impex-plugin " + getComment();
243            }
244            String name = descriptor.getName();
245            String version = descriptor.getVersion();
246            String comment = " Auto-generated by the " + name;
247            if (isIncludeVersionInComment()) {
248                comment += " v" + version + " ";
249            }
250            if (!StringUtils.isEmpty(getComment())) {
251                comment += getComment();
252            }
253            return comment;
254        }
255    
256        @Override
257        protected void configureTask() throws MojoExecutionException {
258            setComment(getUpdatedComment());
259            try {
260                JdbcConfigurer configurer = new JdbcConfigurer();
261                configurer.updateConfiguration(this);
262                configurer.validateConfiguration(this);
263            } catch (PropertyHandlingException e) {
264                throw new MojoExecutionException("Error handling properties", e);
265            }
266            super.configureTask();
267            DumpTask task = (DumpTask) super.getAntTask();
268            task.setIncludePatterns(StringFilter.getListFromCSV(getIncludes()));
269            task.setExcludePatterns(StringFilter.getListFromCSV(getExcludes()));
270            task.setDriverProperties(driverProperties);
271        }
272    
273        public String getArtifactId() {
274            return artifactId;
275        }
276    
277        public void setArtifactId(final String artifactId) {
278            this.artifactId = artifactId;
279        }
280    
281        public boolean isAntCompatibilityMode() {
282            return antCompatibilityMode;
283        }
284    
285        public void setAntCompatibilityMode(final boolean antCompatibilityMode) {
286            this.antCompatibilityMode = antCompatibilityMode;
287        }
288    
289        /**
290         * @return the includeVersionInComment
291         */
292        public boolean isIncludeVersionInComment() {
293            return includeVersionInComment;
294        }
295    
296        /**
297         * @param includeVersionInComment
298         *            the includeVersionInComment to set
299         */
300        public void setIncludeVersionInComment(final boolean includeVersionInComment) {
301            this.includeVersionInComment = includeVersionInComment;
302        }
303    
304        public Properties getDriverProperties() {
305            return driverProperties;
306        }
307    
308        public void setDriverProperties(Properties driverProperties) {
309            this.driverProperties = driverProperties;
310        }
311    }