Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    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
 *             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: tao $'
12
 *     '$Date: 2007-04-10 16:31:36 -0700 (Tue, 10 Apr 2007) $'
13
 * '$Revision: 3224 $'
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 java.util.Vector;
33

    
34
/** a utility class that represents a single term in a query */
35
public class QueryTerm
36
{
37

    
38
    private boolean casesensitive = false;
39

    
40
    private String searchmode = null;
41

    
42
    private String value = null;
43

    
44
    private String pathexpr = null;
45

    
46
    private boolean percentageSymbol = false;
47

    
48
    private int countPercentageSearchItem = 0;
49

    
50
    /**
51
     * Construct a new instance of a query term for a free text search (using
52
     * the value only)
53
     *
54
     * @param casesensitive
55
     *            flag indicating whether case is used to match
56
     * @param searchmode
57
     *            determines what kind of substring match is performed (one of
58
     *            starts-with|ends-with|contains|matches-exactly)
59
     * @param value
60
     *            the text value to match
61
     */
62
    public QueryTerm(boolean casesensitive, String searchmode, String value)
63
    {
64
        this.casesensitive = casesensitive;
65
        this.searchmode = searchmode;
66
        this.value = value;
67
    }
68

    
69
    /**
70
     * Construct a new instance of a query term for a structured search
71
     * (matching the value only for those nodes in the pathexpr)
72
     *
73
     * @param casesensitive
74
     *            flag indicating whether case is used to match
75
     * @param searchmode
76
     *            determines what kind of substring match is performed (one of
77
     *            starts-with|ends-with|contains|matches-exactly)
78
     * @param value
79
     *            the text value to match
80
     * @param pathexpr
81
     *            the hierarchical path to the nodes to be searched
82
     */
83
    public QueryTerm(boolean casesensitive, String searchmode, String value,
84
            String pathexpr)
85
    {
86
        this(casesensitive, searchmode, value);
87
        this.pathexpr = pathexpr;
88
    }
89

    
90
    /** determine if the QueryTerm is case sensitive */
91
    public boolean isCaseSensitive()
92
    {
93
        return casesensitive;
94
    }
95

    
96
    /** get the searchmode parameter */
97
    public String getSearchMode()
98
    {
99
        return searchmode;
100
    }
101

    
102
    /** get the Value parameter */
103
    public String getValue()
104
    {
105
        return value;
106
    }
107

    
108
    /** get the path expression parameter */
109
    public String getPathExpression()
110
    {
111
        return pathexpr;
112
    }
113

    
114
    /** get the percentage count for one query term */
115
    public int getPercentageSymbolCount()
116
    {
117
        return countPercentageSearchItem;
118
    }
119

    
120
    /**
121
     * create a SQL serialization of the query that this instance represents
122
     */
123
    public String printSQL(boolean useXMLIndex)
124
    {
125
        StringBuffer self = new StringBuffer();
126

    
127
        // Uppercase the search string if case match is not important
128
        String casevalue = null;
129
        String nodedataterm = null;
130
        boolean notEqual = false;
131
        if (casesensitive) {
132
            nodedataterm = "nodedata";
133
            casevalue = value;
134
        } else {
135
            nodedataterm = "UPPER(nodedata)";
136
            casevalue = value.toUpperCase();
137
        }
138

    
139
        // Add appropriate wildcards to search string
140
        String searchexpr = null;
141
        if (searchmode.equals("starts-with")) {
142
            searchexpr = nodedataterm + " LIKE '" + casevalue + "%' ";
143
        } else if (searchmode.equals("ends-with")) {
144
            searchexpr = nodedataterm + " LIKE '%" + casevalue + "' ";
145
        } else if (searchmode.equals("contains")) {
146
            if (!casevalue.equals("%")) {
147
                searchexpr = nodedataterm + " LIKE '%" + casevalue + "%' ";
148
            } else {
149
                searchexpr = nodedataterm + " LIKE '" + casevalue + "' ";
150
                // find percentage symbol
151
                percentageSymbol = true;
152
            }
153
        } else if (searchmode.equals("not-contains")) {
154
        	notEqual = true;
155
            searchexpr = nodedataterm + " LIKE '%" + casevalue + "%' ";
156
        } else if (searchmode.equals("equals")) {
157
            searchexpr = nodedataterm + " = '" + casevalue + "' ";
158
        } else if (searchmode.equals("isnot-equal")) {
159
        	notEqual = true;
160
            searchexpr = nodedataterm + " = '" + casevalue + "' ";
161
        } else {
162
            String oper = null;
163
            if (searchmode.equals("greater-than")) {
164
                oper = ">";
165
                nodedataterm = "nodedatanumerical";
166
            } else if (searchmode.equals("greater-than-equals")) {
167
                oper = ">=";
168
                nodedataterm = "nodedatanumerical";
169
            } else if (searchmode.equals("less-than")) {
170
                oper = "<";
171
                nodedataterm = "nodedatanumerical";
172
            } else if (searchmode.equals("less-than-equals")) {
173
                oper = "<=";
174
                nodedataterm = "nodedatanumerical";
175
            } else {
176
                System.out
177
                        .println("NOT expected case. NOT recognized operator: "
178
                                + searchmode);
179
                return null;
180
            }
181
            try {
182
                // it is number; numeric comparison
183
                // but we need to make sure there is no string in node data
184
                searchexpr = nodedataterm + " " + oper + " "
185
                        + new Double(casevalue) + " ";
186
            } catch (NumberFormatException nfe) {
187
                // these are characters; character comparison
188
                searchexpr = nodedataterm + " " + oper + " '" + casevalue
189
                        + "' ";
190
            }
191
        }
192

    
193

    
194
        // to check xml_path_index can be used
195
        boolean usePathIndex = false;
196

    
197
        // if pathexpr has been specified in metacat.properties for indexing
198
        if(pathexpr != null){
199
            if(MetaCatUtil.pathsForIndexing.contains(pathexpr)){
200
                usePathIndex = true;
201
            }
202
        }
203

    
204
        if(usePathIndex){
205
            // using xml_path_index table.....
206
        	if(notEqual == true){
207
        		self.append("SELECT DISTINCT docid from xml_path_index WHERE");
208
        		self.append(" docid NOT IN (Select docid FROM xml_path_index WHERE ");
209
        		self.append(searchexpr);
210
        		self.append("AND path LIKE '" + pathexpr + "') ");
211
        	} else {
212
        		self.append("SELECT DISTINCT docid FROM xml_path_index WHERE ");
213
        		self.append(searchexpr);
214
        		self.append("AND path LIKE '" + pathexpr + "' ");	
215
        	}
216

    
217
        } else {
218
            // using xml_nodes and xml_index tables
219

    
220
        	if(notEqual == true){
221
        		self.append("SELECT DISTINCT docid from xml_nodes WHERE");
222
        		self.append(" docid NOT IN (Select docid FROM xml_nodes WHERE ");
223
        	} else {
224
        		self.append("(SELECT DISTINCT docid FROM xml_nodes WHERE ");
225
        	}
226
        	self.append(searchexpr);
227
        	
228
            if (pathexpr != null) {
229

    
230
                // use XML Index
231
                if (useXMLIndex) {
232
                    if (!hasAttributeInPath(pathexpr)) {
233
                        // without attributes in path
234
                        self.append("AND parentnodeid IN ");
235
                    } else {
236
                        // has a attribute in path
237
                        String attributeName = QuerySpecification
238
                            .getAttributeName(pathexpr);
239
                        self.append(
240
                            "AND nodetype LIKE 'ATTRIBUTE' AND nodename LIKE '"
241
                            + attributeName + "' ");
242
                        // and the path expression includes element content other than
243
                        // just './' or '../'
244
                        if ( (!pathexpr.startsWith(QuerySpecification.
245
                            ATTRIBUTESYMBOL)) &&
246
                            (!pathexpr.startsWith("./" +
247
                                                  QuerySpecification.ATTRIBUTESYMBOL)) &&
248
                            (!pathexpr.startsWith("../" +
249
                                                  QuerySpecification.ATTRIBUTESYMBOL))) {
250

    
251
                            self.append("AND parentnodeid IN ");
252
                            pathexpr = QuerySpecification
253
                                .newPathExpressionWithOutAttribute(pathexpr);
254
                        }
255
                    }
256
                    self.append(
257
                        "(SELECT nodeid FROM xml_index WHERE path LIKE "
258
                        + "'" + pathexpr + "') ");
259
                }
260
                else {
261
                    // without using XML Index; using nested statements instead
262
                    self.append("AND parentnodeid IN ");
263
                    self.append(useNestedStatements(pathexpr));
264
                }
265
            }
266
            else if ( (value.trim()).equals("%")) {
267
                //if pathexpr is null and search value is %, is a
268
                // percentageSearchItem
269
                // the count number will be increase one
270
                countPercentageSearchItem++;
271

    
272
            }
273
            self.append(") ");
274
        }
275

    
276
        return self.toString();
277
    }
278

    
279
    /** A method to judge if a path have attribute */
280
    private boolean hasAttributeInPath(String path)
281
    {
282
        if (path.indexOf(QuerySpecification.ATTRIBUTESYMBOL) != -1) {
283
            return true;
284
        } else {
285
            return false;
286
        }
287
    }
288

    
289
    /*
290
     * Constraint the query with @pathexp without using the XML Index, but
291
     * nested SQL statements instead. The query migth be slower.
292
     */
293
    public static String useNestedStatements(String pathexpr)
294
    {
295
      System.out.println("pathexpr: " + pathexpr);
296
        StringBuffer nestedStmts = new StringBuffer();
297
        Vector nodes = new Vector();
298
        String path = pathexpr;
299
        int inx = 0;
300

    
301
        do {
302
            inx = path.lastIndexOf("/");
303

    
304
            nodes.addElement(path.substring(inx + 1));
305
            path = path.substring(0, Math.abs(inx));
306
        } while (inx > 0);
307

    
308
        // nested statements
309
        int i = 0;
310
        for (i = 0; i < nodes.size() - 1; i++) {
311
            nestedStmts.append("(SELECT nodeid FROM xml_nodes"
312
                    + " WHERE nodename LIKE '" + (String) nodes.elementAt(i)
313
                    + "'" + " AND parentnodeid IN ");
314
        }
315
        // for the last statement: it is without " AND parentnodeid IN "
316
        nestedStmts.append("(SELECT nodeid FROM xml_nodes"
317
                + " WHERE nodename LIKE '" + (String) nodes.elementAt(i) + "'");
318
        // node.size() number of closing brackets
319
        for (i = 0; i < nodes.size(); i++) {
320
            nestedStmts.append(")");
321
        }
322

    
323
        return nestedStmts.toString();
324
    }
325

    
326
    /**
327
     * create a String description of the query that this instance represents.
328
     * This should become a way to get the XML serialization of the query.
329
     */
330
    public String toString()
331
    {
332

    
333
        return this.printSQL(true);
334
    }
335
    
336
    /**
337
     * Compare two query terms to see if they have same search value.
338
     * @param term
339
     * @return
340
     */
341
    public boolean hasSameSearchValue(QueryTerm term)
342
    {
343
    	boolean same = false;
344
    	if (term != null)
345
    	{
346
    		String searchValue = term.getValue();
347
    		if (searchValue != null && this.value != null)
348
    		{
349
    			if (searchValue.equalsIgnoreCase(this.value))
350
    			{
351
    				same = true;
352
    			}
353
    		}
354
    	}
355
    	return same;
356
    }
357
}
(56-56/66)