001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.ecoinformatics.seek.querybuilder;
031
032import java.util.Enumeration;
033import java.util.Vector;
034
035/**
036 * A boolean operator class that can "contain" one or more conditions
037 * (DBWhereCondition). <i>cond</i> AND <i>cond</i> AND <i>cond</i><br>
038 * An operator contains a "closure" object that used for the sole purpose of
039 * showing where the operator containment ends in the UI.
040 */
041public class DBWhereOperator extends DBWhereListCellBase {
042        public static final String OR_OPER = "OR";
043        public static final String AND_OPER = "AND";
044
045        protected Vector mItems = new Vector();
046        protected boolean mIsClosure = false;
047        protected DBWhereOperator mClosure = null;
048
049        /**
050         * Constructor - Creates a "closure" object, Must be manually add and
051         * removed from parent the closure object must also be manually added and
052         * removed
053         * 
054         * @param aParent
055         *            the parent of the object
056         * @param aIsClosure
057         *            indicates whether it is a closure object
058         */
059        public DBWhereOperator(DBWhereOperator aParent, boolean aIsClosure) {
060                super(aParent);
061                setName(AND_OPER); // default to an AND (for no real reason)
062
063                if (!aIsClosure) {
064                        mClosure = new DBWhereOperator(aParent, true);
065                }
066        }
067
068        /**
069         * Copy Constructor - Copy all fields, but creates a new "closure" object,
070         * Must be manually add and removed from parent the closure object must also
071         * be manually added and removed
072         * 
073         * @param aParent
074         *            the parent of the object
075         * @param aObj
076         *            the object to be copied
077         */
078        public DBWhereOperator(DBWhereOperator aParent, DBWhereOperator aObj) {
079                super(aParent);
080                setName(AND_OPER); // default to an AND (for no real reason)
081                mClosure = new DBWhereOperator(aParent, true);
082
083                for (Enumeration et = aObj.mItems.elements(); et.hasMoreElements();) {
084                        Object obj = et.nextElement();
085                        if (obj instanceof DBWhereOperator) {
086                                mItems.add(new DBWhereOperator(this, (DBWhereOperator) obj));
087                        } else {
088                                mItems.add(new DBWhereCondition(this, (DBWhereCondition) obj));
089                        }
090                }
091        }
092
093        /**
094   *
095   */
096        public void finalize() {
097
098        }
099
100        /**
101         * Sets the depth
102         * 
103         * @param aDepth
104         *            the depth in the object tree
105         */
106        public void setDepth(int aDepth) {
107                super.setDepth(aDepth);
108                if (mClosure != null) {
109                        mClosure.setDepth(aDepth);
110                }
111        }
112
113        /**
114         * Return the close object
115         * 
116         * @return the closure object
117         */
118        public DBWhereOperator getClosure() {
119                return mClosure;
120        }
121
122        /**
123         * Indicates whether it is a closure object
124         * 
125         * @return true is it is a "closure" item
126         */
127        public boolean isClosure() {
128                return mIsClosure;
129        }
130
131        /**
132         * Sets whether it is a closure object
133         * 
134         * @param aVal
135         *            whether it is a closure item
136         */
137        public void SetClosure(boolean aVal) {
138                mIsClosure = aVal;
139        }
140
141        /**
142         * Add new item "after" the item identified
143         * 
144         * @param aNewObj
145         *            the new item
146         * @param aAfterObject
147         *            the item the new item will be placed after
148         */
149        public void addAfter(DBWhereIFace aNewObj, Object aAfterObject) {
150                aNewObj.setDepth(getDepth() + 1);
151                int newInx = aAfterObject == null ? 0
152                                : mItems.indexOf(aAfterObject) + 1;
153                mItems.add(newInx, aNewObj);
154        }
155
156        /**
157         * Adds new item "at the end of the list
158         * 
159         * @param aNewObj
160         *            the new item
161         */
162        public void append(DBWhereIFace aNewObj) {
163                aNewObj.setDepth(getDepth() + 1);
164                mItems.add(aNewObj);
165        }
166
167        /**
168         * Removes an item
169         * 
170         * @param aObj
171         */
172        public void remove(DBWhereIFace aObj) {
173                mItems.remove(aObj);
174        }
175
176        /**
177         * Returns an enumeration for the child of the operator
178         * 
179         * @return the enumeration
180         */
181        public Enumeration getEnumeration() {
182                return mItems.elements();
183        }
184
185        /**
186         * Returns the number of children
187         * 
188         * @return number of children
189         */
190        public int getNumChildern() {
191                return mItems.size();
192        }
193
194        /**
195         * Sets the name
196         * 
197         * @param aName
198         *            the name as a string
199         */
200        public void setName(String aName) {
201                super.setName(aName);
202                if (mClosure != null) {
203                        mClosure.setName(aName);
204                }
205        }
206
207        /**
208         * Sets the operator name (calls setName)
209         * 
210         * @param operator
211         *            The operator to set.
212         */
213        public void setOperator(String operator) {
214                setName(operator);
215        }
216
217        /**
218         * Indicates whether it is an operator
219         * 
220         * @return Returns true if an operator
221         */
222        public boolean isOperator() {
223                return true;
224        }
225
226        /**
227         * Returns name
228         */
229        public String toString() {
230                return mIsClosure ? "/" + mName : mName;
231        }
232
233}