001: /*
002: * $Header$
003: * $Revision: 7067 $
004: * $Date: 2007-07-09 02:45:41 -0700 $
005: *
006: * ====================================================================
007: *
008: * Copyright 1999-2004 The Apache Software Foundation
009: *
010: * Licensed under the Apache License, Version 2.0 (the "License");
011: * you may not use this file except in compliance with the License.
012: * You may obtain a copy of the License at
013: *
014: * http://www.apache.org/licenses/LICENSE-2.0
015: *
016: * Unless required by applicable law or agreed to in writing, software
017: * distributed under the License is distributed on an "AS IS" BASIS,
018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019: * See the License for the specific language governing permissions and
020: * limitations under the License.
021: *
022: */
023: package org.apache.slide.index.lucene.expressions;
024:
025: import java.util.Date;
026:
027: import org.apache.lucene.index.Term;
028: import org.apache.lucene.search.RangeQuery;
029:
030: import org.apache.slide.index.lucene.Index;
031: import org.apache.slide.index.lucene.IndexConfiguration;
032: import org.apache.slide.search.BadQueryException;
033: import org.jdom.Element;
034:
035: /**
036: * Implements <code>gt</code> and <code>gte</code> expression.
037: */
038: public class GtExpression extends AbstractLuceneExpression implements
039: RangeOperator {
040: private String field;
041: private String value;
042: private boolean inclusive;
043:
044: public GtExpression(Index index, Element element, boolean inclusive)
045: throws BadQueryException {
046: super (index);
047: this .inclusive = inclusive;
048:
049: IndexConfiguration config = index.getConfiguration();
050: Element prop = getPropertyElement(element);
051: this .field = IndexConfiguration.generateFieldName(prop
052: .getNamespaceURI(), prop.getName());
053: Element literal = getLiteralElement(element);
054:
055: String upperBound;
056: if (index.getConfiguration().isDateProperty(
057: prop.getNamespaceURI(), prop.getName())) {
058: Date date = IndexConfiguration.getDateValue(literal
059: .getTextTrim());
060: this .value = config.dateToIndexString(date);
061: upperBound = Index.DATE_UPPER_BOUND;
062: } else if (index.getConfiguration().isIntProperty(
063: prop.getNamespaceURI(), prop.getName())) {
064: this .value = config.intToIndexString(Long.parseLong(literal
065: .getTextTrim()));
066: upperBound = Index.INT_UPPER_BOUND;
067: } else {
068: this .value = literal.getTextTrim();
069:
070: if (!index.getConfiguration().isCaseSensitive())
071: value = value.toLowerCase();
072:
073: upperBound = Index.STRING_UPPER_BOUND;
074: }
075:
076: RangeQuery rangeQuery = new RangeQuery(new Term(this .field,
077: this .value), new Term(this .field, upperBound),
078: inclusive); // inclusive or not
079:
080: setQuery(rangeQuery);
081: }
082:
083: public String getField() {
084: return this .field;
085: }
086:
087: public String getValue() {
088: return this .value;
089: }
090:
091: public boolean inclusive() {
092: return this .inclusive;
093: }
094:
095: public String toString() {
096: StringBuffer b = new StringBuffer();
097:
098: b.append('(').append(this .field).append('>').append(
099: this .inclusive ? "=" : "").append(this .value).append(
100: ')');
101:
102: return b.toString();
103: }
104: }
|