01: /*
02: * $Header$
03: * $Revision: 7067 $
04: * $Date: 2007-07-09 02:45:41 -0700 $
05: *
06: * ====================================================================
07: *
08: * Copyright 1999-2004 The Apache Software Foundation
09: *
10: * Licensed under the Apache License, Version 2.0 (the "License");
11: * you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software
17: * distributed under the License is distributed on an "AS IS" BASIS,
18: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19: * See the License for the specific language governing permissions and
20: * limitations under the License.
21: *
22: */
23: package org.apache.slide.index.lucene.expressions;
24:
25: import org.apache.lucene.index.Term;
26: import org.apache.lucene.search.BooleanClause;
27: import org.apache.lucene.search.BooleanQuery;
28: import org.apache.lucene.search.TermQuery;
29:
30: import org.apache.slide.index.lucene.Index;
31: import org.apache.slide.index.lucene.IndexConfiguration;
32: import org.apache.slide.search.BadQueryException;
33: import org.jdom.Element;
34:
35: /**
36: * Implements <code>eq</code> and <code>not-eq</code>.
37: */
38: public class EqExpression extends AbstractLuceneExpression {
39:
40: public EqExpression(Index index, Element element, boolean negated)
41: throws BadQueryException {
42: super (index);
43:
44: IndexConfiguration config = index.getConfiguration();
45: Element prop = getPropertyElement(element);
46: String field = IndexConfiguration.generateFieldName(prop
47: .getNamespaceURI(), prop.getName());
48: Element literal = getLiteralElement(element);
49:
50: String value;
51: if (index.getConfiguration().isDateProperty(
52: prop.getNamespaceURI(), prop.getName())) {
53: value = config.dateToIndexString(IndexConfiguration
54: .getDateValue(literal.getTextTrim()));
55: } else if (index.getConfiguration().isIntProperty(
56: prop.getNamespaceURI(), prop.getName())) {
57: value = config.intToIndexString(Long.parseLong(literal
58: .getTextTrim()));
59: } else {
60: value = literal.getTextTrim();
61:
62: if (!index.getConfiguration().isCaseSensitive())
63: value = value.toLowerCase();
64: }
65:
66: Term term = new Term(field, value);
67:
68: setQuery(new TermQuery(term));
69:
70: if (negated) {
71: BooleanQuery booleanQuery = new BooleanQuery();
72: booleanQuery.add(new TermQuery(new Term(
73: Index.IS_DEFINED_FIELD_NAME, field)),
74: BooleanClause.Occur.MUST);
75: booleanQuery.add(getQuery(), BooleanClause.Occur.MUST_NOT);
76: setQuery(booleanQuery);
77: }
78: }
79: }
|