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:
023: import org.openharmonise.commons.dsi.*;
024:
025: /**
026: *
027: * A collection of where conditions that can be applied to a DML statement.
028: * In this context a condition can either be a <code>WhereCondition</code> or
029: * a <code>WhereConditions</code> object. This object represents a set of conditions
030: * which can be strung together with a stringing operator such as 'and' or 'or', for
031: * example; '[condition 1] and [condition 2] and [condition 3]' has three conditions
032: * strung together with the 'and' operator but '[[condition 1] or [condition 2]] and [condition 3]'
033: * has two top level conditions strung with 'and' with one nested conditional
034: * group containing two conditions string together with 'or'.
035: *
036: * @author Michael Bell
037: * @version $Revision: 1.1 $
038: *
039: */
040: public class WhereConditionGroup {
041:
042: /**
043: * List of where conditions
044: */
045: private List m_wheres = null;
046:
047: /**
048: * The stringing operator used to string conditions together
049: */
050: private String m_sOperator = "and";
051:
052: /**
053: * Constructs collection for where conditions
054: */
055: public WhereConditionGroup() {
056: m_wheres = new Vector(16);
057: }
058:
059: /**
060: * Sets the stringing operator for this collection of where conditions.
061: *
062: * @param sOp the stringing operator
063: * @throws DataStoreException if the stringing operator is invalid
064: */
065: public void setStringingOperator(String sOp)
066: throws DataStoreException {
067: if (sOp.equalsIgnoreCase("and") || sOp.equalsIgnoreCase("or")) {
068: m_sOperator = sOp;
069: } else {
070: throw new DataStoreException("Invalid operator");
071: }
072: }
073:
074: /**
075: * Returns the stringing operator for this collection of where conditions.
076: *
077: * @return the stringing operator
078: */
079: public String getStringingOperator() {
080: return m_sOperator;
081: }
082:
083: /**
084: * Adds the collection of where condition to this collection.
085: *
086: * @param where the collection of where conditions to add
087: */
088: public void addCondition(WhereConditionGroup where) {
089: m_wheres.add(where);
090: }
091:
092: /**
093: * Adds a where condition to this collection.
094: *
095: * @param where the where condition to add
096: */
097: public void addCondition(WhereCondition where) {
098: m_wheres.add(where);
099: }
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 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)
133: throws DataStoreException {
134: //add data
135: m_wheres.add(new WhereCondition(Col1, i));
136: }
137:
138: /**
139: * Adds a where condition from the given parameters using the specified
140: * comparison operator.
141: *
142: * @param Col1 the conditional column reference
143: * @param sOperator the comparison operator
144: * @param i the conditional value
145: * @throws DataStoreException if the condition is invalid
146: */
147: public void addCondition(ColumnRef Col1, String sOperator, int i)
148: throws DataStoreException {
149: //add data
150: m_wheres.add(new WhereCondition(Col1, sOperator, i));
151: }
152:
153: /**
154: * Adds a where condition from the given parameters using the default
155: * comparison operator.
156: *
157: * @param Col1 the conditional column reference
158: * @param obj the conditional value
159: * @throws DataStoreException if the condition is invalid
160: */
161: public void addCondition(ColumnRef Col1, Object obj)
162: throws DataStoreException {
163: //add data
164: m_wheres.add(new WhereCondition(Col1, obj));
165: }
166:
167: /**
168: * Adds a where condition from the given parameters using the specified
169: * comparison operator.
170: *
171: * @param Col1 the conditional column reference
172: * @param sOperator the comparison operator
173: * @param obj the conditional value
174: * @throws DataStoreException if the condition is invalid
175: */
176: public void addCondition(ColumnRef Col1, String sOperator,
177: Object obj) throws DataStoreException {
178: //add data
179: m_wheres.add(new WhereCondition(Col1, sOperator, obj));
180: }
181:
182: /**
183: * Adds a where condition from the given parameters using the default
184: * comparison operator.
185: *
186: * @param Col1 the conditional column reference
187: * @param vals the list of conditional values
188: * @throws DataStoreException if the condition is invalid
189: */
190: public void addCondition(ColumnRef Col1, List vals)
191: throws DataStoreException {
192: //add data
193: m_wheres.add(new WhereCondition(Col1, vals));
194: }
195:
196: /**
197: * Adds a where condition from the given parameters using the specified
198: * comparison operator.
199: *
200: * @param Col1 the conditional column reference
201: * @param sOperator the comparison operator
202: * @param vals the list of conditional values
203: * @throws DataStoreException if the condition is invalid
204: */
205: public void addCondition(ColumnRef Col1, String sOperator, List vals)
206: throws DataStoreException {
207: //add data
208: m_wheres.add(new WhereCondition(Col1, sOperator, vals));
209: }
210:
211: /**
212: * Returns the column name for the column reference of the condition
213: * at the specified index.
214: *
215: * @param index the index of the condition
216: * @return the column name for the condition column reference
217: * @throws DataStoreException if the condition at the specified index
218: * is not a <code>WhereCondition</code>
219: */
220: public String getColumnName(int index) throws DataStoreException {
221: if (m_wheres.get(index) instanceof WhereCondition) {
222: return (((WhereCondition) m_wheres.get(index))
223: .getColumnName());
224: } else {
225: throw new DataStoreException("Invalid Class: "
226: + m_wheres.get(index).getClass().getName());
227: }
228: }
229:
230: /**
231: * Returns the table name for the column reference of the condition
232: * at the specified index.
233: *
234: * @param index the index of the condition
235: * @return the table name for the condition column reference
236: * @throws DataStoreException if the condition at the specified index
237: * is not a <code>WhereCondition</code>
238: */
239: public String getTableName(int index) throws DataStoreException {
240: if (m_wheres.get(index) instanceof WhereCondition) {
241: return (((WhereCondition) m_wheres.get(index))
242: .getTableName());
243: } else {
244: throw new DataStoreException("Invalid Class: "
245: + m_wheres.get(index).getClass().getName());
246: }
247: }
248:
249: /**
250: * Returns the full string representation of the column reference for
251: * the condition at the specified index.
252: *
253: * @param index the index of the condition
254: * @return the full string representation of the column reference
255: * @throws DataStoreException if the condition at the specified index
256: * is not a <code>WhereCondition</code>
257: */
258: public String getFullColumnRef(int index) throws DataStoreException {
259: if (m_wheres.get(index) instanceof WhereCondition) {
260: return (((WhereCondition) m_wheres.get(index))
261: .getFullColumnRef());
262: } else {
263: throw new DataStoreException("Invalid Class: "
264: + m_wheres.get(index).getClass().getName());
265: }
266: }
267:
268: /**
269: * Returns the comparison operator of the condition at the specified
270: * index.
271: *
272: * @param index the index of the condition
273: * @return the comparison operator of the condition
274: * @throws DataStoreException if the condition at the specified index
275: * is not a <code>WhereCondition</code>
276: */
277: public String getOperator(int index) throws DataStoreException {
278: if (m_wheres.get(index) instanceof WhereCondition) {
279: return (((WhereCondition) m_wheres.get(index))
280: .getOperator());
281: } else {
282: throw new DataStoreException("Invalid Class: "
283: + m_wheres.get(index).getClass().getName());
284: }
285: }
286:
287: /**
288: * Returns the list of conditional values for the condition at the
289: * specified index.
290: *
291: * @param index the index of the condition
292: * @return the list of conditional values for the condition
293: * @throws DataStoreException if the condition at the specified index
294: * is not a <code>WhereCondition</code>
295: */
296: public List getValues(int index) throws DataStoreException {
297: if (m_wheres.get(index) instanceof WhereCondition) {
298: return ((WhereCondition) m_wheres.get(index)).getValues();
299: } else {
300: throw new DataStoreException("Invalid Class: "
301: + m_wheres.get(index).getClass().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
328: .get(i);
329: List tables = wheres.getTableList();
330:
331: Iterator iter = tables.iterator();
332:
333: while (iter.hasNext()) {
334: String sTableName = (String) iter.next();
335: vecr.add(sTableName);
336: }
337: }
338: }
339:
340: return vecr;
341: }
342:
343: /**
344: * Returns the number of conditions in this collection, not including
345: * those conditions contained in <code>WhereConditions</code> which have been
346: * added to this collection as a condition.
347: *
348: * @return the number of conditions in this collection
349: */
350: public int size() {
351: return (m_wheres.size());
352: }
353:
354: /**
355: * Removes all conditions from this collection.
356: *
357: */
358: public void empty() {
359: if (m_wheres != null) {
360: m_wheres.clear();
361: }
362: }
363:
364: /**
365: * Returns the full list of column references contained in this
366: * collection of where conditions.
367: *
368: * @return the list of column references
369: */
370: public List getColumnRefs() {
371: Vector cols = new Vector();
372:
373: Iterator iter = m_wheres.iterator();
374:
375: while (iter.hasNext()) {
376: Object where = iter.next();
377:
378: if (where instanceof WhereCondition) {
379: cols.add(((WhereCondition) where).getColumnRef());
380: } else if (where instanceof WhereConditionGroup) {
381: cols.addAll(((WhereConditionGroup) where)
382: .getColumnRefs());
383: }
384: }
385:
386: return cols;
387: }
388: }
|