KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > generator > TableRelationshipsReader


1
2 /*
3 LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
4
5
6     Copyright (C) 2003 Together
7
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation; either
11     version 2.1 of the License, or (at your option) any later version.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16     Lesser General Public License for more details.
17
18     You should have received a copy of the GNU Lesser General Public
19     License along with this library; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */

22
23 package org.webdocwf.util.loader.generator;
24
25 import java.sql.Connection JavaDoc;
26 import java.sql.ResultSet JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Hashtable JavaDoc;
30 import java.util.List JavaDoc;
31
32 import org.webdocwf.util.loader.LoaderException;
33 import org.webdocwf.util.loader.logging.Logger;
34 import org.webdocwf.util.loader.logging.StandardLogger;
35
36 /**
37  *
38  * TableRelationshipsReader class read the data which describe database relationships
39  * between the tables.
40  * @author Radoslav Dutina
41  * @version 1.0
42  */

43 public class TableRelationshipsReader {
44
45     private static List JavaDoc listPrimaryKeys = null;
46     private static List JavaDoc listIndexVariables = null;
47     private static List JavaDoc listForeignVariables = null;
48     private Hashtable JavaDoc htIndexAndReplacement = new Hashtable JavaDoc();
49     private RelationshipsAttributes relationshipsAttributes = new RelationshipsAttributes();
50     private Logger logger;
51     /**
52      * Construct object TableRelationshipsReader with associated parameters.
53      * @param tableName is name of the table form which we retrieve data.
54      * @param conn is the named connection.
55      * @param catalogName is the name of the current database.
56      */

57     public TableRelationshipsReader(Connection JavaDoc conn, String JavaDoc tableName, String JavaDoc catalogName, InputParameters generatorParameters, int constraintCount) throws LoaderException{
58         setLogger();
59         this.logger.write("full", "TableRelationshipsReader is started.");
60         listPrimaryKeys = new ArrayList JavaDoc();
61         listIndexVariables = new ArrayList JavaDoc();
62         listForeignVariables = new ArrayList JavaDoc();
63         String JavaDoc maxConstraintLenght = generatorParameters.getMaxConstraintLength();
64         int maxConstraintInt = 0;
65         if (!maxConstraintLenght.equalsIgnoreCase("") && !maxConstraintLenght.equalsIgnoreCase("-1")) {
66             maxConstraintInt = Integer.parseInt(maxConstraintLenght);
67         }
68         // int constraintCount=0;
69

70         try {
71             boolean unique = false;
72             boolean app = false;
73             if(generatorParameters.getSourceType() != null &&
74                     (generatorParameters.getSourceType().equals("Hsqldb") ||
75                             generatorParameters.getSourceType().equals("HypersonicSQL") ||
76                             generatorParameters.getSourceType().equals("DB2") )) {
77                 catalogName = null;
78             }
79             //read meta data
80
ResultSet JavaDoc rs2 = conn.getMetaData().getIndexInfo(catalogName, null, tableName, false, true);
81             ResultSet JavaDoc rs3 = conn.getMetaData().getPrimaryKeys(catalogName, null, tableName);
82             ResultSet JavaDoc rs5 = conn.getMetaData().getImportedKeys(catalogName, null, tableName);
83             
84 // read primary keys
85
while (rs3.next()) {
86                 //primaryKeys
87
String JavaDoc primaryKeyName = rs3.getString(6);
88                 if (maxConstraintInt > 0 && primaryKeyName.length() > maxConstraintInt) {
89                     String JavaDoc newName = primaryKeyName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length());
90                     primaryKeyName = newName + String.valueOf(constraintCount);
91                     constraintCount++;
92                 }
93                 listPrimaryKeys.add(primaryKeyName);
94                 //columnName
95
listPrimaryKeys.add(rs3.getString(4));
96             }
97             rs3.close();
98             
99 // read indexes
100
while (rs2.next()) {
101                 if (rs2.getString(6) != null) {
102                     //zoran comment this line,this was restrictive and preventing
103
// creating index on column that is primary key also
104
// if (!rs2.getString(9).equalsIgnoreCase(rs3.getString(4))) {
105
//unique
106
listIndexVariables.add(rs2.getString(4));
107                     //index
108
String JavaDoc indexName = rs2.getString(6);
109                     //ZK Added next line
110
String JavaDoc keyIndexName = indexName;
111                     if (maxConstraintInt > 0 && indexName.length() > maxConstraintInt) {
112                         String JavaDoc newName = indexName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length());
113                         indexName = newName + String.valueOf(constraintCount);
114                         //constraintCount++;
115
//ZK 9.7 2004 Added next block of code.Fixed problem with generation of index on few columns.
116
if (!htIndexAndReplacement.containsKey(keyIndexName)) {
117                             htIndexAndReplacement.put(keyIndexName, indexName);
118                             constraintCount++;
119                         } else {
120                             indexName = htIndexAndReplacement.get(keyIndexName).toString();
121                         }
122                         //end
123
}
124                     //index name
125
listIndexVariables.add(indexName);
126                     //columnName
127
listIndexVariables.add(rs2.getString(9));
128                     // }
129
}
130             }
131             rs2.close();
132             
133 // read imported keys
134
while (rs5.next()) {
135                 //ForeignKeyTable
136
listForeignVariables.add(rs5.getString(7));
137                 //foreing key restrictions (lenght)
138
String JavaDoc foreignKeyName = rs5.getString(12);
139                 if (maxConstraintInt > 0 && foreignKeyName.length() > maxConstraintInt) {
140                     String JavaDoc newName = foreignKeyName.substring(0, maxConstraintInt - String.valueOf(constraintCount).length());
141                     foreignKeyName = newName + String.valueOf(constraintCount);
142                     constraintCount++;
143                 }
144                 //RelationShipName
145
listForeignVariables.add(foreignKeyName);
146                 //ForeignKeyValue
147
listForeignVariables.add(rs5.getString(8));
148                 //PrimaryKeyTable
149
listForeignVariables.add(rs5.getString(3));
150                 //PrimariKeyValue
151
listForeignVariables.add(rs5.getString(4));
152             }
153             rs5.close();
154
155             RelationshipsAttributes.setTableName(tableName);
156             RelationshipsAttributes.setPrimaryKeys(getPrimaryKeys());
157             RelationshipsAttributes.setIndexVariables(getIndexVariables());
158             RelationshipsAttributes.setForeignVariables(getForeignVariables());
159
160         } catch (Exception JavaDoc e) {
161                         LoaderException le = new LoaderException("Exception:",e);
162                         this.logger.write("full", "Exception in class TableRelationshipsReader."+"\n"+le.getStackTraceAsString());
163             throw le;
164         }
165         this.logger.write("full", "TableRelationshipsReader is finished.");
166     }
167
168     /**
169      * This method sets the value of listPrimaryKeys parameters.
170      * @param primary_Keys is value of parameter.
171      */

