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
 *    Release: @release@
11
 *
12
 *   '$Author: sgarg $'
13
 *     '$Date: 2005-07-25 11:52:37 -0700 (Mon, 25 Jul 2005) $'
14
 * '$Revision: 2522 $'
15
 *
16
 * This program is free software; you can redistribute it and/or modify
17
 * it under the terms of the GNU General Public License as published by
18
 * the Free Software Foundation; either version 2 of the License, or
19
 * (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
 */
30

    
31
package edu.ucsb.nceas.metacat;
32

    
33
import java.util.Vector;
34

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

    
39
    private boolean casesensitive = false;
40

    
41
    private String searchmode = null;
42

    
43
    private String value = null;
44

    
45
    private String pathexpr = null;
46

    
47
    private boolean percentageSymbol = false;
48

    
49
    private int countPercentageSearchItem = 0;
50

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

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

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

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

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

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

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

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

    
128
        // Uppercase the search string if case match is not important
129
        String casevalue = null;
130
        String nodedataterm = null;
131

    
132
        if (casesensitive) {
133
            nodedataterm = "nodedata";
134
            casevalue = value;
135
        } else {
136
            nodedataterm = "UPPER(nodedata)";
137
            casevalue = value.toUpperCase();
138
        }
139

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

    
192

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

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

    
203
        if(usePathIndex){
204
            // using xml_path_index table.....
205
            self.append("SELECT DISTINCT docid FROM xml_path_index WHERE ");
206
            self.append(searchexpr);
207
            self.append("AND path LIKE '" + pathexpr + "' ");
208

    
209
        } else {
210
            // using xml_nodes and xml_index tables
211

    
212
            self.append("SELECT DISTINCT docid FROM xml_nodes WHERE ");
213
            self.append(searchexpr);
214
            if (pathexpr != null) {
215

    
216
                // use XML Index
217
                if (useXMLIndex) {
218
                    if (!hasAttributeInPath(pathexpr)) {
219
                        // without attributes in path
220
                        self.append("AND parentnodeid IN ");
221
                    }
222
                    else {
223
                        // has a attribute in path
224
                        String attributeName = QuerySpecification
225
                            .getAttributeName(pathexpr);
226
                        self.append(
227
                            "AND nodetype LIKE 'ATTRIBUTE' AND nodename LIKE '"
228
                            + attributeName + "' ");
229
                        // and the path expression includes element content other than
230
                        // just './' or '../'
231
                        if ( (!pathexpr.startsWith(QuerySpecification.
232
                            ATTRIBUTESYMBOL)) &&
233
                            (!pathexpr.startsWith("./" +
234
                                                  QuerySpecification.ATTRIBUTESYMBOL)) &&
235
                            (!pathexpr.startsWith("../" +
236
                                                  QuerySpecification.ATTRIBUTESYMBOL))) {
237

    
238
                            self.append("AND parentnodeid IN ");
239
                            pathexpr = QuerySpecification
240
                                .newPathExpressionWithOutAttribute(pathexpr);
241
                        }
242
                    }
243
                    self.append(
244
                        "(SELECT nodeid FROM xml_index WHERE path LIKE "
245
                        + "'" + pathexpr + "') ");
246
                }
247
                else {
248
                    // without using XML Index; using nested statements instead
249
                    self.append("AND parentnodeid IN ");
250
                    self.append(useNestedStatements(pathexpr));
251
                }
252
            }
253
            else if ( (value.trim()).equals("%")) {
254
                //if pathexpr is null and search value is %, is a
255
                // percentageSearchItem
256
                // the count number will be increase one
257
                countPercentageSearchItem++;
258

    
259
            }
260
        }
261

    
262
        return self.toString();
263
    }
264

    
265
    /** A method to judge if a path have attribute */
266
    private boolean hasAttributeInPath(String path)
267
    {
268
        if (path.indexOf(QuerySpecification.ATTRIBUTESYMBOL) != -1) {
269
            return true;
270
        } else {
271
            return false;
272
        }
273
    }
274

    
275
    /*
276
     * Constraint the query with @pathexp without using the XML Index, but
277
     * nested SQL statements instead. The query migth be slower.
278
     */
279
    public static String useNestedStatements(String pathexpr)
280
    {
281
        StringBuffer nestedStmts = new StringBuffer();
282
        Vector nodes = new Vector();
283
        String path = pathexpr;
284
        int inx = 0;
285

    
286
        do {
287
            inx = path.lastIndexOf("/");
288

    
289
            nodes.addElement(path.substring(inx + 1));
290
            path = path.substring(0, Math.abs(inx));
291
        } while (inx > 0);
292

    
293
        // nested statements
294
        int i = 0;
295
        for (i = 0; i < nodes.size() - 1; i++) {
296
            nestedStmts.append("(SELECT nodeid FROM xml_nodes"
297
                    + " WHERE nodename LIKE '" + (String) nodes.elementAt(i)
298
                    + "'" + " AND parentnodeid IN ");
299
        }
300
        // for the last statement: it is without " AND parentnodeid IN "
301
        nestedStmts.append("(SELECT nodeid FROM xml_nodes"
302
                + " WHERE nodename LIKE '" + (String) nodes.elementAt(i) + "'");
303
        // node.size() number of closing brackets
304
        for (i = 0; i < nodes.size(); i++) {
305
            nestedStmts.append(")");
306
        }
307

    
308
        return nestedStmts.toString();
309
    }
310

    
311
    /**
312
     * create a String description of the query that this instance represents.
313
     * This should become a way to get the XML serialization of the query.
314
     */
315
    public String toString()
316
    {
317

    
318
        return this.printSQL(true);
319
    }
320
}
(54-54/64)