View Javadoc

1   /*
2    * Copyright (c) 2004 International Decision Systems, Inc.  All Rights Reserved.
3    *
4    * By using this Software, You acknowledge that the Software is a valuable asset
5    * and trade secret of either International Decision Systems, Inc. ("IDSI") or a
6    * third party supplier of IDSI and constitutes confidential and proprietary
7    * information.
8    *
9    * NEITHER IDSI NOR ANY AGENT OR PERSON ACTING FOR OR WITH IDSI HAS MADE OR DOES
10   * MAKE ANY STATEMENTS, AFFIRMATIONS, REPRESENTATIONS OR WARRANTIES WHATSOEVER
11   * TO YOU, WHETHER EXPRESS OR IMPLIED, AS TO THE SOFTWARE, THE QUALITY OR
12   * CONDITION OF THE SOFTWARE, OR THE OPERATING CHARACTERISTICS OR RELIABILITY OF
13   * THE SOFTWARE, OR ITS SUITABILITY FOR ANY GENERAL OR PARTICULAR PURPOSE, OR AS
14   * TO ANY OTHER MATTER WHATSOEVER; ANY AND ALL OTHER WARRANTIES INCLUDING
15   * WITHOUT LIMITATION ANY WARRANTIES IMPLIED BY LAW, SUCH AS THE IMPLIED
16   * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND TITLE,
17   * USE AND NON-INFRINGEMENT; ARE HEREBY EXPRESSLY DISCLAIMED AND EXCLUDED.
18  */
19  package net.sourceforge.addam.ddlgen;
20  
21  import org.apache.tools.ant.BuildException;
22  import org.apache.tools.ant.taskdefs.JDBCTask;
23  
24  import java.io.*;
25  import java.sql.Connection;
26  import java.sql.DatabaseMetaData;
27  import java.sql.ResultSet;
28  import java.sql.SQLException;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  /**
33   * User: mkrishna
34   * Date: Nov 10, 2004
35   */
36  
37  public class GenerateTask extends JDBCTask {
38  
39      private String schema = null;
40      private String catalog = null;
41      private final List<GeneratorSpec> generatorSpecs = new ArrayList<GeneratorSpec>();
42  
43      public void setSchema(String schema) {
44          this.schema = schema;
45      }
46  
47      public void setCatalog(String catalog) {
48          this.catalog = catalog;
49      }
50  
51      public void addGenerator(GeneratorSpec spec) {
52          generatorSpecs.add(spec);
53      }
54  
55      public void execute() throws BuildException {
56          Exception exception = null;
57          ResultSet tablesRS = null;
58          Connection connection = null;
59          try {
60              connection = getConnection();
61              DatabaseMetaData dbmd = connection.getMetaData();
62              if (dbmd.storesUpperCaseIdentifiers()) {
63                  catalog = (catalog == null) ? null : catalog.toUpperCase();
64                  schema = (schema == null) ? null : schema.toUpperCase();
65              } else if (dbmd.storesLowerCaseIdentifiers()) {
66                  catalog = (catalog == null) ? null : catalog.toLowerCase();
67                  schema = (schema == null) ? null : schema.toLowerCase();
68              }
69              for (GeneratorSpec spec : generatorSpecs) {
70                  Generator generator = spec.getGenerator();
71                  List<String> objects = generator.getObjects(catalog,schema,spec,connection,dbmd);
72                  for (String dbObject: objects) {
73                      if (spec.includes(dbObject)) {
74                          String vendor = spec.getVendor();
75                          log("generating " + dbObject + " with " + generator.getClass().getName());
76                          BufferedWriter writer = null;
77                          Writer masterScriptWriter = spec.getMasterScriptWriter();
78                          BufferedWriter bufferedMSW = null;
79                          if (masterScriptWriter != null) {
80                              bufferedMSW = new BufferedWriter(masterScriptWriter);
81                          }
82                          try {
83                              writer = new BufferedWriter(spec.getFileWriter(dbObject));
84                              generator.generate(catalog, schema, dbObject, vendor, connection, dbmd, writer);
85                              if (bufferedMSW != null) {
86                                  bufferedMSW.write(spec.getFileName(dbObject));
87                                  bufferedMSW.newLine();
88                                  bufferedMSW.flush();
89                              }
90                          } finally {
91                              if (writer != null) writer.close();
92                          }
93                      }
94                  }
95              }
96          } catch (Exception e) {
97              exception = e;
98          } finally {
99              try {
100                 for (GeneratorSpec spec : generatorSpecs) {
101                     Writer writer = spec.getMasterScriptWriter();
102                     if (writer != null) writer.close();
103                 }
104                 if (tablesRS != null) tablesRS.close();
105                 if (connection != null) connection.close();
106             } catch (SQLException e) {
107                 exception = e;
108             } catch (IOException e) {
109                 exception = e;
110             }
111         }
112         if (exception != null) throw new BuildException(exception);
113     }
114 
115 }