Project

General

Profile

1 1831 tao
/**
2
 *  '$RCSfile$'
3 2366 sgarg
 *    Purpose: A Class that represents a structured query, and can be
4
 *             constructed from an XML serialization conforming to
5
 *             pathquery.dtd. The printSQL() method can be used to print
6 1831 tao
 *             a SQL serialization of the query.
7
 *  Copyright: 2000 Regents of the University of California and the
8
 *             National Center for Ecological Analysis and Synthesis
9
 *    Authors: Matt Jones
10
 *
11
 *   '$Author$'
12
 *     '$Date$'
13
 * '$Revision$'
14
 *
15
 * This program is free software; you can redistribute it and/or modify
16
 * it under the terms of the GNU General Public License as published by
17
 * the Free Software Foundation; either version 2 of the License, or
18
 * (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU General Public License
26
 * along with this program; if not, write to the Free Software
27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 */
29
30
package edu.ucsb.nceas.metacat;
31
32
import edu.ucsb.nceas.dbadapter.*;
33
34
import java.io.*;
35
import java.util.Hashtable;
36
import java.util.Stack;
37
import java.util.Vector;
38
import java.util.Enumeration;
39
40 2677 sgarg
import org.apache.log4j.Logger;
41
42 1831 tao
 /** a utility class that represents a group of terms in a query */
43
public  class QueryGroup {
44
    private String operator = null;  // indicates how query terms are combined
45
    private Vector children = null;  // the list of query terms and groups
46
    private int countPercentageSearchItem = 0;
47 2677 sgarg
    private static Logger logMetacat = Logger.getLogger(QueryGroup.class);
48
49 2366 sgarg
    /**
50
     * construct a new QueryGroup
51 1831 tao
     *
52 2366 sgarg
     * @param operator the boolean conector used to connect query terms
53 1831 tao
     *                    in this query group
54
     */
55
    public QueryGroup(String operator) {
56
      this.operator = operator;
57
      children = new Vector();
58
    }
59
60 2366 sgarg
    /**
61 1831 tao
     * Add a child QueryGroup to this QueryGroup
62
     *
63
     * @param qgroup the query group to be added to the list of terms
64
     */
65
    public void addChild(QueryGroup qgroup) {
66 2366 sgarg
      children.add((Object)qgroup);
67 1831 tao
    }
68
69
    /**
70
     * Add a child QueryTerm to this QueryGroup
71
     *
72
     * @param qterm the query term to be added to the list of terms
73
     */
74
    public void addChild(QueryTerm qterm) {
75 2366 sgarg
      children.add((Object)qterm);
76 1831 tao
    }
77
78
    /**
79
     * Retrieve an Enumeration of query terms for this QueryGroup
80
     */
81
    public Enumeration getChildren() {
82
      return children.elements();
83
    }
84 2366 sgarg
85 1831 tao
    public int getPercentageSymbolCount()
86
    {
87
      return countPercentageSearchItem;
88
    }
89 2366 sgarg
90 1831 tao
    /**
91
     * create a SQL serialization of the query that this instance represents
92
     */
93
    public String printSQL(boolean useXMLIndex) {
94
      StringBuffer self = new StringBuffer();
95 2373 sgarg
      StringBuffer queryString = new StringBuffer();
96
97 1831 tao
      boolean first = true;
98 2677 sgarg
99 1831 tao
      Enumeration en= getChildren();
100
      while (en.hasMoreElements()) {
101
        Object qobject = en.nextElement();
102
        if (qobject instanceof QueryGroup) {
103 2635 sgarg
            QueryGroup qg = (QueryGroup)qobject;
104 2634 sgarg
        	String queryGroupSQL = qg.printSQL(useXMLIndex);
105 2677 sgarg
        	logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: "
106
        			+ queryGroupSQL);
107 2635 sgarg
108 2634 sgarg
        	if (first) {
109
        		first = false;
110
        	} else {
111
        		if(!queryString.toString().equals("") && queryGroupSQL != null &&!queryGroupSQL.equals("")){
112 2503 sgarg
                    queryString.append(" " + operator + " ");
113 2677 sgarg
        		}
114
        	}
115
   		  	queryString.append(queryGroupSQL);
116
117
   		  	// count percerntage number
118
   		  	int count = qg.getPercentageSymbolCount();
119
   		  	countPercentageSearchItem = countPercentageSearchItem + count;
120 1831 tao
        } else if (qobject instanceof QueryTerm) {
121
           QueryTerm qt = (QueryTerm)qobject;
122 2373 sgarg
           String termQueryString = qt.printSQL(useXMLIndex);
123 2677 sgarg
       	   logMetacat.info("In QueryGroup.printSQL.. found a QueryGroup: "
124
        			+ termQueryString);
125 2366 sgarg
           if(!(qt.getSearchMode().equals("contains") && qt.getValue().equals("%"))){
126 2634 sgarg
        	   if (first) {
127 2503 sgarg
                   first = false;
128
               } else {
129
                   if(!queryString.toString().equals("")){
130
                       queryString.append(" " + operator + " ");
131
                   }
132
               }
133 2373 sgarg
               queryString.append(termQueryString);
134 2366 sgarg
           }
135 1831 tao
           // count percerntage number
136
           int count = qt.getPercentageSymbolCount();
137
           countPercentageSearchItem = countPercentageSearchItem + count;
138
        } else {
139
          System.err.println("qobject wrong type: fatal error");
140
        }
141
      }
142 2373 sgarg
143
      if(!queryString.toString().equals("")){
144
          self.append("(");
145
          self.append(queryString.toString());
146
          self.append(")");
147
      }
148 2677 sgarg
149
      logMetacat.info("In QueryGroup.printSQL.. final query returned is: "
150
			+ self.toString());
151 1831 tao
      return self.toString();
152
    }
153
154
    /**
155
     * create a String description of the query that this instance represents.
156
     * This should become a way to get the XML serialization of the query.
157
     */
158
    public String toString() {
159
      StringBuffer self = new StringBuffer();
160
161
      self.append("  (Query group operator=" + operator + "\n");
162
      Enumeration en= getChildren();
163
      while (en.hasMoreElements()) {
164
        Object qobject = en.nextElement();
165
        self.append(qobject);
166
      }
167
      self.append("  )\n");
168
      return self.toString();
169
    }
170
  }