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