001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.commons.dsi.dml;
020:
021: import java.util.*;
022: import java.util.logging.*;
023:
024: import org.openharmonise.commons.dsi.*;
025:
026: /**
027: * A collection of DML join conditions between column references.
028: *
029: * Note: join conditions will be inner joins by default.
030: *
031: * @author Michael Bell
032: * @version $Revision: 1.1 $
033: *
034: */
035: public class JoinConditions {
036:
037: /**
038: * List of column references for left side of column comparison.
039: */
040: private List m_LeftCols = null;
041:
042: /**
043: * List of column references for right side of column comparison.
044: */
045: private List m_RightCols = null;
046:
047: /**
048: * List of indexes for outer joins.
049: */
050: private List m_outerjoins = null;
051:
052: /**
053: * Logger for this class.
054: */
055: private static final Logger m_logger = Logger
056: .getLogger(JoinConditions.class.getName());
057:
058: /**
059: * Constructs collection for join conditions.
060: */
061: public JoinConditions() {
062: //initiaulise new vectors
063: m_LeftCols = new Vector(16);
064: m_RightCols = new Vector(16);
065: m_outerjoins = new Vector();
066: }
067:
068: /**
069: * Adds join condition.
070: *
071: * @param Col1 column reference for left side of equation
072: * @param Col2 column reference for right side of equation
073: * @throws DataStoreException if the condition is invalid
074: */
075: public void addCondition(ColumnRef Col1, ColumnRef Col2)
076: throws DataStoreException {
077:
078: //add data
079: m_LeftCols.add(Col1);
080: m_RightCols.add(Col2);
081: }
082:
083: /**
084: * Adds an outer join condition.
085: *
086: * @param Col1 column reference for left side of equation
087: * @param Col2 column reference for right side of equation
088: * @throws DataStoreException if the condition is invalid
089: */
090: public void addOuterJoin(ColumnRef Col1, ColumnRef Col2)
091: throws DataStoreException {
092: addCondition(Col1, Col2);
093: m_outerjoins.add(new Integer(m_LeftCols.size() - 1));
094: }
095:
096: /**
097: * Returns <code>true</code> if the join condition at the given
098: * index is an outer join.
099: *
100: * @param index the index of join condition
101: * @return <code>true</code> if the join condition at the given
102: */
103: public boolean isOuterJoin(int index) {
104: return (m_outerjoins.contains(new Integer(index)));
105: }
106:
107: /**
108: * Returns the left side column reference of the join condition at
109: * the given index.
110: *
111: * @param index the index of join condition
112: * @return the left side column reference of the join condition
113: */
114: public ColumnRef getLeftColumnRef(int index) {
115: return (ColumnRef) m_LeftCols.get(index);
116: }
117:
118: /**
119: * Returns the right side column reference of the join condition at
120: * the given index.
121: *
122: * @param index the index of join condition
123: * @return the right side column reference of the join condition
124: */
125: public ColumnRef getRightColumnRef(int index) {
126: return (ColumnRef) m_RightCols.get(index);
127: }
128:
129: /**
130: * Returns the table name of the left side column reference of
131: * the join condition at the given index.
132: *
133: * @param index the index of join condition
134: * @return the table name of the left side column reference
135: */
136: public String getLeftTableName(int index) {
137: return (((ColumnRef) m_LeftCols.get(index)).getTable());
138: }
139:
140: /**
141: * Returns the column name of the left side column reference of
142: * the join condition at the given index.
143: *
144: * @param index the index of join condition
145: * @return the column name of the left side column reference
146: */
147: public String getLeftColumnName(int index) {
148: return (((ColumnRef) m_LeftCols.get(index)).getColumn());
149: }
150:
151: /**
152: * Returns the full column reference for the left side column
153: * reference of the join condition at the given index.
154: *
155: * @param index the index of join condition
156: * @return
157: */
158: public String getLeftFullColumnRef(int index) {
159: return (((ColumnRef) m_LeftCols.get(index)).getFullRef());
160: }
161:
162: /**
163: * Returns the column name of the right side column reference of
164: * the join condition at the given index.
165: *
166: * @param index the index of join condition
167: * @return the column name of the right side column reference
168: */
169: public String getRightColumnName(int index) {
170: return (((ColumnRef) m_RightCols.get(index)).getColumn());
171: }
172:
173: /**
174: * Returns the table name of the right side column reference of
175: * the join condition at the given index.
176: *
177: * @param index the index of join condition
178: * @return the table name of the right side column reference
179: */
180: public String getRightTableName(int index) {
181: return (((ColumnRef) m_RightCols.get(index)).getTable());
182: }
183:
184: /**
185: * Returns the full column reference for the right side column
186: * reference of the join condition at the given index.
187: *
188: * @param index the index of join condition
189: * @return
190: */
191: public String getRightFullColumnRef(int index) {
192: return (((ColumnRef) m_RightCols.get(index)).getFullRef());
193: }
194:
195: /**
196: * Returns the list of tables referenced in the join conditions.
197: *
198: * @return the list of tables referenced in the join conditions
199: */
200: public List getTableList() {
201: Vector vec1 = new Vector(16);
202: Vector vec2 = new Vector(16);
203: Vector vecr = null;
204:
205: for (int i = 0; i < m_RightCols.size(); i++) {
206: vec1.add(getLeftTableName(i));
207: vec2.add(getRightTableName(i));
208: }
209:
210: vecr = new Vector(16);
211:
212: for (int i = 0; i < vec1.size(); i++) {
213: if (!vecr.contains(vec1.elementAt(i))) {
214: vecr.add(vec1.elementAt(i));
215: }
216: }
217:
218: for (int i = 0; i < vec2.size(); i++) {
219: if (!vecr.contains(vec2.elementAt(i))) {
220: vecr.add(vec2.elementAt(i));
221: }
222: }
223:
224: return vecr;
225: }
226:
227: /**
228: * Returns the number of conditions.
229: *
230: * @return
231: */
232: public int size() {
233: return (m_RightCols.size());
234: }
235:
236: /**
237: * Empties this collection of join conditions.
238: *
239: */
240: public void empty() {
241: if (m_LeftCols != null) {
242: m_LeftCols.clear();
243: m_RightCols.clear();
244: m_outerjoins.clear();
245: }
246: }
247:
248: /**
249: * Merge given join condition with current join condition.
250: *
251: * @param join the collection of join conditions to be added to this
252: * collection
253: */
254: public void merge(JoinConditions join) throws DataStoreException {
255: for (int i = 0; i < join.size(); i++) {
256: if (join.isOuterJoin(i)) {
257: addOuterJoin(join.getLeftColumnRef(i), join
258: .getRightColumnRef(i));
259: } else {
260: addCondition(join.getLeftColumnRef(i), join
261: .getRightColumnRef(i));
262: }
263: }
264: }
265:
266: /**
267: * Returns a collection of all the inner joins contained in this collection.
268: *
269: * @return the collection of inner joins
270: */
271: public JoinConditions getInnerJoins() {
272: JoinConditions innerJoins = new JoinConditions();
273:
274: for (int i = 0; i < size(); i++) {
275: if (isOuterJoin(i) == false) {
276: try {
277: innerJoins.addCondition(getLeftColumnRef(i),
278: getRightColumnRef(i));
279: } catch (DataStoreException e) {
280: m_logger.log(Level.WARNING,
281: e.getLocalizedMessage(), e);
282: }
283: }
284: }
285:
286: return innerJoins;
287: }
288:
289: /**
290: * Returns a collection of all the outer joins contained in this collection
291: * @return the collection of outer joins.
292: */
293: public JoinConditions getOuterJoins() {
294: JoinConditions outerJoins = new JoinConditions();
295:
296: for (int i = 0; i < m_outerjoins.size(); i++) {
297: int index = ((Integer) m_outerjoins.get(i)).intValue();
298:
299: if (isOuterJoin(index) == true) {
300: try {
301: outerJoins.addCondition(getLeftColumnRef(index),
302: getRightColumnRef(index));
303: } catch (DataStoreException e) {
304: m_logger.log(Level.WARNING,
305: e.getLocalizedMessage(), e);
306: }
307: }
308: }
309:
310: return outerJoins;
311: }
312: }
|