001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.sql.framework.common.utils;
042:
043: import java.util.ArrayList;
044: import java.util.Iterator;
045: import java.util.List;
046: import net.java.hulp.i18n.Logger;
047: import org.netbeans.modules.etl.logger.Localizer;
048: import org.netbeans.modules.etl.logger.LogUtil;
049: import org.netbeans.modules.sql.framework.model.DBConnectionDefinition;
050: import org.netbeans.modules.sql.framework.model.DBTable;
051: import org.netbeans.modules.sql.framework.model.DatabaseModel;
052:
053: /**
054: * Class representing Physical table such that for any two instances t1 and t2 if
055: * (t1.equals(t2)) then both instances represents the same physical table.
056: *
057: * @author Girish Patil
058: * @version $Revision$
059: */
060: public class PhysicalTable {
061:
062: private static boolean SKIP_CONNECTION_URL = false;
063: //Preferably one of the two should be true to assert two tables are equal.
064: private static boolean SKIP_DB_NAME = true;
065: private static transient final Logger mLogger = LogUtil
066: .getLogger(PhysicalTable.class.getName());
067: private static transient final Localizer mLoc = Localizer.get();
068:
069: /**
070: * Creates new instance of PhysicalTable for given DBTable.
071: *
072: * @param sqlTable
073: * @return
074: */
075: public static PhysicalTable getPhysicalTable(DBTable sqlTable) {
076: PhysicalTable pt = null;
077: if (sqlTable != null) {
078: pt = new PhysicalTable();
079: pt.setCatalog(sqlTable.getCatalog());
080: pt.setName(sqlTable.getName());
081: pt.setSchema(sqlTable.getSchema());
082:
083: DatabaseModel dbModel = sqlTable.getParent();
084: if (dbModel != null) {
085: pt.setDbName(dbModel.getModelName());
086: DBConnectionDefinition dbConnDef = dbModel
087: .getConnectionDefinition();
088: if (dbConnDef != null) {
089: pt.setConnectionUrl(dbConnDef.getConnectionURL());
090: }
091: }
092: }
093:
094: return pt;
095: }
096:
097: /**
098: * Returns list of PhysicalTable given given list of DBTable.
099: *
100: * @param sqlTable
101: * @return
102: */
103: public static List<PhysicalTable> getPhysicalTableList(
104: List<DBTable> dbTableList) {
105: List<PhysicalTable> ptList = null;
106: PhysicalTable pt = null;
107: DBTable sqlTable;
108: if (dbTableList != null) {
109: ptList = new ArrayList<PhysicalTable>();
110: for (Iterator<DBTable> itr = dbTableList.iterator(); itr
111: .hasNext();) {
112: sqlTable = itr.next();
113:
114: pt = new PhysicalTable();
115: pt.setCatalog(sqlTable.getCatalog());
116: pt.setName(sqlTable.getName());
117: pt.setSchema(sqlTable.getSchema());
118:
119: DatabaseModel dbModel = sqlTable.getParent();
120: if (dbModel != null) {
121: pt.setDbName(dbModel.getModelName());
122: DBConnectionDefinition dbConnDef = dbModel
123: .getConnectionDefinition();
124: if (dbConnDef != null) {
125: pt.setConnectionUrl(dbConnDef
126: .getConnectionURL());
127: }
128: }
129: ptList.add(pt);
130: }
131: }
132:
133: return ptList;
134: }
135:
136: private String mCatalog;
137: private String mConnectionUrl;
138: private String mName;
139: private String mDbName;
140: private String mSchema;
141:
142: @Override
143: public boolean equals(Object other) {
144: boolean eql = true;
145: PhysicalTable otherTable = null;
146: String blank = "";
147:
148: if (!(other instanceof PhysicalTable)) {
149: eql = false;
150: mLogger.infoNoloc(mLoc.t(
151: "PRSR101: Class cast request for:{0}", other));
152: } else {
153: otherTable = (PhysicalTable) other;
154: }
155:
156: //Check Catalog
157: if (eql) {
158: if ((this .mCatalog == null) || blank.equals(mCatalog)) {
159: if (!((otherTable.getCatalog() == null) || blank
160: .equals(otherTable.getCatalog()))) {
161: eql = false;
162: }
163: } else {
164: if (!this .mCatalog.equals(otherTable.getCatalog())) {
165: eql = false;
166: }
167: }
168: }
169:
170: //Check Schema
171: if (eql) {
172: if ((this .mSchema == null) || blank.equals(mSchema)) {
173: if (!((otherTable.getSchema() == null) || blank
174: .equals(otherTable.getSchema()))) {
175: eql = false;
176: }
177: } else {
178: if (!this .mSchema.equals(otherTable.getSchema())) {
179: eql = false;
180: }
181: }
182: }
183:
184: //Check Name
185: if (eql) {
186: if ((this .mName == null) || blank.equals(mName)) {
187: if (!((otherTable.getName() == null) || blank
188: .equals(otherTable.getName()))) {
189: eql = false;
190: }
191: } else {
192: if (!this .mName.equals(otherTable.getName())) {
193: eql = false;
194: }
195: }
196: }
197:
198: //Check DbName
199: if (eql && (!SKIP_DB_NAME)) {
200: if ((this .mDbName == null) || blank.equals(mDbName)) {
201: if (!((otherTable.getDbName() == null) || blank
202: .equals(otherTable.getDbName()))) {
203: eql = false;
204: }
205: } else {
206: if (!this .mDbName.equals(otherTable.getDbName())) {
207: eql = false;
208: }
209: }
210: }
211:
212: //Check ConnectionUrl
213: if (eql && (!SKIP_CONNECTION_URL)) {
214: if ((this .mConnectionUrl == null)
215: || blank.equals(mConnectionUrl)) {
216: if (!((otherTable.getConnectionUrl() == null) || blank
217: .equals(otherTable.getConnectionUrl()))) {
218: eql = false;
219: }
220: } else {
221: if (!this .mConnectionUrl.equals(otherTable
222: .getConnectionUrl())) {
223: eql = false;
224: }
225: }
226: }
227:
228: return eql;
229: }
230:
231: @Override
232: public int hashCode() {
233: int hash = 7;
234: hash = 53
235: * hash
236: + (this .mCatalog != null ? this .mCatalog.hashCode() : 0);
237: hash = 53
238: * hash
239: + (this .mConnectionUrl != null ? this .mConnectionUrl
240: .hashCode() : 0);
241: hash = 53 * hash
242: + (this .mName != null ? this .mName.hashCode() : 0);
243: hash = 53 * hash
244: + (this .mDbName != null ? this .mDbName.hashCode() : 0);
245: hash = 53 * hash
246: + (this .mSchema != null ? this .mSchema.hashCode() : 0);
247: return hash;
248: }
249:
250: /**
251: * @return Returns the catalog.
252: */
253: public String getCatalog() {
254: return mCatalog;
255: }
256:
257: /**
258: * @return Returns the connectionUrl.
259: */
260: public String getConnectionUrl() {
261: return mConnectionUrl;
262: }
263:
264: /**
265: * @return Returns the name.
266: */
267: public String getName() {
268: return mName;
269: }
270:
271: /**
272: * @return Returns the dbName.
273: */
274: public String getDbName() {
275: return mDbName;
276: }
277:
278: /**
279: * @return Returns the schema.
280: */
281: public String getSchema() {
282: return mSchema;
283: }
284:
285: /**
286: * @param catalog The catalog to set.
287: */
288: public void setCatalog(String catalog) {
289: this .mCatalog = catalog;
290: }
291:
292: /**
293: * @param schema The schema to set.
294: */
295: public void setConnectionUrl(String cUrl) {
296: this .mConnectionUrl = cUrl;
297: }
298:
299: /**
300: * @param name The name to set.
301: */
302: public void setName(String name) {
303: this .mName = name;
304: }
305:
306: /**
307: * @param dbName The dbName to set.
308: */
309: public void setDbName(String dbName) {
310: this .mDbName = dbName;
311: }
312:
313: /**
314: * @param schema The schema to set.
315: */
316: public void setSchema(String schema) {
317: this.mSchema = schema;
318: }
319: }
|