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.generic;
20  
21  import net.sourceforge.addam.ddlgen.generic.ForeignKey;
22  
23  import org.apache.velocity.VelocityContext;
24  
25  import java.sql.Connection;
26  import java.sql.DatabaseMetaData;
27  import java.sql.ResultSet;
28  import java.util.Map;
29  import java.util.TreeMap;
30  
31  
32  /**
33   * User: mkrishna
34   * Date: Nov 17, 2004
35   */
36  
37  public class ForeignKeyGenerator extends net.sourceforge.addam.ddlgen.generic.VelocityGenerator {
38  
39      protected VelocityContext getContext(String catalog, String schema, String object,
40                                           Connection connection, DatabaseMetaData metadata)
41              throws Exception {
42          VelocityContext context = new VelocityContext();
43          ResultSet importedKeys = null;
44          try {
45              Map<String,ForeignKey> foreignKeyMap = new TreeMap<String, ForeignKey>();
46              importedKeys = metadata.getImportedKeys(catalog, schema, object);
47              while (importedKeys.next()) {
48                  String foreignKeyName = importedKeys.getString(12);
49                  if (foreignKeyMap.get(foreignKeyName) == null) {
50                      net.sourceforge.addam.ddlgen.generic.ForeignKey foreignKey = new ForeignKey(importedKeys.getString(7),foreignKeyName, importedKeys.getString(3));
51                      foreignKey.addForeignKeyColumn(importedKeys.getString(8));
52                      foreignKey.addPrimaryKeyColumn(importedKeys.getString(4));
53                      foreignKeyMap.put(foreignKeyName, foreignKey);
54                  } else {
55                      // for foreignKeys with multiple columns
56                      ForeignKey addedForeignKey = (ForeignKey) foreignKeyMap.get(foreignKeyName);
57                      addedForeignKey.addForeignKeyColumn(importedKeys.getString(8));
58                      addedForeignKey.addPrimaryKeyColumn(importedKeys.getString(4));
59                  }
60              }
61              context.put("foreignKeys", foreignKeyMap.values());
62          } finally {
63              if (importedKeys != null) importedKeys.close();
64          }
65          return context;
66      }
67  }