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.Hashtable;
034import java.util.Vector;
035
036/**
037 * This class represents a single query. It is made up of three different parts:<br>
038 * 1) The "select" part that describes all the fields that will be "displayed"
039 * by the query<br>
040 * 2) The "joins" part that is basically table/field name pairs<br>
041 * 3) The "where" part which describes a heirarchical (tree) of operators and
042 * conditions <br>
043 */
044public class DBQueryDef {
045        /** The query was built ok with no errors **/
046        public static final int BUILD_OK = 0;
047        /** A generic error during parsing and building **/
048        public static final int BUILD_ERROR = 1;
049        /** More than likely the where clause had a mix of ANDs and ORs **/
050        public static final int BUILD_TOO_COMPLEX_WHERE = 2;
051        /** Was unable to convert a set of complex ORs to a standard format **/
052        public static final int BUILD_TOO_COMPLEX_JOINS = 3;
053
054        protected boolean mIsAdv = false;
055        protected Vector mSelects = new Vector();
056        protected Vector mTables = new Vector();
057        protected Vector mJoins = null;
058        protected DBWhereIFace mWhere = null;
059
060        /**
061         * Default Constructor
062         * 
063         */
064        public DBQueryDef() {
065        }
066
067        /**
068         * Returns whether it considers the query to be "advanced"<br>
069         * An advanced query is one that was built with the advanced tab and then
070         * saved out. It is more of a hint to the tool when reading queries back in.
071         * 
072         * @return true if it is advanced, false if standard
073         */
074        public boolean isAdv() {
075                return mIsAdv;
076        }
077
078        /**
079         * Sets the hint as to whether it is an advanced query
080         * 
081         * @param aIsAdv
082         *            The mIsAdv to set.
083         */
084        public void setIsAdv(boolean aIsAdv) {
085                mIsAdv = aIsAdv;
086        }
087
088        /**
089         * Adds an item to the selects vector
090         * 
091         * @param aItem
092         */
093        public void addSelectItem(DBSelectTableModelItem aItem) {
094                mSelects.add(aItem);
095        }
096
097        /**
098         * Sets the where object
099         * 
100         * @param aItem
101         */
102        public void setWhere(DBWhereIFace aItem) {
103                mWhere = aItem;
104        }
105
106        /**
107         * A copy of the where object and all of its children. It traverses and
108         * builds a duplicate tree of the where object
109         * 
110         * @return a copy of the where object and all of its children
111         */
112        public DBWhereIFace getWhere() {
113                DBWhereIFace whereTree = null;
114                if (mWhere != null) {
115                        if (mWhere instanceof DBWhereOperator) {
116                                whereTree = new DBWhereOperator(null, (DBWhereOperator) mWhere);
117                        } else {
118                                whereTree = new DBWhereCondition(null,
119                                                (DBWhereCondition) mWhere);
120                        }
121                }
122
123                return whereTree;
124        }
125
126        /**
127         * The vector of items representing the the items to be disdplayed<br>
128         * The items are object of class DBSelectTableModelItem
129         * 
130         * @return the Vector of selects, (the display elements)
131         */
132        public Vector getSelects() {
133                return mSelects;
134        }
135
136        /**
137         * Returns a vector of pairs of items, meaning this vector MUST have an even
138         * number of items. The objects are of class DBSelectTableModelItem
139         * 
140         * @return the vector of join pairs
141         */
142        public Vector getJoins() {
143                return mJoins;
144        }
145
146        /**
147         * Sets a vector of DBSelectTableModelItem "pairs", assumes that the vector
148         * always holds an even number of elements, one left and right in pairs
149         * 
150         * @param aJoins
151         *            the join vector
152         */
153        public void setJoins(Vector aJoins) {
154                mJoins = aJoins;
155        }
156
157        /**
158         * Loads the hashtable with the table names from the DBSelectTableModelItem
159         * objects in the vector
160         * 
161         * @param aVector
162         *            the vector of DBSelectTableModelItem items
163         * @param aHashtable
164         *            the hashtable
165         */
166        private void addTableNamesToHash(Vector aVector, Hashtable aHashtable) {
167                if (aVector != null) {
168                        for (Enumeration e = mSelects.elements(); e.hasMoreElements();) {
169                                String tblName = ((DBSelectTableModelItem) e.nextElement())
170                                                .getTableName();
171                                if (tblName.length() > 0) {
172                                        aHashtable.put(tblName, tblName);
173                                }
174                        }
175                }
176        }
177
178        /**
179         * Adds table item to "table" list, it will create a new DBQueryDefTable
180         * object
181         * 
182         * @param aId
183         *            id of table
184         * @param aName
185         *            name of table
186         * @param aX
187         *            x coord
188         * @param aY
189         *            y coord
190         */
191        public void addTable(int aId, String aName, int aX, int aY) {
192                mTables.add(new DBQueryDefTable(aId, aName, aX, aY));
193        }
194
195        /**
196         * Adds table item to "from" list
197         * 
198         * @param aName
199         *            name of table
200         */
201        public void addTable(String aName) {
202                mTables.add(new DBQueryDefTable(-1, aName, -1, -1));
203        }
204
205        /**
206         * Adds table DBQueryDefTable item to "table" list
207         * 
208         * @param aItem
209         *            DBQueryDefTable item
210         */
211        public void addTable(DBQueryDefTable aItem) {
212                mTables.add(aItem);
213        }
214
215        /**
216         * Returns a vector of table names that would represent the "table" portion
217         * 
218         * @return vector
219         */
220        public Vector getTables() {
221                return mTables;
222        }
223
224        /**
225         * Clears all the vectors and the where object
226         * 
227         */
228        public void clear() {
229                mSelects.clear();
230                mTables.clear();
231                mJoins.clear();
232                mWhere = null;
233        }
234
235}