KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > commons > dsi > dml > JoinConditions


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.commons.dsi.dml;
20
21 import java.util.*;
22 import java.util.logging.*;
23
24 import org.openharmonise.commons.dsi.*;
25
26
27 /**
28  * A collection of DML join conditions between column references.
29  *
30  * Note: join conditions will be inner joins by default.
31  *
32  * @author Michael Bell
33  * @version $Revision: 1.1 $
34  *
35  */

36 public class JoinConditions {
37     
38     /**
39      * List of column references for left side of column comparison.
40      */

41     private List m_LeftCols = null;
42     
43     /**
44      * List of column references for right side of column comparison.
45      */

46     private List m_RightCols = null;
47     
48     /**
49      * List of indexes for outer joins.
50      */

51     private List m_outerjoins = null;
52     
53     /**
54      * Logger for this class.
55      */

56     private static final Logger m_logger = Logger.getLogger(JoinConditions.class.getName());
57
58     /**
59      * Constructs collection for join conditions.
60      */

61     public JoinConditions() {
62         //initiaulise new vectors
63
m_LeftCols = new Vector(16);
64         m_RightCols = new Vector(16);
65         m_outerjoins = new Vector();
66     }
67
68     /**
69      * Adds join condition.
70      *
71      * @param Col1 column reference for left side of equation
72      * @param Col2 column reference for right side of equation
73      * @throws DataStoreException if the condition is invalid
74      */

75     public void addCondition(ColumnRef Col1, ColumnRef Col2)
76                       throws DataStoreException {
77
78         //add data
79
m_LeftCols.add(Col1);
80         m_RightCols.add(Col2);
81     }
82
83     /**
84      * Adds an outer join condition.
85      *
86      * @param Col1 column reference for left side of equation
87      * @param Col2 column reference for right side of equation
88      * @throws DataStoreException if the condition is invalid
89      */

90     public void addOuterJoin(ColumnRef Col1, ColumnRef Col2)
91                       throws DataStoreException {
92         addCondition(Col1, Col2);
93         m_outerjoins.add(new Integer JavaDoc(m_LeftCols.size() - 1));
94     }
95
96     /**
97      * Returns <code>true</code> if the join condition at the given
98      * index is an outer join.
99      *
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 JavaDoc(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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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),
258                              join.getRightColumnRef(i));
259             } else {
260                 addCondition(join.getLeftColumnRef(i),
261                              join.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), getRightColumnRef(i));
278             } catch (DataStoreException e) {
279                 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
280             }
281         }
282       }
283       
284       return innerJoins;
285     }
286     
287   /**
288    * Returns a collection of all the outer joins contained in this collection
289    * @return the collection of outer joins.
290    */

291   public JoinConditions getOuterJoins() {
292     JoinConditions outerJoins = new JoinConditions();
293       
294     for (int i = 0; i < m_outerjoins.size(); i++) {
295       int index = ((Integer JavaDoc)m_outerjoins.get(i)).intValue();
296       
297       if (isOuterJoin(index) == true) {
298         try {
299           outerJoins.addCondition(getLeftColumnRef(index), getRightColumnRef(index));
300         } catch (DataStoreException e) {
301             m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
302         }
303       }
304     }
305       
306     return outerJoins;
307   }
308 }
Popular Tags