KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.util.*;
23
24 import org.openharmonise.commons.dsi.*;
25
26 /**
27  *
28  * A collection of where conditions that can be applied to a DML statement.
29  * In this context a condition can either be a <code>WhereCondition</code> or
30  * a <code>WhereConditions</code> object. This object represents a set of conditions
31  * which can be strung together with a stringing operator such as 'and' or 'or', for
32  * example; '[condition 1] and [condition 2] and [condition 3]' has three conditions
33  * strung together with the 'and' operator but '[[condition 1] or [condition 2]] and [condition 3]'
34  * has two top level conditions strung with 'and' with one nested conditional
35  * group containing two conditions string together with 'or'.
36  *
37  * @author Michael Bell
38  * @version $Revision: 1.1 $
39  *
40  */

41 public class WhereConditionGroup {
42     
43     /**
44      * List of where conditions
45      */

46     private List m_wheres = null;
47     
48     /**
49      * The stringing operator used to string conditions together
50      */

51     private String JavaDoc m_sOperator = "and";
52
53     /**
54      * Constructs collection for where conditions
55      */

56     public WhereConditionGroup() {
57         m_wheres = new Vector(16);
58     }
59
60     /**
61      * Sets the stringing operator for this collection of where conditions.
62      *
63      * @param sOp the stringing operator
64      * @throws DataStoreException if the stringing operator is invalid
65      */

66     public void setStringingOperator(String JavaDoc sOp) throws DataStoreException {
67         if (sOp.equalsIgnoreCase("and") || sOp.equalsIgnoreCase("or")) {
68             m_sOperator = sOp;
69         } else {
70             throw new DataStoreException("Invalid operator");
71         }
72     }
73
74     /**
75      * Returns the stringing operator for this collection of where conditions.
76      *
77      * @return the stringing operator
78      */

79     public String JavaDoc getStringingOperator() {
80         return m_sOperator;
81     }
82
83     /**
84      * Adds the collection of where condition to this collection.
85      *
86      * @param where the collection of where conditions to add
87      */

88     public void addCondition(WhereConditionGroup where) {
89         m_wheres.add(where);
90     }
91
92     /**
93      * Adds a where condition to this collection.
94      *
95      * @param where the where condition to add
96      */

97     public void addCondition(WhereCondition where) {
98         m_wheres.add(where);
99     }
100
101     /**
102      * Returns the condition, either a <code>WhereCondition</code> or a
103      * <code>WhereConditions</code>, at the specified position in this collection.
104      *
105      * @param index index of condition to return
106      * @return the condition at the specified position
107      */

108     public Object JavaDoc getCondition(int index) {
109         return (m_wheres.get(index));
110     }
111
112     /**
113      * Returns <code>true</code> if the condition object at the specified position
114      * is a <code>WhereCondition</code> rather than a <code>WhereConditions</code> object.
115      *
116      * @param index index of condition
117      * @return <code>true</code> if the condition object at the specified
118      * position
119      */

120     public boolean isWhereConditionsLeaf(int index) {
121         return (m_wheres.get(index) instanceof WhereCondition);
122     }
123
124     /**
125      * Adds a where condition from the given parameters using the default
126      * comparison operator.
127      *
128      * @param Col1 the conditional column reference
129      * @param i the conditional value
130      * @throws DataStoreException if the condition is invalid
131      */

132     public void addCondition(ColumnRef Col1, int i) throws DataStoreException {
133         //add data
134
m_wheres.add(new WhereCondition(Col1, i));
135     }
136
137     /**
138      * Adds a where condition from the given parameters using the specified
139      * comparison operator.
140      *
141      * @param Col1 the conditional column reference
142      * @param sOperator the comparison operator
143      * @param i the conditional value
144      * @throws DataStoreException if the condition is invalid
145      */

146     public void addCondition(ColumnRef Col1, String JavaDoc sOperator, int i)
147                       throws DataStoreException {
148         //add data
149
m_wheres.add(new WhereCondition(Col1, sOperator, i));
150     }
151
152     /**
153      * Adds a where condition from the given parameters using the default
154      * comparison operator.
155      *
156      * @param Col1 the conditional column reference
157      * @param obj the conditional value
158      * @throws DataStoreException if the condition is invalid
159      */

160     public void addCondition(ColumnRef Col1, Object JavaDoc obj)
161                       throws DataStoreException {
162         //add data
163
m_wheres.add(new WhereCondition(Col1, obj));
164     }
165
166     /**
167      * Adds a where condition from the given parameters using the specified
168      * comparison operator.
169      *
170      * @param Col1 the conditional column reference
171      * @param sOperator the comparison operator
172      * @param obj the conditional value
173      * @throws DataStoreException if the condition is invalid
174      */

175     public void addCondition(ColumnRef Col1, String JavaDoc sOperator, Object JavaDoc obj)
176                       throws DataStoreException {
177         //add data
178
m_wheres.add(new WhereCondition(Col1, sOperator, obj));
179     }
180
181     /**
182      * Adds a where condition from the given parameters using the default
183      * comparison operator.
184      *
185      * @param Col1 the conditional column reference
186      * @param vals the list of conditional values
187      * @throws DataStoreException if the condition is invalid
188      */

189     public void addCondition(ColumnRef Col1, List vals)
190                       throws DataStoreException {
191         //add data
192
m_wheres.add(new WhereCondition(Col1, vals));
193     }
194
195     /**
196      * Adds a where condition from the given parameters using the specified
197      * comparison operator.
198      *
199      * @param Col1 the conditional column reference
200      * @param sOperator the comparison operator
201      * @param vals the list of conditional values
202      * @throws DataStoreException if the condition is invalid
203      */

204     public void addCondition(ColumnRef Col1, String JavaDoc sOperator, List vals)
205                       throws DataStoreException {
206         //add data
207
m_wheres.add(new WhereCondition(Col1, sOperator, vals));
208     }
209
210     /**
211      * Returns the column name for the column reference of the condition
212      * at the specified index.
213      *
214      * @param index the index of the condition
215      * @return the column name for the condition column reference
216      * @throws DataStoreException if the condition at the specified index
217      * is not a <code>WhereCondition</code>
218      */

219     public String JavaDoc getColumnName(int index) throws DataStoreException {
220         if (m_wheres.get(index) instanceof WhereCondition) {
221             return (((WhereCondition) m_wheres.get(index)).getColumnName());
222         } else {
223             throw new DataStoreException("Invalid Class: " +
224                                          m_wheres.get(index).getClass()
225                                                  .getName());
226         }
227     }
228
229     /**
230      * Returns the table name for the column reference of the condition
231      * at the specified index.
232      *
233      * @param index the index of the condition
234      * @return the table name for the condition column reference
235      * @throws DataStoreException if the condition at the specified index
236      * is not a <code>WhereCondition</code>
237      */

238     public String JavaDoc getTableName(int index) throws DataStoreException {
239         if (m_wheres.get(index) instanceof WhereCondition) {
240             return (((WhereCondition) m_wheres.get(index)).getTableName());
241         } else {
242             throw new DataStoreException("Invalid Class: " +
243                                          m_wheres.get(index).getClass()
244                                                  .getName());
245         }
246     }
247
248     /**
249      * Returns the full string representation of the column reference for
250      * the condition at the specified index.
251      *
252      * @param index the index of the condition
253      * @return the full string representation of the column reference
254      * @throws DataStoreException if the condition at the specified index
255      * is not a <code>WhereCondition</code>
256      */

257     public String JavaDoc getFullColumnRef(int index) throws DataStoreException {
258         if (m_wheres.get(index) instanceof WhereCondition) {
259             return (((WhereCondition) m_wheres.get(index)).getFullColumnRef());
260         } else {
261             throw new DataStoreException("Invalid Class: " +
262                                          m_wheres.get(index).getClass()
263                                                  .getName());
264         }
265     }
266
267     /**
268      * Returns the comparison operator of the condition at the specified
269      * index.
270      *
271      * @param index the index of the condition
272      * @return the comparison operator of the condition
273      * @throws DataStoreException if the condition at the specified index
274      * is not a <code>WhereCondition</code>
275      */

276     public String JavaDoc getOperator(int index) throws DataStoreException {
277         if (m_wheres.get(index) instanceof WhereCondition) {
278             return (((WhereCondition) m_wheres.get(index)).getOperator());
279         } else {
280             throw new DataStoreException("Invalid Class: " +
281                                          m_wheres.get(index).getClass()
282                                                  .getName());
283         }
284     }
285
286     /**
287      * Returns the list of conditional values for the condition at the
288      * specified index.
289      *
290      * @param index the index of the condition
291      * @return the list of conditional values for the condition
292      * @throws DataStoreException if the condition at the specified index
293      * is not a <code>WhereCondition</code>
294      */

295     public List getValues(int index) throws DataStoreException {
296         if (m_wheres.get(index) instanceof WhereCondition) {
297             return ((WhereCondition) m_wheres.get(index)).getValues();
298         } else {
299             throw new DataStoreException("Invalid Class: " +
300                                          m_wheres.get(index).getClass()
301                                                  .getName());
302         }
303     }
304
305     /**
306      * Returns the list of all tables included in this collection of
307      * where conditions.
308      *
309      * @return the list of all tables
310      * @throws DataStoreException if an error occurs accessing the table names
311      */

312     public List getTableList() throws DataStoreException {
313         Vector vecr = new Vector();
314
315         if ((m_wheres == null) || (m_wheres.size() == 0)) {
316             return vecr;
317         }
318
319         for (int i = 0; i < m_wheres.size(); i++) {
320             if (m_wheres.get(i) instanceof WhereCondition) {
321                 WhereCondition where = (WhereCondition) m_wheres.get(i);
322
323                 if (vecr.contains(where.getTableName()) == false) {
324                     vecr.add(where.getTableName());
325                 }
326             } else if (m_wheres.get(i) instanceof WhereConditionGroup) {
327                 WhereConditionGroup wheres = (WhereConditionGroup) m_wheres.get(i);
328                 List tables = wheres.getTableList();
329
330                 Iterator iter = tables.iterator();
331                 
332                 while (iter.hasNext()) {
333                     String JavaDoc sTableName = (String JavaDoc) iter.next();
334                     vecr.add(sTableName);
335                 }
336             }
337         }
338
339         return vecr;
340     }
341
342     /**
343      * Returns the number of conditions in this collection, not including
344      * those conditions contained in <code>WhereConditions</code> which have been
345      * added to this collection as a condition.
346      *
347      * @return the number of conditions in this collection
348      */

349     public int size() {
350         return (m_wheres.size());
351     }
352
353     /**
354      * Removes all conditions from this collection.
355      *
356      */

357     public void empty() {
358         if (m_wheres != null) {
359             m_wheres.clear();
360         }
361     }
362
363     /**
364      * Returns the full list of column references contained in this
365      * collection of where conditions.
366      *
367      * @return the list of column references
368      */

369     public List getColumnRefs() {
370         Vector cols = new Vector();
371         
372         Iterator iter = m_wheres.iterator();
373         
374         while(iter.hasNext()) {
375             Object JavaDoc where = iter.next();
376             
377             if(where instanceof WhereCondition) {
378                 cols.add(((WhereCondition)where).getColumnRef());
379             } else if(where instanceof WhereConditionGroup) {
380                 cols.addAll(((WhereConditionGroup)where).getColumnRefs());
381             }
382         }
383         
384         return cols;
385     }
386 }
Popular Tags