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