001: /*
002: LoaderGenerator - tool for generated xml, sql and doml file needed for Octopus.
003:
004:
005: Copyright (C) 2003 Together
006:
007: This library is free software; you can redistribute it and/or
008: modify it under the terms of the GNU Lesser General Public
009: License as published by the Free Software Foundation; either
010: version 2.1 of the License, or (at your option) any later version.
011:
012: This library is distributed in the hope that it will be useful,
013: but WITHOUT ANY WARRANTY; without even the implied warranty of
014: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: Lesser General Public License for more details.
016:
017: You should have received a copy of the GNU Lesser General Public
018: License along with this library; if not, write to the Free Software
019: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021:
022: package org.webdocwf.util.loader.generator;
023:
024: import java.sql.Connection;
025: import java.sql.ResultSet;
026: import java.util.ArrayList;
027: import java.util.Arrays;
028: import java.util.Hashtable;
029: import java.util.List;
030:
031: import org.webdocwf.util.loader.LoaderException;
032: import org.webdocwf.util.loader.logging.Logger;
033: import org.webdocwf.util.loader.logging.StandardLogger;
034:
035: /**
036: *
037: * TableRelationshipsReader class read the data which describe database relationships
038: * between the tables.
039: * @author Radoslav Dutina
040: * @version 1.0
041: */
042: public class TableRelationshipsReader {
043:
044: private static List listPrimaryKeys = null;
045: private static List listIndexVariables = null;
046: private static List listForeignVariables = null;
047: private Hashtable htIndexAndReplacement = new Hashtable();
048: private RelationshipsAttributes relationshipsAttributes = new RelationshipsAttributes();
049: private Logger logger;
050:
051: /**
052: * Construct object TableRelationshipsReader with associated parameters.
053: * @param tableName is name of the table form which we retrieve data.
054: * @param conn is the named connection.
055: * @param catalogName is the name of the current database.
056: */
057: public TableRelationshipsReader(Connection conn, String tableName,
058: String catalogName, InputParameters generatorParameters,
059: int constraintCount) throws LoaderException {
060: setLogger();
061: this .logger.write("full",
062: "TableRelationshipsReader is started.");
063: listPrimaryKeys = new ArrayList();
064: listIndexVariables = new ArrayList();
065: listForeignVariables = new ArrayList();
066: String maxConstraintLenght = generatorParameters
067: .getMaxConstraintLength();
068: int maxConstraintInt = 0;
069: if (!maxConstraintLenght.equalsIgnoreCase("")
070: && !maxConstraintLenght.equalsIgnoreCase("-1")) {
071: maxConstraintInt = Integer.parseInt(maxConstraintLenght);
072: }
073: // int constraintCount=0;
074:
075: try {
076: boolean unique = false;
077: boolean app = false;
078: if (generatorParameters.getSourceType() != null
079: && (generatorParameters.getSourceType().equals(
080: "Hsqldb")
081: || generatorParameters.getSourceType()
082: .equals("HypersonicSQL") || generatorParameters
083: .getSourceType().equals("DB2"))) {
084: catalogName = null;
085: }
086: //read meta data
087: ResultSet rs2 = conn.getMetaData().getIndexInfo(
088: catalogName, null, tableName, false, true);
089: ResultSet rs3 = conn.getMetaData().getPrimaryKeys(
090: catalogName, null, tableName);
091: ResultSet rs5 = conn.getMetaData().getImportedKeys(
092: catalogName, null, tableName);
093:
094: // read primary keys
095: while (rs3.next()) {
096: //primaryKeys
097: String primaryKeyName = rs3.getString(6);
098: if (maxConstraintInt > 0
099: && primaryKeyName.length() > maxConstraintInt) {
100: String newName = primaryKeyName.substring(0,
101: maxConstraintInt
102: - String.valueOf(constraintCount)
103: .length());
104: primaryKeyName = newName
105: + String.valueOf(constraintCount);
106: constraintCount++;
107: }
108: listPrimaryKeys.add(primaryKeyName);
109: //columnName
110: listPrimaryKeys.add(rs3.getString(4));
111: }
112: rs3.close();
113:
114: // read indexes
115: while (rs2.next()) {
116: if (rs2.getString(6) != null) {
117: //zoran comment this line,this was restrictive and preventing
118: // creating index on column that is primary key also
119: // if (!rs2.getString(9).equalsIgnoreCase(rs3.getString(4))) {
120: //unique
121: listIndexVariables.add(rs2.getString(4));
122: //index
123: String indexName = rs2.getString(6);
124: //ZK Added next line
125: String keyIndexName = indexName;
126: if (maxConstraintInt > 0
127: && indexName.length() > maxConstraintInt) {
128: String newName = indexName.substring(0,
129: maxConstraintInt
130: - String.valueOf(
131: constraintCount)
132: .length());
133: indexName = newName
134: + String.valueOf(constraintCount);
135: //constraintCount++;
136: //ZK 9.7 2004 Added next block of code.Fixed problem with generation of index on few columns.
137: if (!htIndexAndReplacement
138: .containsKey(keyIndexName)) {
139: htIndexAndReplacement.put(keyIndexName,
140: indexName);
141: constraintCount++;
142: } else {
143: indexName = htIndexAndReplacement.get(
144: keyIndexName).toString();
145: }
146: //end
147: }
148: //index name
149: listIndexVariables.add(indexName);
150: //columnName
151: listIndexVariables.add(rs2.getString(9));
152: // }
153: }
154: }
155: rs2.close();
156:
157: // read imported keys
158: while (rs5.next()) {
159: //ForeignKeyTable
160: listForeignVariables.add(rs5.getString(7));
161: //foreing key restrictions (lenght)
162: String foreignKeyName = rs5.getString(12);
163: if (maxConstraintInt > 0
164: && foreignKeyName.length() > maxConstraintInt) {
165: String newName = foreignKeyName.substring(0,
166: maxConstraintInt
167: - String.valueOf(constraintCount)
168: .length());
169: foreignKeyName = newName
170: + String.valueOf(constraintCount);
171: constraintCount++;
172: }
173: //RelationShipName
174: listForeignVariables.add(foreignKeyName);
175: //ForeignKeyValue
176: listForeignVariables.add(rs5.getString(8));
177: //PrimaryKeyTable
178: listForeignVariables.add(rs5.getString(3));
179: //PrimariKeyValue
180: listForeignVariables.add(rs5.getString(4));
181: }
182: rs5.close();
183:
184: RelationshipsAttributes.setTableName(tableName);
185: RelationshipsAttributes.setPrimaryKeys(getPrimaryKeys());
186: RelationshipsAttributes
187: .setIndexVariables(getIndexVariables());
188: RelationshipsAttributes
189: .setForeignVariables(getForeignVariables());
190:
191: } catch (Exception e) {
192: LoaderException le = new LoaderException("Exception:", e);
193: this .logger.write("full",
194: "Exception in class TableRelationshipsReader."
195: + "\n" + le.getStackTraceAsString());
196: throw le;
197: }
198: this .logger.write("full",
199: "TableRelationshipsReader is finished.");
200: }
201:
202: /**
203: * This method sets the value of listPrimaryKeys parameters.
204: * @param primary_Keys is value of parameter.
205: */
206: public static void setPrimaryKeys(String[] primary_Keys) {
207: listPrimaryKeys = Arrays.asList(primary_Keys);
208: }
209:
210: /**
211: * This method read the value of listPrimaryKeys parameters.
212: * @return value of parameter.
213: */
214: public static String[] getPrimaryKeys() {
215: String[] ret = new String[listPrimaryKeys.size()];
216: listPrimaryKeys.toArray(ret);
217: return ret;
218: }
219:
220: /**
221: * This method sets the value of listIndexVariables parameters.
222: * @param index_Variables is value of parameter.
223: */
224: public static void setIndexVariables(String[] index_Variables) {
225: listIndexVariables = Arrays.asList(index_Variables);
226: }
227:
228: /**
229: * This method read the value of listIndexVariables parameters.
230: * @return value of parameter.
231: */
232: public static String[] getIndexVariables() {
233: String[] ret = new String[listIndexVariables.size()];
234: listIndexVariables.toArray(ret);
235: return ret;
236: }
237:
238: /**
239: * This method sets the value of listForeignVariables parameters.
240: * @param foreign_Variables is the value of parameter.
241: */
242: public static void setForeignVariables(String[] foreign_Variables) {
243: listForeignVariables = Arrays.asList(foreign_Variables);
244: }
245:
246: /**
247: * This method read the value of listForeignVariables parameters.
248: * @return value of parameter.
249: */
250: public static String[] getForeignVariables() {
251: String[] ret = new String[listForeignVariables.size()];
252: listForeignVariables.toArray(ret);
253: return ret;
254: }
255:
256: /**
257: * This method read the value of RelationshipsAttributes parameters.
258: * @return references to RelationshipsAttributes objects.
259: */
260: public RelationshipsAttributes getRelationshipsAttributes() {
261:
262: return relationshipsAttributes;
263: }
264:
265: /**
266: * This method will set logger object
267: * @param logger
268: */
269: private void setLogger() {
270: this.logger = StandardLogger.getCentralLogger();
271: }
272: }
|