172     public static void setPrimaryKeys(String JavaDoc[] primary_Keys) {
173         listPrimaryKeys = Arrays.asList(primary_Keys);
174     }
175
176     /**
177      * This method read the value of listPrimaryKeys parameters.
178      * @return value of parameter.
179      */

180     public static String JavaDoc[] getPrimaryKeys() {
181         String JavaDoc[] ret = new String JavaDoc[listPrimaryKeys.size()];
182         listPrimaryKeys.toArray(ret);
183         return ret;
184     }
185
186     /**
187      * This method sets the value of listIndexVariables parameters.
188      * @param index_Variables is value of parameter.
189      */

190     public static void setIndexVariables(String JavaDoc[] index_Variables) {
191         listIndexVariables = Arrays.asList(index_Variables);
192     }
193
194     /**
195      * This method read the value of listIndexVariables parameters.
196      * @return value of parameter.
197      */

198     public static String JavaDoc[] getIndexVariables() {
199         String JavaDoc[] ret = new String JavaDoc[listIndexVariables.size()];
200         listIndexVariables.toArray(ret);
201         return ret;
202     }
203
204     /**
205      * This method sets the value of listForeignVariables parameters.
206      * @param foreign_Variables is the value of parameter.
207      */

208     public static void setForeignVariables(String JavaDoc[] foreign_Variables) {
209         listForeignVariables = Arrays.asList(foreign_Variables);
210     }
211
212     /**
213      * This method read the value of listForeignVariables parameters.
214      * @return value of parameter.
215      */

216     public static String JavaDoc[] getForeignVariables() {
217         String JavaDoc[] ret = new String JavaDoc[listForeignVariables.size()];
218         listForeignVariables.toArray(ret);
219         return ret;
220     }
221
222     /**
223      * This method read the value of RelationshipsAttributes parameters.
224      * @return references to RelationshipsAttributes objects.
225      */

226     public RelationshipsAttributes getRelationshipsAttributes() {
227
228         return relationshipsAttributes;
229     }
230     /**
231           * This method will set logger object
232           * @param logger
233             */

234     private void setLogger() {
235         this.logger = StandardLogger.getCentralLogger();
236     }
237 }
238
Popular Tags