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.Calendar;
026: import java.util.Date;
027:
028: import org.apache.lucene.index.Term;
029: import org.apache.lucene.search.RangeQuery;
030:
031: import org.apache.slide.index.lucene.Index;
032: import org.apache.slide.index.lucene.IndexConfiguration;
033: import org.apache.slide.search.BadQueryException;
034: import org.jdom.Element;
035:
036: /**
037: * Implements <code>lt</code> and <code>lte</code> expression.
038: */
039: public class LtExpression extends AbstractLuceneExpression implements
040: RangeOperator {
041: private String field;
042: private String value;
043: private boolean inclusive;
044:
045: public LtExpression(Index index, Element element, boolean inclusive)
046: throws BadQueryException {
047: super (index);
048: this .inclusive = inclusive;
049:
050: IndexConfiguration config = index.getConfiguration();
051: Element prop = getPropertyElement(element);
052: this .field = IndexConfiguration.generateFieldName(prop
053: .getNamespaceURI(), prop.getName());
054: Element literal = getLiteralElement(element);
055:
056: String lowerBound;
057: if (index.getConfiguration().isDateProperty(
058: prop.getNamespaceURI(), prop.getName())) {
059: Date date = IndexConfiguration.getDateValue(literal
060: .getTextTrim());
061: Calendar c = Calendar.getInstance();
062: c.setTime(date);
063: inclusive = c.get(Calendar.SECOND) > 0;
064: this .value = config.dateToIndexString(date);
065: lowerBound = Index.DATE_LOWER_BOUND;
066: } else if (index.getConfiguration().isIntProperty(
067: prop.getNamespaceURI(), prop.getName())) {
068: this .value = config.intToIndexString(Long.parseLong(literal
069: .getTextTrim()));
070: lowerBound = Index.INT_LOWER_BOUND;
071: } else {
072: this .value = literal.getTextTrim();
073:
074: if (!index.getConfiguration().isCaseSensitive())
075: value = value.toLowerCase();
076:
077: lowerBound = Index.STRING_LOWER_BOUND;
078: }
079:
080: RangeQuery rangeQuery = new RangeQuery(new Term(this .field,
081: lowerBound), new Term(this .field, this .value),
082: inclusive); // inclusive or not
083:
084: setQuery(rangeQuery);
085: }
086:
087: public String getField() {
088: return this .field;
089: }
090:
091: public String getValue() {
092: return this .value;
093: }
094:
095: public boolean inclusive() {
096: return this .inclusive;
097: }
098:
099: public String toString() {
100: StringBuffer b = new StringBuffer();
101:
102: b.append('(').append(this .field).append('<').append(
103: this .inclusive ? "=" : "").append(this .value).append(
104: ')');
105:
106: return b.toString();
107: }
108: }
|